debian-mirror-gitlab/spec/javascripts/issue_spec.js

219 lines
6.3 KiB
JavaScript
Raw Normal View History

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