reactjs - Jest/React/Redux error: Actions may not have an undefined "type" property. Have you misspelled a constant? Action: {} -
this error haunting me while no matter how try write tests actions, gives me this:
"actions may not have undefined "type" property. have misspelled constant? action: {} "
but if console log action i'm trying test, prints out type defined should be. i'm using jest framework testing react/redux app.
here action testing:
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')) }) ]) }
action creators:
export function getsuccessdata (response, actiontype) { return { payload: response, type: actiontype } } export function geterrordata (err, errortype) { alert.error(err) return { payload: err, type: errortype } }
and test attempts:
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')) })
and 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' } }
does know might missing , how test these kind of actions properly? thank in advance!
you calling getsuccessdata()
no arguments, actiontype
undefined:
export function getsuccessdata(response, actiontype) { return { payload: response, type: actiontype // `actiontype` undefined } }
types should typically defined string constants, better specify in action creator:
const get_user_data = 'get_user_data'; export function getsuccessdata(response) { return { type: get_user_data, payload: response, } }
see also: http://redux.js.org/docs/recipes/reducingboilerplate.html
Comments
Post a Comment