Commit a33d35c0 authored by Clement Ho's avatar Clement Ho Committed by Jacob Schatz

Replace $.ajax with axios in approvers select

parent 1d237d4b
import Api from '~/api'; import Api from '~/api';
import { __ } from '~/locale';
import Flash from '~/flash';
import axios from '~/lib/utils/axios_utils';
export default class ApproversSelect { export default class ApproversSelect {
constructor() { constructor() {
...@@ -147,7 +150,7 @@ export default class ApproversSelect { ...@@ -147,7 +150,7 @@ export default class ApproversSelect {
} }
static saveApprovers(fieldName) { static saveApprovers(fieldName) {
const $input = window.$(`[name="${fieldName}"]`); const $input = $(`[name="${fieldName}"]`);
const newValue = $input.val(); const newValue = $input.val();
const $loadWrapper = $('.load-wrapper'); const $loadWrapper = $('.load-wrapper');
const $approverSelect = $('.js-select-user-and-group'); const $approverSelect = $('.js-select-user-and-group');
...@@ -158,23 +161,24 @@ export default class ApproversSelect { ...@@ -158,23 +161,24 @@ export default class ApproversSelect {
const $form = $('.js-add-approvers').closest('form'); const $form = $('.js-add-approvers').closest('form');
$loadWrapper.removeClass('hidden'); $loadWrapper.removeClass('hidden');
window.$.ajax({
url: $form.attr('action'), axios.post($form.attr('action'), `_method=PATCH&${[encodeURIComponent(fieldName)]}=${newValue}`, {
type: 'POST', headers: {
data: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
_method: 'PATCH',
[fieldName]: newValue,
}, },
success: ApproversSelect.updateApproverList, }).then(({ data }) => {
complete() { ApproversSelect.updateApproverList(data);
ApproversSelect.saveApproversComplete($input, $approverSelect, $loadWrapper);
}).catch(() => {
Flash(__('An error occurred while adding approver'));
ApproversSelect.saveApproversComplete($input, $approverSelect, $loadWrapper);
});
}
static saveApproversComplete($input, $approverSelect, $loadWrapper) {
$input.val(''); $input.val('');
$approverSelect.select2('val', ''); $approverSelect.select2('val', '');
$loadWrapper.addClass('hidden'); $loadWrapper.addClass('hidden');
},
error() {
window.Flash('Failed to add Approver', 'alert');
},
});
} }
static removeApprover(e) { static removeApprover(e) {
...@@ -182,17 +186,17 @@ export default class ApproversSelect { ...@@ -182,17 +186,17 @@ export default class ApproversSelect {
const target = e.currentTarget; const target = e.currentTarget;
const $loadWrapper = $('.load-wrapper'); const $loadWrapper = $('.load-wrapper');
$loadWrapper.removeClass('hidden'); $loadWrapper.removeClass('hidden');
$.ajax({
url: target.getAttribute('href'), axios.post(target.getAttribute('href'), '_method=DELETE', {
type: 'POST', headers: {
data: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
_method: 'DELETE',
},
success: ApproversSelect.updateApproverList,
complete: () => $loadWrapper.addClass('hidden'),
error() {
window.Flash('Failed to remove Approver', 'alert');
}, },
}).then(({ data }) => {
ApproversSelect.updateApproverList(data);
$loadWrapper.addClass('hidden');
}).catch(() => {
Flash(__('An error occurred while removing approver'));
$loadWrapper.addClass('hidden');
}); });
} }
......
import ApproversSelect from 'ee/approvers_select'; import ApproversSelect from 'ee/approvers_select';
import ClassSpecHelper from './helpers/class_spec_helper';
describe('ApproversSelect', () => { describe('ApproversSelect', () => {
describe('saveApprovers', () => { describe('saveApproversComplete', () => {
let complete; let $input;
const $input = jasmine.createSpyObj('$input', ['val']); let $approverSelect;
let $loadWrapper;
beforeEach(() => { beforeEach(() => {
spyOn(window, '$').and.returnValue($input); $input = {
spyOn(window.$, 'ajax').and.callFake((options) => { val: jasmine.createSpy(),
complete = options.complete; };
});
$input.val.and.returnValue('newValue'); $approverSelect = {
select2: jasmine.createSpy(),
};
ApproversSelect.saveApprovers('fieldName'); $loadWrapper = {
}); addClass: jasmine.createSpy(),
};
ClassSpecHelper.itShouldBeAStaticMethod(ApproversSelect, 'saveApprovers'); ApproversSelect.saveApproversComplete($input, $approverSelect, $loadWrapper);
});
describe('when request completes', () => {
it('should empty the $input value', () => { it('should empty the $input value', () => {
$input.val.calls.reset();
complete();
expect($input.val).toHaveBeenCalledWith(''); expect($input.val).toHaveBeenCalledWith('');
}); });
it('should empty the select2 value', () => {
expect($approverSelect.select2).toHaveBeenCalledWith('val', '');
});
it('should hide loadWrapper', () => {
expect($loadWrapper.addClass).toHaveBeenCalledWith('hidden');
}); });
}); });
}); });
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