gitlab-org--gitlab-foss/spec/javascripts/issue_spec.js

218 lines
6.2 KiB
JavaScript
Raw Normal View History

/* eslint-disable one-var, no-use-before-define */
import $ from 'jquery';
2018-01-31 04:27:30 -05:00
import MockAdapter from 'axios-mock-adapter';
2018-01-30 09:53:28 -05:00
import axios from '~/lib/utils/axios_utils';
2017-03-01 14:54:04 -05:00
import Issue from '~/issue';
import '~/lib/utils/text_utility';
2016-07-24 16:45:11 -04:00
2017-03-01 14:54:04 -05:00
describe('Issue', function() {
let $boxClosed, $boxOpen, $btn;
2016-08-26 19:06:57 -04:00
preloadFixtures('issues/closed-issue.html');
preloadFixtures('issues/issue-with-task-list.html');
preloadFixtures('issues/open-issue.html');
2016-08-26 19:06:57 -04:00
function expectErrorMessage() {
const $flashMessage = $('div.flash-alert');
2018-10-09 14:03:09 -04:00
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);
2018-10-09 14:03:09 -04:00
expect($btn).toHaveText(isIssueOpen ? 'Close issue' : 'Reopen issue');
2016-08-26 19:06:57 -04:00
}
function expectNewBranchButtonState(isPending, canCreate) {
if (Issue.$btnNewBranch.length === 0) {
return;
}
const $available = Issue.$btnNewBranch.find('.available');
2018-10-09 14:03:09 -04:00
expect($available).toHaveText('New branch');
if (!isPending && canCreate) {
expect($available).toBeVisible();
} else {
expect($available).toBeHidden();
}
const $unavailable = Issue.$btnNewBranch.find('.unavailable');
2018-10-09 14:03:09 -04:00
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(isIssueInitiallyOpen) {
$boxClosed = $('div.status-box-issue-closed');
2018-10-09 14:03:09 -04:00
2017-01-11 22:49:57 -05:00
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');
2018-10-09 14:03:09 -04:00
2017-01-11 22:49:57 -05:00
expect($boxOpen).toExist();
expect($boxOpen).toHaveText('Open');
2016-08-26 19:06:57 -04:00
$btn = $('.js-issuable-close-button');
2018-10-09 14:03:09 -04:00
expect($btn).toExist();
expect($btn).toHaveText(isIssueInitiallyOpen ? 'Close issue' : 'Reopen issue');
2016-08-26 19:06:57 -04:00
}
2018-10-17 03:13:26 -04:00
[true, false].forEach(isIssueInitiallyOpen => {
describe(`with ${isIssueInitiallyOpen ? 'open' : 'closed'} issue`, function() {
const action = isIssueInitiallyOpen ? 'close' : 'reopen';
2018-01-30 09:53:28 -05:00
let mock;
2018-01-30 09:53:28 -05:00
function mockCloseButtonResponseSuccess(url, response) {
mock.onPut(url).reply(() => {
expectNewBranchButtonState(true, false);
return [200, response];
});
}
function mockCloseButtonResponseError(url) {
mock.onPut(url).networkError();
}
2018-01-31 12:51:53 -05:00
function mockCanCreateBranch(canCreateBranch) {
mock.onGet(/(.*)\/can_create_branch$/).reply(200, {
can_create_branch: canCreateBranch,
suggested_branch_name: 'foo-99',
2018-01-31 12:51:53 -05:00
});
}
beforeEach(function() {
if (isIssueInitiallyOpen) {
loadFixtures('issues/open-issue.html');
} else {
loadFixtures('issues/closed-issue.html');
}
2018-01-31 12:51:53 -05:00
mock = new MockAdapter(axios);
mock.onGet(/(.*)\/related_branches$/).reply(200, {});
findElements(isIssueInitiallyOpen);
this.issue = new Issue();
expectIssueState(isIssueInitiallyOpen);
this.$triggeredButton = $btn;
this.$projectIssuesCounter = $('.issue_counter').first();
this.$projectIssuesCounter.text('1,001');
2018-01-31 12:51:53 -05:00
spyOn(axios, 'get').and.callThrough();
});
2016-08-26 19:06:57 -04:00
2018-01-30 09:53:28 -05:00
afterEach(() => {
mock.restore();
2018-01-31 12:51:53 -05:00
$('div.flash-alert').remove();
2018-01-30 09:53:28 -05:00
});
it(`${action}s the issue`, function(done) {
mockCloseButtonResponseSuccess(this.$triggeredButton.attr('href'), {
2018-10-17 03:13:26 -04:00
id: 34,
2016-07-24 16:45:11 -04:00
});
2018-01-31 12:51:53 -05:00
mockCanCreateBranch(!isIssueInitiallyOpen);
2018-01-30 09:53:28 -05:00
this.$triggeredButton.trigger('click');
2016-08-26 19:06:57 -04:00
2018-01-30 09:53:28 -05:00
setTimeout(() => {
expectIssueState(!isIssueInitiallyOpen);
2018-10-09 14:03:09 -04:00
2018-01-30 09:53:28 -05:00
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
expect(this.$projectIssuesCounter.text()).toBe(isIssueInitiallyOpen ? '1,000' : '1,002');
expectNewBranchButtonState(false, !isIssueInitiallyOpen);
done();
});
});
2016-08-26 19:06:57 -04:00
2018-01-30 09:53:28 -05:00
it(`fails to ${action} the issue if saved:false`, function(done) {
mockCloseButtonResponseSuccess(this.$triggeredButton.attr('href'), {
2018-10-17 03:13:26 -04:00
saved: false,
2016-07-24 16:45:11 -04:00
});
2018-01-31 12:51:53 -05:00
mockCanCreateBranch(isIssueInitiallyOpen);
2018-01-30 09:53:28 -05:00
this.$triggeredButton.trigger('click');
2016-08-26 19:06:57 -04:00
2018-01-30 09:53:28 -05:00
setTimeout(() => {
expectIssueState(isIssueInitiallyOpen);
2018-10-09 14:03:09 -04:00
2018-01-30 09:53:28 -05:00
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
expectErrorMessage();
2018-10-09 14:03:09 -04:00
2018-01-30 09:53:28 -05:00
expect(this.$projectIssuesCounter.text()).toBe('1,001');
expectNewBranchButtonState(false, isIssueInitiallyOpen);
done();
});
2016-07-24 16:45:11 -04:00
});
2016-08-26 19:06:57 -04:00
2018-01-30 09:53:28 -05:00
it(`fails to ${action} the issue if HTTP error occurs`, function(done) {
mockCloseButtonResponseError(this.$triggeredButton.attr('href'));
2018-01-31 12:51:53 -05:00
mockCanCreateBranch(isIssueInitiallyOpen);
this.$triggeredButton.trigger('click');
2017-03-16 20:10:35 -04:00
2018-01-30 09:53:28 -05:00
setTimeout(() => {
expectIssueState(isIssueInitiallyOpen);
2018-10-09 14:03:09 -04:00
2018-01-30 09:53:28 -05:00
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
expectErrorMessage();
2018-10-09 14:03:09 -04:00
2018-01-30 09:53:28 -05:00
expect(this.$projectIssuesCounter.text()).toBe('1,001');
expectNewBranchButtonState(false, isIssueInitiallyOpen);
done();
});
});
2016-08-26 19:06:57 -04:00
it('disables the new branch button if Ajax call fails', function() {
2018-01-30 09:53:28 -05:00
mockCloseButtonResponseError(this.$triggeredButton.attr('href'));
2018-01-31 12:51:53 -05:00
mock.onGet(/(.*)\/can_create_branch$/).networkError();
this.$triggeredButton.trigger('click');
2016-08-26 19:06:57 -04:00
expectNewBranchButtonState(false, false);
2016-07-24 16:45:11 -04:00
});
2016-08-26 19:06:57 -04:00
2018-01-31 12:51:53 -05:00
it('does not trigger Ajax call if new branch button is missing', function(done) {
2018-01-30 09:53:28 -05:00
mockCloseButtonResponseError(this.$triggeredButton.attr('href'));
Issue.$btnNewBranch = $();
this.canCreateBranchDeferred = null;
2016-08-26 19:06:57 -04:00
this.$triggeredButton.trigger('click');
2018-01-31 12:51:53 -05:00
setTimeout(() => {
expect(axios.get).not.toHaveBeenCalled();
done();
});
});
2016-07-24 16:45:11 -04:00
});
});
2017-03-01 14:54:04 -05:00
});