bigbuffet-rw/app/soapbox/__mocks__/api.ts

34 lines
973 B
TypeScript
Raw Normal View History

2022-03-18 10:20:03 -07:00
import { jest } from '@jest/globals';
2022-05-26 10:37:22 -07:00
import { AxiosInstance, AxiosResponse } from 'axios';
2022-03-18 11:16:59 -07:00
import MockAdapter from 'axios-mock-adapter';
2022-05-26 10:37:22 -07:00
import LinkHeader from 'http-link-header';
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;
2022-05-26 10:37:22 -07:00
export const getLinks = (response: AxiosResponse): LinkHeader => {
return new LinkHeader(response.headers?.link);
};
2022-03-18 10:20:03 -07:00
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;
};