2019-10-16 14:08:01 -04:00
|
|
|
import { mount } from '@vue/test-utils';
|
2020-08-17 17:09:56 -04:00
|
|
|
import axios from 'axios';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { merge } from 'lodash';
|
|
|
|
import Vuex from 'vuex';
|
2021-10-07 14:11:28 -04:00
|
|
|
import originalRelease from 'test_fixtures/api/releases/release.json';
|
2021-07-27 08:10:54 -04:00
|
|
|
import setWindowLocation from 'helpers/set_window_location_helper';
|
2021-07-01 17:08:38 -04:00
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2020-03-04 04:08:20 -05:00
|
|
|
import * as commonUtils from '~/lib/utils/common_utils';
|
2021-02-14 13:09:20 -05:00
|
|
|
import ReleaseEditNewApp from '~/releases/components/app_edit_new.vue';
|
2020-04-01 08:08:00 -04:00
|
|
|
import AssetLinksForm from '~/releases/components/asset_links_form.vue';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { BACK_URL_PARAM } from '~/releases/constants';
|
2019-10-16 14:08:01 -04:00
|
|
|
|
2020-09-30 14:09:52 -04:00
|
|
|
const originalMilestones = originalRelease.milestones;
|
2021-07-01 17:08:38 -04:00
|
|
|
const releasesPagePath = 'path/to/releases/page';
|
2020-09-30 14:09:52 -04:00
|
|
|
|
2020-07-21 17:09:12 -04:00
|
|
|
describe('Release edit/new component', () => {
|
2019-10-16 14:08:01 -04:00
|
|
|
let wrapper;
|
2020-03-04 04:08:20 -05:00
|
|
|
let release;
|
2019-10-16 14:08:01 -04:00
|
|
|
let actions;
|
2020-04-14 14:09:54 -04:00
|
|
|
let getters;
|
2019-11-12 13:06:57 -05:00
|
|
|
let state;
|
2020-05-07 17:09:26 -04:00
|
|
|
let mock;
|
2019-10-16 14:08:01 -04:00
|
|
|
|
2020-10-06 23:08:19 -04:00
|
|
|
const factory = async ({ featureFlags = {}, store: storeUpdates = {} } = {}) => {
|
2019-11-12 13:06:57 -05:00
|
|
|
state = {
|
2020-03-04 04:08:20 -05:00
|
|
|
release,
|
2019-10-16 14:08:01 -04:00
|
|
|
markdownDocsPath: 'path/to/markdown/docs',
|
2021-07-01 17:08:38 -04:00
|
|
|
releasesPagePath,
|
2020-05-07 17:09:26 -04:00
|
|
|
projectId: '8',
|
2020-11-10 16:08:51 -05:00
|
|
|
groupId: '42',
|
|
|
|
groupMilestonesAvailable: true,
|
2019-10-16 14:08:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
actions = {
|
2020-08-10 11:09:49 -04:00
|
|
|
initializeRelease: jest.fn(),
|
|
|
|
saveRelease: jest.fn(),
|
2020-04-01 08:08:00 -04:00
|
|
|
addEmptyAssetLink: jest.fn(),
|
2019-10-16 14:08:01 -04:00
|
|
|
};
|
|
|
|
|
2020-04-14 14:09:54 -04:00
|
|
|
getters = {
|
|
|
|
isValid: () => true,
|
2020-07-30 17:09:35 -04:00
|
|
|
isExistingRelease: () => true,
|
2020-04-14 14:09:54 -04:00
|
|
|
validationErrors: () => ({
|
|
|
|
assets: {
|
|
|
|
links: [],
|
2020-02-14 16:09:08 -05:00
|
|
|
},
|
2020-04-14 14:09:54 -04:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = new Vuex.Store(
|
|
|
|
merge(
|
|
|
|
{
|
|
|
|
modules: {
|
2021-03-25 17:09:13 -04:00
|
|
|
editNew: {
|
2020-04-14 14:09:54 -04:00
|
|
|
namespaced: true,
|
|
|
|
actions,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
storeUpdates,
|
|
|
|
),
|
|
|
|
);
|
2019-10-16 14:08:01 -04:00
|
|
|
|
2020-07-21 17:09:12 -04:00
|
|
|
wrapper = mount(ReleaseEditNewApp, {
|
2020-01-10 04:07:49 -05:00
|
|
|
store,
|
2020-04-01 08:08:00 -04:00
|
|
|
provide: {
|
|
|
|
glFeatures: featureFlags,
|
|
|
|
},
|
2020-01-10 04:07:49 -05:00
|
|
|
});
|
2020-08-10 11:09:49 -04:00
|
|
|
|
2020-10-06 23:08:19 -04:00
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
wrapper.element.querySelectorAll('input').forEach((input) => jest.spyOn(input, 'focus'));
|
2020-03-04 04:08:20 -05:00
|
|
|
};
|
2019-10-16 14:08:01 -04:00
|
|
|
|
2020-03-04 04:08:20 -05:00
|
|
|
beforeEach(() => {
|
2021-07-27 08:10:54 -04:00
|
|
|
setWindowLocation(TEST_HOST);
|
2021-07-01 17:08:38 -04:00
|
|
|
|
2020-05-07 17:09:26 -04:00
|
|
|
mock = new MockAdapter(axios);
|
2020-03-04 04:08:20 -05:00
|
|
|
gon.api_version = 'v4';
|
2019-10-16 14:08:01 -04:00
|
|
|
|
2020-05-07 17:09:26 -04:00
|
|
|
mock.onGet('/api/v4/projects/8/milestones').reply(200, originalMilestones);
|
|
|
|
|
2020-03-04 04:08:20 -05:00
|
|
|
release = commonUtils.convertObjectPropsToCamelCase(originalRelease, { deep: true });
|
2019-10-16 14:08:01 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 04:08:20 -05:00
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
2019-10-16 14:08:01 -04:00
|
|
|
});
|
|
|
|
|
2020-04-14 14:09:54 -04:00
|
|
|
const findSubmitButton = () => wrapper.find('button[type=submit]');
|
2020-08-13 20:10:28 -04:00
|
|
|
const findForm = () => wrapper.find('form');
|
2020-04-14 14:09:54 -04:00
|
|
|
|
2020-03-04 04:08:20 -05:00
|
|
|
describe(`basic functionality tests: all tests unrelated to the "${BACK_URL_PARAM}" parameter`, () => {
|
2020-10-06 23:08:19 -04:00
|
|
|
beforeEach(async () => {
|
|
|
|
await factory();
|
|
|
|
});
|
2019-10-16 14:08:01 -04:00
|
|
|
|
2020-08-10 11:09:49 -04:00
|
|
|
it('calls initializeRelease when the component is created', () => {
|
|
|
|
expect(actions.initializeRelease).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('focuses the first non-disabled input element once the page is shown', () => {
|
|
|
|
const firstEnabledInput = wrapper.element.querySelector('input:enabled');
|
|
|
|
const allInputs = wrapper.element.querySelectorAll('input');
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
allInputs.forEach((input) => {
|
2020-08-10 11:09:49 -04:00
|
|
|
const expectedFocusCalls = input === firstEnabledInput ? 1 : 0;
|
|
|
|
expect(input.focus).toHaveBeenCalledTimes(expectedFocusCalls);
|
|
|
|
});
|
2020-03-04 04:08:20 -05:00
|
|
|
});
|
2019-11-12 13:06:57 -05:00
|
|
|
|
2020-03-04 04:08:20 -05:00
|
|
|
it('renders the description text at the top of the page', () => {
|
|
|
|
expect(wrapper.find('.js-subtitle-text').text()).toBe(
|
2021-03-30 14:10:47 -04:00
|
|
|
'Releases are based on Git tags. We recommend tags that use semantic versioning, for example v1.0.0, v2.1.0-pre.',
|
2020-03-04 04:08:20 -05:00
|
|
|
);
|
|
|
|
});
|
2019-10-16 14:08:01 -04:00
|
|
|
|
2020-03-04 04:08:20 -05:00
|
|
|
it('renders the correct release title in the "Release title" field', () => {
|
|
|
|
expect(wrapper.find('#release-title').element.value).toBe(release.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the release notes in the "Release notes" textarea', () => {
|
|
|
|
expect(wrapper.find('#release-notes').element.value).toBe(release.description);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the "Save changes" button as type="submit"', () => {
|
2020-04-14 14:09:54 -04:00
|
|
|
expect(findSubmitButton().attributes('type')).toBe('submit');
|
2020-03-04 04:08:20 -05:00
|
|
|
});
|
2019-10-16 14:08:01 -04:00
|
|
|
|
2020-08-10 11:09:49 -04:00
|
|
|
it('calls saveRelease when the form is submitted', () => {
|
2020-08-13 20:10:28 -04:00
|
|
|
findForm().trigger('submit');
|
|
|
|
|
2020-08-10 11:09:49 -04:00
|
|
|
expect(actions.saveRelease).toHaveBeenCalledTimes(1);
|
2020-03-04 04:08:20 -05:00
|
|
|
});
|
2019-10-16 14:08:01 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 04:08:20 -05:00
|
|
|
describe(`when the URL does not contain a "${BACK_URL_PARAM}" parameter`, () => {
|
2020-10-06 23:08:19 -04:00
|
|
|
beforeEach(async () => {
|
|
|
|
await factory();
|
|
|
|
});
|
2020-03-04 04:08:20 -05:00
|
|
|
|
|
|
|
it(`renders a "Cancel" button with an href pointing to "${BACK_URL_PARAM}"`, () => {
|
|
|
|
const cancelButton = wrapper.find('.js-cancel-button');
|
|
|
|
expect(cancelButton.attributes().href).toBe(state.releasesPagePath);
|
|
|
|
});
|
2019-10-16 14:08:01 -04:00
|
|
|
});
|
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
// eslint-disable-next-line no-script-url
|
|
|
|
const xssBackUrl = 'javascript:alert(1)';
|
|
|
|
describe.each`
|
|
|
|
backUrl | expectedHref
|
|
|
|
${`${TEST_HOST}/back/url`} | ${`${TEST_HOST}/back/url`}
|
|
|
|
${`/back/url?page=2`} | ${`/back/url?page=2`}
|
|
|
|
${`back/url?page=3`} | ${`back/url?page=3`}
|
|
|
|
${'http://phishing.test/back/url'} | ${releasesPagePath}
|
|
|
|
${'//phishing.test/back/url'} | ${releasesPagePath}
|
|
|
|
${xssBackUrl} | ${releasesPagePath}
|
|
|
|
`(
|
|
|
|
`when the URL contains a "${BACK_URL_PARAM}=$backUrl" parameter`,
|
|
|
|
({ backUrl, expectedHref }) => {
|
|
|
|
beforeEach(async () => {
|
2021-07-27 08:10:54 -04:00
|
|
|
setWindowLocation(`${TEST_HOST}?${BACK_URL_PARAM}=${encodeURIComponent(backUrl)}`);
|
2021-07-01 17:08:38 -04:00
|
|
|
|
|
|
|
await factory();
|
|
|
|
});
|
2020-03-04 04:08:20 -05:00
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
it(`renders a "Cancel" button with an href pointing to ${expectedHref}`, () => {
|
|
|
|
const cancelButton = wrapper.find('.js-cancel-button');
|
|
|
|
expect(cancelButton.attributes().href).toBe(expectedHref);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-08-10 11:09:49 -04:00
|
|
|
describe('when creating a new release', () => {
|
2020-10-06 23:08:19 -04:00
|
|
|
beforeEach(async () => {
|
|
|
|
await factory({
|
2020-08-10 11:09:49 -04:00
|
|
|
store: {
|
|
|
|
modules: {
|
2021-03-25 17:09:13 -04:00
|
|
|
editNew: {
|
2020-08-10 11:09:49 -04:00
|
|
|
getters: {
|
|
|
|
isExistingRelease: () => false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the submit button with the text "Create release"', () => {
|
|
|
|
expect(findSubmitButton().text()).toBe('Create release');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when editing an existing release', () => {
|
2020-10-06 23:08:19 -04:00
|
|
|
beforeEach(async () => {
|
|
|
|
await factory();
|
|
|
|
});
|
2020-08-10 11:09:49 -04:00
|
|
|
|
|
|
|
it('renders the submit button with the text "Save changes"', () => {
|
|
|
|
expect(findSubmitButton().text()).toBe('Save changes');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-01 08:08:00 -04:00
|
|
|
describe('asset links form', () => {
|
2020-10-06 23:08:19 -04:00
|
|
|
beforeEach(factory);
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-10-06 23:08:19 -04:00
|
|
|
it('renders the asset links portion of the form', () => {
|
|
|
|
expect(wrapper.find(AssetLinksForm).exists()).toBe(true);
|
2020-04-01 08:08:00 -04:00
|
|
|
});
|
|
|
|
});
|
2020-04-14 14:09:54 -04:00
|
|
|
|
|
|
|
describe('validation', () => {
|
|
|
|
describe('when the form is valid', () => {
|
2020-10-06 23:08:19 -04:00
|
|
|
beforeEach(async () => {
|
|
|
|
await factory({
|
2020-04-14 14:09:54 -04:00
|
|
|
store: {
|
|
|
|
modules: {
|
2021-03-25 17:09:13 -04:00
|
|
|
editNew: {
|
2020-04-14 14:09:54 -04:00
|
|
|
getters: {
|
|
|
|
isValid: () => true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the submit button as enabled', () => {
|
|
|
|
expect(findSubmitButton().attributes('disabled')).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the form is invalid', () => {
|
2020-10-06 23:08:19 -04:00
|
|
|
beforeEach(async () => {
|
|
|
|
await factory({
|
2020-04-14 14:09:54 -04:00
|
|
|
store: {
|
|
|
|
modules: {
|
2021-03-25 17:09:13 -04:00
|
|
|
editNew: {
|
2020-04-14 14:09:54 -04:00
|
|
|
getters: {
|
|
|
|
isValid: () => false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the submit button as disabled', () => {
|
|
|
|
expect(findSubmitButton().attributes('disabled')).toBe('disabled');
|
|
|
|
});
|
2020-08-13 20:10:28 -04:00
|
|
|
|
|
|
|
it('does not allow the form to be submitted', () => {
|
|
|
|
findForm().trigger('submit');
|
|
|
|
|
|
|
|
expect(actions.saveRelease).not.toHaveBeenCalled();
|
|
|
|
});
|
2020-04-14 14:09:54 -04:00
|
|
|
});
|
|
|
|
});
|
2019-10-16 14:08:01 -04:00
|
|
|
});
|