2017-08-17 22:00:37 +05:30
|
|
|
/* eslint-disable space-before-function-paren, one-var, one-var-declaration-per-line, no-use-before-define, comma-dangle, max-len */
|
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
|
|
|
|
|
|
|
preloadFixtures('issues/closed-issue.html.raw');
|
|
|
|
preloadFixtures('issues/issue-with-task-list.html.raw');
|
|
|
|
preloadFixtures('issues/open-issue.html.raw');
|
|
|
|
|
|
|
|
function expectErrorMessage() {
|
|
|
|
const $flashMessage = $('div.flash-alert');
|
|
|
|
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);
|
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');
|
|
|
|
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');
|
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');
|
2017-08-17 22:00:37 +05:30
|
|
|
expect($boxClosed).toExist();
|
|
|
|
expect($boxClosed).toHaveText('Closed');
|
|
|
|
|
|
|
|
$boxOpen = $('div.status-box-open');
|
|
|
|
expect($boxOpen).toExist();
|
|
|
|
expect($boxOpen).toHaveText('Open');
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
$btn = $('.js-issuable-close-button');
|
|
|
|
expect($btn).toExist();
|
|
|
|
expect($btn).toHaveText(isIssueInitiallyOpen ? 'Close issue' : 'Reopen issue');
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
[true, false].forEach((isIssueInitiallyOpen) => {
|
|
|
|
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,
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
if (isIssueInitiallyOpen) {
|
|
|
|
loadFixtures('issues/open-issue.html.raw');
|
|
|
|
} else {
|
|
|
|
loadFixtures('issues/closed-issue.html.raw');
|
|
|
|
}
|
|
|
|
|
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'), {
|
2016-09-13 17:45:13 +05:30
|
|
|
id: 34
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
mockCanCreateBranch(!isIssueInitiallyOpen);
|
|
|
|
|
|
|
|
this.$triggeredButton.trigger('click');
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expectIssueState(!isIssueInitiallyOpen);
|
|
|
|
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'), {
|
2016-09-13 17:45:13 +05:30
|
|
|
saved: false
|
|
|
|
});
|
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);
|
|
|
|
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
|
|
|
|
expectErrorMessage();
|
|
|
|
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);
|
|
|
|
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
|
|
|
|
expectErrorMessage();
|
|
|
|
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
|
|
|
});
|