Tests: Allow stubbing API

stable/1.0.x
Alex Gleason 2020-06-09 17:05:28 -05:00
rodzic 6810f8d0b6
commit 842f35ebc5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
2 zmienionych plików z 18 dodań i 6 usunięć

Wyświetl plik

@ -1,10 +1,17 @@
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
const api = jest.requireActual('../api').default; 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) => { export default (...params) => {
const axiosInstance = api(...params); const axios = api(...params);
const mock = new MockAdapter(axiosInstance); setupMock(axios);
mock.onGet('/instance/about/index.html').reply(200, '<h1>Hello world</h1>'); return axios;
return axiosInstance;
}; };

Wyświetl plik

@ -6,12 +6,17 @@ import {
fetchAboutPage, fetchAboutPage,
} from '../about'; } from '../about';
import { Map as ImmutableMap } from 'immutable'; import { Map as ImmutableMap } from 'immutable';
import { __stub as stubApi } from 'soapbox/api';
const middlewares = [thunk]; const middlewares = [thunk];
const mockStore = configureMockStore(middlewares); const mockStore = configureMockStore(middlewares);
describe('About actions', () => { describe('fetchAboutPage()', () => {
it('creates FETCH_ABOUT_PAGE_SUCCESS when fetching about page has been done', () => { beforeAll(() => stubApi(mock => {
mock.onGet('/instance/about/index.html').reply(200, '<h1>Hello world</h1>');
}));
it('creates the expected actions', () => {
const expectedActions = [ const expectedActions = [
{ type: FETCH_ABOUT_PAGE_REQUEST, slug: 'index' }, { type: FETCH_ABOUT_PAGE_REQUEST, slug: 'index' },
{ type: FETCH_ABOUT_PAGE_SUCCESS, slug: 'index' }, { type: FETCH_ABOUT_PAGE_SUCCESS, slug: 'index' },