Commit 7c659e5b authored by Phil Hughes's avatar Phil Hughes

Converted issue.js to axios

parent e74e6fcb
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, one-var, no-underscore-dangle, one-var-declaration-per-line, object-shorthand, no-unused-vars, no-new, comma-dangle, consistent-return, quotes, dot-notation, quote-props, prefer-arrow-callback, max-len */ /* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, one-var, no-underscore-dangle, one-var-declaration-per-line, object-shorthand, no-unused-vars, no-new, comma-dangle, consistent-return, quotes, dot-notation, quote-props, prefer-arrow-callback, max-len */
import 'vendor/jquery.waitforimages'; import 'vendor/jquery.waitforimages';
import axios from './lib/utils/axios_utils';
import { addDelimiter } from './lib/utils/text_utility'; import { addDelimiter } from './lib/utils/text_utility';
import Flash from './flash'; import flash from './flash';
import TaskList from './task_list'; import TaskList from './task_list';
import CreateMergeRequestDropdown from './create_merge_request_dropdown'; import CreateMergeRequestDropdown from './create_merge_request_dropdown';
import IssuablesHelper from './helpers/issuables_helper'; import IssuablesHelper from './helpers/issuables_helper';
...@@ -42,12 +43,8 @@ export default class Issue { ...@@ -42,12 +43,8 @@ export default class Issue {
this.disableCloseReopenButton($button); this.disableCloseReopenButton($button);
url = $button.attr('href'); url = $button.attr('href');
return $.ajax({ return axios.put(url)
type: 'PUT', .then(({ data }) => {
url: url
})
.fail(() => new Flash(issueFailMessage))
.done((data) => {
const isClosedBadge = $('div.status-box-issue-closed'); const isClosedBadge = $('div.status-box-issue-closed');
const isOpenBadge = $('div.status-box-open'); const isOpenBadge = $('div.status-box-open');
const projectIssuesCounter = $('.issue_counter'); const projectIssuesCounter = $('.issue_counter');
...@@ -74,9 +71,10 @@ export default class Issue { ...@@ -74,9 +71,10 @@ export default class Issue {
} }
} }
} else { } else {
new Flash(issueFailMessage); flash(issueFailMessage);
} }
}) })
.catch(() => flash(issueFailMessage))
.then(() => { .then(() => {
this.disableCloseReopenButton($button, false); this.disableCloseReopenButton($button, false);
}); });
...@@ -115,24 +113,22 @@ export default class Issue { ...@@ -115,24 +113,22 @@ export default class Issue {
static initMergeRequests() { static initMergeRequests() {
var $container; var $container;
$container = $('#merge-requests'); $container = $('#merge-requests');
return $.getJSON($container.data('url')).fail(function() { return axios.get($container.data('url'))
return new Flash('Failed to load referenced merge requests'); .then(({ data }) => {
}).done(function(data) {
if ('html' in data) { if ('html' in data) {
return $container.html(data.html); $container.html(data.html);
} }
}); }).catch(() => flash('Failed to load referenced merge requests'));
} }
static initRelatedBranches() { static initRelatedBranches() {
var $container; var $container;
$container = $('#related-branches'); $container = $('#related-branches');
return $.getJSON($container.data('url')).fail(function() { return axios.get($container.data('url'))
return new Flash('Failed to load related branches'); .then(({ data }) => {
}).done(function(data) {
if ('html' in data) { if ('html' in data) {
return $container.html(data.html); $container.html(data.html);
} }
}); }).catch(() => flash('Failed to load related branches'));
} }
} }
/* eslint-disable space-before-function-paren, one-var, one-var-declaration-per-line, no-use-before-define, comma-dangle, max-len */ /* eslint-disable space-before-function-paren, one-var, one-var-declaration-per-line, no-use-before-define, comma-dangle, max-len */
import MockAdaptor from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import Issue from '~/issue'; import Issue from '~/issue';
import '~/lib/utils/text_utility'; import '~/lib/utils/text_utility';
...@@ -88,6 +90,7 @@ describe('Issue', function() { ...@@ -88,6 +90,7 @@ describe('Issue', function() {
[true, false].forEach((isIssueInitiallyOpen) => { [true, false].forEach((isIssueInitiallyOpen) => {
describe(`with ${isIssueInitiallyOpen ? 'open' : 'closed'} issue`, function() { describe(`with ${isIssueInitiallyOpen ? 'open' : 'closed'} issue`, function() {
const action = isIssueInitiallyOpen ? 'close' : 'reopen'; const action = isIssueInitiallyOpen ? 'close' : 'reopen';
let mock;
function ajaxSpy(req) { function ajaxSpy(req) {
if (req.url === this.$triggeredButton.attr('href')) { if (req.url === this.$triggeredButton.attr('href')) {
...@@ -104,6 +107,18 @@ describe('Issue', function() { ...@@ -104,6 +107,18 @@ describe('Issue', function() {
return null; return null;
} }
function mockCloseButtonResponseSuccess(url, response) {
mock.onPut(url).reply(() => {
expectNewBranchButtonState(true, false);
return [200, response];
});
}
function mockCloseButtonResponseError(url) {
mock.onPut(url).networkError();
}
beforeEach(function() { beforeEach(function() {
if (isIssueInitiallyOpen) { if (isIssueInitiallyOpen) {
loadFixtures('issues/open-issue.html.raw'); loadFixtures('issues/open-issue.html.raw');
...@@ -123,68 +138,87 @@ describe('Issue', function() { ...@@ -123,68 +138,87 @@ describe('Issue', function() {
this.issueStateDeferred = new jQuery.Deferred(); this.issueStateDeferred = new jQuery.Deferred();
this.canCreateBranchDeferred = new jQuery.Deferred(); this.canCreateBranchDeferred = new jQuery.Deferred();
mock = new MockAdaptor(axios);
spyOn(jQuery, 'ajax').and.callFake(ajaxSpy.bind(this)); spyOn(jQuery, 'ajax').and.callFake(ajaxSpy.bind(this));
}); });
it(`${action}s the issue`, function() { afterEach(() => {
this.$triggeredButton.trigger('click'); mock.restore();
this.issueStateDeferred.resolve({ });
it(`${action}s the issue`, function(done) {
mockCloseButtonResponseSuccess(this.$triggeredButton.attr('href'), {
id: 34 id: 34
}); });
this.$triggeredButton.trigger('click');
this.canCreateBranchDeferred.resolve({ this.canCreateBranchDeferred.resolve({
can_create_branch: !isIssueInitiallyOpen can_create_branch: !isIssueInitiallyOpen
}); });
setTimeout(() => {
expectIssueState(!isIssueInitiallyOpen); expectIssueState(!isIssueInitiallyOpen);
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull(); expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
expect(this.$projectIssuesCounter.text()).toBe(isIssueInitiallyOpen ? '1,000' : '1,002'); expect(this.$projectIssuesCounter.text()).toBe(isIssueInitiallyOpen ? '1,000' : '1,002');
expectNewBranchButtonState(false, !isIssueInitiallyOpen); expectNewBranchButtonState(false, !isIssueInitiallyOpen);
done();
});
}); });
it(`fails to ${action} the issue if saved:false`, function() { it(`fails to ${action} the issue if saved:false`, function(done) {
this.$triggeredButton.trigger('click'); mockCloseButtonResponseSuccess(this.$triggeredButton.attr('href'), {
this.issueStateDeferred.resolve({
saved: false saved: false
}); });
this.$triggeredButton.trigger('click');
this.canCreateBranchDeferred.resolve({ this.canCreateBranchDeferred.resolve({
can_create_branch: isIssueInitiallyOpen can_create_branch: isIssueInitiallyOpen
}); });
setTimeout(() => {
expectIssueState(isIssueInitiallyOpen); expectIssueState(isIssueInitiallyOpen);
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull(); expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
expectErrorMessage(); expectErrorMessage();
expect(this.$projectIssuesCounter.text()).toBe('1,001'); expect(this.$projectIssuesCounter.text()).toBe('1,001');
expectNewBranchButtonState(false, isIssueInitiallyOpen); expectNewBranchButtonState(false, isIssueInitiallyOpen);
done();
});
}); });
it(`fails to ${action} the issue if HTTP error occurs`, function() { it(`fails to ${action} the issue if HTTP error occurs`, function(done) {
mockCloseButtonResponseError(this.$triggeredButton.attr('href'));
this.$triggeredButton.trigger('click'); this.$triggeredButton.trigger('click');
this.issueStateDeferred.reject();
this.canCreateBranchDeferred.resolve({ this.canCreateBranchDeferred.resolve({
can_create_branch: isIssueInitiallyOpen can_create_branch: isIssueInitiallyOpen
}); });
setTimeout(() => {
expectIssueState(isIssueInitiallyOpen); expectIssueState(isIssueInitiallyOpen);
expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull(); expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
expectErrorMessage(); expectErrorMessage();
expect(this.$projectIssuesCounter.text()).toBe('1,001'); expect(this.$projectIssuesCounter.text()).toBe('1,001');
expectNewBranchButtonState(false, isIssueInitiallyOpen); expectNewBranchButtonState(false, isIssueInitiallyOpen);
done();
});
}); });
it('disables the new branch button if Ajax call fails', function() { it('disables the new branch button if Ajax call fails', function() {
mockCloseButtonResponseError(this.$triggeredButton.attr('href'));
this.$triggeredButton.trigger('click'); this.$triggeredButton.trigger('click');
this.issueStateDeferred.reject();
this.canCreateBranchDeferred.reject(); this.canCreateBranchDeferred.reject();
expectNewBranchButtonState(false, false); expectNewBranchButtonState(false, false);
}); });
it('does not trigger Ajax call if new branch button is missing', function() { it('does not trigger Ajax call if new branch button is missing', function() {
mockCloseButtonResponseError(this.$triggeredButton.attr('href'));
Issue.$btnNewBranch = $(); Issue.$btnNewBranch = $();
this.canCreateBranchDeferred = null; this.canCreateBranchDeferred = null;
this.$triggeredButton.trigger('click'); this.$triggeredButton.trigger('click');
this.issueStateDeferred.reject();
}); });
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment