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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

194 lines
5.3 KiB
JavaScript
Raw Normal View History

2021-03-08 18:12:59 +05:30
import MockAdapter from 'axios-mock-adapter';
2022-10-11 01:57:18 +05:30
import {
axiosInstance,
addSubscription,
removeSubscription,
fetchGroups,
getCurrentUser,
addJiraConnectSubscription,
updateInstallation,
} from '~/jira_connect/subscriptions/api';
2021-10-27 15:23:28 +05:30
import { getJwt } from '~/jira_connect/subscriptions/utils';
2021-03-08 18:12:59 +05:30
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', () => {
2022-10-11 01:57:18 +05:30
let axiosMock;
let originalGon;
2021-03-08 18:12:59 +05:30
let response;
const mockAddPath = 'addPath';
const mockRemovePath = 'removePath';
const mockNamespace = 'namespace';
const mockJwt = 'jwt';
2022-10-11 01:57:18 +05:30
const mockAccessToken = 'accessToken';
2021-03-08 18:12:59 +05:30
const mockResponse = { success: true };
beforeEach(() => {
2022-10-11 01:57:18 +05:30
axiosMock = new MockAdapter(axiosInstance);
originalGon = window.gon;
window.gon = { api_version: 'v4' };
2021-03-08 18:12:59 +05:30
});
afterEach(() => {
2022-10-11 01:57:18 +05:30
axiosMock.restore();
window.gon = originalGon;
2021-03-08 18:12:59 +05:30
response = null;
});
describe('addSubscription', () => {
const makeRequest = () => addSubscription(mockAddPath, mockNamespace);
it('returns success response', async () => {
2022-10-11 01:57:18 +05:30
jest.spyOn(axiosInstance, 'post');
axiosMock
2021-03-08 18:12:59 +05:30
.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();
2022-10-11 01:57:18 +05:30
expect(axiosInstance.post).toHaveBeenCalledWith(mockAddPath, {
2021-03-08 18:12:59 +05:30
jwt: mockJwt,
namespace_path: mockNamespace,
});
expect(response.data).toEqual(mockResponse);
});
});
describe('removeSubscription', () => {
const makeRequest = () => removeSubscription(mockRemovePath);
it('returns success response', async () => {
2022-10-11 01:57:18 +05:30
jest.spyOn(axiosInstance, 'delete');
axiosMock.onDelete(mockRemovePath).replyOnce(httpStatus.OK, mockResponse);
2021-03-08 18:12:59 +05:30
response = await makeRequest();
2021-04-29 21:17:54 +05:30
expect(getJwt).toHaveBeenCalled();
2022-10-11 01:57:18 +05:30
expect(axiosInstance.delete).toHaveBeenCalledWith(mockRemovePath, {
2021-03-08 18:12:59 +05:30
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 () => {
2022-10-11 01:57:18 +05:30
jest.spyOn(axiosInstance, 'get');
axiosMock
2021-03-08 18:12:59 +05:30
.onGet(mockGroupsPath, {
page: mockPage,
per_page: mockPerPage,
})
.replyOnce(httpStatus.OK, mockResponse);
response = await makeRequest();
2022-10-11 01:57:18 +05:30
expect(axiosInstance.get).toHaveBeenCalledWith(mockGroupsPath, {
2021-03-08 18:12:59 +05:30
params: {
page: mockPage,
per_page: mockPerPage,
},
});
expect(response.data).toEqual(mockResponse);
});
});
2022-10-11 01:57:18 +05:30
describe('getCurrentUser', () => {
const makeRequest = () => getCurrentUser();
it('returns success response', async () => {
const expectedUrl = '/api/v4/user';
jest.spyOn(axiosInstance, 'get');
axiosMock.onGet(expectedUrl).replyOnce(httpStatus.OK, mockResponse);
response = await makeRequest();
expect(axiosInstance.get).toHaveBeenCalledWith(expectedUrl, {});
expect(response.data).toEqual(mockResponse);
});
});
describe('addJiraConnectSubscription', () => {
const makeRequest = () =>
addJiraConnectSubscription(mockNamespace, { jwt: mockJwt, accessToken: mockAccessToken });
it('returns success response', async () => {
const expectedUrl = '/api/v4/integrations/jira_connect/subscriptions';
jest.spyOn(axiosInstance, 'post');
axiosMock.onPost(expectedUrl).replyOnce(httpStatus.OK, mockResponse);
response = await makeRequest();
expect(axiosInstance.post).toHaveBeenCalledWith(
expectedUrl,
{
jwt: mockJwt,
namespace_path: mockNamespace,
},
{ headers: { Authorization: `Bearer ${mockAccessToken}` } },
);
expect(response.data).toEqual(mockResponse);
});
});
describe('updateInstallation', () => {
const expectedUrl = '/-/jira_connect/installations';
it.each`
instanceUrl | expectedInstanceUrl
${'https://gitlab.com'} | ${null}
${'https://gitlab.mycompany.com'} | ${'https://gitlab.mycompany.com'}
`(
'when instanceUrl is $instanceUrl, it passes `instance_url` as $expectedInstanceUrl',
async ({ instanceUrl, expectedInstanceUrl }) => {
const makeRequest = () => updateInstallation(instanceUrl);
jest.spyOn(axiosInstance, 'put');
axiosMock
.onPut(expectedUrl, {
jwt: mockJwt,
installation: {
instance_url: expectedInstanceUrl,
},
})
.replyOnce(httpStatus.OK, mockResponse);
response = await makeRequest();
expect(getJwt).toHaveBeenCalled();
expect(axiosInstance.put).toHaveBeenCalledWith(expectedUrl, {
jwt: mockJwt,
installation: {
instance_url: expectedInstanceUrl,
},
});
expect(response.data).toEqual(mockResponse);
},
);
});
2021-03-08 18:12:59 +05:30
});