Skip to content

Instantly share code, notes, and snippets.

@iamdey
Created August 16, 2017 12:54
Show Gist options
  • Save iamdey/71528fd155d10099d0bb0e4a56d2b558 to your computer and use it in GitHub Desktop.
Save iamdey/71528fd155d10099d0bb0e4a56d2b558 to your computer and use it in GitHub Desktop.
how to unit test with jest a redux middleware with promises
// unit test of redux middleware w/ jest
import configureStore from './configure-store';
const middleware = store => next => (action) => {
if (action.type === 'FOO') {
Promise.resolve({ type: 'FOO_SUCCESS', payload: {} }).then(act => store.dispatch(act);
}
return next(action);
};
const store = configureStore({});
describe('handle FOO', async () => {
it('dispatch FOO_SUCCESS', () => {
store.dispatch = jest.fn(store.dispatch);
await middleware(store)(() => {})({ type: 'FOO', payload: {});
// Following is always false because the dispatch is within a promise wich is resolved after the
// next(action) is returned.
expect(store.dispatch.mock.calls).toContain({ type: 'FOO_SUCCESS', payload: {} });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment