简体   繁体   中英

Error: Action may not be undefined in async action testing in Redux app with nock and mock-store

bit stumped with this one. Following the Redux docs to setup tests for my async actions ( docs here ), I am getting the error:

Actions may not be an undefined. (at dispatch (node_modules/redux-mock-store/lib/index.js:35:19))

Testing this action:

export const FETCH_TRANSACTIONS = 'FETCH_TRANSACTIONS'

function fetchTransactionsSuccess (transactions) {
  return {
    type: FETCH_TRANSACTIONS,
    payload: transactions
  }
}

export const fetchTransactions = () => dispatch => axios.get('/api/transactions')
  .then(transactions => dispatch(fetchTransactionsSuccess(transactions)))
  .catch(err => dispatch(handleErr(err)))

And this is the test itself. Any help would be amazing. Been staring at this so long my eyes hurt.

import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import * as actions from '../../client/actions/actionCreators'
import nock from 'nock'
import expect from 'expect'

const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)

describe('async actions', () => {
  afterEach(() => {
    nock.cleanAll()
  })

  it('dispatches FETCH_TRANSACTIONS when data is returned', () => {
    nock('http://localhost:3000/')
      .get('/api/transactions')
      .reply(200, [
        {
          "_id": "588900efdf9d3e0905a2d604",
          "amount": 4.50,
          "name": "Cashew Nuts",
          "__v": 0,
          "date": "2017-01-25T00:00:00.000Z",
          "user": "58c2a33cc6cd5a5d15a8fc0c"
        },
        {
          "_id": "58890108df9d3e0905a2d605",
          "amount": 6.25,
          "name": "Monmouth Coffee",
          "__v": 0,
          "date": "2017-01-25T00:00:00.000Z",
          "user": "58c2a33cc6cd5a5d15a8fc0c"
        }
      ])

    const expectedActions = [
      {
        type: actions.FETCH_TRANSACTIONS,
        payload: [
          {
            "_id": "588900efdf9d3e0905a2d604",
            "amount": 4.50,
            "name": "Cashew Nuts",
            "__v": 0,
            "date": "2017-01-25T00:00:00.000Z",
            "user": "58c2a33cc6cd5a5d15a8fc0c"
          },
          {
            "_id": "58890108df9d3e0905a2d605",
            "amount": 6.25,
            "name": "Monmouth Coffee",
            "__v": 0,
            "date": "2017-01-25T00:00:00.000Z",
            "user": "58c2a33cc6cd5a5d15a8fc0c"
          }
        ]
      }
    ]

    const store = mockStore({ transactions: [] })
    console.log(actions)
    return store.dispatch(actions.fetchTransactions())
      .then(() => {
        expect(store.getActions()).toEqual(expectedActions)
      })
  })
})

UPDATE The handleErr function returns setCurrentUser which is another action (which the original action called with dispatch:

export function handleErr (err) {
  if (err.status === 401 || err.status === 404) {
    localStorage.removeItem('mm-jwtToken')
    setAuthToken(false)
    return setCurrentUser({})
  }
}

There is known issue with mocking axios request with nock . So I believe that promise chain in your fetchTransactions action creator falls to the catch clause. Please check your handleErr function, is it returning valid Action? I bet it returns undefined and that is why you have this error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM