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

160 lines
5.3 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';
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';
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
});
2021-03-08 18:12:59 +05:30
it('escapes html from project name', (done) => {
2020-05-24 23:13:21 +05:30
test.$toggleButton.dropdown('toggle');
setImmediate(() => {
expect(test.$content.find('.js-move-issue-dropdown-item')[1].innerHTML.trim()).toEqual(
'&lt;img src=x onerror=alert(document.domain)&gt; foo / bar',
);
done();
});
});
});
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);
});
2021-03-08 18:12:59 +05:30
it('should remove loading state from confirm button on failure', (done) => {
2020-05-24 23:13:21 +05:30
jest.spyOn(window, 'Flash').mockImplementation(() => {});
jest.spyOn(test.mediator, 'moveIssue').mockReturnValue(Promise.reject());
test.mediator.setMoveToProjectId(7);
test.sidebarMoveIssue.onConfirmClicked();
expect(test.mediator.moveIssue).toHaveBeenCalled();
// Wait for the move issue request to fail
setImmediate(() => {
expect(window.Flash).toHaveBeenCalled();
expect(test.$confirmButton.prop('disabled')).toBeFalsy();
expect(test.$confirmButton.hasClass('is-loading')).toBe(false);
done();
});
});
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();
});
});
2021-03-08 18:12:59 +05:30
it('should set moveToProjectId on dropdown item "No project" click', (done) => {
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
setImmediate(() => {
2021-03-08 18:12:59 +05:30
test.$content.find('.js-move-issue-dropdown-item').eq(0).trigger('click');
2020-05-24 23:13:21 +05:30
expect(test.mediator.setMoveToProjectId).toHaveBeenCalledWith(0);
expect(test.$confirmButton.prop('disabled')).toBeTruthy();
done();
});
});
2021-03-08 18:12:59 +05:30
it('should set moveToProjectId on dropdown item click', (done) => {
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
setImmediate(() => {
2021-03-08 18:12:59 +05:30
test.$content.find('.js-move-issue-dropdown-item').eq(1).trigger('click');
2020-05-24 23:13:21 +05:30
expect(test.mediator.setMoveToProjectId).toHaveBeenCalledWith(20);
expect(test.$confirmButton.attr('disabled')).toBe(undefined);
done();
});
});
});