2017-09-12 49 views
1

此錯誤是困擾我,而不管我如何努力寫我的動作測試中,它給了我這樣的: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' } }

有誰知道我可能會丟失什麼以及如何正確測試這些操作? 提前謝謝!

回答

2

要調用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

+0

謝謝,我將它們改爲常量,但它沒有解決問題,仍然得到相同的錯誤。 – rokija

相關問題