Commit b96db550 authored by pburdette's avatar pburdette

Add more coverage

Add a bit more test coverage
to ensure the button is tested
properly.
parent 87ae36b1
---
title: Disable trigger manual job button after click
merge_request: 57885
author:
type: fixed
import { GlButton } from '@gitlab/ui'; import { GlButton } from '@gitlab/ui';
import { shallowMount, createLocalVue } from '@vue/test-utils'; import { createLocalVue, mount, shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
import Form from '~/jobs/components/manual_variables_form.vue'; import Form from '~/jobs/components/manual_variables_form.vue';
const localVue = createLocalVue(); const localVue = createLocalVue();
Vue.use(Vuex);
describe('Manual Variables Form', () => { describe('Manual Variables Form', () => {
let wrapper; let wrapper;
let store;
const requiredProps = { const requiredProps = {
action: { action: {
...@@ -16,19 +21,22 @@ describe('Manual Variables Form', () => { ...@@ -16,19 +21,22 @@ describe('Manual Variables Form', () => {
variablesSettingsUrl: '/settings', variablesSettingsUrl: '/settings',
}; };
const factory = (props = {}) => { const createComponent = (props = {}, mountFn = shallowMount) => {
wrapper = shallowMount(localVue.extend(Form), { store = new Vuex.Store({
actions: {
triggerManualJob: jest.fn(),
},
});
wrapper = mountFn(localVue.extend(Form), {
propsData: props, propsData: props,
localVue, localVue,
store,
}); });
}; };
const findTriggerBtn = () => wrapper.find('[data-testid="trigger-manual-job-btn"]'); const findTriggerBtn = () => wrapper.find('[data-testid="trigger-manual-job-btn"]');
beforeEach(() => {
factory(requiredProps);
});
afterEach((done) => { afterEach((done) => {
// The component has a `nextTick` callback after some events so we need // The component has a `nextTick` callback after some events so we need
// to wait for those to finish before destroying. // to wait for those to finish before destroying.
...@@ -40,70 +48,92 @@ describe('Manual Variables Form', () => { ...@@ -40,70 +48,92 @@ describe('Manual Variables Form', () => {
}); });
}); });
it('renders empty form with correct placeholders', () => { describe('shallowMount', () => {
expect(wrapper.find({ ref: 'inputKey' }).attributes('placeholder')).toBe('Input variable key'); beforeEach(() => {
expect(wrapper.find({ ref: 'inputSecretValue' }).attributes('placeholder')).toBe( createComponent(requiredProps);
'Input variable value', });
);
});
it('renders help text with provided link', () => { it('renders empty form with correct placeholders', () => {
expect(wrapper.find('p').text()).toBe( expect(wrapper.find({ ref: 'inputKey' }).attributes('placeholder')).toBe(
'Specify variable values to be used in this run. The values specified in CI/CD settings will be used as default', 'Input variable key',
); );
expect(wrapper.find({ ref: 'inputSecretValue' }).attributes('placeholder')).toBe(
'Input variable value',
);
});
expect(wrapper.find('a').attributes('href')).toBe(requiredProps.variablesSettingsUrl); it('renders help text with provided link', () => {
}); expect(wrapper.find('p').text()).toBe(
'Specify variable values to be used in this run. The values specified in CI/CD settings will be used as default',
);
it('trigger button is not disabled on mount', () => { expect(wrapper.find('a').attributes('href')).toBe(requiredProps.variablesSettingsUrl);
expect(findTriggerBtn().props('disabled')).toBe(false); });
});
describe('when adding a new variable', () => { describe('when adding a new variable', () => {
it('creates a new variable when user types a new key and resets the form', (done) => { it('creates a new variable when user types a new key and resets the form', (done) => {
wrapper.vm wrapper.vm
.$nextTick() .$nextTick()
.then(() => wrapper.find({ ref: 'inputKey' }).setValue('new key')) .then(() => wrapper.find({ ref: 'inputKey' }).setValue('new key'))
.then(() => { .then(() => {
expect(wrapper.vm.variables.length).toBe(1); expect(wrapper.vm.variables.length).toBe(1);
expect(wrapper.vm.variables[0].key).toBe('new key'); expect(wrapper.vm.variables[0].key).toBe('new key');
expect(wrapper.find({ ref: 'inputKey' }).attributes('value')).toBe(undefined); expect(wrapper.find({ ref: 'inputKey' }).attributes('value')).toBe(undefined);
}) })
.then(done) .then(done)
.catch(done.fail); .catch(done.fail);
});
it('creates a new variable when user types a new value and resets the form', (done) => {
wrapper.vm
.$nextTick()
.then(() => wrapper.find({ ref: 'inputSecretValue' }).setValue('new value'))
.then(() => {
expect(wrapper.vm.variables.length).toBe(1);
expect(wrapper.vm.variables[0].secret_value).toBe('new value');
expect(wrapper.find({ ref: 'inputSecretValue' }).attributes('value')).toBe(undefined);
})
.then(done)
.catch(done.fail);
});
}); });
it('creates a new variable when user types a new value and resets the form', (done) => { describe('when deleting a variable', () => {
wrapper.vm beforeEach((done) => {
.$nextTick() wrapper.vm.variables = [
.then(() => wrapper.find({ ref: 'inputSecretValue' }).setValue('new value')) {
.then(() => { key: 'new key',
expect(wrapper.vm.variables.length).toBe(1); secret_value: 'value',
expect(wrapper.vm.variables[0].secret_value).toBe('new value'); id: '1',
expect(wrapper.find({ ref: 'inputSecretValue' }).attributes('value')).toBe(undefined); },
}) ];
.then(done)
.catch(done.fail); wrapper.vm.$nextTick(done);
});
it('removes the variable row', () => {
wrapper.find(GlButton).vm.$emit('click');
expect(wrapper.vm.variables.length).toBe(0);
});
}); });
}); });
describe('when deleting a variable', () => { describe('mount', () => {
beforeEach((done) => { beforeEach(() => {
wrapper.vm.variables = [ createComponent(requiredProps, mount);
{ });
key: 'new key',
secret_value: 'value', it('trigger button is not disabled on mount', () => {
id: '1', expect(findTriggerBtn().props('disabled')).toBe(false);
},
];
wrapper.vm.$nextTick(done);
}); });
it('removes the variable row', () => { it('trigger button is disabled after trigger action', async () => {
wrapper.find(GlButton).vm.$emit('click'); expect(findTriggerBtn().props('disabled')).toBe(false);
await findTriggerBtn().trigger('click');
expect(wrapper.vm.variables.length).toBe(0); expect(findTriggerBtn().props('disabled')).toBe(true);
}); });
}); });
}); });
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