Commit f3256282 authored by Scott Hampton's avatar Scott Hampton

Merge branch 'pb-refactor-trigger-block-debt' into 'master'

Refactor trigger block and spec

See merge request gitlab-org/gitlab!53151
parents 7b958af0 6839f2bc
<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 = '••••••'; const DEFAULT_TD_CLASSES = 'gl-w-half gl-font-sm! gl-border-gray-200!';
const DEFAULT_TH_CLASSES =
'gl-bg-transparent! gl-border-b-solid! gl-border-b-gray-200! gl-border-b-1!';
export default { export default {
fields: [
{
key: 'key',
label: __('Key'),
tdAttr: { 'data-testid': 'trigger-build-key' },
tdClass: DEFAULT_TD_CLASSES,
thClass: DEFAULT_TH_CLASSES,
},
{
key: 'value',
label: __('Value'),
tdAttr: { 'data-testid': 'trigger-build-value' },
tdClass: DEFAULT_TD_CLASSES,
thClass: DEFAULT_TH_CLASSES,
},
],
components: { components: {
GlButton, GlButton,
GlTable,
}, },
props: { props: {
trigger: { trigger: {
...@@ -21,7 +40,7 @@ export default { ...@@ -21,7 +40,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 +54,41 @@ export default { ...@@ -35,45 +54,41 @@ 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" v-if="hasValues"
class="group js-reveal-variables trigger-variables-btn" class="gl-mt-2"
size="small" size="small"
data-testid="trigger-reveal-values-button"
@click="toggleValues" @click="toggleValues"
>{{ getToggleButtonText }}</gl-button >{{ getToggleButtonText }}</gl-button
> >
</p> </p>
<table class="js-build-variables trigger-build-variables"> <gl-table :items="trigger.variables" :fields="$options.fields" small bordered>
<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>
...@@ -533,10 +533,10 @@ RSpec.describe 'Jobs', :clean_gitlab_redis_shared_state do ...@@ -533,10 +533,10 @@ RSpec.describe 'Jobs', :clean_gitlab_redis_shared_state do
expect(page).to have_content('Trigger token') expect(page).to have_content('Trigger token')
expect(page).to have_content('Trigger variables') expect(page).to have_content('Trigger variables')
expect(page).not_to have_css('.js-reveal-variables') expect(page).not_to have_selector('[data-testid="trigger-reveal-values-button"]')
expect(page).to have_selector('.js-build-variable', text: 'TRIGGER_KEY_1') expect(page).to have_selector('[data-testid="trigger-build-key"]', text: 'TRIGGER_KEY_1')
expect(page).to have_selector('.js-build-value', text: '••••••') expect(page).to have_selector('[data-testid="trigger-build-value"]', text: '••••••')
end end
end end
...@@ -571,17 +571,17 @@ RSpec.describe 'Jobs', :clean_gitlab_redis_shared_state do ...@@ -571,17 +571,17 @@ RSpec.describe 'Jobs', :clean_gitlab_redis_shared_state do
expect(page).to have_content('Trigger token') expect(page).to have_content('Trigger token')
expect(page).to have_content('Trigger variables') expect(page).to have_content('Trigger variables')
expect(page).to have_css('.js-reveal-variables') expect(page).to have_selector('[data-testid="trigger-reveal-values-button"]')
expect(page).to have_selector('.js-build-variable', text: 'TRIGGER_KEY_1') expect(page).to have_selector('[data-testid="trigger-build-key"]', text: 'TRIGGER_KEY_1')
expect(page).to have_selector('.js-build-value', text: '••••••') expect(page).to have_selector('[data-testid="trigger-build-value"]', text: '••••••')
end end
it 'reveals values on button click', :js do it 'reveals values on button click', :js do
click_button 'Reveal values' click_button 'Reveal values'
expect(page).to have_selector('.js-build-variable', text: 'TRIGGER_KEY_1') expect(page).to have_selector('[data-testid="trigger-build-key"]', text: 'TRIGGER_KEY_1')
expect(page).to have_selector('.js-build-value', text: 'TRIGGER_VALUE_1') expect(page).to have_selector('[data-testid="trigger-build-value"]', text: 'TRIGGER_VALUE_1')
end end
end end
......
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 and no variables', () => {
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, { const hiddenValue = '••••••';
const gcsVar = { key: 'UPLOAD_TO_GCS', value: 'false', public: false };
const s3Var = { key: 'UPLOAD_TO_S3', value: 'true', public: false };
createComponent({
trigger: { trigger: {
short_token: 'bd7e', variables: [gcsVar, s3Var],
variables: [
{ key: 'UPLOAD_TO_GCS', value: 'false', public: false },
{ key: 'UPLOAD_TO_S3', value: 'true', public: false },
],
}, },
}); });
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(hiddenValue);
expect(vm.$el.querySelector('.js-build-variables').textContent).toContain( expect(findVariableValue(1).text()).toBe(hiddenValue);
'UPLOAD_TO_S3',
);
expect(vm.$el.querySelector('.js-build-variables').textContent).toContain('true'); expect(findVariableKey(0).text()).toBe(gcsVar.key);
expect(findVariableKey(1).text()).toBe(s3Var.key);
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(gcsVar.value);
expect(findVariableValue(1).text()).toBe(s3Var.value);
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