From ee231234eb70b3a40d95c4874c6d6d1b2ca0a39d Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 5 Sep 2022 03:13:23 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- .../jira_connect/subscriptions/api_spec.js | 81 ++++++++++++++++++---- 1 file changed, 67 insertions(+), 14 deletions(-) (limited to 'spec/frontend/jira_connect/subscriptions/api_spec.js') diff --git a/spec/frontend/jira_connect/subscriptions/api_spec.js b/spec/frontend/jira_connect/subscriptions/api_spec.js index 57b11bdbc27..76a0b8fa82c 100644 --- a/spec/frontend/jira_connect/subscriptions/api_spec.js +++ b/spec/frontend/jira_connect/subscriptions/api_spec.js @@ -1,7 +1,13 @@ import MockAdapter from 'axios-mock-adapter'; -import { addSubscription, removeSubscription, fetchGroups } from '~/jira_connect/subscriptions/api'; +import { + axiosInstance, + addSubscription, + removeSubscription, + fetchGroups, + getCurrentUser, + addJiraConnectSubscription, +} from '~/jira_connect/subscriptions/api'; import { getJwt } from '~/jira_connect/subscriptions/utils'; -import axios from '~/lib/utils/axios_utils'; import httpStatus from '~/lib/utils/http_status'; jest.mock('~/jira_connect/subscriptions/utils', () => ({ @@ -9,21 +15,26 @@ jest.mock('~/jira_connect/subscriptions/utils', () => ({ })); describe('JiraConnect API', () => { - let mock; + let axiosMock; + let originalGon; let response; const mockAddPath = 'addPath'; const mockRemovePath = 'removePath'; const mockNamespace = 'namespace'; const mockJwt = 'jwt'; + const mockAccessToken = 'accessToken'; const mockResponse = { success: true }; beforeEach(() => { - mock = new MockAdapter(axios); + axiosMock = new MockAdapter(axiosInstance); + originalGon = window.gon; + window.gon = { api_version: 'v4' }; }); afterEach(() => { - mock.restore(); + axiosMock.restore(); + window.gon = originalGon; response = null; }); @@ -31,8 +42,8 @@ describe('JiraConnect API', () => { const makeRequest = () => addSubscription(mockAddPath, mockNamespace); it('returns success response', async () => { - jest.spyOn(axios, 'post'); - mock + jest.spyOn(axiosInstance, 'post'); + axiosMock .onPost(mockAddPath, { jwt: mockJwt, namespace_path: mockNamespace, @@ -42,7 +53,7 @@ describe('JiraConnect API', () => { response = await makeRequest(); expect(getJwt).toHaveBeenCalled(); - expect(axios.post).toHaveBeenCalledWith(mockAddPath, { + expect(axiosInstance.post).toHaveBeenCalledWith(mockAddPath, { jwt: mockJwt, namespace_path: mockNamespace, }); @@ -54,13 +65,13 @@ describe('JiraConnect API', () => { const makeRequest = () => removeSubscription(mockRemovePath); it('returns success response', async () => { - jest.spyOn(axios, 'delete'); - mock.onDelete(mockRemovePath).replyOnce(httpStatus.OK, mockResponse); + jest.spyOn(axiosInstance, 'delete'); + axiosMock.onDelete(mockRemovePath).replyOnce(httpStatus.OK, mockResponse); response = await makeRequest(); expect(getJwt).toHaveBeenCalled(); - expect(axios.delete).toHaveBeenCalledWith(mockRemovePath, { + expect(axiosInstance.delete).toHaveBeenCalledWith(mockRemovePath, { params: { jwt: mockJwt, }, @@ -81,8 +92,8 @@ describe('JiraConnect API', () => { }); it('returns success response', async () => { - jest.spyOn(axios, 'get'); - mock + jest.spyOn(axiosInstance, 'get'); + axiosMock .onGet(mockGroupsPath, { page: mockPage, per_page: mockPerPage, @@ -91,7 +102,7 @@ describe('JiraConnect API', () => { response = await makeRequest(); - expect(axios.get).toHaveBeenCalledWith(mockGroupsPath, { + expect(axiosInstance.get).toHaveBeenCalledWith(mockGroupsPath, { params: { page: mockPage, per_page: mockPerPage, @@ -100,4 +111,46 @@ describe('JiraConnect API', () => { expect(response.data).toEqual(mockResponse); }); }); + + 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); + }); + }); }); -- cgit v1.2.1