测试redux thunk异步操作给undefined而不是promise

时间:2017-12-11 15:43:59

标签: reactjs redux enzyme jest redux-thunk

我有一个创建动作的功能

export function dispatchAction (type, payload) {
 return dispatch => {
  dispatch({type: type, payload: payload})
 }
}

我正在为它编写测试

import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import * as actions from './actions
const mockStore = configureMockStore([thunk])
const store = mockStore({})

describe('dispatch action', () => {
 it('should return action based on type and payload', () => {
  const type = 'TEST'
  const payload = 'payload'
  return store.dispatch(actions.dispatchAction(type, payload)).then(() 
   => {
    expect(store.getActions())
     .toEqual({type, payload})
    })
   })
 })

但我收到Cannot read property 'then' of undefined

的错误

1 个答案:

答案 0 :(得分:1)

根据docs

  

内部函数的任何返回值都可以作为   发送本身的返回值。

您不会在dispatchAction中返回任何内容,因此会显示错误消息。如果您需要Promise,则必须返回Promise