Reputation: 1
How can we mock implementation for redux thunks.
I have thunks in a file:- src/actions.js
export const getRequest = () => async dispatch => {
return await dispatch(
fetchRequestInfo(),
);
};
export const getBanks = () => async dispatch => {
const response = dispatch(getRequest());
if (!!response) {
dispatch(success());
} else {
dispatch(failure());
}
};
In my src/actions.spec.js
, I want to mock getRequest
and simply return a json data, like:-
import * as actions from 'actions';
const mockReqResponse = async () => {
return {data: '1123'};
};
const mockGetReq = jest.spyOn(actions, 'getRequest').mockImplementation(() => mockReqResponse);
describe('getBanks should work', async () => {
const store = mockStore(); // mocked redux store
await store.dispatch(action.getBanks());
})
The code flow does not reach to the mock implementation of getRequest
which is mockReqResponse
. Am I doing anything wrong ?
Upvotes: 0
Views: 218
Reputation: 2925
It's a common problem when testing thunks from the same module. You can either move getRequest
to a separate module, or check the top answer to this question: How to mock functions in the same module using Jest? It's a bit weird but it works. This allows you to test thunks that depend on a couple of other thunks (or simple action creators) from the same module.
Upvotes: 0