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

时间:2017-09-12 09:32:12

标签: reactjs testing redux action jestjs

这个错误让我困扰了一段时间无论我如何尝试为行动编写测试,它都给了我这个:

"操作可能没有未定义的"类型"属性。你拼错了吗?行动:{} "

但是如果我控制日志记录我尝试测试的操作,它会打印出应该定义的类型。 我使用jest框架来测试react / redux app。

以下是我正在测试的操作:

  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(actions.updateUserGenderAction()):

  

承诺{         {有效载荷:          {getUserDataAction:[Function:getUserDataAction],            updateUserGenderAction:[Object],            updateFirstNameAction:[Function:updateFirstNameAction],            updateLastNameAction:[Function:updateLastNameAction],            updateMiddleNameAction:[Function:updateMiddleNameAction],            updateCountryAction:[Function:updateCountryAction],            changePasswordAction:[Function:changePasswordAction],            updatePrimaryEmailAction:[Function:updatePrimaryEmailAction],            updateUsernameAction:[Function:updateUsernameAction],            updateChangeAlternateEmailAction:[Function:updateChangeAlternateEmailAction],            updateAddPhoneAction:[Function:updateAddPhoneAction],            updateEditPhoneAction:[Function:updateEditPhoneAction],            getUserSessionAction:[Function:getUserSessionAction],            deleteUserSessionAction:[Function:deleteUserSessionAction],            updateDefaultMailingAddressAction:[Function:updateDefaultMailingAddressAction],            deleteMailingAddressAction:[Function:deleteMailingAddressAction],            addMailingAddressAction:[Function:addMailingAddressAction],            editMailingAddressAction:[Function:editMailingAddressAction],            getSuccessData:[Function:getSuccessData],            getErrorData:[Function:getErrorData]},         输入:' GET_USER_DATA' }}

有谁知道我可能会遗漏什么以及如何正确测试这些行为? 先感谢您!

1 个答案:

答案 0 :(得分:3)

您正在调用没有参数的getSuccessData(),因此actionType未定义:

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

Types should typically be defined as string constants,因此最好在动作创建者中指定它:

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