2018-06-13 18:48:59 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-01-14 19:10:45 -05:00
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2019-07-16 06:02:37 -04:00
|
|
|
import confidentialState from '~/confidential_merge_request/state';
|
2022-01-07 07:10:47 -05:00
|
|
|
import CreateMergeRequestDropdown from '~/issues/create_merge_request_dropdown';
|
2021-02-14 13:09:20 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2018-06-13 18:48:59 -04:00
|
|
|
|
|
|
|
describe('CreateMergeRequestDropdown', () => {
|
|
|
|
let axiosMock;
|
|
|
|
let dropdown;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock = new MockAdapter(axios);
|
|
|
|
|
2019-07-05 06:14:56 -04:00
|
|
|
document.body.innerHTML = `
|
2018-06-13 18:48:59 -04:00
|
|
|
<div id="dummy-wrapper-element">
|
|
|
|
<div class="available"></div>
|
|
|
|
<div class="unavailable">
|
2022-03-23 08:07:27 -04:00
|
|
|
<div class="js-create-mr-spinner"></div>
|
2018-06-13 18:48:59 -04:00
|
|
|
<div class="text"></div>
|
|
|
|
</div>
|
|
|
|
<div class="js-ref"></div>
|
2019-07-05 06:14:56 -04:00
|
|
|
<div class="js-create-mr"></div>
|
2021-03-23 11:09:28 -04:00
|
|
|
<div class="js-create-merge-request">
|
|
|
|
<span class="js-spinner"></span>
|
|
|
|
</div>
|
2018-06-13 18:48:59 -04:00
|
|
|
<div class="js-create-target"></div>
|
|
|
|
<div class="js-dropdown-toggle"></div>
|
|
|
|
</div>
|
2019-07-05 06:14:56 -04:00
|
|
|
`;
|
2018-06-13 18:48:59 -04:00
|
|
|
|
|
|
|
const dummyElement = document.getElementById('dummy-wrapper-element');
|
|
|
|
dropdown = new CreateMergeRequestDropdown(dummyElement);
|
|
|
|
dropdown.refsPath = `${TEST_HOST}/dummy/refs?search=`;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
axiosMock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getRef', () => {
|
2022-04-18 14:10:36 -04:00
|
|
|
it('escapes branch names correctly', async () => {
|
2018-06-13 18:48:59 -04:00
|
|
|
const endpoint = `${dropdown.refsPath}contains%23hash`;
|
2019-07-05 06:14:56 -04:00
|
|
|
jest.spyOn(axios, 'get');
|
2018-06-13 18:48:59 -04:00
|
|
|
axiosMock.onGet(endpoint).replyOnce({});
|
|
|
|
|
2022-04-18 14:10:36 -04:00
|
|
|
await dropdown.getRef('contains#hash');
|
|
|
|
expect(axios.get).toHaveBeenCalledWith(
|
|
|
|
endpoint,
|
|
|
|
expect.objectContaining({ cancelToken: expect.anything() }),
|
|
|
|
);
|
2018-06-13 18:48:59 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('updateCreatePaths', () => {
|
|
|
|
it('escapes branch names correctly', () => {
|
|
|
|
dropdown.createBranchPath = `${TEST_HOST}/branches?branch_name=some-branch&issue=42`;
|
2022-03-07 13:19:30 -05:00
|
|
|
dropdown.createMrPath = `${TEST_HOST}/create_merge_request?merge_request%5Bsource_branch%5D=test&merge_request%5Btarget_branch%5D=master&merge_request%5Bissue_iid%5D=42`;
|
2018-06-13 18:48:59 -04:00
|
|
|
|
|
|
|
dropdown.updateCreatePaths('branch', 'contains#hash');
|
|
|
|
|
|
|
|
expect(dropdown.createBranchPath).toBe(
|
|
|
|
`${TEST_HOST}/branches?branch_name=contains%23hash&issue=42`,
|
|
|
|
);
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-06-13 18:48:59 -04:00
|
|
|
expect(dropdown.createMrPath).toBe(
|
2022-03-07 13:19:30 -05:00
|
|
|
`${TEST_HOST}/create_merge_request?merge_request%5Bsource_branch%5D=contains%23hash&merge_request%5Btarget_branch%5D=master&merge_request%5Bissue_iid%5D=42`,
|
2018-06-13 18:48:59 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2019-07-16 06:02:37 -04:00
|
|
|
|
|
|
|
describe('enable', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
dropdown.createMergeRequestButton.classList.add('disabled');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
confidentialState.selectedProject = {};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('enables button when not confidential issue', () => {
|
|
|
|
dropdown.enable();
|
|
|
|
|
|
|
|
expect(dropdown.createMergeRequestButton.classList).not.toContain('disabled');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('enables when can create confidential issue', () => {
|
|
|
|
document.querySelector('.js-create-mr').setAttribute('data-is-confidential', 'true');
|
|
|
|
confidentialState.selectedProject = { name: 'test' };
|
|
|
|
|
|
|
|
dropdown.enable();
|
|
|
|
|
|
|
|
expect(dropdown.createMergeRequestButton.classList).not.toContain('disabled');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not enable when can not create confidential issue', () => {
|
|
|
|
document.querySelector('.js-create-mr').setAttribute('data-is-confidential', 'true');
|
|
|
|
|
|
|
|
dropdown.enable();
|
|
|
|
|
|
|
|
expect(dropdown.createMergeRequestButton.classList).toContain('disabled');
|
|
|
|
});
|
|
|
|
});
|
2021-03-23 11:09:28 -04:00
|
|
|
|
|
|
|
describe('setLoading', () => {
|
|
|
|
it.each`
|
|
|
|
loading | hasClass
|
|
|
|
${true} | ${false}
|
|
|
|
${false} | ${true}
|
|
|
|
`('it toggle loading spinner when loading is $loading', ({ loading, hasClass }) => {
|
|
|
|
dropdown.setLoading(loading);
|
|
|
|
|
|
|
|
expect(document.querySelector('.js-spinner').classList.contains('gl-display-none')).toEqual(
|
|
|
|
hasClass,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-06-13 18:48:59 -04:00
|
|
|
});
|