debian-mirror-gitlab/spec/frontend/create_merge_request_dropdown_spec.js

123 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-03-08 18:12:59 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2019-09-30 21:07:59 +05:30
import confidentialState from '~/confidential_merge_request/state';
2021-03-11 19:13:27 +05:30
import CreateMergeRequestDropdown from '~/create_merge_request_dropdown';
import axios from '~/lib/utils/axios_utils';
2018-11-08 19:23:39 +05:30
describe('CreateMergeRequestDropdown', () => {
let axiosMock;
let dropdown;
beforeEach(() => {
axiosMock = new MockAdapter(axios);
2019-09-30 21:07:59 +05:30
document.body.innerHTML = `
2018-11-08 19:23:39 +05:30
<div id="dummy-wrapper-element">
<div class="available"></div>
<div class="unavailable">
2021-06-08 01:23:25 +05:30
<div class="gl-spinner"></div>
2018-11-08 19:23:39 +05:30
<div class="text"></div>
</div>
<div class="js-ref"></div>
2019-09-30 21:07:59 +05:30
<div class="js-create-mr"></div>
2021-04-29 21:17:54 +05:30
<div class="js-create-merge-request">
<span class="js-spinner"></span>
</div>
2018-11-08 19:23:39 +05:30
<div class="js-create-target"></div>
<div class="js-dropdown-toggle"></div>
</div>
2019-09-30 21:07:59 +05:30
`;
2018-11-08 19:23:39 +05:30
const dummyElement = document.getElementById('dummy-wrapper-element');
dropdown = new CreateMergeRequestDropdown(dummyElement);
dropdown.refsPath = `${TEST_HOST}/dummy/refs?search=`;
});
afterEach(() => {
axiosMock.restore();
});
describe('getRef', () => {
2021-03-08 18:12:59 +05:30
it('escapes branch names correctly', (done) => {
2018-11-08 19:23:39 +05:30
const endpoint = `${dropdown.refsPath}contains%23hash`;
2019-09-30 21:07:59 +05:30
jest.spyOn(axios, 'get');
2018-11-08 19:23:39 +05:30
axiosMock.onGet(endpoint).replyOnce({});
dropdown
.getRef('contains#hash')
.then(() => {
2021-12-11 22:18:48 +05:30
expect(axios.get).toHaveBeenCalledWith(
endpoint,
expect.objectContaining({ cancelToken: expect.anything() }),
);
2018-11-08 19:23:39 +05:30
})
.then(done)
.catch(done.fail);
});
});
describe('updateCreatePaths', () => {
it('escapes branch names correctly', () => {
dropdown.createBranchPath = `${TEST_HOST}/branches?branch_name=some-branch&issue=42`;
2021-06-08 01:23:25 +05:30
dropdown.createMrPath = `${TEST_HOST}/create_merge_request?branch_name=some-branch&ref=main`;
2018-11-08 19:23:39 +05:30
dropdown.updateCreatePaths('branch', 'contains#hash');
expect(dropdown.createBranchPath).toBe(
`${TEST_HOST}/branches?branch_name=contains%23hash&issue=42`,
);
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(dropdown.createMrPath).toBe(
2021-06-08 01:23:25 +05:30
`${TEST_HOST}/create_merge_request?branch_name=contains%23hash&ref=main`,
2018-11-08 19:23:39 +05:30
);
});
});
2019-09-30 21:07:59 +05:30
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-04-29 21:17:54 +05:30
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-11-08 19:23:39 +05:30
});