Testing Redux Thunk Action Creator
I've got a redux action creator that utilizes redux-thunk to do some logic to determine what to dispatch to the store. Its not promise-based, like an HTTP request would be, so I a
Solution 1:
Is there a reason to explicitly NOT return a promise in your action creator? It looks like getRemoveFileMetrics
is returning the promise, it just gets swallowed in handleSelection
...
Easiest solution is to just return the promise:
exportconsthandleSelection = (value, cacheKey) => {
returndispatch => {
if (value === "removeFiles") {
returndispatch(getRemoveFileMetrics(cacheKey));
}
dispatch({ type: HANDLE_SELECTION, value });
returnnewPromise();
};
};
Otherwise, you'll need make your assertions after the event loop is finished. You can do with a setTimeout wrapped in a Promise to get the .then
behavior.
it("should dispatch HANDLE_SELECTION when selecting operation", () => {
const store = mockStore({});
const value = "switchVersion";
const expectedAction = [{
type: MOA.HANDLE_SELECTION,
value,
}];
store.dispatch(MOA.handleSelection(value));
// flush outstanding async tasksreturnnewPromise(resolve => {
setTimeout(resolve, 0);
})
.then(() => {
const returnedActions = store.getActions();
expect(returnedActions).toEqual(expectedAction);
});
});
Post a Comment for "Testing Redux Thunk Action Creator"