2022-03-18 10:20:03 -07:00
|
|
|
import { jest } from '@jest/globals';
|
2022-03-18 11:16:59 -07:00
|
|
|
import { AxiosInstance } from 'axios';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2022-03-18 10:20:03 -07:00
|
|
|
|
|
|
|
const api = jest.requireActual('../api') as Record<string, Function>;
|
|
|
|
let mocks: Array<Function> = [];
|
|
|
|
|
|
|
|
export const __stub = (func: Function) => mocks.push(func);
|
|
|
|
export const __clear = (): Function[] => mocks = [];
|
|
|
|
|
|
|
|
const setupMock = (axios: AxiosInstance) => {
|
2022-04-12 10:13:51 -07:00
|
|
|
const mock = new MockAdapter(axios, { onNoMatch: 'throwException' });
|
2022-03-18 10:20:03 -07:00
|
|
|
mocks.map(func => func(mock));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const staticClient = api.staticClient;
|
|
|
|
|
|
|
|
export const baseClient = (...params: any[]) => {
|
|
|
|
const axios = api.baseClient(...params);
|
|
|
|
setupMock(axios);
|
|
|
|
return axios;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default (...params: any[]) => {
|
|
|
|
const axios = api.default(...params);
|
|
|
|
setupMock(axios);
|
|
|
|
return axios;
|
|
|
|
};
|