此錯誤是困擾我,而不管我如何努力寫我的動作測試中,它給了我這樣的:Jest/React/Redux錯誤:操作可能沒有未定義的「類型」屬性。你拼錯了一個常數嗎?操作:{}
「操作可能沒有未定義的‘類型’屬性有你拼寫錯誤的常數?行動:{} 「
但如果我控制檯記錄該操作我試圖測試,它打印出它應該是定義的類型。 我正在使用jest框架來測試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(actions.updateUserGenderAction() ):
Promise { { payload: { 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] }, type: 'GET_USER_DATA' } }
有誰知道我可能會丟失什麼以及如何正確測試這些操作? 提前謝謝!
謝謝,我將它們改爲常量,但它沒有解決問題,仍然得到相同的錯誤。 – rokija