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

166 lines
4.8 KiB
JavaScript
Raw Normal View History

/* eslint-disable space-before-function-paren, no-var, one-var, one-var-declaration-per-line, no-use-before-define, indent, no-trailing-spaces, comma-dangle, padded-blocks, max-len */
/* global Issue */
2016-07-24 20:45:11 +00:00
/*= require lib/utils/text_utility */
/*= require issue */
(function() {
2016-08-26 23:06:57 +00:00
var INVALID_URL = 'http://goesnowhere.nothing/whereami';
var $boxClosed, $boxOpen, $btnClose, $btnReopen;
fixture.preload('issues/closed-issue.html.raw');
fixture.preload('issues/issue-with-task-list.html.raw');
fixture.preload('issues/open-issue.html.raw');
2016-08-26 23:06:57 +00:00
function expectErrorMessage() {
var $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);
expectVisibility($btnClose, isIssueOpen);
expectVisibility($btnReopen, !isIssueOpen);
}
function expectPendingRequest(req, $triggeredButton) {
expect(req.type).toBe('PUT');
expect(req.url).toBe($triggeredButton.attr('href'));
expect($triggeredButton).toHaveProp('disabled', true);
}
function expectVisibility($element, shouldBeVisible) {
if (shouldBeVisible) {
expect($element).not.toHaveClass('hidden');
} else {
expect($element).toHaveClass('hidden');
}
}
function findElements() {
$boxClosed = $('div.status-box-closed');
expect($boxClosed).toExist();
expect($boxClosed).toHaveText('Closed');
$boxOpen = $('div.status-box-open');
expect($boxOpen).toExist();
expect($boxOpen).toHaveText('Open');
$btnClose = $('.btn-close.btn-grouped');
expect($btnClose).toExist();
expect($btnClose).toHaveText('Close issue');
$btnReopen = $('.btn-reopen.btn-grouped');
expect($btnReopen).toExist();
expect($btnReopen).toHaveText('Reopen issue');
}
2016-07-24 20:45:11 +00:00
describe('Issue', function() {
describe('task lists', function() {
fixture.load('issues/issue-with-task-list.html.raw');
2016-07-24 20:45:11 +00:00
beforeEach(function() {
this.issue = new Issue();
2016-07-24 20:45:11 +00:00
});
2016-07-24 20:45:11 +00:00
it('modifies the Markdown field', function() {
spyOn(jQuery, 'ajax').and.stub();
$('input[type=checkbox]').attr('checked', true).trigger('change');
expect($('.js-task-list-field').val()).toBe('- [x] Task List Item');
2016-07-24 20:45:11 +00:00
});
it('submits an ajax request on tasklist:changed', function() {
2016-07-24 20:45:11 +00:00
spyOn(jQuery, 'ajax').and.callFake(function(req) {
expect(req.type).toBe('PATCH');
expect(req.url).toBe(gl.TEST_HOST + '/frontend-fixtures/issues-project/issues/1.json'); // eslint-disable-line prefer-template
expect(req.data.issue.description).not.toBe(null);
2016-07-24 20:45:11 +00:00
});
$('.js-task-list-field').trigger('tasklist:changed');
2016-07-24 20:45:11 +00:00
});
});
});
2016-08-26 23:06:57 +00:00
describe('close issue', function() {
2016-07-24 20:45:11 +00:00
beforeEach(function() {
fixture.load('issues/open-issue.html.raw');
2016-08-26 23:06:57 +00:00
findElements();
this.issue = new Issue();
expectIssueState(true);
2016-07-24 20:45:11 +00:00
});
2016-08-26 23:06:57 +00:00
2016-07-24 20:45:11 +00:00
it('closes an issue', function() {
spyOn(jQuery, 'ajax').and.callFake(function(req) {
2016-08-26 23:06:57 +00:00
expectPendingRequest(req, $btnClose);
req.success({
2016-07-24 20:45:11 +00:00
id: 34
});
});
2016-08-26 23:06:57 +00:00
2016-07-24 20:45:11 +00:00
$btnClose.trigger('click');
2016-08-26 23:06:57 +00:00
expectIssueState(false);
expect($btnClose).toHaveProp('disabled', false);
2016-07-24 20:45:11 +00:00
});
2016-08-26 23:06:57 +00:00
2016-07-24 20:45:11 +00:00
it('fails to close an issue with success:false', function() {
spyOn(jQuery, 'ajax').and.callFake(function(req) {
2016-08-26 23:06:57 +00:00
expectPendingRequest(req, $btnClose);
req.success({
2016-07-24 20:45:11 +00:00
saved: false
});
});
2016-08-26 23:06:57 +00:00
$btnClose.attr('href', INVALID_URL);
2016-07-24 20:45:11 +00:00
$btnClose.trigger('click');
2016-08-26 23:06:57 +00:00
expectIssueState(true);
expect($btnClose).toHaveProp('disabled', false);
expectErrorMessage();
2016-07-24 20:45:11 +00:00
});
2016-08-26 23:06:57 +00:00
2016-07-24 20:45:11 +00:00
it('fails to closes an issue with HTTP error', function() {
spyOn(jQuery, 'ajax').and.callFake(function(req) {
2016-08-26 23:06:57 +00:00
expectPendingRequest(req, $btnClose);
req.error();
2016-07-24 20:45:11 +00:00
});
2016-08-26 23:06:57 +00:00
$btnClose.attr('href', INVALID_URL);
2016-07-24 20:45:11 +00:00
$btnClose.trigger('click');
2016-08-26 23:06:57 +00:00
expectIssueState(true);
expect($btnClose).toHaveProp('disabled', true);
expectErrorMessage();
});
});
describe('reopen issue', function() {
beforeEach(function() {
fixture.load('issues/closed-issue.html.raw');
2016-08-26 23:06:57 +00:00
findElements();
this.issue = new Issue();
expectIssueState(false);
2016-07-24 20:45:11 +00:00
});
2016-08-26 23:06:57 +00:00
it('reopens an issue', function() {
2016-07-24 20:45:11 +00:00
spyOn(jQuery, 'ajax').and.callFake(function(req) {
2016-08-26 23:06:57 +00:00
expectPendingRequest(req, $btnReopen);
req.success({
2016-07-24 20:45:11 +00:00
id: 34
});
});
2016-08-26 23:06:57 +00:00
2016-07-24 20:45:11 +00:00
$btnReopen.trigger('click');
2016-08-26 23:06:57 +00:00
expectIssueState(true);
expect($btnReopen).toHaveProp('disabled', false);
2016-07-24 20:45:11 +00:00
});
});
}).call(this);