soapbox/app/soapbox/api/__mocks__/index.ts

40 wiersze
1.1 KiB
TypeScript
Czysty Zwykły widok Historia

2022-03-18 17:20:03 +00:00
import { jest } from '@jest/globals';
2022-03-18 18:16:59 +00:00
import MockAdapter from 'axios-mock-adapter';
2022-05-26 17:37:22 +00:00
import LinkHeader from 'http-link-header';
2022-03-18 17:20:03 +00:00
import type { AxiosInstance, AxiosResponse } from 'axios';
2022-11-15 13:43:26 +00:00
const api = jest.requireActual('../index') as Record<string, Function>;
2022-03-18 17:20:03 +00:00
let mocks: Array<Function> = [];
2022-06-21 18:14:45 +00:00
export const __stub = (func: (mock: MockAdapter) => void) => mocks.push(func);
2022-03-18 17:20:03 +00:00
export const __clear = (): Function[] => mocks = [];
const setupMock = (axios: AxiosInstance) => {
2022-04-12 17:13:51 +00:00
const mock = new MockAdapter(axios, { onNoMatch: 'throwException' });
2022-03-18 17:20:03 +00:00
mocks.map(func => func(mock));
};
export const staticClient = api.staticClient;
2022-05-26 17:37:22 +00:00
export const getLinks = (response: AxiosResponse): LinkHeader => {
return new LinkHeader(response.headers?.link);
};
2022-10-03 15:03:43 +00:00
export const getNextLink = (response: AxiosResponse) => {
const nextLink = new LinkHeader(response.headers?.link);
return nextLink.refs.find((ref) => ref.uri)?.uri;
};
2022-03-18 17:20:03 +00: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;
};