Jest/React/Redux 错误:操作可能没有未定义的"类型"属性。你拼错常量了吗?操作:{}



无论我如何尝试为动作编写测试,它都会困扰我一段时间

"操作可能没有未定义的"类型"属性。您是否拼写了常数?动作:{}"

但是,如果我要测量要测试的操作,它将打印出应定义的类型。我正在使用开玩笑的框架来测试react/redux应用。

这是我正在测试的动作:

  export const updateUserGenderAction = (valueToUpdate) => {
  return (dispatch) => Promise.all([
    patchUpdateGender(valueToUpdate).then((response) => {
      dispatch(getSuccessData(response, 'GET_USER_DATA'))
    }).catch((error) => {
      dispatch(getErrorData(error, 'GET_USER_ERROR'))
    })
  ])
}

动作创造者:

export function getSuccessData (response, actionType) {
  return {
    payload: response,
    type: actionType
  }
}
export function getErrorData (err, errorType) {
  Alert.error(err)
  return {
    payload: err,
    type: errorType
  }
}

和测试尝试:

  it('creates updateUserGender action', () => {
    console.log(actions.updateUserGenderAction())
    const expectedActions = [
            { type: 'GET_USER_DATA', payload: userData },
            { type: 'GET_USER_ERROR', payload: userData }
    ]
    return store.dispatch(actions.updateUserGenderAction()).then(() => {
      expect(store.getActions()).toEqual(expectedActions)
    })
  })

  it('calls updateUserGenderAction', async () => {
    actions.updateUserGenderAction = jest.fn(() => {
      return Promise.resolve(getSuccessData(response, 'GET_USER_DATA'))
    })
    let mockStore = configureMockStore()
    let store = mockStore({})
    await store.dispatch(getSuccessData())
    expect(store.getActions()).toEqual(getSuccessData(response, 'GET_USER_DATA'))
  })

和console.log(action.updateusergenderAction()):

承诺{ {有效载荷: {getUserDataAction:[函数:getUserDataAction], UpdateUserGenderAction:[对象], updateFirstNalkeaction:[函数:updatefirstnameaction], updatelastnameaction:[函数:updatelastnameaction], updatemiddlenameaction:[功能:updatemiddlenameaction], updatecountryaction:[功能:updatecountryaction], ChangePasswordAction:[函数:change passwordaction], Update PrimaryMailaction:[函数:Update PrimaryEmailaction], updateUsernAmeaction:[函数:updateUsernAmeaction], UPDATECHEANENATERTENTATEEMAILACTION:[功能:UpdatechanGealTernateEmailAction], updateadDphoneAction:[函数:updateadDphoneAction], 更新肾上腺素:[函数:更新deDitphoneaction], getUsersessionAction:[功能:getusersessionAction], DeleteUsersessionAction:[函数:DeleteUsersessionAction], updatedEfaultMailingAddressAction:[函数:UpdatedEfateDefaultMailingAddressAction], DeleteMailingAddressaction:[函数:DeletemailingAddressaction], addMailingAddressAction:[函数:addMailingAddressaction], EditMailingMailingAddressAction:[函数:EditMailingMailingAddressaction], getuccessdata:[函数:getCuccessData], getErrordata:[函数:getErrordata]}, 类型:'get_user_data'}}

有人知道我可能缺少什么以及如何正确测试这类行动吗?先感谢您!

您在没有参数的情况下调用getSuccessData(),因此actionType不确定:

export function getSuccessData(response, actionType) {
  return {
    payload: response,
    type: actionType // `actionType` is undefined
  }
}

类型通常应定义为字符串常数,因此最好在动作创建者中指定它:

const GET_USER_DATA = 'GET_USER_DATA';
export function getSuccessData(response) {
  return {
    type: GET_USER_DATA,
    payload: response,
  }
}

另请参阅:http://redux.js.org/docs/recipes/reducingboilerplate.html

最新更新