Fix API mock

This commit is contained in:
Alex Gleason 2022-03-18 12:20:03 -05:00
parent 608958f1e8
commit 1fe7a72790
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 34 additions and 31 deletions

View file

@ -1,26 +0,0 @@
import MockAdapter from 'axios-mock-adapter';
const api = jest.requireActual('../api');
let mocks = [];
export const __stub = func => mocks.push(func);
export const __clear = () => mocks = [];
const setupMock = axios => {
const mock = new MockAdapter(axios);
mocks.map(func => func(mock));
};
export const staticClient = api.staticClient;
export const baseClient = (...params) => {
const axios = api.baseClient(...params);
setupMock(axios);
return axios;
};
export default (...params) => {
const axios = api.default(...params);
setupMock(axios);
return axios;
};

View file

@ -0,0 +1,28 @@
import MockAdapter from 'axios-mock-adapter';
import { AxiosInstance } from 'axios';
import { jest } from '@jest/globals';
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) => {
const mock = new MockAdapter(axios);
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;
};

View file

@ -1,8 +1,6 @@
import { Map as ImmutableMap } from 'immutable';
import { STATUSES_IMPORT } from 'soapbox/actions/importer';
import { __stub } from 'soapbox/api';
import { mockStore } from 'soapbox/test_helpers';
import { mockStore, rootState } from 'soapbox/test_helpers';
import { fetchContext } from '../statuses';
@ -15,7 +13,7 @@ describe('fetchContext()', () => {
.reply(200, statuses);
});
const store = mockStore(ImmutableMap());
const store = mockStore(rootState);
store.dispatch(fetchContext('017ed505-5926-392f-256a-f86d5075df70')).then(context => {
const actions = store.getActions();

View file

@ -11,6 +11,9 @@ import thunk from 'redux-thunk';
import rootReducer from 'soapbox/reducers';
export const rootState = rootReducer(undefined, {});
export const getState = () => rootState;
// Mock Redux
// https://redux.js.org/recipes/writing-tests/
const middlewares = [thunk];
@ -20,7 +23,7 @@ export const mockStore = configureMockStore(middlewares);
export const createComponent = (children, props = {}) => {
props = ImmutableMap({
locale: 'en',
store: mockStore(rootReducer(ImmutableMap(), {})),
store: mockStore(rootState),
}).merge(props);
return renderer.create(