Fix API mock
This commit is contained in:
parent
608958f1e8
commit
1fe7a72790
4 changed files with 34 additions and 31 deletions
|
@ -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;
|
||||
};
|
28
app/soapbox/__mocks__/api.ts
Normal file
28
app/soapbox/__mocks__/api.ts
Normal 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;
|
||||
};
|
|
@ -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();
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue