2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2019-09-10 15:07:06 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-08-14 03:26:19 -04:00
|
|
|
import SidebarMediator from '~/sidebar/sidebar_mediator';
|
|
|
|
import SidebarStore from '~/sidebar/stores/sidebar_store';
|
|
|
|
import SidebarService from '~/sidebar/services/sidebar_service';
|
|
|
|
import SidebarMoveIssue from '~/sidebar/lib/sidebar_move_issue';
|
|
|
|
import Mock from './mock_data';
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
describe('SidebarMoveIssue', function() {
|
2019-09-10 15:07:06 -04:00
|
|
|
let mock;
|
|
|
|
|
2017-08-14 03:26:19 -04:00
|
|
|
beforeEach(() => {
|
2019-09-10 15:07:06 -04:00
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
const mockData = Mock.responseMap.GET['/autocomplete/projects?project_id=15'];
|
|
|
|
mock.onGet('/autocomplete/projects?project_id=15').reply(200, mockData);
|
2017-08-14 03:26:19 -04:00
|
|
|
this.mediator = new SidebarMediator(Mock.mediator);
|
|
|
|
this.$content = $(`
|
|
|
|
<div class="dropdown">
|
|
|
|
<div class="js-toggle"></div>
|
2018-04-24 22:56:27 -04:00
|
|
|
<div class="dropdown-menu">
|
|
|
|
<div class="dropdown-content"></div>
|
|
|
|
</div>
|
2017-08-14 03:26:19 -04:00
|
|
|
<div class="js-confirm-button"></div>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
this.$toggleButton = this.$content.find('.js-toggle');
|
|
|
|
this.$confirmButton = this.$content.find('.js-confirm-button');
|
|
|
|
|
|
|
|
this.sidebarMoveIssue = new SidebarMoveIssue(
|
|
|
|
this.mediator,
|
|
|
|
this.$toggleButton,
|
|
|
|
this.$confirmButton,
|
|
|
|
);
|
|
|
|
this.sidebarMoveIssue.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
SidebarService.singleton = null;
|
|
|
|
SidebarStore.singleton = null;
|
|
|
|
SidebarMediator.singleton = null;
|
|
|
|
|
|
|
|
this.sidebarMoveIssue.destroy();
|
2019-09-10 15:07:06 -04:00
|
|
|
mock.restore();
|
2017-08-14 03:26:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('init', () => {
|
|
|
|
it('should initialize the dropdown and listeners', () => {
|
|
|
|
spyOn(this.sidebarMoveIssue, 'initDropdown');
|
|
|
|
spyOn(this.sidebarMoveIssue, 'addEventListeners');
|
|
|
|
|
|
|
|
this.sidebarMoveIssue.init();
|
|
|
|
|
|
|
|
expect(this.sidebarMoveIssue.initDropdown).toHaveBeenCalled();
|
|
|
|
expect(this.sidebarMoveIssue.addEventListeners).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('destroy', () => {
|
|
|
|
it('should remove the listeners', () => {
|
|
|
|
spyOn(this.sidebarMoveIssue, 'removeEventListeners');
|
|
|
|
|
|
|
|
this.sidebarMoveIssue.destroy();
|
|
|
|
|
|
|
|
expect(this.sidebarMoveIssue.removeEventListeners).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('initDropdown', () => {
|
|
|
|
it('should initialize the gl_dropdown', () => {
|
|
|
|
spyOn($.fn, 'glDropdown');
|
|
|
|
|
|
|
|
this.sidebarMoveIssue.initDropdown();
|
|
|
|
|
|
|
|
expect($.fn.glDropdown).toHaveBeenCalled();
|
|
|
|
});
|
2018-04-18 03:40:36 -04:00
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
it('escapes html from project name', done => {
|
2018-04-18 03:40:36 -04:00
|
|
|
this.$toggleButton.dropdown('toggle');
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-10-17 03:13:26 -04:00
|
|
|
expect(this.$content.find('.js-move-issue-dropdown-item')[1].innerHTML.trim()).toEqual(
|
|
|
|
'<img src=x onerror=alert(document.domain)> foo / bar',
|
|
|
|
);
|
2018-04-18 03:40:36 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 03:26:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('onConfirmClicked', () => {
|
|
|
|
it('should move the issue with valid project ID', () => {
|
|
|
|
spyOn(this.mediator, 'moveIssue').and.returnValue(Promise.resolve());
|
|
|
|
this.mediator.setMoveToProjectId(7);
|
|
|
|
|
|
|
|
this.sidebarMoveIssue.onConfirmClicked();
|
|
|
|
|
|
|
|
expect(this.mediator.moveIssue).toHaveBeenCalled();
|
2018-02-20 17:20:48 -05:00
|
|
|
expect(this.$confirmButton.prop('disabled')).toBeTruthy();
|
2017-08-14 03:26:19 -04:00
|
|
|
expect(this.$confirmButton.hasClass('is-loading')).toBe(true);
|
|
|
|
});
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
it('should remove loading state from confirm button on failure', done => {
|
2017-08-14 03:26:19 -04:00
|
|
|
spyOn(window, 'Flash');
|
|
|
|
spyOn(this.mediator, 'moveIssue').and.returnValue(Promise.reject());
|
|
|
|
this.mediator.setMoveToProjectId(7);
|
|
|
|
|
|
|
|
this.sidebarMoveIssue.onConfirmClicked();
|
|
|
|
|
|
|
|
expect(this.mediator.moveIssue).toHaveBeenCalled();
|
|
|
|
// Wait for the move issue request to fail
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(window.Flash).toHaveBeenCalled();
|
2018-02-20 17:20:48 -05:00
|
|
|
expect(this.$confirmButton.prop('disabled')).toBeFalsy();
|
2017-08-14 03:26:19 -04:00
|
|
|
expect(this.$confirmButton.hasClass('is-loading')).toBe(false);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not move the issue with id=0', () => {
|
|
|
|
spyOn(this.mediator, 'moveIssue');
|
|
|
|
this.mediator.setMoveToProjectId(0);
|
|
|
|
|
|
|
|
this.sidebarMoveIssue.onConfirmClicked();
|
|
|
|
|
|
|
|
expect(this.mediator.moveIssue).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
it('should set moveToProjectId on dropdown item "No project" click', done => {
|
2017-08-14 03:26:19 -04:00
|
|
|
spyOn(this.mediator, 'setMoveToProjectId');
|
|
|
|
|
|
|
|
// Open the dropdown
|
|
|
|
this.$toggleButton.dropdown('toggle');
|
|
|
|
|
|
|
|
// Wait for the autocomplete request to finish
|
|
|
|
setTimeout(() => {
|
2018-10-17 03:13:26 -04:00
|
|
|
this.$content
|
|
|
|
.find('.js-move-issue-dropdown-item')
|
|
|
|
.eq(0)
|
|
|
|
.trigger('click');
|
2017-08-14 03:26:19 -04:00
|
|
|
|
|
|
|
expect(this.mediator.setMoveToProjectId).toHaveBeenCalledWith(0);
|
2018-02-20 17:20:48 -05:00
|
|
|
expect(this.$confirmButton.prop('disabled')).toBeTruthy();
|
2017-08-14 03:26:19 -04:00
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
it('should set moveToProjectId on dropdown item click', done => {
|
2017-08-14 03:26:19 -04:00
|
|
|
spyOn(this.mediator, 'setMoveToProjectId');
|
|
|
|
|
|
|
|
// Open the dropdown
|
|
|
|
this.$toggleButton.dropdown('toggle');
|
|
|
|
|
|
|
|
// Wait for the autocomplete request to finish
|
|
|
|
setTimeout(() => {
|
2018-10-17 03:13:26 -04:00
|
|
|
this.$content
|
|
|
|
.find('.js-move-issue-dropdown-item')
|
|
|
|
.eq(1)
|
|
|
|
.trigger('click');
|
2017-08-14 03:26:19 -04:00
|
|
|
|
|
|
|
expect(this.mediator.setMoveToProjectId).toHaveBeenCalledWith(20);
|
|
|
|
expect(this.$confirmButton.attr('disabled')).toBe(undefined);
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
});
|