2020-05-19 15:08:04 +00:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-02-14 18:09:20 +00:00
|
|
|
import $ from 'jquery';
|
2021-06-30 03:07:30 +00:00
|
|
|
import createFlash from '~/flash';
|
2020-05-19 15:08:04 +00:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-02-14 18:09:20 +00:00
|
|
|
import SidebarMoveIssue from '~/sidebar/lib/sidebar_move_issue';
|
|
|
|
import SidebarService from '~/sidebar/services/sidebar_service';
|
2020-05-19 15:08:04 +00:00
|
|
|
import SidebarMediator from '~/sidebar/sidebar_mediator';
|
|
|
|
import SidebarStore from '~/sidebar/stores/sidebar_store';
|
|
|
|
import Mock from './mock_data';
|
|
|
|
|
2021-06-30 03:07:30 +00:00
|
|
|
jest.mock('~/flash');
|
|
|
|
|
2020-05-19 15:08:04 +00:00
|
|
|
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-08-24 18:10:19 +00:00
|
|
|
it('should initialize the deprecatedJQueryDropdown', () => {
|
2020-05-19 15:08:04 +00:00
|
|
|
test.sidebarMoveIssue.initDropdown();
|
|
|
|
|
2020-08-24 18:10:19 +00:00
|
|
|
expect(test.sidebarMoveIssue.$dropdownToggle.data('deprecatedJQueryDropdown')).toBeTruthy();
|
2020-05-19 15:08:04 +00:00
|
|
|
});
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
it('escapes html from project name', (done) => {
|
2020-05-19 15:08:04 +00:00
|
|
|
test.$toggleButton.dropdown('toggle');
|
|
|
|
|
|
|
|
setImmediate(() => {
|
|
|
|
expect(test.$content.find('.js-move-issue-dropdown-item')[1].innerHTML.trim()).toEqual(
|
|
|
|
'<img src=x onerror=alert(document.domain)> 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);
|
|
|
|
});
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
it('should remove loading state from confirm button on failure', (done) => {
|
2020-05-19 15:08:04 +00:00
|
|
|
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(() => {
|
2021-06-30 03:07:30 +00:00
|
|
|
expect(createFlash).toHaveBeenCalled();
|
2020-05-19 15:08:04 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
it('should set moveToProjectId on dropdown item "No project" click', (done) => {
|
2020-05-19 15:08:04 +00:00
|
|
|
jest.spyOn(test.mediator, 'setMoveToProjectId').mockImplementation(() => {});
|
|
|
|
|
|
|
|
// Open the dropdown
|
|
|
|
test.$toggleButton.dropdown('toggle');
|
|
|
|
|
|
|
|
// Wait for the autocomplete request to finish
|
|
|
|
setImmediate(() => {
|
2020-12-23 12:10:26 +00:00
|
|
|
test.$content.find('.js-move-issue-dropdown-item').eq(0).trigger('click');
|
2020-05-19 15:08:04 +00:00
|
|
|
|
|
|
|
expect(test.mediator.setMoveToProjectId).toHaveBeenCalledWith(0);
|
|
|
|
expect(test.$confirmButton.prop('disabled')).toBeTruthy();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
it('should set moveToProjectId on dropdown item click', (done) => {
|
2020-05-19 15:08:04 +00:00
|
|
|
jest.spyOn(test.mediator, 'setMoveToProjectId').mockImplementation(() => {});
|
|
|
|
|
|
|
|
// Open the dropdown
|
|
|
|
test.$toggleButton.dropdown('toggle');
|
|
|
|
|
|
|
|
// Wait for the autocomplete request to finish
|
|
|
|
setImmediate(() => {
|
2020-12-23 12:10:26 +00:00
|
|
|
test.$content.find('.js-move-issue-dropdown-item').eq(1).trigger('click');
|
2020-05-19 15:08:04 +00:00
|
|
|
|
|
|
|
expect(test.mediator.setMoveToProjectId).toHaveBeenCalledWith(20);
|
|
|
|
expect(test.$confirmButton.attr('disabled')).toBe(undefined);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|