react redux - How can I check in a jest test if a thunk action within a thunk action creator has been dispatched? -


here's generalized example:

// myactions.js export const actionone = () => (dispatch) => {     dispatch(actiontwo()); };  export const actiontwo = () => ({     type: 'some_type', }); 

i test actiontwo has been either called or dispatched, ideally without test knowing going on in actiontwo, because have different test takes care of that.

i using redux-mock-store dispatch tested action mocked store , calling store.getactions() find out if expected actions within thunk action creator have been dispatched. feel not right way go in particular scenario because test test more should. want know if actiontwo has been called @ all.

i'm aware of spyon , jest.mock, i've been unable use either solve problem. here's generalized test looks like:

// myactions.test.js import configuremockstore 'redux-mock-store'; import thunk 'redux-thunk'; import * actions 'myactions';  const mockstore = configuremockstore([thunk]);  test('actionone', () => {     const store = mockstore();      return store.dispatch(actions.actionone()).then(() => {         // todo: check if actions.actiontwo called     }); });  test('actiontwo', () => {     const store = mockstore();      return store.dispatch(actions.actiontwo()).then(() => {         expect(store.getactions()).toequal([{ type: 'some_type' }]);     });  }); 

i'm grateful suggestions!

took me while, figured out. it's not ideal (because involves small change tested code), closest ideal get.

// myactions.js export const actionone = () => (dispatch) => {     dispatch(exports.actiontwo()); };  export const actiontwo = () => ({     type: 'some_type', }); 

the important change exports.actiontwo(). way, make sure can overwrite function's implementation outside (the test file) , overwriting function called within imported file.

now can add following test file:

beforeeach(() => {     actions.actiontwo = jest.fn(() => () => promise.resolve()); }); 

actiontwo being mocked , can use tobecalledwith , other expectations on it. if wish test actual implementation within same test file, can store in variable before calling beforeeach, like:

const actiontwo = actions.actiontwo; 

and in test setup implementation, can overwrite mock calling

actions.actiontwo = actiontwo; 

that's it. can make sure ignore side effects exported function , test actual unit.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -