Commit 74890212 authored by Nathan Friend's avatar Nathan Friend

Refactor ee and non-ee api_spec.js

Updates api_spec.js (and its ee/ counterpart) to use status codes from
http_status.js and refactors away some duplicate boilerplate.
parent 38640295
...@@ -22,6 +22,7 @@ const httpStatusCodes = { ...@@ -22,6 +22,7 @@ const httpStatusCodes = {
CONFLICT: 409, CONFLICT: 409,
GONE: 410, GONE: 410,
UNPROCESSABLE_ENTITY: 422, UNPROCESSABLE_ENTITY: 422,
INTERNAL_SERVER_ERROR: 500,
SERVICE_UNAVAILABLE: 503, SERVICE_UNAVAILABLE: 503,
}; };
......
...@@ -48,7 +48,7 @@ describe('Api', () => { ...@@ -48,7 +48,7 @@ describe('Api', () => {
const callback = jest.fn(); const callback = jest.fn();
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/ldap/${provider}/groups.json`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/ldap/${provider}/groups.json`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -75,7 +75,7 @@ describe('Api', () => { ...@@ -75,7 +75,7 @@ describe('Api', () => {
iid: 5, iid: 5,
}; };
mock.onPost(expectedUrl).reply(200, expectedRes); mock.onPost(expectedUrl).reply(httpStatus.OK, expectedRes);
Api.createChildEpic({ groupId, parentEpicIid, title }) Api.createChildEpic({ groupId, parentEpicIid, title })
.then(({ data }) => { .then(({ data }) => {
...@@ -100,7 +100,7 @@ describe('Api', () => { ...@@ -100,7 +100,7 @@ describe('Api', () => {
include_descendant_groups: true, include_descendant_groups: true,
}, },
}) })
.reply(200, mockEpics); .reply(httpStatus.OK, mockEpics);
Api.groupEpics({ groupId }) Api.groupEpics({ groupId })
.then(({ data }) => { .then(({ data }) => {
...@@ -127,7 +127,7 @@ describe('Api', () => { ...@@ -127,7 +127,7 @@ describe('Api', () => {
search: 'foo', search: 'foo',
}, },
}) })
.reply(200, mockEpics); .reply(httpStatus.OK, mockEpics);
Api.groupEpics({ groupId, search: 'foo' }) Api.groupEpics({ groupId, search: 'foo' })
.then(({ data }) => { .then(({ data }) => {
...@@ -156,7 +156,7 @@ describe('Api', () => { ...@@ -156,7 +156,7 @@ describe('Api', () => {
issue: mockIssue, issue: mockIssue,
}; };
mock.onPost(expectedUrl).reply(200, expectedRes); mock.onPost(expectedUrl).reply(httpStatus.OK, expectedRes);
Api.addEpicIssue({ groupId, epicIid: mockEpics[0].iid, issueId: mockIssue.id }) Api.addEpicIssue({ groupId, epicIid: mockEpics[0].iid, issueId: mockIssue.id })
.then(({ data }) => { .then(({ data }) => {
...@@ -183,7 +183,7 @@ describe('Api', () => { ...@@ -183,7 +183,7 @@ describe('Api', () => {
issue: mockIssue, issue: mockIssue,
}; };
mock.onDelete(expectedUrl).reply(200, expectedRes); mock.onDelete(expectedUrl).reply(httpStatus.OK, expectedRes);
Api.removeEpicIssue({ Api.removeEpicIssue({
groupId, groupId,
...@@ -248,7 +248,7 @@ describe('Api', () => { ...@@ -248,7 +248,7 @@ describe('Api', () => {
label_ids: labelIds, label_ids: labelIds,
}; };
const expectedUrl = analyticsMockData.endpoints.tasksByTypeData; const expectedUrl = analyticsMockData.endpoints.tasksByTypeData;
mock.onGet(expectedUrl).reply(200, tasksByTypeResponse); mock.onGet(expectedUrl).reply(httpStatus.OK, tasksByTypeResponse);
Api.cycleAnalyticsTasksByType(groupId, params) Api.cycleAnalyticsTasksByType(groupId, params)
.then(({ data, config: { params: reqParams } }) => { .then(({ data, config: { params: reqParams } }) => {
...@@ -272,7 +272,7 @@ describe('Api', () => { ...@@ -272,7 +272,7 @@ describe('Api', () => {
}; };
const expectedUrl = analyticsMockData.endpoints.tasksByTypeTopLabelsData; const expectedUrl = analyticsMockData.endpoints.tasksByTypeTopLabelsData;
mock.onGet(expectedUrl).reply(200, response); mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsTopLabels(groupId, params) Api.cycleAnalyticsTopLabels(groupId, params)
.then(({ data, config: { url, params: reqParams } }) => { .then(({ data, config: { url, params: reqParams } }) => {
...@@ -293,7 +293,7 @@ describe('Api', () => { ...@@ -293,7 +293,7 @@ describe('Api', () => {
}; };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/summary`; const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/summary`;
mock.onGet(expectedUrl).reply(200, response); mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsSummaryData(groupId, params) Api.cycleAnalyticsSummaryData(groupId, params)
.then(responseObj => .then(responseObj =>
...@@ -319,7 +319,7 @@ describe('Api', () => { ...@@ -319,7 +319,7 @@ describe('Api', () => {
}; };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/time_summary`; const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/time_summary`;
mock.onGet(expectedUrl).reply(200, response); mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsTimeSummaryData(groupId, params) Api.cycleAnalyticsTimeSummaryData(groupId, params)
.then(responseObj => .then(responseObj =>
...@@ -343,7 +343,7 @@ describe('Api', () => { ...@@ -343,7 +343,7 @@ describe('Api', () => {
'cycle_analytics[created_before]': createdBefore, 'cycle_analytics[created_before]': createdBefore,
}; };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages`; const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages`;
mock.onGet(expectedUrl).reply(200, response); mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsGroupStagesAndEvents(groupId, params) Api.cycleAnalyticsGroupStagesAndEvents(groupId, params)
.then(responseObj => .then(responseObj =>
...@@ -365,7 +365,7 @@ describe('Api', () => { ...@@ -365,7 +365,7 @@ describe('Api', () => {
...defaultParams, ...defaultParams,
}; };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}/records`; const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}/records`;
mock.onGet(expectedUrl).reply(200, response); mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsStageEvents(groupId, stageId, params) Api.cycleAnalyticsStageEvents(groupId, stageId, params)
.then(responseObj => .then(responseObj =>
...@@ -387,7 +387,7 @@ describe('Api', () => { ...@@ -387,7 +387,7 @@ describe('Api', () => {
...defaultParams, ...defaultParams,
}; };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}/median`; const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}/median`;
mock.onGet(expectedUrl).reply(200, response); mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsStageMedian(groupId, stageId, params) Api.cycleAnalyticsStageMedian(groupId, stageId, params)
.then(responseObj => .then(responseObj =>
...@@ -413,7 +413,7 @@ describe('Api', () => { ...@@ -413,7 +413,7 @@ describe('Api', () => {
end_event_label_id: null, end_event_label_id: null,
}; };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages`; const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages`;
mock.onPost(expectedUrl).reply(200, response); mock.onPost(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsCreateStage(groupId, customStage) Api.cycleAnalyticsCreateStage(groupId, customStage)
.then(({ data, config: { data: reqData, url } }) => { .then(({ data, config: { data: reqData, url } }) => {
...@@ -434,7 +434,7 @@ describe('Api', () => { ...@@ -434,7 +434,7 @@ describe('Api', () => {
hidden: true, hidden: true,
}; };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}`; const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}`;
mock.onPut(expectedUrl).reply(200, response); mock.onPut(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsUpdateStage(stageId, groupId, stageData) Api.cycleAnalyticsUpdateStage(stageId, groupId, stageData)
.then(({ data, config: { data: reqData, url } }) => { .then(({ data, config: { data: reqData, url } }) => {
...@@ -451,7 +451,7 @@ describe('Api', () => { ...@@ -451,7 +451,7 @@ describe('Api', () => {
it('deletes the specified data', done => { it('deletes the specified data', done => {
const response = { id: stageId, hidden: true, custom: true }; const response = { id: stageId, hidden: true, custom: true };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}`; const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}`;
mock.onDelete(expectedUrl).reply(200, response); mock.onDelete(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsRemoveStage(stageId, groupId) Api.cycleAnalyticsRemoveStage(stageId, groupId)
.then(({ data, config: { url } }) => { .then(({ data, config: { url } }) => {
...@@ -471,7 +471,7 @@ describe('Api', () => { ...@@ -471,7 +471,7 @@ describe('Api', () => {
...defaultParams, ...defaultParams,
}; };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/thursday/duration_chart`; const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/thursday/duration_chart`;
mock.onGet(expectedUrl).reply(200, response); mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsDurationChart(groupId, stageId, params) Api.cycleAnalyticsDurationChart(groupId, stageId, params)
.then(responseObj => .then(responseObj =>
...@@ -514,7 +514,7 @@ describe('Api', () => { ...@@ -514,7 +514,7 @@ describe('Api', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'get'); jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).reply(200, response); mock.onGet(expectedUrl).reply(httpStatus.OK, response);
return Api.groupActivityMergeRequestsCount(groupId).then(({ data }) => { return Api.groupActivityMergeRequestsCount(groupId).then(({ data }) => {
expect(data).toEqual(response); expect(data).toEqual(response);
...@@ -530,7 +530,7 @@ describe('Api', () => { ...@@ -530,7 +530,7 @@ describe('Api', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'get'); jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, response); mock.onGet(expectedUrl).replyOnce(httpStatus.OK, response);
return Api.groupActivityIssuesCount(groupId).then(({ data }) => { return Api.groupActivityIssuesCount(groupId).then(({ data }) => {
expect(data).toEqual(response); expect(data).toEqual(response);
...@@ -546,7 +546,7 @@ describe('Api', () => { ...@@ -546,7 +546,7 @@ describe('Api', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'get'); jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).reply(200, response); mock.onGet(expectedUrl).reply(httpStatus.OK, response);
return Api.groupActivityNewMembersCount(groupId).then(({ data }) => { return Api.groupActivityNewMembersCount(groupId).then(({ data }) => {
expect(data).toEqual(response); expect(data).toEqual(response);
...@@ -574,7 +574,7 @@ describe('Api', () => { ...@@ -574,7 +574,7 @@ describe('Api', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'get'); jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, apiResponse); mock.onGet(expectedUrl).replyOnce(httpStatus.OK, apiResponse);
return Api.getGeoReplicableItems(mockReplicableType, mockParams).then(({ data }) => { return Api.getGeoReplicableItems(mockReplicableType, mockParams).then(({ data }) => {
expect(data).toEqual(apiResponse); expect(data).toEqual(apiResponse);
...@@ -592,7 +592,7 @@ describe('Api', () => { ...@@ -592,7 +592,7 @@ describe('Api', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'post'); jest.spyOn(axios, 'post');
mock.onPost(`${expectedUrl}/${mockAction}`).replyOnce(201, apiResponse); mock.onPost(`${expectedUrl}/${mockAction}`).replyOnce(httpStatus.CREATED, apiResponse);
return Api.initiateAllGeoReplicableSyncs(mockReplicableType, mockAction).then( return Api.initiateAllGeoReplicableSyncs(mockReplicableType, mockAction).then(
({ data }) => { ({ data }) => {
...@@ -613,7 +613,9 @@ describe('Api', () => { ...@@ -613,7 +613,9 @@ describe('Api', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'put'); jest.spyOn(axios, 'put');
mock.onPut(`${expectedUrl}/${mockProjectId}/${mockAction}`).replyOnce(201, apiResponse); mock
.onPut(`${expectedUrl}/${mockProjectId}/${mockAction}`)
.replyOnce(httpStatus.CREATED, apiResponse);
return Api.initiateGeoReplicableSync(mockReplicableType, { return Api.initiateGeoReplicableSync(mockReplicableType, {
projectId: mockProjectId, projectId: mockProjectId,
...@@ -639,7 +641,7 @@ describe('Api', () => { ...@@ -639,7 +641,7 @@ describe('Api', () => {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/vulnerabilities/${id}/${action}`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/vulnerabilities/${id}/${action}`;
const expectedResponse = { id, action, test: 'test' }; const expectedResponse = { id, action, test: 'test' };
mock.onPost(expectedUrl).replyOnce(200, expectedResponse); mock.onPost(expectedUrl).replyOnce(httpStatus.OK, expectedResponse);
return Api.changeVulnerabilityState(id, action).then(({ data }) => { return Api.changeVulnerabilityState(id, action).then(({ data }) => {
expect(mock.history.post).toContainEqual(expect.objectContaining({ url: expectedUrl })); expect(mock.history.post).toContainEqual(expect.objectContaining({ url: expectedUrl }));
...@@ -666,7 +668,7 @@ describe('Api', () => { ...@@ -666,7 +668,7 @@ describe('Api', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'post'); jest.spyOn(axios, 'post');
mock.onPost(expectedUrl).replyOnce(201, mockNode); mock.onPost(expectedUrl).replyOnce(httpStatus.CREATED, mockNode);
return Api.createGeoNode(mockNode).then(({ data }) => { return Api.createGeoNode(mockNode).then(({ data }) => {
expect(data).toEqual(mockNode); expect(data).toEqual(mockNode);
...@@ -686,7 +688,7 @@ describe('Api', () => { ...@@ -686,7 +688,7 @@ describe('Api', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'put'); jest.spyOn(axios, 'put');
mock.onPut(`${expectedUrl}/${mockNode.id}`).replyOnce(201, mockNode); mock.onPut(`${expectedUrl}/${mockNode.id}`).replyOnce(httpStatus.CREATED, mockNode);
return Api.updateGeoNode(mockNode).then(({ data }) => { return Api.updateGeoNode(mockNode).then(({ data }) => {
expect(data).toEqual(mockNode); expect(data).toEqual(mockNode);
...@@ -715,7 +717,7 @@ describe('Api', () => { ...@@ -715,7 +717,7 @@ describe('Api', () => {
describe('fetchFeatureFlagUserLists', () => { describe('fetchFeatureFlagUserLists', () => {
it('GETs the right url', () => { it('GETs the right url', () => {
mock.onGet(expectedUrl).replyOnce(200, []); mock.onGet(expectedUrl).replyOnce(httpStatus.OK, []);
return Api.fetchFeatureFlagUserLists(projectId).then(({ data }) => { return Api.fetchFeatureFlagUserLists(projectId).then(({ data }) => {
expect(data).toEqual([]); expect(data).toEqual([]);
...@@ -729,7 +731,7 @@ describe('Api', () => { ...@@ -729,7 +731,7 @@ describe('Api', () => {
name: 'mock_user_list', name: 'mock_user_list',
user_xids: '1,2,3,4', user_xids: '1,2,3,4',
}; };
mock.onPost(expectedUrl, mockUserListData).replyOnce(200, mockUserList); mock.onPost(expectedUrl, mockUserListData).replyOnce(httpStatus.OK, mockUserList);
return Api.createFeatureFlagUserList(projectId, mockUserListData).then(({ data }) => { return Api.createFeatureFlagUserList(projectId, mockUserListData).then(({ data }) => {
expect(data).toEqual(mockUserList); expect(data).toEqual(mockUserList);
...@@ -739,7 +741,7 @@ describe('Api', () => { ...@@ -739,7 +741,7 @@ describe('Api', () => {
describe('fetchFeatureFlagUserList', () => { describe('fetchFeatureFlagUserList', () => {
it('GETs the right url', () => { it('GETs the right url', () => {
mock.onGet(`${expectedUrl}/1`).replyOnce(200, mockUserList); mock.onGet(`${expectedUrl}/1`).replyOnce(httpStatus.OK, mockUserList);
return Api.fetchFeatureFlagUserList(projectId, 1).then(({ data }) => { return Api.fetchFeatureFlagUserList(projectId, 1).then(({ data }) => {
expect(data).toEqual(mockUserList); expect(data).toEqual(mockUserList);
...@@ -749,7 +751,9 @@ describe('Api', () => { ...@@ -749,7 +751,9 @@ describe('Api', () => {
describe('updateFeatureFlagUserList', () => { describe('updateFeatureFlagUserList', () => {
it('PUTs the right url', () => { it('PUTs the right url', () => {
mock.onPut(`${expectedUrl}/1`).replyOnce(200, { ...mockUserList, user_xids: '5' }); mock
.onPut(`${expectedUrl}/1`)
.replyOnce(httpStatus.OK, { ...mockUserList, user_xids: '5' });
return Api.updateFeatureFlagUserList(projectId, { return Api.updateFeatureFlagUserList(projectId, {
...mockUserList, ...mockUserList,
...@@ -762,7 +766,7 @@ describe('Api', () => { ...@@ -762,7 +766,7 @@ describe('Api', () => {
describe('deleteFeatureFlagUserList', () => { describe('deleteFeatureFlagUserList', () => {
it('DELETEs the right url', () => { it('DELETEs the right url', () => {
mock.onDelete(`${expectedUrl}/1`).replyOnce(200, 'deleted'); mock.onDelete(`${expectedUrl}/1`).replyOnce(httpStatus.OK, 'deleted');
return Api.deleteFeatureFlagUserList(projectId, 1).then(({ data }) => { return Api.deleteFeatureFlagUserList(projectId, 1).then(({ data }) => {
expect(data).toBe('deleted'); expect(data).toBe('deleted');
...@@ -779,7 +783,7 @@ describe('Api', () => { ...@@ -779,7 +783,7 @@ describe('Api', () => {
it('fetches applications settings', () => { it('fetches applications settings', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'get'); jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, apiResponse); mock.onGet(expectedUrl).replyOnce(httpStatus.OK, apiResponse);
return Api.getApplicationSettings().then(({ data }) => { return Api.getApplicationSettings().then(({ data }) => {
expect(data).toEqual(apiResponse); expect(data).toEqual(apiResponse);
...@@ -794,7 +798,7 @@ describe('Api', () => { ...@@ -794,7 +798,7 @@ describe('Api', () => {
it('updates applications settings', () => { it('updates applications settings', () => {
jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'put'); jest.spyOn(axios, 'put');
mock.onPut(expectedUrl).replyOnce(201, apiResponse); mock.onPut(expectedUrl).replyOnce(httpStatus.CREATED, apiResponse);
return Api.updateApplicationSettings(mockReq).then(({ data }) => { return Api.updateApplicationSettings(mockReq).then(({ data }) => {
expect(data).toEqual(apiResponse); expect(data).toEqual(apiResponse);
......
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import Api from '~/api'; import Api from '~/api';
import httpStatus from '~/lib/utils/http_status';
describe('Api', () => { describe('Api', () => {
const dummyApiVersion = 'v3000'; const dummyApiVersion = 'v3000';
...@@ -57,7 +58,7 @@ describe('Api', () => { ...@@ -57,7 +58,7 @@ describe('Api', () => {
it('fetch all group packages', () => { it('fetch all group packages', () => {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/packages`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/packages`;
jest.spyOn(axios, 'get'); jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, apiResponse); mock.onGet(expectedUrl).replyOnce(httpStatus.OK, apiResponse);
return Api.groupPackages(groupId).then(({ data }) => { return Api.groupPackages(groupId).then(({ data }) => {
expect(data).toEqual(apiResponse); expect(data).toEqual(apiResponse);
...@@ -70,7 +71,7 @@ describe('Api', () => { ...@@ -70,7 +71,7 @@ describe('Api', () => {
it('fetch all project packages', () => { it('fetch all project packages', () => {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/packages`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/packages`;
jest.spyOn(axios, 'get'); jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, apiResponse); mock.onGet(expectedUrl).replyOnce(httpStatus.OK, apiResponse);
return Api.projectPackages(projectId).then(({ data }) => { return Api.projectPackages(projectId).then(({ data }) => {
expect(data).toEqual(apiResponse); expect(data).toEqual(apiResponse);
...@@ -92,7 +93,7 @@ describe('Api', () => { ...@@ -92,7 +93,7 @@ describe('Api', () => {
const expectedUrl = `foo`; const expectedUrl = `foo`;
jest.spyOn(Api, 'buildProjectPackageUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildProjectPackageUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'get'); jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, apiResponse); mock.onGet(expectedUrl).replyOnce(httpStatus.OK, apiResponse);
return Api.projectPackage(projectId, packageId).then(({ data }) => { return Api.projectPackage(projectId, packageId).then(({ data }) => {
expect(data).toEqual(apiResponse); expect(data).toEqual(apiResponse);
...@@ -107,7 +108,7 @@ describe('Api', () => { ...@@ -107,7 +108,7 @@ describe('Api', () => {
jest.spyOn(Api, 'buildProjectPackageUrl').mockReturnValue(expectedUrl); jest.spyOn(Api, 'buildProjectPackageUrl').mockReturnValue(expectedUrl);
jest.spyOn(axios, 'delete'); jest.spyOn(axios, 'delete');
mock.onDelete(expectedUrl).replyOnce(200, true); mock.onDelete(expectedUrl).replyOnce(httpStatus.OK, true);
return Api.deleteProjectPackage(projectId, packageId).then(({ data }) => { return Api.deleteProjectPackage(projectId, packageId).then(({ data }) => {
expect(data).toEqual(true); expect(data).toEqual(true);
...@@ -121,7 +122,7 @@ describe('Api', () => { ...@@ -121,7 +122,7 @@ describe('Api', () => {
it('fetches a group', done => { it('fetches a group', done => {
const groupId = '123456'; const groupId = '123456';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}`;
mock.onGet(expectedUrl).reply(200, { mock.onGet(expectedUrl).reply(httpStatus.OK, {
name: 'test', name: 'test',
}); });
...@@ -137,7 +138,7 @@ describe('Api', () => { ...@@ -137,7 +138,7 @@ describe('Api', () => {
const groupId = '54321'; const groupId = '54321';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/members`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/members`;
const expectedData = [{ id: 7 }]; const expectedData = [{ id: 7 }];
mock.onGet(expectedUrl).reply(200, expectedData); mock.onGet(expectedUrl).reply(httpStatus.OK, expectedData);
Api.groupMembers(groupId) Api.groupMembers(groupId)
.then(({ data }) => { .then(({ data }) => {
...@@ -153,7 +154,7 @@ describe('Api', () => { ...@@ -153,7 +154,7 @@ describe('Api', () => {
const query = 'dummy query'; const query = 'dummy query';
const options = { unused: 'option' }; const options = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups.json`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups.json`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -171,7 +172,7 @@ describe('Api', () => { ...@@ -171,7 +172,7 @@ describe('Api', () => {
it('fetches namespaces', done => { it('fetches namespaces', done => {
const query = 'dummy query'; const query = 'dummy query';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/namespaces.json`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/namespaces.json`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -191,7 +192,7 @@ describe('Api', () => { ...@@ -191,7 +192,7 @@ describe('Api', () => {
const options = { unused: 'option' }; const options = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`;
window.gon.current_user_id = 1; window.gon.current_user_id = 1;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -208,7 +209,7 @@ describe('Api', () => { ...@@ -208,7 +209,7 @@ describe('Api', () => {
const query = 'dummy query'; const query = 'dummy query';
const options = { unused: 'option' }; const options = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects.json`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -226,7 +227,7 @@ describe('Api', () => { ...@@ -226,7 +227,7 @@ describe('Api', () => {
it('update a project with the given payload', done => { it('update a project with the given payload', done => {
const projectPath = 'foo'; const projectPath = 'foo';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}`;
mock.onPut(expectedUrl).reply(200, { foo: 'bar' }); mock.onPut(expectedUrl).reply(httpStatus.OK, { foo: 'bar' });
Api.updateProject(projectPath, { foo: 'bar' }) Api.updateProject(projectPath, { foo: 'bar' })
.then(({ data }) => { .then(({ data }) => {
...@@ -243,7 +244,7 @@ describe('Api', () => { ...@@ -243,7 +244,7 @@ describe('Api', () => {
const options = { unused: 'option' }; const options = { unused: 'option' };
const projectPath = 'gitlab-org%2Fgitlab-ce'; const projectPath = 'gitlab-org%2Fgitlab-ce';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/users`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/users`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -265,7 +266,7 @@ describe('Api', () => { ...@@ -265,7 +266,7 @@ describe('Api', () => {
it('fetches all merge requests for a project', done => { it('fetches all merge requests for a project', done => {
const mockData = [{ source_branch: 'foo' }, { source_branch: 'bar' }]; const mockData = [{ source_branch: 'foo' }, { source_branch: 'bar' }];
mock.onGet(expectedUrl).reply(200, mockData); mock.onGet(expectedUrl).reply(httpStatus.OK, mockData);
Api.projectMergeRequests(projectPath) Api.projectMergeRequests(projectPath)
.then(({ data }) => { .then(({ data }) => {
expect(data.length).toEqual(2); expect(data.length).toEqual(2);
...@@ -281,7 +282,7 @@ describe('Api', () => { ...@@ -281,7 +282,7 @@ describe('Api', () => {
source_branch: 'bar', source_branch: 'bar',
}; };
const mockData = [{ source_branch: 'bar' }]; const mockData = [{ source_branch: 'bar' }];
mock.onGet(expectedUrl, { params }).reply(200, mockData); mock.onGet(expectedUrl, { params }).reply(httpStatus.OK, mockData);
Api.projectMergeRequests(projectPath, params) Api.projectMergeRequests(projectPath, params)
.then(({ data }) => { .then(({ data }) => {
...@@ -298,7 +299,7 @@ describe('Api', () => { ...@@ -298,7 +299,7 @@ describe('Api', () => {
const projectPath = 'abc'; const projectPath = 'abc';
const mergeRequestId = '123456'; const mergeRequestId = '123456';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}`;
mock.onGet(expectedUrl).reply(200, { mock.onGet(expectedUrl).reply(httpStatus.OK, {
title: 'test', title: 'test',
}); });
...@@ -316,7 +317,7 @@ describe('Api', () => { ...@@ -316,7 +317,7 @@ describe('Api', () => {
const projectPath = 'abc'; const projectPath = 'abc';
const mergeRequestId = '123456'; const mergeRequestId = '123456';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/changes`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/changes`;
mock.onGet(expectedUrl).reply(200, { mock.onGet(expectedUrl).reply(httpStatus.OK, {
title: 'test', title: 'test',
}); });
...@@ -334,7 +335,7 @@ describe('Api', () => { ...@@ -334,7 +335,7 @@ describe('Api', () => {
const projectPath = 'abc'; const projectPath = 'abc';
const mergeRequestId = '123456'; const mergeRequestId = '123456';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/versions`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/merge_requests/${mergeRequestId}/versions`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
id: 123, id: 123,
}, },
...@@ -356,7 +357,7 @@ describe('Api', () => { ...@@ -356,7 +357,7 @@ describe('Api', () => {
const params = { scope: 'active' }; const params = { scope: 'active' };
const mockData = [{ id: 4 }]; const mockData = [{ id: 4 }];
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/runners`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectPath}/runners`;
mock.onGet(expectedUrl, { params }).reply(200, mockData); mock.onGet(expectedUrl, { params }).reply(httpStatus.OK, mockData);
Api.projectRunners(projectPath, { params }) Api.projectRunners(projectPath, { params })
.then(({ data }) => { .then(({ data }) => {
...@@ -380,7 +381,7 @@ describe('Api', () => { ...@@ -380,7 +381,7 @@ describe('Api', () => {
expect(config.data).toBe(JSON.stringify(expectedData)); expect(config.data).toBe(JSON.stringify(expectedData));
return [ return [
200, httpStatus.OK,
{ {
name: 'test', name: 'test',
}, },
...@@ -404,7 +405,7 @@ describe('Api', () => { ...@@ -404,7 +405,7 @@ describe('Api', () => {
expect(config.data).toBe(JSON.stringify(expectedData)); expect(config.data).toBe(JSON.stringify(expectedData));
return [ return [
200, httpStatus.OK,
{ {
name: 'test', name: 'test',
}, },
...@@ -423,7 +424,7 @@ describe('Api', () => { ...@@ -423,7 +424,7 @@ describe('Api', () => {
const groupId = '123456'; const groupId = '123456';
const query = 'dummy query'; const query = 'dummy query';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/projects.json`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/groups/${groupId}/projects.json`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -445,7 +446,7 @@ describe('Api', () => { ...@@ -445,7 +446,7 @@ describe('Api', () => {
)}/repository/commits/${sha}`; )}/repository/commits/${sha}`;
it('fetches a single commit', () => { it('fetches a single commit', () => {
mock.onGet(expectedUrl).reply(200, { id: sha }); mock.onGet(expectedUrl).reply(httpStatus.OK, { id: sha });
return Api.commit(projectId, sha).then(({ data: commit }) => { return Api.commit(projectId, sha).then(({ data: commit }) => {
expect(commit.id).toBe(sha); expect(commit.id).toBe(sha);
...@@ -453,7 +454,7 @@ describe('Api', () => { ...@@ -453,7 +454,7 @@ describe('Api', () => {
}); });
it('fetches a single commit without stats', () => { it('fetches a single commit without stats', () => {
mock.onGet(expectedUrl, { params: { stats: false } }).reply(200, { id: sha }); mock.onGet(expectedUrl, { params: { stats: false } }).reply(httpStatus.OK, { id: sha });
return Api.commit(projectId, sha, { stats: false }).then(({ data: commit }) => { return Api.commit(projectId, sha, { stats: false }).then(({ data: commit }) => {
expect(commit.id).toBe(sha); expect(commit.id).toBe(sha);
...@@ -470,7 +471,7 @@ describe('Api', () => { ...@@ -470,7 +471,7 @@ describe('Api', () => {
const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}/${encodeURIComponent( const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}/${encodeURIComponent(
templateKey, templateKey,
)}`; )}`;
mock.onGet(expectedUrl).reply(200, 'test'); mock.onGet(expectedUrl).reply(httpStatus.OK, 'test');
Api.issueTemplate(namespace, project, templateKey, templateType, (error, response) => { Api.issueTemplate(namespace, project, templateKey, templateType, (error, response) => {
expect(response).toBe('test'); expect(response).toBe('test');
...@@ -483,7 +484,7 @@ describe('Api', () => { ...@@ -483,7 +484,7 @@ describe('Api', () => {
it('fetches a list of templates', done => { it('fetches a list of templates', done => {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/gitlab-org%2Fgitlab-ce/templates/licenses`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/gitlab-org%2Fgitlab-ce/templates/licenses`;
mock.onGet(expectedUrl).reply(200, 'test'); mock.onGet(expectedUrl).reply(httpStatus.OK, 'test');
Api.projectTemplates('gitlab-org/gitlab-ce', 'licenses', {}, response => { Api.projectTemplates('gitlab-org/gitlab-ce', 'licenses', {}, response => {
expect(response).toBe('test'); expect(response).toBe('test');
...@@ -497,7 +498,7 @@ describe('Api', () => { ...@@ -497,7 +498,7 @@ describe('Api', () => {
const data = { unused: 'option' }; const data = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/gitlab-org%2Fgitlab-ce/templates/licenses/test%20license`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/gitlab-org%2Fgitlab-ce/templates/licenses/test%20license`;
mock.onGet(expectedUrl).reply(200, 'test'); mock.onGet(expectedUrl).reply(httpStatus.OK, 'test');
Api.projectTemplate('gitlab-org/gitlab-ce', 'licenses', 'test license', data, response => { Api.projectTemplate('gitlab-org/gitlab-ce', 'licenses', 'test license', data, response => {
expect(response).toBe('test'); expect(response).toBe('test');
...@@ -511,7 +512,7 @@ describe('Api', () => { ...@@ -511,7 +512,7 @@ describe('Api', () => {
const query = 'dummy query'; const query = 'dummy query';
const options = { unused: 'option' }; const options = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users.json`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users.json`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -531,7 +532,7 @@ describe('Api', () => { ...@@ -531,7 +532,7 @@ describe('Api', () => {
it('fetches single user', done => { it('fetches single user', done => {
const userId = '123456'; const userId = '123456';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}`;
mock.onGet(expectedUrl).reply(200, { mock.onGet(expectedUrl).reply(httpStatus.OK, {
name: 'testuser', name: 'testuser',
}); });
...@@ -547,7 +548,7 @@ describe('Api', () => { ...@@ -547,7 +548,7 @@ describe('Api', () => {
describe('user counts', () => { describe('user counts', () => {
it('fetches single user counts', done => { it('fetches single user counts', done => {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/user_counts`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/user_counts`;
mock.onGet(expectedUrl).reply(200, { mock.onGet(expectedUrl).reply(httpStatus.OK, {
merge_requests: 4, merge_requests: 4,
}); });
...@@ -564,7 +565,7 @@ describe('Api', () => { ...@@ -564,7 +565,7 @@ describe('Api', () => {
it('fetches single user status', done => { it('fetches single user status', done => {
const userId = '123456'; const userId = '123456';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}/status`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}/status`;
mock.onGet(expectedUrl).reply(200, { mock.onGet(expectedUrl).reply(httpStatus.OK, {
message: 'testmessage', message: 'testmessage',
}); });
...@@ -583,7 +584,7 @@ describe('Api', () => { ...@@ -583,7 +584,7 @@ describe('Api', () => {
const options = { unused: 'option' }; const options = { unused: 'option' };
const userId = '123456'; const userId = '123456';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}/projects`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users/${userId}/projects`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -602,7 +603,7 @@ describe('Api', () => { ...@@ -602,7 +603,7 @@ describe('Api', () => {
const projectId = 'example/foobar'; const projectId = 'example/foobar';
const commitSha = 'abc123def'; const commitSha = 'abc123def';
const expectedUrl = `${dummyUrlRoot}/${projectId}/commit/${commitSha}/pipelines`; const expectedUrl = `${dummyUrlRoot}/${projectId}/commit/${commitSha}/pipelines`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -629,7 +630,7 @@ describe('Api', () => { ...@@ -629,7 +630,7 @@ describe('Api', () => {
jest.spyOn(axios, 'post'); jest.spyOn(axios, 'post');
mock.onPost(expectedUrl).replyOnce(200, { mock.onPost(expectedUrl).replyOnce(httpStatus.OK, {
name: branch, name: branch,
}); });
...@@ -652,7 +653,7 @@ describe('Api', () => { ...@@ -652,7 +653,7 @@ describe('Api', () => {
jest.spyOn(axios, 'get'); jest.spyOn(axios, 'get');
mock.onGet(expectedUrl).replyOnce(200, ['fork']); mock.onGet(expectedUrl).replyOnce(httpStatus.OK, ['fork']);
Api.projectForks(dummyProjectPath, { visibility: 'private' }) Api.projectForks(dummyProjectPath, { visibility: 'private' })
.then(({ data }) => { .then(({ data }) => {
...@@ -666,153 +667,166 @@ describe('Api', () => { ...@@ -666,153 +667,166 @@ describe('Api', () => {
}); });
}); });
describe('release', () => { describe('release-related methods', () => {
const dummyProjectPath = 'gitlab-org/gitlab'; const dummyProjectPath = 'gitlab-org/gitlab';
const dummyTagName = 'v1.3'; const dummyTagName = 'v1.3';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent( const baseReleaseUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent(
dummyProjectPath, dummyProjectPath,
)}/releases/${encodeURIComponent(dummyTagName)}`; )}/releases`;
describe('when the release is successfully returned', () => { describe('releases', () => {
it('resolves the Promise', () => { const expectedUrl = baseReleaseUrl;
mock.onGet(expectedUrl).replyOnce(200);
return Api.release(dummyProjectPath, dummyTagName).then(() => { describe('when releases are successfully returned', () => {
expect(mock.history.get).toHaveLength(1); it('resolves the Promise', () => {
mock.onGet(expectedUrl).replyOnce(httpStatus.OK);
return Api.releases(dummyProjectPath).then(() => {
expect(mock.history.get).toHaveLength(1);
});
}); });
}); });
});
describe('when an error occurs while fetching the release', () => { describe('when an error occurs while fetching releases', () => {
it('rejects the Promise', () => { it('rejects the Promise', () => {
mock.onGet(expectedUrl).replyOnce(500); mock.onGet(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
return Api.release(dummyProjectPath, dummyTagName).catch(() => { return Api.releases(dummyProjectPath).catch(() => {
expect(mock.history.get).toHaveLength(1); expect(mock.history.get).toHaveLength(1);
});
}); });
}); });
}); });
});
describe('createRelease', () => { describe('release', () => {
const dummyProjectPath = 'gitlab-org/gitlab'; const expectedUrl = `${baseReleaseUrl}/${encodeURIComponent(dummyTagName)}`;
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent(
dummyProjectPath,
)}/releases`;
const release = { describe('when the release is successfully returned', () => {
name: 'Version 1.0', it('resolves the Promise', () => {
}; mock.onGet(expectedUrl).replyOnce(httpStatus.OK);
describe('when the release is successfully created', () => { return Api.release(dummyProjectPath, dummyTagName).then(() => {
it('resolves the Promise', () => { expect(mock.history.get).toHaveLength(1);
mock.onPost(expectedUrl, release).replyOnce(201); });
});
});
return Api.createRelease(dummyProjectPath, release).then(() => { describe('when an error occurs while fetching the release', () => {
expect(mock.history.post).toHaveLength(1); it('rejects the Promise', () => {
mock.onGet(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
return Api.release(dummyProjectPath, dummyTagName).catch(() => {
expect(mock.history.get).toHaveLength(1);
});
}); });
}); });
}); });
describe('when an error occurs while creating the release', () => { describe('createRelease', () => {
it('rejects the Promise', () => { const expectedUrl = baseReleaseUrl;
mock.onPost(expectedUrl, release).replyOnce(500);
return Api.createRelease(dummyProjectPath, release).catch(() => { const release = {
expect(mock.history.post).toHaveLength(1); name: 'Version 1.0',
};
describe('when the release is successfully created', () => {
it('resolves the Promise', () => {
mock.onPost(expectedUrl, release).replyOnce(httpStatus.CREATED);
return Api.createRelease(dummyProjectPath, release).then(() => {
expect(mock.history.post).toHaveLength(1);
});
});
});
describe('when an error occurs while creating the release', () => {
it('rejects the Promise', () => {
mock.onPost(expectedUrl, release).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
return Api.createRelease(dummyProjectPath, release).catch(() => {
expect(mock.history.post).toHaveLength(1);
});
}); });
}); });
}); });
});
describe('updateRelease', () => { describe('updateRelease', () => {
const dummyProjectPath = 'gitlab-org/gitlab'; const expectedUrl = `${baseReleaseUrl}/${encodeURIComponent(dummyTagName)}`;
const dummyTagName = 'v1.3';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent(
dummyProjectPath,
)}/releases/${encodeURIComponent(dummyTagName)}`;
const release = { const release = {
name: 'Version 1.0', name: 'Version 1.0',
}; };
describe('when the release is successfully created', () => { describe('when the release is successfully updated', () => {
it('resolves the Promise', () => { it('resolves the Promise', () => {
mock.onPut(expectedUrl, release).replyOnce(200); mock.onPut(expectedUrl, release).replyOnce(httpStatus.OK);
return Api.updateRelease(dummyProjectPath, dummyTagName, release).then(() => { return Api.updateRelease(dummyProjectPath, dummyTagName, release).then(() => {
expect(mock.history.put).toHaveLength(1); expect(mock.history.put).toHaveLength(1);
});
}); });
}); });
});
describe('when an error occurs while creating the release', () => { describe('when an error occurs while updating the release', () => {
it('rejects the Promise', () => { it('rejects the Promise', () => {
mock.onPut(expectedUrl, release).replyOnce(500); mock.onPut(expectedUrl, release).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
return Api.updateRelease(dummyProjectPath, dummyTagName, release).catch(() => { return Api.updateRelease(dummyProjectPath, dummyTagName, release).catch(() => {
expect(mock.history.put).toHaveLength(1); expect(mock.history.put).toHaveLength(1);
});
}); });
}); });
}); });
});
describe('createReleaseLink', () => { describe('createReleaseLink', () => {
const dummyProjectPath = 'gitlab-org/gitlab'; const expectedUrl = `${baseReleaseUrl}/${dummyTagName}/assets/links`;
const dummyReleaseTag = 'v1.3'; const expectedLink = {
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent( url: 'https://example.com',
dummyProjectPath, name: 'An example link',
)}/releases/${dummyReleaseTag}/assets/links`; };
const expectedLink = {
url: 'https://example.com',
name: 'An example link',
};
describe('when the Release is successfully created', () => { describe('when the Release is successfully created', () => {
it('resolves the Promise', () => { it('resolves the Promise', () => {
mock.onPost(expectedUrl, expectedLink).replyOnce(201); mock.onPost(expectedUrl, expectedLink).replyOnce(httpStatus.CREATED);
return Api.createReleaseLink(dummyProjectPath, dummyReleaseTag, expectedLink).then(() => { return Api.createReleaseLink(dummyProjectPath, dummyTagName, expectedLink).then(() => {
expect(mock.history.post).toHaveLength(1); expect(mock.history.post).toHaveLength(1);
});
}); });
}); });
});
describe('when an error occurs while creating the Release', () => { describe('when an error occurs while creating the Release', () => {
it('rejects the Promise', () => { it('rejects the Promise', () => {
mock.onPost(expectedUrl, expectedLink).replyOnce(500); mock.onPost(expectedUrl, expectedLink).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
return Api.createReleaseLink(dummyProjectPath, dummyReleaseTag, expectedLink).catch(() => { return Api.createReleaseLink(dummyProjectPath, dummyTagName, expectedLink).catch(() => {
expect(mock.history.post).toHaveLength(1); expect(mock.history.post).toHaveLength(1);
});
}); });
}); });
}); });
});
describe('deleteReleaseLink', () => { describe('deleteReleaseLink', () => {
const dummyProjectPath = 'gitlab-org/gitlab'; const dummyLinkId = '4';
const dummyReleaseTag = 'v1.3'; const expectedUrl = `${baseReleaseUrl}/${dummyTagName}/assets/links/${dummyLinkId}`;
const dummyLinkId = '4';
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${encodeURIComponent(
dummyProjectPath,
)}/releases/${dummyReleaseTag}/assets/links/${dummyLinkId}`;
describe('when the Release is successfully deleted', () => { describe('when the Release is successfully deleted', () => {
it('resolves the Promise', () => { it('resolves the Promise', () => {
mock.onDelete(expectedUrl).replyOnce(200); mock.onDelete(expectedUrl).replyOnce(httpStatus.OK);
return Api.deleteReleaseLink(dummyProjectPath, dummyReleaseTag, dummyLinkId).then(() => { return Api.deleteReleaseLink(dummyProjectPath, dummyTagName, dummyLinkId).then(() => {
expect(mock.history.delete).toHaveLength(1); expect(mock.history.delete).toHaveLength(1);
});
}); });
}); });
});
describe('when an error occurs while deleting the Release', () => { describe('when an error occurs while deleting the Release', () => {
it('rejects the Promise', () => { it('rejects the Promise', () => {
mock.onDelete(expectedUrl).replyOnce(500); mock.onDelete(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
return Api.deleteReleaseLink(dummyProjectPath, dummyReleaseTag, dummyLinkId).catch(() => { return Api.deleteReleaseLink(dummyProjectPath, dummyTagName, dummyLinkId).catch(() => {
expect(mock.history.delete).toHaveLength(1); expect(mock.history.delete).toHaveLength(1);
});
}); });
}); });
}); });
...@@ -827,7 +841,7 @@ describe('Api', () => { ...@@ -827,7 +841,7 @@ describe('Api', () => {
describe('when the raw file is successfully fetched', () => { describe('when the raw file is successfully fetched', () => {
it('resolves the Promise', () => { it('resolves the Promise', () => {
mock.onGet(expectedUrl).replyOnce(200); mock.onGet(expectedUrl).replyOnce(httpStatus.OK);
return Api.getRawFile(dummyProjectPath, dummyFilePath).then(() => { return Api.getRawFile(dummyProjectPath, dummyFilePath).then(() => {
expect(mock.history.get).toHaveLength(1); expect(mock.history.get).toHaveLength(1);
...@@ -837,7 +851,7 @@ describe('Api', () => { ...@@ -837,7 +851,7 @@ describe('Api', () => {
describe('when an error occurs while getting a raw file', () => { describe('when an error occurs while getting a raw file', () => {
it('rejects the Promise', () => { it('rejects the Promise', () => {
mock.onPost(expectedUrl).replyOnce(500); mock.onPost(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
return Api.getRawFile(dummyProjectPath, dummyFilePath).catch(() => { return Api.getRawFile(dummyProjectPath, dummyFilePath).catch(() => {
expect(mock.history.get).toHaveLength(1); expect(mock.history.get).toHaveLength(1);
...@@ -859,7 +873,7 @@ describe('Api', () => { ...@@ -859,7 +873,7 @@ describe('Api', () => {
describe('when the merge request is successfully created', () => { describe('when the merge request is successfully created', () => {
it('resolves the Promise', () => { it('resolves the Promise', () => {
mock.onPost(expectedUrl, options).replyOnce(201); mock.onPost(expectedUrl, options).replyOnce(httpStatus.CREATED);
return Api.createProjectMergeRequest(dummyProjectPath, options).then(() => { return Api.createProjectMergeRequest(dummyProjectPath, options).then(() => {
expect(mock.history.post).toHaveLength(1); expect(mock.history.post).toHaveLength(1);
...@@ -869,7 +883,7 @@ describe('Api', () => { ...@@ -869,7 +883,7 @@ describe('Api', () => {
describe('when an error occurs while getting a raw file', () => { describe('when an error occurs while getting a raw file', () => {
it('rejects the Promise', () => { it('rejects the Promise', () => {
mock.onPost(expectedUrl).replyOnce(500); mock.onPost(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
return Api.createProjectMergeRequest(dummyProjectPath).catch(() => { return Api.createProjectMergeRequest(dummyProjectPath).catch(() => {
expect(mock.history.post).toHaveLength(1); expect(mock.history.post).toHaveLength(1);
...@@ -884,7 +898,7 @@ describe('Api', () => { ...@@ -884,7 +898,7 @@ describe('Api', () => {
const issue = 1; const issue = 1;
const expectedArray = [1, 2, 3]; const expectedArray = [1, 2, 3];
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/issues/${issue}`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/issues/${issue}`;
mock.onPut(expectedUrl).reply(200, { assigneeIds: expectedArray }); mock.onPut(expectedUrl).reply(httpStatus.OK, { assigneeIds: expectedArray });
Api.updateIssue(projectId, issue, { assigneeIds: expectedArray }) Api.updateIssue(projectId, issue, { assigneeIds: expectedArray })
.then(({ data }) => { .then(({ data }) => {
...@@ -901,7 +915,7 @@ describe('Api', () => { ...@@ -901,7 +915,7 @@ describe('Api', () => {
const mergeRequest = 1; const mergeRequest = 1;
const expectedArray = [1, 2, 3]; const expectedArray = [1, 2, 3];
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/merge_requests/${mergeRequest}`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/merge_requests/${mergeRequest}`;
mock.onPut(expectedUrl).reply(200, { assigneeIds: expectedArray }); mock.onPut(expectedUrl).reply(httpStatus.OK, { assigneeIds: expectedArray });
Api.updateMergeRequest(projectId, mergeRequest, { assigneeIds: expectedArray }) Api.updateMergeRequest(projectId, mergeRequest, { assigneeIds: expectedArray })
.then(({ data }) => { .then(({ data }) => {
...@@ -918,7 +932,7 @@ describe('Api', () => { ...@@ -918,7 +932,7 @@ describe('Api', () => {
const options = { unused: 'option' }; const options = { unused: 'option' };
const projectId = 8; const projectId = 8;
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/repository/tags`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/repository/tags`;
mock.onGet(expectedUrl).reply(200, [ mock.onGet(expectedUrl).reply(httpStatus.OK, [
{ {
name: 'test', name: 'test',
}, },
...@@ -946,7 +960,7 @@ describe('Api', () => { ...@@ -946,7 +960,7 @@ describe('Api', () => {
updated_at: '2020-07-10T05:10:35.122Z', updated_at: '2020-07-10T05:10:35.122Z',
}; };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/freeze_periods`; const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${projectId}/freeze_periods`;
mock.onGet(expectedUrl).reply(200, [freezePeriod]); mock.onGet(expectedUrl).reply(httpStatus.OK, [freezePeriod]);
return Api.freezePeriods(projectId).then(({ data }) => { return Api.freezePeriods(projectId).then(({ data }) => {
expect(data[0]).toStrictEqual(freezePeriod); expect(data[0]).toStrictEqual(freezePeriod);
...@@ -974,7 +988,7 @@ describe('Api', () => { ...@@ -974,7 +988,7 @@ describe('Api', () => {
describe('when the freeze period is successfully created', () => { describe('when the freeze period is successfully created', () => {
it('resolves the Promise', () => { it('resolves the Promise', () => {
mock.onPost(expectedUrl, options).replyOnce(201, expectedResult); mock.onPost(expectedUrl, options).replyOnce(httpStatus.CREATED, expectedResult);
return Api.createFreezePeriod(projectId, options).then(({ data }) => { return Api.createFreezePeriod(projectId, options).then(({ data }) => {
expect(data).toStrictEqual(expectedResult); expect(data).toStrictEqual(expectedResult);
...@@ -998,7 +1012,7 @@ describe('Api', () => { ...@@ -998,7 +1012,7 @@ describe('Api', () => {
jest.spyOn(axios, 'post'); jest.spyOn(axios, 'post');
mock.onPost(expectedUrl).replyOnce(200, { mock.onPost(expectedUrl).replyOnce(httpStatus.OK, {
web_url: redirectUrl, web_url: redirectUrl,
}); });
......
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