Skip to content

Commit

Permalink
test: api request coverage (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Feb 19, 2024
1 parent 102c934 commit 7efe0d9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/utils/__snapshots__/api-requests.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`apiRequest should make a request with the correct parameters 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",
"Authorization": "token yourAuthToken",
"Cache-Control": "no-cache",
"Content-Type": "application/json",
}
`;
41 changes: 41 additions & 0 deletions src/utils/api-requests.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import axios from 'axios';
import { apiRequest, apiRequestAuth } from './api-requests';

jest.mock('axios');

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);

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

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

describe('apiRequestAuth', () => {
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);

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

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

0 comments on commit 7efe0d9

Please sign in to comment.