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 */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
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';
|
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-07-05 12:36:58 -04:00
|
|
|
let $boxClosed, $boxOpen, $btn;
|
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);
|
2017-07-05 12:36:58 -04:00
|
|
|
expect($btn).toHaveText(isIssueOpen ? 'Close issue' : 'Reopen issue');
|
2016-08-26 19:06:57 -04:00
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 12:36:58 -04:00
|
|
|
function findElements(isIssueInitiallyOpen) {
|
2017-12-12 22:44:59 -05:00
|
|
|
$boxClosed = $('div.status-box-issue-closed');
|
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');
|
|
|
|
expect($boxOpen).toExist();
|
|
|
|
expect($boxOpen).toHaveText('Open');
|
2016-08-26 19:06:57 -04:00
|
|
|
|
2017-07-05 12:36:58 -04:00
|
|
|
$btn = $('.js-issuable-close-button');
|
|
|
|
expect($btn).toExist();
|
|
|
|
expect($btn).toHaveText(isIssueInitiallyOpen ? 'Close issue' : 'Reopen issue');
|
2016-08-26 19:06:57 -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';
|
2018-01-30 09:53:28 -05:00
|
|
|
let mock;
|
2017-04-12 15:40:35 -04:00
|
|
|
|
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,
|
2018-04-05 11:17:02 -04:00
|
|
|
suggested_branch_name: 'foo-99',
|
2018-01-31 12:51:53 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
if (isIssueInitiallyOpen) {
|
|
|
|
loadFixtures('issues/open-issue.html.raw');
|
|
|
|
} else {
|
|
|
|
loadFixtures('issues/closed-issue.html.raw');
|
|
|
|
}
|
|
|
|
|
2018-01-31 12:51:53 -05:00
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
|
|
|
mock.onGet(/(.*)\/related_branches$/).reply(200, {});
|
|
|
|
mock.onGet(/(.*)\/referenced_merge_requests$/).reply(200, {});
|
|
|
|
|
2017-07-05 12:36:58 -04:00
|
|
|
findElements(isIssueInitiallyOpen);
|
2017-04-12 15:40:35 -04:00
|
|
|
this.issue = new Issue();
|
|
|
|
expectIssueState(isIssueInitiallyOpen);
|
2017-07-05 12:36:58 -04:00
|
|
|
|
|
|
|
this.$triggeredButton = $btn;
|
2017-04-12 15:40:35 -04:00
|
|
|
|
2017-09-01 03:41:31 -04:00
|
|
|
this.$projectIssuesCounter = $('.issue_counter').first();
|
2017-04-12 15:40:35 -04:00
|
|
|
this.$projectIssuesCounter.text('1,001');
|
|
|
|
|
2018-01-31 12:51:53 -05:00
|
|
|
spyOn(axios, 'get').and.callThrough();
|
2017-04-12 15:40:35 -04:00
|
|
|
});
|
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'), {
|
2016-07-24 16:45:11 -04:00
|
|
|
id: 34
|
|
|
|
});
|
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);
|
|
|
|
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
|
|
|
|
expect(this.$projectIssuesCounter.text()).toBe(isIssueInitiallyOpen ? '1,000' : '1,002');
|
|
|
|
expectNewBranchButtonState(false, !isIssueInitiallyOpen);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-04-12 15:40:35 -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 saved:false`, function(done) {
|
|
|
|
mockCloseButtonResponseSuccess(this.$triggeredButton.attr('href'), {
|
2016-07-24 16:45:11 -04:00
|
|
|
saved: false
|
|
|
|
});
|
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);
|
|
|
|
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
|
|
|
|
expectErrorMessage();
|
|
|
|
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);
|
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
this.$triggeredButton.trigger('click');
|
2017-03-16 20:10:35 -04:00
|
|
|
|
2018-01-30 09:53:28 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
expectIssueState(isIssueInitiallyOpen);
|
|
|
|
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
|
|
|
|
expectErrorMessage();
|
|
|
|
expect(this.$projectIssuesCounter.text()).toBe('1,001');
|
|
|
|
expectNewBranchButtonState(false, isIssueInitiallyOpen);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-04-12 15:40:35 -04:00
|
|
|
});
|
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() {
|
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();
|
|
|
|
|
2017-04-12 15:40:35 -04:00
|
|
|
this.$triggeredButton.trigger('click');
|
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
|
|
|
|
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'));
|
2017-04-12 15:40:35 -04:00
|
|
|
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');
|
2018-01-31 12:51:53 -05:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(axios.get).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-04-12 15:40:35 -04:00
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
});
|
2017-03-01 14:54:04 -05:00
|
|
|
});
|