2021-01-05 07:10:36 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { addSubscription, removeSubscription, fetchGroups } from '~/jira_connect/api';
|
2021-01-05 07:10:36 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import httpStatus from '~/lib/utils/http_status';
|
|
|
|
|
|
|
|
describe('JiraConnect API', () => {
|
|
|
|
let mock;
|
|
|
|
let response;
|
|
|
|
|
|
|
|
const mockAddPath = 'addPath';
|
|
|
|
const mockRemovePath = 'removePath';
|
|
|
|
const mockNamespace = 'namespace';
|
|
|
|
const mockJwt = 'jwt';
|
|
|
|
const mockResponse = { success: true };
|
|
|
|
|
2021-01-25 13:09:03 -05:00
|
|
|
const tokenSpy = jest.fn((callback) => callback(mockJwt));
|
2021-01-05 07:10:36 -05:00
|
|
|
|
|
|
|
window.AP = {
|
|
|
|
context: {
|
|
|
|
getToken: tokenSpy,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
response = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('addSubscription', () => {
|
|
|
|
const makeRequest = () => addSubscription(mockAddPath, mockNamespace);
|
|
|
|
|
|
|
|
it('returns success response', async () => {
|
|
|
|
jest.spyOn(axios, 'post');
|
|
|
|
mock
|
|
|
|
.onPost(mockAddPath, {
|
|
|
|
jwt: mockJwt,
|
|
|
|
namespace_path: mockNamespace,
|
|
|
|
})
|
|
|
|
.replyOnce(httpStatus.OK, mockResponse);
|
|
|
|
|
|
|
|
response = await makeRequest();
|
|
|
|
|
|
|
|
expect(tokenSpy).toHaveBeenCalled();
|
|
|
|
expect(axios.post).toHaveBeenCalledWith(mockAddPath, {
|
|
|
|
jwt: mockJwt,
|
|
|
|
namespace_path: mockNamespace,
|
|
|
|
});
|
|
|
|
expect(response.data).toEqual(mockResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeSubscription', () => {
|
|
|
|
const makeRequest = () => removeSubscription(mockRemovePath);
|
|
|
|
|
|
|
|
it('returns success response', async () => {
|
|
|
|
jest.spyOn(axios, 'delete');
|
|
|
|
mock.onDelete(mockRemovePath).replyOnce(httpStatus.OK, mockResponse);
|
|
|
|
|
|
|
|
response = await makeRequest();
|
|
|
|
|
|
|
|
expect(tokenSpy).toHaveBeenCalled();
|
|
|
|
expect(axios.delete).toHaveBeenCalledWith(mockRemovePath, {
|
|
|
|
params: {
|
|
|
|
jwt: mockJwt,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(response.data).toEqual(mockResponse);
|
|
|
|
});
|
|
|
|
});
|
2021-01-18 10:10:42 -05:00
|
|
|
|
|
|
|
describe('fetchGroups', () => {
|
|
|
|
const mockGroupsPath = 'groupsPath';
|
|
|
|
const mockPage = 1;
|
|
|
|
const mockPerPage = 10;
|
|
|
|
|
|
|
|
const makeRequest = () =>
|
|
|
|
fetchGroups(mockGroupsPath, {
|
|
|
|
page: mockPage,
|
|
|
|
perPage: mockPerPage,
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns success response', async () => {
|
|
|
|
jest.spyOn(axios, 'get');
|
|
|
|
mock
|
|
|
|
.onGet(mockGroupsPath, {
|
|
|
|
page: mockPage,
|
|
|
|
per_page: mockPerPage,
|
|
|
|
})
|
|
|
|
.replyOnce(httpStatus.OK, mockResponse);
|
|
|
|
|
|
|
|
response = await makeRequest();
|
|
|
|
|
|
|
|
expect(axios.get).toHaveBeenCalledWith(mockGroupsPath, {
|
|
|
|
params: {
|
|
|
|
page: mockPage,
|
|
|
|
per_page: mockPerPage,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(response.data).toEqual(mockResponse);
|
|
|
|
});
|
|
|
|
});
|
2021-01-05 07:10:36 -05:00
|
|
|
});
|