Skip to content

testing async store.dispatch #546

@kswope

Description

@kswope

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions