Mock API with MSW
This commit is contained in:
@ -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;
|
||||
};
|
||||
8
app/soapbox/__tests__/api.js
Normal file
8
app/soapbox/__tests__/api.js
Normal file
@ -0,0 +1,8 @@
|
||||
import api from 'soapbox/api';
|
||||
import { getState } from 'soapbox/test_helpers';
|
||||
|
||||
test('returns a 404', () => {
|
||||
return api(getState).get('/').catch(error => {
|
||||
expect(error.response).toMatchObject({ data: { error: 'Not implemented' } });
|
||||
});
|
||||
});
|
||||
@ -1,6 +1,6 @@
|
||||
import { Map as ImmutableMap } from 'immutable';
|
||||
|
||||
import { __stub } from 'soapbox/api';
|
||||
import { server, rest } from 'soapbox/msw';
|
||||
import { mockStore } from 'soapbox/test_helpers';
|
||||
|
||||
import { VERIFY_CREDENTIALS_REQUEST } from '../auth';
|
||||
@ -14,10 +14,14 @@ describe('preloadMastodon()', () => {
|
||||
it('creates the expected actions', () => {
|
||||
const data = require('soapbox/__fixtures__/mastodon_initial_state.json');
|
||||
|
||||
__stub(mock => {
|
||||
mock.onGet('/api/v1/accounts/verify_credentials')
|
||||
.reply(200, {});
|
||||
});
|
||||
server.use(
|
||||
rest.get('/api/v1/accounts/verify_credentials', (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json(require('soapbox/__fixtures__/pleroma-account.json')),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const store = mockStore(ImmutableMap());
|
||||
store.dispatch(preloadMastodon(data));
|
||||
|
||||
@ -1,21 +1,21 @@
|
||||
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 { server, rest } from 'soapbox/msw';
|
||||
import { rootState, mockStore } from 'soapbox/test_helpers';
|
||||
|
||||
import { fetchContext } from '../statuses';
|
||||
|
||||
describe('fetchContext()', () => {
|
||||
it('handles Mitra context', done => {
|
||||
const statuses = require('soapbox/__fixtures__/mitra-context.json');
|
||||
server.use(
|
||||
rest.get('/api/v1/statuses/017ed505-5926-392f-256a-f86d5075df70/context', (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json(require('soapbox/__fixtures__/mitra-context.json')),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
__stub(mock => {
|
||||
mock.onGet('/api/v1/statuses/017ed505-5926-392f-256a-f86d5075df70/context')
|
||||
.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();
|
||||
|
||||
13
app/soapbox/msw.js
Normal file
13
app/soapbox/msw.js
Normal file
@ -0,0 +1,13 @@
|
||||
import { rest } from 'msw';
|
||||
import { setupServer } from 'msw/node';
|
||||
|
||||
export const server = setupServer(
|
||||
rest.get('*', (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(404),
|
||||
ctx.json({ error: 'Not implemented' }),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
export { rest } from 'msw';
|
||||
@ -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(
|
||||
|
||||
@ -3,12 +3,14 @@
|
||||
import { configure } from 'enzyme';
|
||||
import Adapter from 'enzyme-adapter-react-16';
|
||||
|
||||
import { __clear as clearApiMocks } from 'soapbox/api';
|
||||
import { server } from 'soapbox/msw';
|
||||
|
||||
// Enzyme
|
||||
const adapter = new Adapter();
|
||||
configure({ adapter });
|
||||
|
||||
// API mocking
|
||||
jest.mock('soapbox/api');
|
||||
afterEach(() => clearApiMocks());
|
||||
// Setup MSW
|
||||
// https://mswjs.io/docs/api/setup-server
|
||||
beforeAll(() => server.listen());
|
||||
afterEach(() => server.resetHandlers());
|
||||
afterAll(() => server.close());
|
||||
|
||||
Reference in New Issue
Block a user