2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2017-12-19 10:12:32 +00:00
|
|
|
import _ from 'underscore';
|
2017-08-14 07:26:19 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
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 07:13:26 +00:00
|
|
|
describe('SidebarMoveIssue', function() {
|
2017-08-14 07:26:19 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
Vue.http.interceptors.push(Mock.sidebarMockInterceptor);
|
|
|
|
this.mediator = new SidebarMediator(Mock.mediator);
|
|
|
|
this.$content = $(`
|
|
|
|
<div class="dropdown">
|
|
|
|
<div class="js-toggle"></div>
|
2018-04-25 02:56:27 +00:00
|
|
|
<div class="dropdown-menu">
|
|
|
|
<div class="dropdown-content"></div>
|
|
|
|
</div>
|
2017-08-14 07:26:19 +00: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();
|
|
|
|
|
|
|
|
Vue.http.interceptors = _.without(Vue.http.interceptors, Mock.sidebarMockInterceptor);
|
|
|
|
});
|
|
|
|
|
|
|
|
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 07:40:36 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('escapes html from project name', done => {
|
2018-04-18 07:40:36 +00:00
|
|
|
this.$toggleButton.dropdown('toggle');
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-10-17 07:13:26 +00: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 07:40:36 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 07:26:19 +00: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 22:20:48 +00:00
|
|
|
expect(this.$confirmButton.prop('disabled')).toBeTruthy();
|
2017-08-14 07:26:19 +00:00
|
|
|
expect(this.$confirmButton.hasClass('is-loading')).toBe(true);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should remove loading state from confirm button on failure', done => {
|
2017-08-14 07:26:19 +00: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 22:20:48 +00:00
|
|
|
expect(this.$confirmButton.prop('disabled')).toBeFalsy();
|
2017-08-14 07:26:19 +00: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 07:13:26 +00:00
|
|
|
it('should set moveToProjectId on dropdown item "No project" click', done => {
|
2017-08-14 07:26:19 +00:00
|
|
|
spyOn(this.mediator, 'setMoveToProjectId');
|
|
|
|
|
|
|
|
// Open the dropdown
|
|
|
|
this.$toggleButton.dropdown('toggle');
|
|
|
|
|
|
|
|
// Wait for the autocomplete request to finish
|
|
|
|
setTimeout(() => {
|
2018-10-17 07:13:26 +00:00
|
|
|
this.$content
|
|
|
|
.find('.js-move-issue-dropdown-item')
|
|
|
|
.eq(0)
|
|
|
|
.trigger('click');
|
2017-08-14 07:26:19 +00:00
|
|
|
|
|
|
|
expect(this.mediator.setMoveToProjectId).toHaveBeenCalledWith(0);
|
2018-02-20 22:20:48 +00:00
|
|
|
expect(this.$confirmButton.prop('disabled')).toBeTruthy();
|
2017-08-14 07:26:19 +00:00
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should set moveToProjectId on dropdown item click', done => {
|
2017-08-14 07:26:19 +00:00
|
|
|
spyOn(this.mediator, 'setMoveToProjectId');
|
|
|
|
|
|
|
|
// Open the dropdown
|
|
|
|
this.$toggleButton.dropdown('toggle');
|
|
|
|
|
|
|
|
// Wait for the autocomplete request to finish
|
|
|
|
setTimeout(() => {
|
2018-10-17 07:13:26 +00:00
|
|
|
this.$content
|
|
|
|
.find('.js-move-issue-dropdown-item')
|
|
|
|
.eq(1)
|
|
|
|
.trigger('click');
|
2017-08-14 07:26:19 +00:00
|
|
|
|
|
|
|
expect(this.mediator.setMoveToProjectId).toHaveBeenCalledWith(20);
|
|
|
|
expect(this.$confirmButton.attr('disabled')).toBe(undefined);
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
});
|