Skip to content

Commit

Permalink
test(api-request): cover scenario where no data object is provided to…
Browse files Browse the repository at this point in the history
… call
  • Loading branch information
setchy committed Feb 20, 2024
1 parent 36af40a commit 5169f06
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/utils/__snapshots__/api-requests.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ exports[`apiRequest should make a request with the correct parameters 1`] = `
}
`;

exports[`apiRequest should make a request with the correct parameters and default data 1`] = `
{
"Accept": "application/json",
"Cache-Control": "no-cache",
"Content-Type": "application/json",
}
`;

exports[`apiRequestAuth should make an authenticated request with the correct parameters 1`] = `
{
"Accept": "application/json",
Expand All @@ -16,3 +24,12 @@ exports[`apiRequestAuth should make an authenticated request with the correct pa
"Content-Type": "application/json",
}
`;

exports[`apiRequestAuth should make an authenticated request with the correct parameters and default data 1`] = `
{
"Accept": "application/json",
"Authorization": "token yourAuthToken",
"Cache-Control": "no-cache",
"Content-Type": "application/json",
}
`;
37 changes: 32 additions & 5 deletions src/utils/api-requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { apiRequest, apiRequestAuth } from './api-requests';

jest.mock('axios');

const url = 'https://example.com';
const method = 'get';

describe('apiRequest', () => {
it('should make a request with the correct parameters', async () => {
const url = 'https://example.com';
const method = 'get';
const data = { key: 'value' };

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

expect(axios.defaults.headers.common).toMatchSnapshot();
});

it('should make a request with the correct parameters and default data', async () => {
const data = {};
await apiRequest(url, method);

expect(axios).toHaveBeenCalledWith({
method,
url,
data,
});

expect(axios.defaults.headers.common).toMatchSnapshot();
});
});

describe('apiRequestAuth', () => {
const token = 'yourAuthToken';

it('should make an authenticated request with the correct parameters', async () => {
const url = 'https://example.com';
const method = 'get';
const token = 'yourAuthToken';
const data = { key: 'value' };

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

expect(axios.defaults.headers.common).toMatchSnapshot();
});

it('should make an authenticated request with the correct parameters and default data', async () => {
const data = {};

await apiRequestAuth(url, method, token);

expect(axios).toHaveBeenCalledWith({
method,
url,
data,
});

expect(axios.defaults.headers.common).toMatchSnapshot();
});
});

0 comments on commit 5169f06

Please sign in to comment.