Commit b394ef8d authored by Justin Ho's avatar Justin Ho

Refactor Jira connect api calls to use axios

parent 5f003f9d
import axios from 'axios';
const getJwt = async () => {
return AP.context.getToken();
};
export const addSubscription = async (addPath, namespace) => {
const jwt = await getJwt();
return axios.post(addPath, {
jwt,
namespace_path: namespace,
});
};
export const removeSubscription = async (removePath) => {
const jwt = await getJwt();
return axios.delete(removePath, {
params: {
jwt,
},
});
};
import Vue from 'vue';
import $ from 'jquery';
import App from './components/app.vue';
import { addSubscription, removeSubscription } from '~/jira_connect/api';
const store = {
state: {
......@@ -35,38 +36,25 @@ const initJiraFormHandlers = () => {
});
$('#add-subscription-form').on('submit', function onAddSubscriptionForm(e) {
const actionUrl = $(this).attr('action');
const addPath = $(this).attr('action');
const namespace = $('#namespace-input').val();
e.preventDefault();
AP.context.getToken((token) => {
// eslint-disable-next-line no-jquery/no-ajax
$.post(actionUrl, {
jwt: token,
namespace_path: $('#namespace-input').val(),
format: 'json',
})
.done(reqComplete)
.fail((err) => reqFailed(err, 'Failed to add namespace. Please try again.'));
});
addSubscription(addPath, namespace)
.then(reqComplete)
.catch((err) => reqFailed(err.response.data, 'Failed to add namespace. Please try again.'));
});
$('.remove-subscription').on('click', function onRemoveSubscriptionClick(e) {
const href = $(this).attr('href');
const removePath = $(this).attr('href');
e.preventDefault();
AP.context.getToken((token) => {
// eslint-disable-next-line no-jquery/no-ajax
$.ajax({
url: href,
method: 'DELETE',
data: {
jwt: token,
format: 'json',
},
})
.done(reqComplete)
.fail((err) => reqFailed(err, 'Failed to remove namespace. Please try again.'));
});
removeSubscription(removePath)
.then(reqComplete)
.catch((err) =>
reqFailed(err.response.data, 'Failed to remove namespace. Please try again.'),
);
});
};
......
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import httpStatus from '~/lib/utils/http_status';
import { addSubscription, removeSubscription } from '~/jira_connect/api';
describe('JiraConnect API', () => {
let mock;
let response;
const addPath = 'addPath';
const removePath = 'removePath';
const namespace = 'namespace';
const jwt = 'jwt';
const successResponse = { success: true };
const tokenSpy = jest.fn().mockReturnValue(jwt);
window.AP = {
context: {
getToken: tokenSpy,
},
};
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
response = null;
});
describe('addSubscription', () => {
const makeRequest = () => addSubscription(addPath, namespace);
it('returns success response', async () => {
jest.spyOn(axios, 'post');
mock
.onPost(addPath, {
jwt,
namespace_path: namespace,
})
.replyOnce(httpStatus.OK, successResponse);
response = await makeRequest();
expect(tokenSpy).toHaveBeenCalled();
expect(axios.post).toHaveBeenCalledWith(addPath, {
jwt,
namespace_path: namespace,
});
expect(response.data).toEqual(successResponse);
});
});
describe('removeSubscription', () => {
const makeRequest = () => removeSubscription(removePath);
it('returns success response', async () => {
jest.spyOn(axios, 'delete');
mock.onDelete(removePath).replyOnce(httpStatus.OK, successResponse);
response = await makeRequest();
expect(tokenSpy).toHaveBeenCalled();
expect(axios.delete).toHaveBeenCalledWith(removePath, {
params: {
jwt,
},
});
expect(response.data).toEqual(successResponse);
});
});
});
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