Skip to content

test: cover scenarios where no data object is provided to api call #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});