From 842f35ebc5af4a82bcc4a206f5b6c5354bc3dd1e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 9 Jun 2020 17:05:28 -0500 Subject: [PATCH] Tests: Allow stubbing API --- app/soapbox/__mocks__/api.js | 15 +++++++++++---- app/soapbox/actions/__tests__/about-test.js | 9 +++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/soapbox/__mocks__/api.js b/app/soapbox/__mocks__/api.js index 9f841d2bd..b59b546ab 100644 --- a/app/soapbox/__mocks__/api.js +++ b/app/soapbox/__mocks__/api.js @@ -1,10 +1,17 @@ import MockAdapter from 'axios-mock-adapter'; const api = jest.requireActual('../api').default; +let mocks = []; + +export const __stub = func => mocks.push(func); + +const setupMock = axios => { + const mock = new MockAdapter(axios); + mocks.map(func => func(mock)); +}; export default (...params) => { - const axiosInstance = api(...params); - const mock = new MockAdapter(axiosInstance); - mock.onGet('/instance/about/index.html').reply(200, '

Hello world

'); - return axiosInstance; + const axios = api(...params); + setupMock(axios); + return axios; }; diff --git a/app/soapbox/actions/__tests__/about-test.js b/app/soapbox/actions/__tests__/about-test.js index df0a84572..a07dc2fda 100644 --- a/app/soapbox/actions/__tests__/about-test.js +++ b/app/soapbox/actions/__tests__/about-test.js @@ -6,12 +6,17 @@ import { fetchAboutPage, } from '../about'; import { Map as ImmutableMap } from 'immutable'; +import { __stub as stubApi } from 'soapbox/api'; const middlewares = [thunk]; const mockStore = configureMockStore(middlewares); -describe('About actions', () => { - it('creates FETCH_ABOUT_PAGE_SUCCESS when fetching about page has been done', () => { +describe('fetchAboutPage()', () => { + beforeAll(() => stubApi(mock => { + mock.onGet('/instance/about/index.html').reply(200, '

Hello world

'); + })); + + it('creates the expected actions', () => { const expectedActions = [ { type: FETCH_ABOUT_PAGE_REQUEST, slug: 'index' }, { type: FETCH_ABOUT_PAGE_SUCCESS, slug: 'index' },