How To Unit Test Async Redux Actions To Mock Ajax Response
I am creating a middleware for making ajax requests using async actions. The middleware intercepts original action, performs ajax request, and re-dispatches the original action alo
Solution 1:
Note: answer below is slightly outdated.
A much simpler updated approach is described here. You can still do it the other way too, though.
We now have a section on testing async action creators in the official docs.
For async action creators using Redux Thunk or other middleware, it’s best to completely mock the Redux store for tests. You can still use applyMiddleware()
with a mock store, as shown below. You can also use nock to mock the HTTP requests.
functionfetchTodosRequest() {
return {
type: ADD_TODOS_REQUEST
};
}
functionfetchTodosSuccess(body) {
return {
type: ADD_TODOS_SUCCESS,
body
};
}
functionfetchTodosFailure(ex) {
return {
type: ADD_TODOS_FAILURE,
ex
};
}
exportfunctionfetchTodos(data) {
returndispatch => {
dispatch(fetchTodosRequest());
returnfetch('http://example.com/todos')
.then(res => res.json())
.then(json =>dispatch(addTodosSuccess(json.body)))
.catch(ex =>dispatch(addTodosFailure(ex)));
};
}
can be tested like:
import expect from'expect';
import { applyMiddleware } from'redux';
import thunk from'redux-thunk';
import * as actions from'../../actions/counter';
import * as types from'../../constants/ActionTypes';
import nock from'nock';
const middlewares = [thunk];
/**
* Creates a mock of Redux store with middleware.
*/functionmockStore(getState, expectedActions, onLastAction) {
if (!Array.isArray(expectedActions)) {
thrownewError('expectedActions should be an array of expected actions.');
}
if (typeof onLastAction !== 'undefined' && typeof onLastAction !== 'function') {
thrownewError('onLastAction should either be undefined or function.');
}
functionmockStoreWithoutMiddleware() {
return {
getState() {
returntypeof getState === 'function' ?
getState() :
getState;
},
dispatch(action) {
const expectedAction = expectedActions.shift();
expect(action).toEqual(expectedAction);
if (onLastAction && !expectedActions.length) {
onLastAction();
}
return action;
}
}
}
const mockStoreWithMiddleware = applyMiddleware(
...middlewares
)(mockStoreWithoutMiddleware);
returnmockStoreWithMiddleware();
}
describe('async actions', () => {
afterEach(() => {
nock.cleanAll();
});
it('creates FETCH_TODO_SUCCESS when fetching todos has been done', (done) => {
nock('http://example.com/')
.get('/todos')
.reply(200, { todos: ['do something'] });
const expectedActions = [
{ type: types.FETCH_TODO_REQUEST },
{ type: types.FETCH_TODO_SUCCESS, body: { todos: ['do something'] } }
]
const store = mockStore({ todos: [] }, expectedActions, done);
store.dispatch(actions.fetchTodos());
});
});
Solution 2:
Turns out, I don't need to mock any store methods or anything. Its as simple as mocking the ajax request. I am using superagent
, so I did it something like this
const mockResponse = {
body: {
data: 'something'
}
};
spyOn(superagent.Request.prototype, 'end').and.callFake((cb) => {
cb(null, mockResponse); // callback with mocked response
});
// and expect it to be calledexpect(superagent.Request.prototype.end).toHaveBeenCalled();
Post a Comment for "How To Unit Test Async Redux Actions To Mock Ajax Response"