debian-mirror-gitlab/spec/frontend/jira_connect/subscriptions/api_spec.js

104 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-03-08 18:12:59 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-10-27 15:23:28 +05:30
import { addSubscription, removeSubscription, fetchGroups } from '~/jira_connect/subscriptions/api';
import { getJwt } from '~/jira_connect/subscriptions/utils';
2021-03-08 18:12:59 +05:30
import axios from '~/lib/utils/axios_utils';
import httpStatus from '~/lib/utils/http_status';
2021-10-27 15:23:28 +05:30
jest.mock('~/jira_connect/subscriptions/utils', () => ({
2021-04-29 21:17:54 +05:30
getJwt: jest.fn().mockResolvedValue('jwt'),
}));
2021-03-08 18:12:59 +05:30
describe('JiraConnect API', () => {
let mock;
let response;
const mockAddPath = 'addPath';
const mockRemovePath = 'removePath';
const mockNamespace = 'namespace';
const mockJwt = 'jwt';
const mockResponse = { success: true };
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();
2021-04-29 21:17:54 +05:30
expect(getJwt).toHaveBeenCalled();
2021-03-08 18:12:59 +05:30
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();
2021-04-29 21:17:54 +05:30
expect(getJwt).toHaveBeenCalled();
2021-03-08 18:12:59 +05:30
expect(axios.delete).toHaveBeenCalledWith(mockRemovePath, {
params: {
jwt: mockJwt,
},
});
expect(response.data).toEqual(mockResponse);
});
});
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);
});
});
});