Skip to content

Commit 0be4553

Browse files
setchyadufr
authored andcommitted
test(api-request): cover scenario where no data object is provided to call (gitify-app#804)
1 parent da3a92f commit 0be4553

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/utils/__snapshots__/api-requests.test.ts.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ exports[`apiRequest should make a request with the correct parameters 1`] = `
88
}
99
`;
1010

11+
exports[`apiRequest should make a request with the correct parameters and default data 1`] = `
12+
{
13+
"Accept": "application/json",
14+
"Cache-Control": "no-cache",
15+
"Content-Type": "application/json",
16+
}
17+
`;
18+
1119
exports[`apiRequestAuth should make an authenticated request with the correct parameters 1`] = `
1220
{
1321
"Accept": "application/json",
@@ -16,3 +24,12 @@ exports[`apiRequestAuth should make an authenticated request with the correct pa
1624
"Content-Type": "application/json",
1725
}
1826
`;
27+
28+
exports[`apiRequestAuth should make an authenticated request with the correct parameters and default data 1`] = `
29+
{
30+
"Accept": "application/json",
31+
"Authorization": "token yourAuthToken",
32+
"Cache-Control": "no-cache",
33+
"Content-Type": "application/json",
34+
}
35+
`;

src/utils/api-requests.test.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { apiRequest, apiRequestAuth } from './api-requests';
33

44
jest.mock('axios');
55

6+
const url = 'https://example.com';
7+
const method = 'get';
8+
69
describe('apiRequest', () => {
710
it('should make a request with the correct parameters', async () => {
8-
const url = 'https://example.com';
9-
const method = 'get';
1011
const data = { key: 'value' };
1112

1213
await apiRequest(url, method, data);
@@ -19,13 +20,25 @@ describe('apiRequest', () => {
1920

2021
expect(axios.defaults.headers.common).toMatchSnapshot();
2122
});
23+
24+
it('should make a request with the correct parameters and default data', async () => {
25+
const data = {};
26+
await apiRequest(url, method);
27+
28+
expect(axios).toHaveBeenCalledWith({
29+
method,
30+
url,
31+
data,
32+
});
33+
34+
expect(axios.defaults.headers.common).toMatchSnapshot();
35+
});
2236
});
2337

2438
describe('apiRequestAuth', () => {
39+
const token = 'yourAuthToken';
40+
2541
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';
2942
const data = { key: 'value' };
3043

3144
await apiRequestAuth(url, method, token, data);
@@ -38,4 +51,18 @@ describe('apiRequestAuth', () => {
3851

3952
expect(axios.defaults.headers.common).toMatchSnapshot();
4053
});
54+
55+
it('should make an authenticated request with the correct parameters and default data', async () => {
56+
const data = {};
57+
58+
await apiRequestAuth(url, method, token);
59+
60+
expect(axios).toHaveBeenCalledWith({
61+
method,
62+
url,
63+
data,
64+
});
65+
66+
expect(axios.defaults.headers.common).toMatchSnapshot();
67+
});
4168
});

0 commit comments

Comments
 (0)