From 1fe7a72790a22290ce0c6e6a48a5c93df6bdacbd Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 18 Mar 2022 12:20:03 -0500 Subject: [PATCH] Fix API mock --- app/soapbox/__mocks__/api.js | 26 ----------------- app/soapbox/__mocks__/api.ts | 28 +++++++++++++++++++ .../actions/__tests__/statuses-test.js | 6 ++-- app/soapbox/test_helpers.js | 5 +++- 4 files changed, 34 insertions(+), 31 deletions(-) delete mode 100644 app/soapbox/__mocks__/api.js create mode 100644 app/soapbox/__mocks__/api.ts diff --git a/app/soapbox/__mocks__/api.js b/app/soapbox/__mocks__/api.js deleted file mode 100644 index fcb3a3253..000000000 --- a/app/soapbox/__mocks__/api.js +++ /dev/null @@ -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; -}; diff --git a/app/soapbox/__mocks__/api.ts b/app/soapbox/__mocks__/api.ts new file mode 100644 index 000000000..a0b91a967 --- /dev/null +++ b/app/soapbox/__mocks__/api.ts @@ -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; +let mocks: Array = []; + +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; +}; diff --git a/app/soapbox/actions/__tests__/statuses-test.js b/app/soapbox/actions/__tests__/statuses-test.js index 71a0596a4..aa1ac9f3d 100644 --- a/app/soapbox/actions/__tests__/statuses-test.js +++ b/app/soapbox/actions/__tests__/statuses-test.js @@ -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(); diff --git a/app/soapbox/test_helpers.js b/app/soapbox/test_helpers.js index c9de38dc4..90f41119e 100644 --- a/app/soapbox/test_helpers.js +++ b/app/soapbox/test_helpers.js @@ -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(