Created
August 16, 2017 12:54
-
-
Save iamdey/71528fd155d10099d0bb0e4a56d2b558 to your computer and use it in GitHub Desktop.
how to unit test with jest a redux middleware with promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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