0

My team recently started working on a project that uses redux-promise. The payload in each action is an axios call, like this:

function fetchUsers() {
    return {
        type: actionTypes.FETCH_USERS,
        payload: axios.post('users')
    }
}

Redux-promise takes care of dispatching "a copy of the action with the resolved value of the promise" (their words). The reducer expects the data, not a promise:

function userReducer(state, action) {
    switch (action.type) {
        case actionTypes.FETCH_USERS:
            return action.payload.data
        default:
            return state
    }
}

The application works as intended, but writing tests has been a challenge. What's are the best practices for writing tests in this environment? I'd like to write tests for the action creator and for the reducer.

Joe Maffei
  • 1,855
  • 1
  • 15
  • 18

1 Answers1

-1

Redux tests has a very nice tutorial here under 'Async Action Creators'

Basically all you need to do is

  1. Use a library like nock to mock the server response
  2. Use the built in redux-mock-store to test the redux state after calling the actions
gafi
  • 12,113
  • 2
  • 30
  • 32
  • I've looked at this tutorial, but it doesn't seem to apply to redux-promise. There are 3 actions for each operation: fetchTodosRequest, fetchTodosSuccess, and fetchTodosFailure. Redux-promise takes care of dispatching "a copy of the action with the resolved value of the promise". Also, nock doesn't seem to play well with axios, as documented [here](https://github.com/mzabriskie/axios/issues/305). – Joe Maffei Aug 26 '17 at 02:25