Skip to content

Commit ce082da

Browse files
setchyadufr
authored andcommitted
test: api request coverage (#792)
1 parent e28e49d commit ce082da

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`apiRequest should make a request with the correct parameters 1`] = `
4+
{
5+
"Accept": "application/json",
6+
"Cache-Control": "no-cache",
7+
"Content-Type": "application/json",
8+
}
9+
`;
10+
11+
exports[`apiRequestAuth should make an authenticated request with the correct parameters 1`] = `
12+
{
13+
"Accept": "application/json",
14+
"Authorization": "token yourAuthToken",
15+
"Cache-Control": "no-cache",
16+
"Content-Type": "application/json",
17+
}
18+
`;

src/utils/api-requests.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import axios from 'axios';
2+
import { apiRequest, apiRequestAuth } from './api-requests';
3+
4+
jest.mock('axios');
5+
6+
describe('apiRequest', () => {
7+
it('should make a request with the correct parameters', async () => {
8+
const url = 'https://example.com';
9+
const method = 'get';
10+
const data = { key: 'value' };
11+
12+
await apiRequest(url, method, data);
13+
14+
expect(axios).toHaveBeenCalledWith({
15+
method,
16+
url,
17+
data,
18+
});
19+
20+
expect(axios.defaults.headers.common).toMatchSnapshot();
21+
});
22+
});
23+
24+
describe('apiRequestAuth', () => {
25+
it('should make an authenticated request with the correct parameters', async () => {
26+
const url = 'https://example.com';
27+
const method = 'get';
28+
const token = 'yourAuthToken';
29+
const data = { key: 'value' };
30+
31+
await apiRequestAuth(url, method, token, data);
32+
33+
expect(axios).toHaveBeenCalledWith({
34+
method,
35+
url,
36+
data,
37+
});
38+
39+
expect(axios.defaults.headers.common).toMatchSnapshot();
40+
});
41+
});

0 commit comments

Comments
 (0)