【问题标题】:testing redux thunk async actions give undefined instead of promise测试 redux thunk 异步操作给出未定义而不是承诺
【发布时间】:2018-05-25 04:22:20
【问题描述】:

我有一个创建动作的函数

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 的错误。

【问题讨论】:

  • 什么是mockStore。那个函数返回什么?
  • 更新了新代码

标签: reactjs redux enzyme jestjs redux-thunk


【解决方案1】:

根据docs

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

您没有在dispatchAction 中返回任何内容,因此出现了错误消息。如果你想要一个Promise,那么你必须返回一个Promise

【讨论】:

    猜你喜欢
    • 2017-03-03
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2016-12-20
    • 2018-06-14
    • 1970-01-01
    相关资源
    最近更新 更多