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

164 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import _ from 'underscore';
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-12-13 13:39:08 +05:30
describe('SidebarMoveIssue', function() {
2018-03-17 18:26:18 +05:30
beforeEach(() => {
Vue.http.interceptors.push(Mock.sidebarMockInterceptor);
this.mediator = new SidebarMediator(Mock.mediator);
this.$content = $(`
<div class="dropdown">
<div class="js-toggle"></div>
2018-11-08 19:23:39 +05:30
<div class="dropdown-menu">
<div class="dropdown-content"></div>
</div>
2018-03-17 18:26:18 +05:30
<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-05-01 15:08:00 +05:30
2018-12-13 13:39:08 +05:30
it('escapes html from project name', done => {
2018-05-01 15:08:00 +05:30
this.$toggleButton.dropdown('toggle');
setTimeout(() => {
2018-12-13 13:39:08 +05:30
expect(this.$content.find('.js-move-issue-dropdown-item')[1].innerHTML.trim()).toEqual(
'&lt;img src=x onerror=alert(document.domain)&gt; foo / bar',
);
2018-05-01 15:08:00 +05:30
done();
});
});
2018-03-17 18:26:18 +05:30
});
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-03-27 19:54:05 +05:30
expect(this.$confirmButton.prop('disabled')).toBeTruthy();
2018-03-17 18:26:18 +05:30
expect(this.$confirmButton.hasClass('is-loading')).toBe(true);
});
2018-12-13 13:39:08 +05:30
it('should remove loading state from confirm button on failure', done => {
2018-03-17 18:26:18 +05:30
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-03-27 19:54:05 +05:30
expect(this.$confirmButton.prop('disabled')).toBeFalsy();
2018-03-17 18:26:18 +05:30
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-12-13 13:39:08 +05:30
it('should set moveToProjectId on dropdown item "No project" click', done => {
2018-03-17 18:26:18 +05:30
spyOn(this.mediator, 'setMoveToProjectId');
// Open the dropdown
this.$toggleButton.dropdown('toggle');
// Wait for the autocomplete request to finish
setTimeout(() => {
2018-12-13 13:39:08 +05:30
this.$content
.find('.js-move-issue-dropdown-item')
.eq(0)
.trigger('click');
2018-03-17 18:26:18 +05:30
expect(this.mediator.setMoveToProjectId).toHaveBeenCalledWith(0);
2018-03-27 19:54:05 +05:30
expect(this.$confirmButton.prop('disabled')).toBeTruthy();
2018-03-17 18:26:18 +05:30
done();
}, 0);
});
2018-12-13 13:39:08 +05:30
it('should set moveToProjectId on dropdown item click', done => {
2018-03-17 18:26:18 +05:30
spyOn(this.mediator, 'setMoveToProjectId');
// Open the dropdown
this.$toggleButton.dropdown('toggle');
// Wait for the autocomplete request to finish
setTimeout(() => {
2018-12-13 13:39:08 +05:30
this.$content
.find('.js-move-issue-dropdown-item')
.eq(1)
.trigger('click');
2018-03-17 18:26:18 +05:30
expect(this.mediator.setMoveToProjectId).toHaveBeenCalledWith(20);
expect(this.$confirmButton.attr('disabled')).toBe(undefined);
done();
}, 0);
});
});