Skip to content
This repository was archived by the owner on Jan 16, 2022. It is now read-only.

Commit 803da1c

Browse files
ayusharmajuanpicado
authored andcommitted
fix: adds unit tests for api service (#235)
* refactor: adds unit tests for api service * refactor: fix test dummy url
1 parent 5cb47ed commit 803da1c

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

jest/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ global.__APP_VERSION__ = '1.0.0';
1515
// @ts-ignore : Property '__VERDACCIO_BASENAME_UI_OPTIONS' does not exist on type 'Global'.
1616
global.__VERDACCIO_BASENAME_UI_OPTIONS = {};
1717

18+
global.VERDACCIO_API_URL = 'https://verdaccio.tld';
19+
1820
const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock;
1921
customGlobal.fetch = require('jest-fetch-mock');
2022
customGlobal.fetchMock = customGlobal.fetch;

src/utils/api.test.ts

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { handleResponseType } from '../../src/utils/api';
1+
import api, { handleResponseType } from '../../src/utils/api';
22

33
describe('api', () => {
44
describe('handleResponseType', () => {
@@ -29,4 +29,118 @@ describe('api', () => {
2929
expect(handled).toEqual([true, blob]);
3030
});
3131
});
32+
33+
describe('api client', () => {
34+
let fetchSpy;
35+
36+
beforeEach(() => {
37+
fetchSpy = jest.spyOn(window, 'fetch');
38+
});
39+
40+
afterEach(() => {
41+
fetchSpy.mockRestore();
42+
});
43+
44+
test('when there is no VERDACCIO_URL is defined', () => {
45+
const { VERDACCIO_API_URL } = window;
46+
delete window.VERDACCIO_API_URL;
47+
// @ts-ignore
48+
window.VERDACCIO_API_URL = undefined;
49+
50+
expect(() => {
51+
api.request('https://verdaccio.tld');
52+
}).toThrow(new Error('VERDACCIO_API_URL is not defined!'));
53+
54+
window.VERDACCIO_API_URL = VERDACCIO_API_URL;
55+
});
56+
57+
test('when url is a resource url', async () => {
58+
fetchSpy.mockImplementation(() =>
59+
Promise.resolve({
60+
headers: new Headers({
61+
'Content-Type': 'application/json',
62+
}),
63+
ok: true,
64+
json: () => ({ a: 1 }),
65+
})
66+
);
67+
68+
const response = await api.request('/resource');
69+
70+
expect(fetchSpy).toHaveBeenCalledWith('https://verdaccio.tld/resource', {
71+
credentials: 'same-origin',
72+
headers: {},
73+
method: 'GET',
74+
});
75+
expect(response).toEqual({ a: 1 });
76+
});
77+
78+
test('when there is token from storage', async () => {
79+
jest.resetModules();
80+
jest.doMock('./storage', () => ({ getItem: () => 'token-xx-xx-xx' }));
81+
82+
fetchSpy.mockImplementation(() =>
83+
Promise.resolve({
84+
headers: new Headers({
85+
'Content-Type': 'application/json',
86+
}),
87+
ok: true,
88+
json: () => ({ c: 3 }),
89+
})
90+
);
91+
92+
const api = require('../../src/utils/api').default;
93+
const response = await api.request('/resource', 'GET');
94+
95+
expect(fetchSpy).toHaveBeenCalledWith('https://verdaccio.tld/resource', {
96+
credentials: 'same-origin',
97+
headers: {
98+
Authorization: 'Bearer token-xx-xx-xx',
99+
},
100+
method: 'GET',
101+
});
102+
expect(response).toEqual({ c: 3 });
103+
});
104+
105+
test('when url is a cross origin url', async () => {
106+
fetchSpy.mockImplementation(() =>
107+
Promise.resolve({
108+
headers: new Headers({
109+
'Content-Type': 'application/json',
110+
}),
111+
ok: true,
112+
json: () => ({ b: 2 }),
113+
})
114+
);
115+
116+
const response = await api.request('https://verdaccio.xyz/resource');
117+
expect(fetchSpy).toHaveBeenCalledWith('https://verdaccio.xyz/resource', {
118+
credentials: 'same-origin',
119+
headers: {},
120+
method: 'GET',
121+
});
122+
expect(response).toEqual({ b: 2 });
123+
});
124+
125+
test('when api returns an error 3.x.x - 4.x.x', async () => {
126+
fetchSpy.mockImplementation(() =>
127+
Promise.resolve({
128+
headers: new Headers({
129+
'Content-Type': 'application/json',
130+
}),
131+
ok: false,
132+
json: () => {},
133+
})
134+
);
135+
136+
await expect(api.request('/resource')).rejects.toThrow(new Error('something went wrong'));
137+
});
138+
139+
test('when api returns an error 5.x.x', async () => {
140+
const errorMessage = 'Internal server error';
141+
fetchSpy.mockImplementation(() => Promise.reject(new Error(errorMessage)));
142+
143+
await expect(api.request('/resource')).rejects.toThrow(new Error(errorMessage));
144+
});
145+
});
32146
});

0 commit comments

Comments
 (0)