-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchAPI.test.mjs
More file actions
50 lines (42 loc) · 1.32 KB
/
FetchAPI.test.mjs
File metadata and controls
50 lines (42 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const {FetchAPI} = require('./FetchAPIClass.cjs');
const mockFetch = jest.fn();
const headers = {
accept: 'application/json',
'Bridge-Version': '2021-06-01',
'content-type': 'application/json',
'Client-Id': 'YOUR_CLIENT_ID',
'Client-Secret': 'YOUR_CLIENT_SECRET',
};
const fetchAPI = new FetchAPI(headers, mockFetch);
describe('FetchAPI', () => {
it('should call fetch with the correct arguments', async () => {
const mockResponse = {
ok: true,
json: jest.fn().mockResolvedValue({ data: 'dummy data' }),
};
mockFetch.mockResolvedValue(mockResponse);
const url = 'https://example.com/api/data';
const method = 'GET';
const body = { key: 'value' };
const options = { option1: 'value1' };
await fetchAPI.request(url, method, body, options);
expect(mockFetch).toHaveBeenCalledWith(url, {
method,
headers,
body: JSON.stringify(body),
...options,
});
});
it('should throw an error on non-ok response', async () => {
const mockResponse = {
ok: false,
};
mockFetch.mockResolvedValue(mockResponse);
const url = 'https://example.com/api/data';
const method = 'GET';
const body = { key: 'value' };
await expect(fetchAPI.request(url, method, body)).rejects.toThrowError(
'Erreur lors de l\'appel à l\'API'
);
});
});