Skip to content

Document a simpler way to test async action creators #1716

@gaearon

Description

@gaearon

Since Redux Thunk 2.1.0, we can do something like this:

import * as api from '../api'

export default function configureStore(mocks) {
  return createStore(
    reducer,
    applyMiddleware(thunk.withExtraArgument({
      api,
      // can also pass other modules you need in ACs and want to mock in tests
    }))
  );
}

Your action creators now don’t depend on those modules:

export function getUser(id) {
  return (dispatch, getState, { api }) =>
    api.getUser(id).then(() => dispatch({ ... }))
  }
}

In tests, you should be able to call them directly with spies and pass your mocks as the third argument.
Looks perfectly testable to me. Seems like there is need for a third-party library.

What am I missing?

cc @arnaudbenard

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