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

232 lines
6.6 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
2020-03-13 15:44:24 +05:30
describe('Issue', () => {
let testContext;
beforeEach(() => {
testContext = {};
});
2017-09-10 17:25:29 +05:30
let $boxClosed, $boxOpen, $btn;
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +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 => {
2020-03-13 15:44:24 +05:30
describe(`with ${isIssueInitiallyOpen ? 'open' : 'closed'} issue`, () => {
2017-08-17 22:00:37 +05:30
const action = isIssueInitiallyOpen ? 'close' : 'reopen';
2018-03-17 18:26:18 +05:30
let mock;
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
function setup() {
testContext.issue = new Issue();
expectIssueState(isIssueInitiallyOpen);
testContext.$projectIssuesCounter = $('.issue_counter').first();
testContext.$projectIssuesCounter.text('1,001');
}
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
}
2020-03-13 15:44:24 +05:30
beforeEach(() => {
2017-08-17 22:00:37 +05:30
if (isIssueInitiallyOpen) {
2019-07-07 11:18:12 +05:30
loadFixtures('issues/open-issue.html');
2017-08-17 22:00:37 +05:30
} else {
2019-07-07 11:18:12 +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, {});
2020-03-13 15:44:24 +05:30
jest.spyOn(axios, 'get');
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
findElements(isIssueInitiallyOpen);
2020-03-13 15:44:24 +05:30
testContext.$triggeredButton = $btn;
2018-03-17 18:26:18 +05:30
});
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
});
2020-03-13 15:44:24 +05:30
it(`${action}s the issue`, done => {
mockCloseButtonResponseSuccess(testContext.$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);
2020-03-13 15:44:24 +05:30
setup();
testContext.$triggeredButton.trigger('click');
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
setImmediate(() => {
2018-03-17 18:26:18 +05:30
expectIssueState(!isIssueInitiallyOpen);
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
expect(testContext.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
expect(testContext.$projectIssuesCounter.text()).toBe(
isIssueInitiallyOpen ? '1,000' : '1,002',
);
2018-03-17 18:26:18 +05:30
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
2020-03-13 15:44:24 +05:30
it(`fails to ${action} the issue if saved:false`, done => {
mockCloseButtonResponseSuccess(testContext.$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
2020-03-13 15:44:24 +05:30
setup();
testContext.$triggeredButton.trigger('click');
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
setImmediate(() => {
2018-03-17 18:26:18 +05:30
expectIssueState(isIssueInitiallyOpen);
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
expect(testContext.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
2018-03-17 18:26:18 +05:30
expectErrorMessage();
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
expect(testContext.$projectIssuesCounter.text()).toBe('1,001');
2018-03-17 18:26:18 +05:30
expectNewBranchButtonState(false, isIssueInitiallyOpen);
done();
});
2016-09-13 17:45:13 +05:30
});
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
it(`fails to ${action} the issue if HTTP error occurs`, done => {
mockCloseButtonResponseError(testContext.$triggeredButton.attr('href'));
2018-03-17 18:26:18 +05:30
mockCanCreateBranch(isIssueInitiallyOpen);
2020-03-13 15:44:24 +05:30
setup();
testContext.$triggeredButton.trigger('click');
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
setImmediate(() => {
2018-03-17 18:26:18 +05:30
expectIssueState(isIssueInitiallyOpen);
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
expect(testContext.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
2018-03-17 18:26:18 +05:30
expectErrorMessage();
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
expect(testContext.$projectIssuesCounter.text()).toBe('1,001');
2018-03-17 18:26:18 +05:30
expectNewBranchButtonState(false, isIssueInitiallyOpen);
done();
});
2017-08-17 22:00:37 +05:30
});
2020-03-13 15:44:24 +05:30
it('disables the new branch button if Ajax call fails', () => {
mockCloseButtonResponseError(testContext.$triggeredButton.attr('href'));
2018-03-17 18:26:18 +05:30
mock.onGet(/(.*)\/can_create_branch$/).networkError();
2020-03-13 15:44:24 +05:30
setup();
testContext.$triggeredButton.trigger('click');
2017-08-17 22:00:37 +05:30
expectNewBranchButtonState(false, false);
});
2020-03-13 15:44:24 +05:30
it('does not trigger Ajax call if new branch button is missing', done => {
mockCloseButtonResponseError(testContext.$triggeredButton.attr('href'));
document.querySelector('#related-branches').remove();
document.querySelector('.create-mr-dropdown-wrap').remove();
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
setup();
testContext.$triggeredButton.trigger('click');
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
setImmediate(() => {
2018-03-17 18:26:18 +05:30
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
});