reduxjs / redux-mock-store

A mock store for testing Redux async action creators and middleware.
MIT License
2.5k stars 148 forks source link

How to test for dispatch and getState together with mockStore #153

Closed rahulCamp closed 6 years ago

rahulCamp commented 6 years ago

Hey, I am using the standard usage of dispatch and getState from returning action Data that considers the store state as a part of the computation and of course I get this error! So is there a workaround for the mockStore setup where getState() will fetch the actual state in the store currently that was passed through mockStore

action.js

TypeError: Cannot read property 'differencePhotos' of undefined

      12 | export const actionName = tag => (dispatch, getState) => {
      13 |   let img = {};
    > 14 |   if (getState().someState.imgs) {
         |       ^
      15 |     img = getState().someState.imgs;
      16 |     delete img[tag];
      17 |   }

action.test.js

  it('check function call for imgs', () => {
    store = mockStore(INIT_STATE);
    return store.dispatch(actionName(imgFiles, tag)).then(() => {
      const expectedPayload = { type: ACTION_TYPE_NAME, data: { imgs: { [tag]: imgFiles } } };
      expect(actions[2]).toEqual(expectedPayload);
    });
  });

Thanks

rahulCamp commented 6 years ago

https://github.com/moxystudio/redux-mock-store-await-actions This helps actually!