-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Description
Maybe I'm overlooking something but I'm finding it impossible to test store.dispatch with an async action without a hacky timer solution. In the below example actions.authenticate contains an ajax call. The ajax call is mocked but its still in an async situation. I'm using redux-thunk middlewear. testTimeout() is just setTimeout with a 0 second delay.
Yes I know the design of redux allows the separate testing of actions, reducers, etc, but testing store.dispatch itself is a lot of bang for the buck because it ties store, reducers, actions and middlewear together.
test( 'success', (done) => {
fauxJaxResponse( '/authenticate', { 'the': 'token' } )
store.dispatch( actions.authenticate( 'myname', 'mypass' ) )
testTimeout(()=>{
// store.getState() asserts here
done()
})
})
I could test actions and reducers together (below) bypassing store.dispatch but its incomplete because its bypassing any middlewear. Note that actions.authenticate() returns a thunk that calls mockDispatch when ready
test( 'success', (done) => {
fauxJaxResponse( '/authenticate', { 'the': 'token' } )
var mockDispatch = function( action ) {
state = reducer(action)
// state asserts here
done()
}
actions.authenticate()( mockDispatch )
})
I almost want store.dispatch() to take a callback as a second param, to be called when the dispatch is complete, but that's weird right, and only useful in testing?