Commit ba0dac75 authored by pburdette's avatar pburdette

Refactor trigger block

Refactor trigger block to follow
best practices. Also refactor spec
to use vue test utils.
parent d9537e09
<script> <script>
import { GlAlert, GlBadge, GlLink } from '@gitlab/ui'; import { GlAlert, GlBadge, GlLink } from '@gitlab/ui';
import { s__ } from '../../locale'; import { s__ } from '~/locale';
/** /**
* Renders Stuck Runners block for job's view. * Renders Stuck Runners block for job's view.
*/ */
......
<script> <script>
import { GlButton } from '@gitlab/ui'; import { GlButton, GlTable } from '@gitlab/ui';
import { __ } from '~/locale'; import { __ } from '~/locale';
const HIDDEN_VALUE = '••••••';
export default { export default {
fields: [
{
key: 'key',
label: __('Key'),
tdAttr: { 'data-testid': 'trigger-build-key' },
tdClass: 'gl-w-half gl-font-sm!',
},
{
key: 'value',
label: __('Value'),
tdAttr: { 'data-testid': 'trigger-build-value' },
tdClass: 'gl-w-half gl-font-sm!',
},
],
components: { components: {
GlButton, GlButton,
GlTable,
}, },
props: { props: {
trigger: { trigger: {
...@@ -21,7 +34,7 @@ export default { ...@@ -21,7 +34,7 @@ export default {
}, },
computed: { computed: {
hasVariables() { hasVariables() {
return this.trigger.variables && this.trigger.variables.length > 0; return this.trigger.variables.length > 0;
}, },
getToggleButtonText() { getToggleButtonText() {
return this.showVariableValues ? __('Hide values') : __('Reveal values'); return this.showVariableValues ? __('Hide values') : __('Reveal values');
...@@ -35,45 +48,36 @@ export default { ...@@ -35,45 +48,36 @@ export default {
this.showVariableValues = !this.showVariableValues; this.showVariableValues = !this.showVariableValues;
}, },
getDisplayValue(value) { getDisplayValue(value) {
return this.showVariableValues ? value : HIDDEN_VALUE; return this.showVariableValues ? value : '••••••';
}, },
}, },
}; };
</script> </script>
<template> <template>
<div class="build-widget block"> <div class="block">
<p <p
v-if="trigger.short_token" v-if="trigger.short_token"
class="js-short-token"
:class="{ 'gl-mb-2': hasVariables, 'gl-mb-0': !hasVariables }" :class="{ 'gl-mb-2': hasVariables, 'gl-mb-0': !hasVariables }"
data-testid="trigger-short-token"
> >
<span class="font-weight-bold">{{ __('Trigger token:') }}</span> {{ trigger.short_token }} <span class="gl-font-weight-bold">{{ __('Trigger token:') }}</span> {{ trigger.short_token }}
</p> </p>
<template v-if="hasVariables"> <template v-if="hasVariables">
<p class="trigger-variables-btn-container d-flex"> <p class="gl-display-flex gl-justify-content-space-between gl-align-items-center">
<span class="font-weight-bold">{{ __('Trigger variables:') }}</span> <span class="gl-font-weight-bold">{{ __('Trigger variables:') }}</span>
<gl-button <gl-button v-if="hasValues" class="gl-mt-2" size="small" @click="toggleValues">{{
v-if="hasValues" getToggleButtonText
class="group js-reveal-variables trigger-variables-btn" }}</gl-button>
size="small"
@click="toggleValues"
>{{ getToggleButtonText }}</gl-button
>
</p> </p>
<table class="js-build-variables trigger-build-variables"> <gl-table :items="trigger.variables" :fields="$options.fields" small>
<tr v-for="(variable, index) in trigger.variables" :key="`${variable.key}-${index}`"> <template #cell(value)="data">
<td class="js-build-variable trigger-build-variable trigger-variables-table-cell"> {{ getDisplayValue(data.value) }}
{{ variable.key }} </template>
</td> </gl-table>
<td class="js-build-value trigger-build-value trigger-variables-table-cell">
{{ getDisplayValue(variable.value) }}
</td>
</tr>
</table>
</template> </template>
</div> </div>
</template> </template>
import Vue from 'vue'; import { mount } from '@vue/test-utils';
import mountComponent from 'helpers/vue_mount_component_helper'; import { GlButton, GlTable } from '@gitlab/ui';
import component from '~/jobs/components/trigger_block.vue'; import TriggerBlock from '~/jobs/components/trigger_block.vue';
describe('Trigger block', () => { describe('Trigger block', () => {
const Component = Vue.extend(component); let wrapper;
let vm;
const findRevealButton = () => wrapper.find(GlButton);
const findVariableTable = () => wrapper.find(GlTable);
const findShortToken = () => wrapper.find('[data-testid="trigger-short-token"]');
const findVariableValue = (index) =>
wrapper.findAll('[data-testid="trigger-build-value"]').at(index);
const findVariableKey = (index) => wrapper.findAll('[data-testid="trigger-build-key"]').at(index);
const createComponent = (props) => {
wrapper = mount(TriggerBlock, {
propsData: {
...props,
},
});
};
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
wrapper = null;
}); });
describe('with short token', () => { describe('with short token', () => {
it('renders short token', () => { it('renders short token', () => {
vm = mountComponent(Component, { createComponent({
trigger: { trigger: {
short_token: '0a666b2', short_token: '0a666b2',
variables: [],
}, },
}); });
expect(vm.$el.querySelector('.js-short-token').textContent).toContain('0a666b2'); expect(findShortToken().text()).toContain('0a666b2');
}); });
}); });
describe('without short token', () => { describe('without variables or short token', () => {
beforeEach(() => {
createComponent({ trigger: { variables: [] } });
});
it('does not render short token', () => { it('does not render short token', () => {
vm = mountComponent(Component, { trigger: {} }); expect(findShortToken().exists()).toBe(false);
});
expect(vm.$el.querySelector('.js-short-token')).toBeNull(); it('does not render variables', () => {
expect(findRevealButton().exists()).toBe(false);
expect(findVariableTable().exists()).toBe(false);
}); });
}); });
describe('with variables', () => { describe('with variables', () => {
describe('hide/reveal variables', () => { describe('hide/reveal variables', () => {
it('should toggle variables on click', (done) => { it('should toggle variables on click', async () => {
vm = mountComponent(Component, { createComponent({
trigger: { trigger: {
short_token: 'bd7e', short_token: 'bd7e',
variables: [ variables: [
...@@ -43,58 +65,21 @@ describe('Trigger block', () => { ...@@ -43,58 +65,21 @@ describe('Trigger block', () => {
}, },
}); });
vm.$el.querySelector('.js-reveal-variables').click(); expect(findRevealButton().text()).toBe('Reveal values');
vm.$nextTick()
.then(() => {
expect(vm.$el.querySelector('.js-build-variables')).not.toBeNull();
expect(vm.$el.querySelector('.js-reveal-variables').textContent.trim()).toEqual(
'Hide values',
);
expect(vm.$el.querySelector('.js-build-variables').textContent).toContain(
'UPLOAD_TO_GCS',
);
expect(vm.$el.querySelector('.js-build-variables').textContent).toContain('false'); expect(findVariableValue(0).text()).toBe('••••••');
expect(vm.$el.querySelector('.js-build-variables').textContent).toContain( expect(findVariableValue(1).text()).toBe('••••••');
'UPLOAD_TO_S3',
);
expect(vm.$el.querySelector('.js-build-variables').textContent).toContain('true'); expect(findVariableKey(0).text()).toBe('UPLOAD_TO_GCS');
expect(findVariableKey(1).text()).toBe('UPLOAD_TO_S3');
vm.$el.querySelector('.js-reveal-variables').click(); await findRevealButton().trigger('click');
})
.then(vm.$nextTick)
.then(() => {
expect(vm.$el.querySelector('.js-reveal-variables').textContent.trim()).toEqual(
'Reveal values',
);
expect(vm.$el.querySelector('.js-build-variables').textContent).toContain( expect(findRevealButton().text()).toBe('Hide values');
'UPLOAD_TO_GCS',
);
expect(vm.$el.querySelector('.js-build-value').textContent).toContain('••••••'); expect(findVariableValue(0).text()).toBe('false');
expect(findVariableValue(1).text()).toBe('true');
expect(vm.$el.querySelector('.js-build-variables').textContent).toContain(
'UPLOAD_TO_S3',
);
expect(vm.$el.querySelector('.js-build-value').textContent).toContain('••••••');
})
.then(done)
.catch(done.fail);
});
});
}); });
describe('without variables', () => {
it('does not render variables', () => {
vm = mountComponent(Component, { trigger: {} });
expect(vm.$el.querySelector('.js-reveal-variables')).toBeNull();
expect(vm.$el.querySelector('.js-build-variables')).toBeNull();
}); });
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment