javascript - How do I test asynchronous action creators that rely on previous dispatch calls? -


i have asynchronous action creator split 2 actions, 1 signal start of action , signal end. web request in middle of asynchronous action creator relies on values set first synchronous dispatch:

export function changefilter(from, to) {   return (dispatch, getstate, { fetch }) => {     // updates state new values , sets loading flag     dispatch(changefilterrequest(from, to));      // want use newly-set values build query     // string. however, stubbed implementation of `dispatch`     // doesn't use reducers, `getstate` continues return     // original value.     const { activeusers } = getstate();     return dosomewebrequest(fetch, activeusersparams(activeusers))       .then(response => dispatch(changefiltersuccess(response)))   }; } 

the implementation of redux-mock-store's dispatch pretty sparse, , in (obvious) hindsight, never pass reducers mocked store.

this means first dispatch call has no chance of updating state second dispatch call relies on. web request make has original values to , from dates, not updated ones async action call.

i'd avoid directly using from , to values passed in changefilter there may further transformations applied parameters within changefilterrequest. relying directly on result of getstate allow me dry handful of similar methods filter in different ways.

are there patterns should following these types of tests? there different way should structuring action creators?

i think unit test should precise , focus on scope of function want test changefilter. assuming have unit test changefilterrequest tests how activeusers calculated , added store , fact activeusers created in store side effect of dispaching changefilterrequest should out of scope of test changefilter shouldn't care how activeusers calculated. think can safely add dummy value activeusers in mocked store not dependent on values of to , from pass changefilterrequest , make sure function reading value store , passing activeusersparams.

that said, highly recommended redux-saga alternative redux-thunk handle async operations because makes testing these kind of actions easy without having create mocks or test side effects since actions pure.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -