debian-mirror-gitlab/spec/frontend/pipeline_new/components/pipeline_new_form_spec.js

398 lines
12 KiB
JavaScript
Raw Normal View History

2021-04-17 20:07:23 +05:30
import { GlForm, GlSprintf, GlLoadingIcon } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { mount, shallowMount } from '@vue/test-utils';
2020-11-24 15:15:51 +05:30
import MockAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils';
2021-03-11 19:13:27 +05:30
import httpStatusCodes from '~/lib/utils/http_status';
import { redirectTo } from '~/lib/utils/url_utility';
2020-10-24 23:57:45 +05:30
import PipelineNewForm from '~/pipeline_new/components/pipeline_new_form.vue';
2021-04-17 20:07:23 +05:30
import RefsDropdown from '~/pipeline_new/components/refs_dropdown.vue';
import { mockQueryParams, mockPostParams, mockProjectId, mockError, mockRefs } from '../mock_data';
2020-11-24 15:15:51 +05:30
jest.mock('~/lib/utils/url_utility', () => ({
redirectTo: jest.fn(),
}));
2021-04-17 20:07:23 +05:30
const projectRefsEndpoint = '/root/project/refs';
2021-01-03 14:25:43 +05:30
const pipelinesPath = '/root/project/-/pipelines';
const configVariablesPath = '/root/project/-/pipelines/config_variables';
2021-04-17 20:07:23 +05:30
const newPipelinePostResponse = { id: 1 };
const defaultBranch = 'master';
2020-10-24 23:57:45 +05:30
describe('Pipeline New Form', () => {
let wrapper;
2020-11-24 15:15:51 +05:30
let mock;
2021-04-17 20:07:23 +05:30
let dummySubmitEvent;
2020-10-24 23:57:45 +05:30
const findForm = () => wrapper.find(GlForm);
2021-04-17 20:07:23 +05:30
const findRefsDropdown = () => wrapper.findComponent(RefsDropdown);
2021-03-11 19:13:27 +05:30
const findSubmitButton = () => wrapper.find('[data-testid="run_pipeline_button"]');
2020-10-24 23:57:45 +05:30
const findVariableRows = () => wrapper.findAll('[data-testid="ci-variable-row"]');
const findRemoveIcons = () => wrapper.findAll('[data-testid="remove-ci-variable-row"]');
const findKeyInputs = () => wrapper.findAll('[data-testid="pipeline-form-ci-variable-key"]');
2021-01-03 14:25:43 +05:30
const findValueInputs = () => wrapper.findAll('[data-testid="pipeline-form-ci-variable-value"]');
2020-11-24 15:15:51 +05:30
const findErrorAlert = () => wrapper.find('[data-testid="run-pipeline-error-alert"]');
const findWarningAlert = () => wrapper.find('[data-testid="run-pipeline-warning-alert"]');
const findWarningAlertSummary = () => findWarningAlert().find(GlSprintf);
const findWarnings = () => wrapper.findAll('[data-testid="run-pipeline-warning"]');
2021-01-29 00:20:46 +05:30
const findLoadingIcon = () => wrapper.find(GlLoadingIcon);
2021-04-17 20:07:23 +05:30
const getFormPostParams = () => JSON.parse(mock.history.post[0].data);
const selectBranch = (branch) => {
// Select a branch in the dropdown
findRefsDropdown().vm.$emit('input', {
shortName: branch,
fullName: `refs/heads/${branch}`,
});
};
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
const createComponent = (props = {}, method = shallowMount) => {
2020-10-24 23:57:45 +05:30
wrapper = method(PipelineNewForm, {
2021-04-17 20:07:23 +05:30
provide: {
projectRefsEndpoint,
},
2020-10-24 23:57:45 +05:30
propsData: {
projectId: mockProjectId,
2020-11-24 15:15:51 +05:30
pipelinesPath,
2021-01-03 14:25:43 +05:30
configVariablesPath,
2021-04-17 20:07:23 +05:30
defaultBranch,
refParam: defaultBranch,
2020-10-24 23:57:45 +05:30
settingsLink: '',
2020-11-24 15:15:51 +05:30
maxWarnings: 25,
2020-10-24 23:57:45 +05:30
...props,
},
});
};
beforeEach(() => {
2020-11-24 15:15:51 +05:30
mock = new MockAdapter(axios);
2021-01-03 14:25:43 +05:30
mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {});
2021-04-17 20:07:23 +05:30
mock.onGet(projectRefsEndpoint).reply(httpStatusCodes.OK, mockRefs);
dummySubmitEvent = {
preventDefault: jest.fn(),
};
2020-10-24 23:57:45 +05:30
});
afterEach(() => {
wrapper.destroy();
wrapper = null;
2020-11-24 15:15:51 +05:30
mock.restore();
2020-10-24 23:57:45 +05:30
});
describe('Form', () => {
2021-01-03 14:25:43 +05:30
beforeEach(async () => {
2021-04-17 20:07:23 +05:30
createComponent(mockQueryParams, mount);
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
mock.onPost(pipelinesPath).reply(httpStatusCodes.OK, newPipelinePostResponse);
2021-01-03 14:25:43 +05:30
await waitForPromises();
2020-10-24 23:57:45 +05:30
});
2021-01-03 14:25:43 +05:30
2020-11-24 15:15:51 +05:30
it('displays the correct values for the provided query params', async () => {
2021-04-17 20:07:23 +05:30
expect(findRefsDropdown().props('value')).toEqual({ shortName: 'tag-1' });
2021-01-03 14:25:43 +05:30
expect(findVariableRows()).toHaveLength(3);
});
2020-10-24 23:57:45 +05:30
2021-01-03 14:25:43 +05:30
it('displays a variable from provided query params', () => {
expect(findKeyInputs().at(0).element.value).toBe('test_var');
expect(findValueInputs().at(0).element.value).toBe('test_var_val');
});
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
it('displays an empty variable for the user to fill out', async () => {
expect(findKeyInputs().at(2).element.value).toBe('');
expect(findValueInputs().at(2).element.value).toBe('');
2020-10-24 23:57:45 +05:30
});
it('does not display remove icon for last row', () => {
2020-11-24 15:15:51 +05:30
expect(findRemoveIcons()).toHaveLength(2);
2020-10-24 23:57:45 +05:30
});
2020-11-24 15:15:51 +05:30
it('removes ci variable row on remove icon button click', async () => {
2021-03-08 18:12:59 +05:30
findRemoveIcons().at(1).trigger('click');
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
await wrapper.vm.$nextTick();
expect(findVariableRows()).toHaveLength(2);
2020-10-24 23:57:45 +05:30
});
2020-11-24 15:15:51 +05:30
it('creates blank variable on input change event', async () => {
2021-01-03 14:25:43 +05:30
const input = findKeyInputs().at(2);
input.element.value = 'test_var_2';
input.trigger('change');
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
await wrapper.vm.$nextTick();
expect(findVariableRows()).toHaveLength(4);
2021-01-03 14:25:43 +05:30
expect(findKeyInputs().at(3).element.value).toBe('');
expect(findValueInputs().at(3).element.value).toBe('');
});
2021-02-22 17:27:13 +05:30
});
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
describe('Pipeline creation', () => {
beforeEach(async () => {
2021-04-17 20:07:23 +05:30
mock.onPost(pipelinesPath).reply(httpStatusCodes.OK, newPipelinePostResponse);
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
await waitForPromises();
});
2021-03-11 19:13:27 +05:30
2021-04-17 20:07:23 +05:30
it('does not submit the native HTML form', async () => {
createComponent();
findForm().vm.$emit('submit', dummySubmitEvent);
expect(dummySubmitEvent.preventDefault).toHaveBeenCalled();
});
2021-03-11 19:13:27 +05:30
it('disables the submit button immediately after submitting', async () => {
createComponent();
expect(findSubmitButton().props('disabled')).toBe(false);
findForm().vm.$emit('submit', dummySubmitEvent);
await waitForPromises();
expect(findSubmitButton().props('disabled')).toBe(true);
});
2021-02-22 17:27:13 +05:30
it('creates pipeline with full ref and variables', async () => {
createComponent();
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
findForm().vm.$emit('submit', dummySubmitEvent);
await waitForPromises();
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
expect(getFormPostParams().ref).toEqual(`refs/heads/${defaultBranch}`);
expect(redirectTo).toHaveBeenCalledWith(`${pipelinesPath}/${newPipelinePostResponse.id}`);
2021-02-22 17:27:13 +05:30
});
2021-03-11 19:13:27 +05:30
2021-04-17 20:07:23 +05:30
it('creates a pipeline with short ref and variables from the query params', async () => {
createComponent(mockQueryParams);
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
await waitForPromises();
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
findForm().vm.$emit('submit', dummySubmitEvent);
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
await waitForPromises();
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
expect(getFormPostParams()).toEqual(mockPostParams);
expect(redirectTo).toHaveBeenCalledWith(`${pipelinesPath}/${newPipelinePostResponse.id}`);
2021-02-22 17:27:13 +05:30
});
});
describe('When the ref has been changed', () => {
beforeEach(async () => {
2021-04-17 20:07:23 +05:30
createComponent({}, mount);
2021-02-22 17:27:13 +05:30
await waitForPromises();
});
it('variables persist between ref changes', async () => {
2021-04-17 20:07:23 +05:30
selectBranch('master');
2021-02-22 17:27:13 +05:30
await waitForPromises();
const masterInput = findKeyInputs().at(0);
masterInput.element.value = 'build_var';
masterInput.trigger('change');
await wrapper.vm.$nextTick();
2021-04-17 20:07:23 +05:30
selectBranch('branch-1');
2021-02-22 17:27:13 +05:30
await waitForPromises();
const branchOneInput = findKeyInputs().at(0);
branchOneInput.element.value = 'deploy_var';
branchOneInput.trigger('change');
await wrapper.vm.$nextTick();
2021-04-17 20:07:23 +05:30
selectBranch('master');
2021-02-22 17:27:13 +05:30
await waitForPromises();
expect(findKeyInputs().at(0).element.value).toBe('build_var');
expect(findVariableRows().length).toBe(2);
2021-04-17 20:07:23 +05:30
selectBranch('branch-1');
2021-02-22 17:27:13 +05:30
await waitForPromises();
expect(findKeyInputs().at(0).element.value).toBe('deploy_var');
expect(findVariableRows().length).toBe(2);
2021-01-03 14:25:43 +05:30
});
});
2021-03-11 19:13:27 +05:30
describe('when yml defines a variable', () => {
2021-01-03 14:25:43 +05:30
const mockYmlKey = 'yml_var';
const mockYmlValue = 'yml_var_val';
2021-03-11 19:13:27 +05:30
const mockYmlMultiLineValue = `A value
with multiple
lines`;
2021-01-03 14:25:43 +05:30
const mockYmlDesc = 'A var from yml.';
2021-03-11 19:13:27 +05:30
it('loading icon is shown when content is requested and hidden when received', async () => {
2021-04-17 20:07:23 +05:30
createComponent(mockQueryParams, mount);
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {
[mockYmlKey]: {
value: mockYmlValue,
description: mockYmlDesc,
},
});
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
expect(findLoadingIcon().exists()).toBe(true);
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
await waitForPromises();
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
expect(findLoadingIcon().exists()).toBe(false);
});
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
it('multi-line strings are added to the value field without removing line breaks', async () => {
2021-04-17 20:07:23 +05:30
createComponent(mockQueryParams, mount);
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {
[mockYmlKey]: {
value: mockYmlMultiLineValue,
description: mockYmlDesc,
},
2021-01-29 00:20:46 +05:30
});
2021-03-11 19:13:27 +05:30
await waitForPromises();
expect(findValueInputs().at(0).element.value).toBe(mockYmlMultiLineValue);
2021-01-29 00:20:46 +05:30
});
2021-03-11 19:13:27 +05:30
describe('with description', () => {
2021-01-03 14:25:43 +05:30
beforeEach(async () => {
2021-04-17 20:07:23 +05:30
createComponent(mockQueryParams, mount);
2021-01-03 14:25:43 +05:30
mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {
[mockYmlKey]: {
value: mockYmlValue,
description: mockYmlDesc,
},
});
await waitForPromises();
});
it('displays all the variables', async () => {
expect(findVariableRows()).toHaveLength(4);
});
it('displays a variable from yml', () => {
expect(findKeyInputs().at(0).element.value).toBe(mockYmlKey);
expect(findValueInputs().at(0).element.value).toBe(mockYmlValue);
});
it('displays a variable from provided query params', () => {
expect(findKeyInputs().at(1).element.value).toBe('test_var');
expect(findValueInputs().at(1).element.value).toBe('test_var_val');
});
it('adds a description to the first variable from yml', () => {
2021-03-08 18:12:59 +05:30
expect(findVariableRows().at(0).text()).toContain(mockYmlDesc);
2021-01-03 14:25:43 +05:30
});
it('removes the description when a variable key changes', async () => {
findKeyInputs().at(0).element.value = 'yml_var_modified';
2021-03-08 18:12:59 +05:30
findKeyInputs().at(0).trigger('change');
2021-01-03 14:25:43 +05:30
await wrapper.vm.$nextTick();
2021-03-08 18:12:59 +05:30
expect(findVariableRows().at(0).text()).not.toContain(mockYmlDesc);
2021-01-03 14:25:43 +05:30
});
});
2021-03-11 19:13:27 +05:30
describe('without description', () => {
2021-01-03 14:25:43 +05:30
beforeEach(async () => {
2021-04-17 20:07:23 +05:30
createComponent(mockQueryParams, mount);
2021-01-03 14:25:43 +05:30
mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {
[mockYmlKey]: {
value: mockYmlValue,
description: null,
},
});
await waitForPromises();
});
it('displays all the variables', async () => {
expect(findVariableRows()).toHaveLength(3);
});
2020-11-24 15:15:51 +05:30
});
});
describe('Form errors and warnings', () => {
beforeEach(() => {
createComponent();
2021-03-11 19:13:27 +05:30
});
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
describe('when the refs cannot be loaded', () => {
beforeEach(() => {
mock
.onGet(projectRefsEndpoint, { params: { search: '' } })
.reply(httpStatusCodes.INTERNAL_SERVER_ERROR);
findRefsDropdown().vm.$emit('loadingError');
});
it('shows both an error alert', () => {
expect(findErrorAlert().exists()).toBe(true);
expect(findWarningAlert().exists()).toBe(false);
});
});
2021-03-11 19:13:27 +05:30
describe('when the error response can be handled', () => {
beforeEach(async () => {
mock.onPost(pipelinesPath).reply(httpStatusCodes.BAD_REQUEST, mockError);
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
findForm().vm.$emit('submit', dummySubmitEvent);
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
await waitForPromises();
});
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it('shows both error and warning', () => {
expect(findErrorAlert().exists()).toBe(true);
expect(findWarningAlert().exists()).toBe(true);
});
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it('shows the correct error', () => {
expect(findErrorAlert().text()).toBe(mockError.errors[0]);
});
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it('shows the correct warning title', () => {
const { length } = mockError.warnings;
expect(findWarningAlertSummary().attributes('message')).toBe(`${length} warnings found:`);
});
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
it('shows the correct amount of warnings', () => {
expect(findWarnings()).toHaveLength(mockError.warnings.length);
});
it('re-enables the submit button', () => {
expect(findSubmitButton().props('disabled')).toBe(false);
});
2020-11-24 15:15:51 +05:30
});
2021-03-11 19:13:27 +05:30
describe('when the error response cannot be handled', () => {
beforeEach(async () => {
mock
.onPost(pipelinesPath)
.reply(httpStatusCodes.INTERNAL_SERVER_ERROR, 'something went wrong');
findForm().vm.$emit('submit', dummySubmitEvent);
await waitForPromises();
});
it('re-enables the submit button', () => {
expect(findSubmitButton().props('disabled')).toBe(false);
});
2020-10-24 23:57:45 +05:30
});
});
});