debian-mirror-gitlab/spec/frontend/sidebar/sidebar_move_issue_spec.js

160 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-03-11 19:13:27 +05:30
import $ from 'jquery';
2022-04-04 11:22:00 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2021-09-30 23:02:18 +05:30
import createFlash from '~/flash';
2020-05-24 23:13:21 +05:30
import axios from '~/lib/utils/axios_utils';
2021-03-11 19:13:27 +05:30
import SidebarMoveIssue from '~/sidebar/lib/sidebar_move_issue';
import SidebarService from '~/sidebar/services/sidebar_service';
2020-05-24 23:13:21 +05:30
import SidebarMediator from '~/sidebar/sidebar_mediator';
import SidebarStore from '~/sidebar/stores/sidebar_store';
import Mock from './mock_data';
2021-09-30 23:02:18 +05:30
jest.mock('~/flash');
2020-05-24 23:13:21 +05:30
describe('SidebarMoveIssue', () => {
let mock;
const test = {};
beforeEach(() => {
mock = new MockAdapter(axios);
const mockData = Mock.responseMap.GET['/autocomplete/projects?project_id=15'];
mock.onGet('/autocomplete/projects?project_id=15').reply(200, mockData);
test.mediator = new SidebarMediator(Mock.mediator);
test.$content = $(`
<div class="dropdown">
<div class="js-toggle"></div>
<div class="dropdown-menu">
<div class="dropdown-content"></div>
</div>
<div class="js-confirm-button"></div>
</div>
`);
test.$toggleButton = test.$content.find('.js-toggle');
test.$confirmButton = test.$content.find('.js-confirm-button');
test.sidebarMoveIssue = new SidebarMoveIssue(
test.mediator,
test.$toggleButton,
test.$confirmButton,
);
test.sidebarMoveIssue.init();
});
afterEach(() => {
SidebarService.singleton = null;
SidebarStore.singleton = null;
SidebarMediator.singleton = null;
test.sidebarMoveIssue.destroy();
mock.restore();
});
describe('init', () => {
it('should initialize the dropdown and listeners', () => {
jest.spyOn(test.sidebarMoveIssue, 'initDropdown').mockImplementation(() => {});
jest.spyOn(test.sidebarMoveIssue, 'addEventListeners').mockImplementation(() => {});
test.sidebarMoveIssue.init();
expect(test.sidebarMoveIssue.initDropdown).toHaveBeenCalled();
expect(test.sidebarMoveIssue.addEventListeners).toHaveBeenCalled();
});
});
describe('destroy', () => {
it('should remove the listeners', () => {
jest.spyOn(test.sidebarMoveIssue, 'removeEventListeners').mockImplementation(() => {});
test.sidebarMoveIssue.destroy();
expect(test.sidebarMoveIssue.removeEventListeners).toHaveBeenCalled();
});
});
describe('initDropdown', () => {
2020-11-24 15:15:51 +05:30
it('should initialize the deprecatedJQueryDropdown', () => {
2020-05-24 23:13:21 +05:30
test.sidebarMoveIssue.initDropdown();
2020-11-24 15:15:51 +05:30
expect(test.sidebarMoveIssue.$dropdownToggle.data('deprecatedJQueryDropdown')).toBeTruthy();
2020-05-24 23:13:21 +05:30
});
2022-04-04 11:22:00 +05:30
it('escapes html from project name', async () => {
2020-05-24 23:13:21 +05:30
test.$toggleButton.dropdown('toggle');
2022-04-04 11:22:00 +05:30
await waitForPromises();
expect(test.$content.find('.js-move-issue-dropdown-item')[1].innerHTML.trim()).toEqual(
'&lt;img src=x onerror=alert(document.domain)&gt; foo / bar',
);
2020-05-24 23:13:21 +05:30
});
});
describe('onConfirmClicked', () => {
it('should move the issue with valid project ID', () => {
jest.spyOn(test.mediator, 'moveIssue').mockReturnValue(Promise.resolve());
test.mediator.setMoveToProjectId(7);
test.sidebarMoveIssue.onConfirmClicked();
expect(test.mediator.moveIssue).toHaveBeenCalled();
expect(test.$confirmButton.prop('disabled')).toBeTruthy();
expect(test.$confirmButton.hasClass('is-loading')).toBe(true);
});
2022-04-04 11:22:00 +05:30
it('should remove loading state from confirm button on failure', async () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(test.mediator, 'moveIssue').mockReturnValue(Promise.reject());
test.mediator.setMoveToProjectId(7);
test.sidebarMoveIssue.onConfirmClicked();
expect(test.mediator.moveIssue).toHaveBeenCalled();
2022-04-04 11:22:00 +05:30
2020-05-24 23:13:21 +05:30
// Wait for the move issue request to fail
2022-04-04 11:22:00 +05:30
await waitForPromises();
expect(createFlash).toHaveBeenCalled();
expect(test.$confirmButton.prop('disabled')).toBeFalsy();
expect(test.$confirmButton.hasClass('is-loading')).toBe(false);
2020-05-24 23:13:21 +05:30
});
it('should not move the issue with id=0', () => {
jest.spyOn(test.mediator, 'moveIssue').mockImplementation(() => {});
test.mediator.setMoveToProjectId(0);
test.sidebarMoveIssue.onConfirmClicked();
expect(test.mediator.moveIssue).not.toHaveBeenCalled();
});
});
2022-04-04 11:22:00 +05:30
it('should set moveToProjectId on dropdown item "No project" click', async () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(test.mediator, 'setMoveToProjectId').mockImplementation(() => {});
// Open the dropdown
test.$toggleButton.dropdown('toggle');
// Wait for the autocomplete request to finish
2022-04-04 11:22:00 +05:30
await waitForPromises();
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
test.$content.find('.js-move-issue-dropdown-item').eq(0).trigger('click');
expect(test.mediator.setMoveToProjectId).toHaveBeenCalledWith(0);
expect(test.$confirmButton.prop('disabled')).toBeTruthy();
2020-05-24 23:13:21 +05:30
});
2022-04-04 11:22:00 +05:30
it('should set moveToProjectId on dropdown item click', async () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(test.mediator, 'setMoveToProjectId').mockImplementation(() => {});
// Open the dropdown
test.$toggleButton.dropdown('toggle');
// Wait for the autocomplete request to finish
2022-04-04 11:22:00 +05:30
await waitForPromises();
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
test.$content.find('.js-move-issue-dropdown-item').eq(1).trigger('click');
expect(test.mediator.setMoveToProjectId).toHaveBeenCalledWith(20);
expect(test.$confirmButton.attr('disabled')).toBe(undefined);
2020-05-24 23:13:21 +05:30
});
});