2017-04-12 15:40:35 -04:00
|
|
|
/* eslint-disable space-before-function-paren, one-var, one-var-declaration-per-line, no-use-before-define, comma-dangle, max-len */
|
2017-03-01 14:54:04 -05:00
|
|
|
import Issue from '~/issue';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-05-16 17:01:51 -04:00
|
|
|
import '~/lib/utils/text_utility';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-01 14:54:04 -05:00
|
|
|
describe('Issue', function() {
|
2017-04-12 15:40:35 -04:00
|
|
|
let $boxClosed, $boxOpen, $btnClose, $btnReopen;
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2016-12-30 19:14:33 -05:00
|
|
|
preloadFixtures('issues/closed-issue.html.raw');
|
|
|
|
preloadFixtures('issues/issue-with-task-list.html.raw');
|
|
|
|
preloadFixtures('issues/open-issue.html.raw');
|
2016-08-29 12:18:59 -04:00
|
|
|
|
2016-08-26 19:06:57 -04:00
|
|
|
function expectErrorMessage() {
|
2017-04-12 15:40:35 -04:00
|
|
|
const $flashMessage = $('div.flash-alert');
|
2016-08-26 19:06:57 -04:00
|
|
|
expect($flashMessage).toExist();
|
|
|
|
expect($flashMessage).toBeVisible();
|
|
|
|
expect($flashMessage).toHaveText('Unable to update this issue at this time.');
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectIssueState(isIssueOpen) {
|
|
|
|
expectVisibility($boxClosed, !isIssueOpen);
|
|
|
|
expectVisibility($boxOpen, isIssueOpen);
|
|
|
|
|
|
|
|
expectVisibility($btnClose, isIssueOpen);
|
|
|
|
expectVisibility($btnReopen, !isIssueOpen);
|
|
|
|
}
|
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
function expectNewBranchButtonState(isPending, canCreate) {
|
|
|
|
if (Issue.$btnNewBranch.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const $available = Issue.$btnNewBranch.find('.available');
|
|
|
|
expect($available).toHaveText('New branch');
|
|
|
|
|
|
|
|
if (!isPending && canCreate) {
|
|
|
|
expect($available).toBeVisible();
|
|
|
|
} else {
|
|
|
|
expect($available).toBeHidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
const $unavailable = Issue.$btnNewBranch.find('.unavailable');
|
|
|
|
expect($unavailable).toHaveText('New branch unavailable');
|
|
|
|
|
|
|
|
if (!isPending && !canCreate) {
|
|
|
|
expect($unavailable).toBeVisible();
|
|
|
|
} else {
|
|
|
|
expect($unavailable).toBeHidden();
|
|
|
|
}
|
2016-08-26 19:06:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function expectVisibility($element, shouldBeVisible) {
|
|
|
|
if (shouldBeVisible) {
|
|
|
|
expect($element).not.toHaveClass('hidden');
|
|
|
|
} else {
|
|
|
|
expect($element).toHaveClass('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function findElements() {
|
2017-01-11 22:49:57 -05:00
|
|
|
$boxClosed = $('div.status-box-closed');
|
|
|
|
expect($boxClosed).toExist();
|
|
|
|
expect($boxClosed).toHaveText('Closed');
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-01-11 22:49:57 -05:00
|
|
|
$boxOpen = $('div.status-box-open');
|
|
|
|
expect($boxOpen).toExist();
|
|
|
|
expect($boxOpen).toHaveText('Open');
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-01-11 22:49:57 -05:00
|
|
|
$btnClose = $('.btn-close.btn-grouped');
|
|
|
|
expect($btnClose).toExist();
|
|
|
|
expect($btnClose).toHaveText('Close issue');
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-01-11 22:49:57 -05:00
|
|
|
$btnReopen = $('.btn-reopen.btn-grouped');
|
|
|
|
expect($btnReopen).toExist();
|
|
|
|
expect($btnReopen).toHaveText('Reopen issue');
|
2016-08-26 19:06:57 -04:00
|
|
|
}
|
|
|
|
|
2017-03-01 14:54:04 -05:00
|
|
|
describe('task lists', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
loadFixtures('issues/issue-with-task-list.html.raw');
|
|
|
|
this.issue = new Issue();
|
|
|
|
});
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2017-03-01 14:54:04 -05:00
|
|
|
it('submits an ajax request on tasklist:changed', function() {
|
|
|
|
spyOn(jQuery, 'ajax').and.callFake(function(req) {
|
|
|
|
expect(req.type).toBe('PATCH');
|
|
|
|
expect(req.url).toBe(gl.TEST_HOST + '/frontend-fixtures/issues-project/issues/1.json'); // eslint-disable-line prefer-template
|
|
|
|
expect(req.data.issue.description).not.toBe(null);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-03-01 14:54:04 -05:00
|
|
|
|
|
|
|
$('.js-task-list-field').trigger('tasklist:changed');
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
[true, false].forEach((isIssueInitiallyOpen) => {
|
|
|
|
describe(`with ${isIssueInitiallyOpen ? 'open' : 'closed'} issue`, function() {
|
|
|
|
const action = isIssueInitiallyOpen ? 'close' : 'reopen';
|
|
|
|
|
|
|
|
function ajaxSpy(req) {
|
|
|
|
if (req.url === this.$triggeredButton.attr('href')) {
|
|
|
|
expect(req.type).toBe('PUT');
|
|
|
|
expect(this.$triggeredButton).toHaveProp('disabled', true);
|
|
|
|
expectNewBranchButtonState(true, false);
|
|
|
|
return this.issueStateDeferred;
|
2017-05-04 04:09:21 -04:00
|
|
|
} else if (req.url === Issue.createMrDropdownWrap.dataset.canCreatePath) {
|
|
|
|
expect(req.type).toBe('GET');
|
2017-04-12 15:40:35 -04:00
|
|
|
expectNewBranchButtonState(true, false);
|
|
|
|
return this.canCreateBranchDeferred;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(req.url).toBe('unexpected');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
if (isIssueInitiallyOpen) {
|
|
|
|
loadFixtures('issues/open-issue.html.raw');
|
|
|
|
} else {
|
|
|
|
loadFixtures('issues/closed-issue.html.raw');
|
|
|
|
}
|
|
|
|
|
|
|
|
findElements();
|
|
|
|
this.issue = new Issue();
|
|
|
|
expectIssueState(isIssueInitiallyOpen);
|
|
|
|
this.$triggeredButton = isIssueInitiallyOpen ? $btnClose : $btnReopen;
|
|
|
|
|
|
|
|
this.$projectIssuesCounter = $('.issue_counter');
|
|
|
|
this.$projectIssuesCounter.text('1,001');
|
|
|
|
|
|
|
|
this.issueStateDeferred = new jQuery.Deferred();
|
|
|
|
this.canCreateBranchDeferred = new jQuery.Deferred();
|
|
|
|
|
|
|
|
spyOn(jQuery, 'ajax').and.callFake(ajaxSpy.bind(this));
|
|
|
|
});
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
it(`${action}s the issue`, function() {
|
|
|
|
this.$triggeredButton.trigger('click');
|
|
|
|
this.issueStateDeferred.resolve({
|
2016-07-24 16:45:11 -04:00
|
|
|
id: 34
|
|
|
|
});
|
2017-04-12 15:40:35 -04:00
|
|
|
this.canCreateBranchDeferred.resolve({
|
|
|
|
can_create_branch: !isIssueInitiallyOpen
|
|
|
|
});
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
expectIssueState(!isIssueInitiallyOpen);
|
|
|
|
expect(this.$triggeredButton).toHaveProp('disabled', false);
|
|
|
|
expect(this.$projectIssuesCounter.text()).toBe(isIssueInitiallyOpen ? '1,000' : '1,002');
|
|
|
|
expectNewBranchButtonState(false, !isIssueInitiallyOpen);
|
|
|
|
});
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
it(`fails to ${action} the issue if saved:false`, function() {
|
|
|
|
this.$triggeredButton.trigger('click');
|
|
|
|
this.issueStateDeferred.resolve({
|
2016-07-24 16:45:11 -04:00
|
|
|
saved: false
|
|
|
|
});
|
2017-04-12 15:40:35 -04:00
|
|
|
this.canCreateBranchDeferred.resolve({
|
|
|
|
can_create_branch: isIssueInitiallyOpen
|
|
|
|
});
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
expectIssueState(isIssueInitiallyOpen);
|
|
|
|
expect(this.$triggeredButton).toHaveProp('disabled', false);
|
|
|
|
expectErrorMessage();
|
|
|
|
expect(this.$projectIssuesCounter.text()).toBe('1,001');
|
|
|
|
expectNewBranchButtonState(false, isIssueInitiallyOpen);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
it(`fails to ${action} the issue if HTTP error occurs`, function() {
|
|
|
|
this.$triggeredButton.trigger('click');
|
|
|
|
this.issueStateDeferred.reject();
|
|
|
|
this.canCreateBranchDeferred.resolve({
|
|
|
|
can_create_branch: isIssueInitiallyOpen
|
2017-03-16 20:10:35 -04:00
|
|
|
});
|
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
expectIssueState(isIssueInitiallyOpen);
|
|
|
|
expect(this.$triggeredButton).toHaveProp('disabled', true);
|
|
|
|
expectErrorMessage();
|
|
|
|
expect(this.$projectIssuesCounter.text()).toBe('1,001');
|
|
|
|
expectNewBranchButtonState(false, isIssueInitiallyOpen);
|
|
|
|
});
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
it('disables the new branch button if Ajax call fails', function() {
|
|
|
|
this.$triggeredButton.trigger('click');
|
|
|
|
this.issueStateDeferred.reject();
|
|
|
|
this.canCreateBranchDeferred.reject();
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
expectNewBranchButtonState(false, false);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
it('does not trigger Ajax call if new branch button is missing', function() {
|
|
|
|
Issue.$btnNewBranch = $();
|
|
|
|
this.canCreateBranchDeferred = null;
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
this.$triggeredButton.trigger('click');
|
|
|
|
this.issueStateDeferred.reject();
|
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
});
|
2017-03-01 14:54:04 -05:00
|
|
|
});
|