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

165 lines
4.7 KiB
JavaScript
Raw Normal View History

2017-01-11 22:49:57 -05:00
/* eslint-disable space-before-function-paren, no-var, one-var, one-var-declaration-per-line, no-use-before-define, comma-dangle, max-len */
/* global Issue */
2016-07-24 16:45:11 -04:00
/*= require lib/utils/text_utility */
/*= require issue */
(function() {
2016-08-26 19:06:57 -04:00
var INVALID_URL = 'http://goesnowhere.nothing/whereami';
var $boxClosed, $boxOpen, $btnClose, $btnReopen;
preloadFixtures('issues/closed-issue.html.raw');
preloadFixtures('issues/issue-with-task-list.html.raw');
preloadFixtures('issues/open-issue.html.raw');
2016-08-26 19:06:57 -04: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() {
2017-01-11 22:49:57 -05:00
$boxClosed = $('div.status-box-closed');
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-01-11 22:49:57 -05:00
$btnClose = $('.btn-close.btn-grouped');
expect($btnClose).toExist();
expect($btnClose).toHaveText('Close issue');
2016-08-26 19:06:57 -04:00
2017-01-11 22:49:57 -05:00
$btnReopen = $('.btn-reopen.btn-grouped');
expect($btnReopen).toExist();
expect($btnReopen).toHaveText('Reopen issue');
2016-08-26 19:06:57 -04:00
}
2016-07-24 16:45:11 -04:00
describe('Issue', function() {
describe('task lists', function() {
2016-07-24 16:45:11 -04:00
beforeEach(function() {
loadFixtures('issues/issue-with-task-list.html.raw');
this.issue = new Issue();
2016-07-24 16:45:11 -04:00
});
2016-07-24 16:45:11 -04: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 16:45:11 -04:00
});
it('submits an ajax request on tasklist:changed', function() {
2016-07-24 16:45:11 -04: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 16:45:11 -04:00
});
$('.js-task-list-field').trigger('tasklist:changed');
2016-07-24 16:45:11 -04:00
});
});
});
2016-08-26 19:06:57 -04:00
describe('close issue', function() {
2016-07-24 16:45:11 -04:00
beforeEach(function() {
loadFixtures('issues/open-issue.html.raw');
2016-08-26 19:06:57 -04:00
findElements();
this.issue = new Issue();
expectIssueState(true);
2016-07-24 16:45:11 -04:00
});
2016-08-26 19:06:57 -04:00
2016-07-24 16:45:11 -04:00
it('closes an issue', function() {
spyOn(jQuery, 'ajax').and.callFake(function(req) {
2016-08-26 19:06:57 -04:00
expectPendingRequest(req, $btnClose);
req.success({
2016-07-24 16:45:11 -04:00
id: 34
});
});
2016-08-26 19:06:57 -04:00
2016-07-24 16:45:11 -04:00
$btnClose.trigger('click');
2016-08-26 19:06:57 -04:00
expectIssueState(false);
expect($btnClose).toHaveProp('disabled', false);
2016-07-24 16:45:11 -04:00
});
2016-08-26 19:06:57 -04:00
2016-07-24 16:45:11 -04:00
it('fails to close an issue with success:false', function() {
spyOn(jQuery, 'ajax').and.callFake(function(req) {
2016-08-26 19:06:57 -04:00
expectPendingRequest(req, $btnClose);
req.success({
2016-07-24 16:45:11 -04:00
saved: false
});
});
2016-08-26 19:06:57 -04:00
$btnClose.attr('href', INVALID_URL);
2016-07-24 16:45:11 -04:00
$btnClose.trigger('click');
2016-08-26 19:06:57 -04:00
expectIssueState(true);
expect($btnClose).toHaveProp('disabled', false);
expectErrorMessage();
2016-07-24 16:45:11 -04:00
});
2016-08-26 19:06:57 -04:00
2016-07-24 16:45:11 -04:00
it('fails to closes an issue with HTTP error', function() {
spyOn(jQuery, 'ajax').and.callFake(function(req) {
2016-08-26 19:06:57 -04:00
expectPendingRequest(req, $btnClose);
req.error();
2016-07-24 16:45:11 -04:00
});
2016-08-26 19:06:57 -04:00
$btnClose.attr('href', INVALID_URL);
2016-07-24 16:45:11 -04:00
$btnClose.trigger('click');
2016-08-26 19:06:57 -04:00
expectIssueState(true);
expect($btnClose).toHaveProp('disabled', true);
expectErrorMessage();
});
});
describe('reopen issue', function() {
beforeEach(function() {
loadFixtures('issues/closed-issue.html.raw');
2016-08-26 19:06:57 -04:00
findElements();
this.issue = new Issue();
expectIssueState(false);
2016-07-24 16:45:11 -04:00
});
2016-08-26 19:06:57 -04:00
it('reopens an issue', function() {
2016-07-24 16:45:11 -04:00
spyOn(jQuery, 'ajax').and.callFake(function(req) {
2016-08-26 19:06:57 -04:00
expectPendingRequest(req, $btnReopen);
req.success({
2016-07-24 16:45:11 -04:00
id: 34
});
});
2016-08-26 19:06:57 -04:00
2016-07-24 16:45:11 -04:00
$btnReopen.trigger('click');
2016-08-26 19:06:57 -04:00
expectIssueState(true);
expect($btnReopen).toHaveProp('disabled', false);
2016-07-24 16:45:11 -04:00
});
});
}).call(this);