redux-saga-test-plan icon indicating copy to clipboard operation
redux-saga-test-plan copied to clipboard

Passing store to expectSaga

Open stinodes opened this issue 7 years ago • 6 comments

Is the remainder of #52 (passing in a (mock)store) still planned?
I have found a case in my app where it would seem useful to either be able to pass in a mock-store or to access a mock-store from the expectSaga-api (sort-of like the io-object that lives somewhere there).

If adding store is still planned, but on hold or something, maybe I could give it a shot? 😄

Cheers

stinodes avatar Jan 22 '18 10:01 stinodes

Hi, @stinodes. I had put integration with a real store on hold because it would likely need some additional code in your main store creation to ensure redux-saga-test-plan can integrate with the store. I didn't like that idea, but it might be the only way around it. Because expectSaga builds in its own dispatch functionality, we would need a middleware/store enhancer to wrap over the store's dispatch. These are my rough thoughts coming back to me because I've lost context since I last explored this. That being said, you're welcome to explore as well. If you think of something please share so we can discuss before opening a PR. I just want to make sure whatever implementation doesn't require users to change code in lots of places or anything.

jfairbank avatar Feb 06 '18 13:02 jfairbank

Thanks for the reply!

What you said about it requiring some middleware/enhancer does seem to be the case on second thought, I only realise that now.

I did play around with the code a little bit, and got the (simple) withReducer tests green with a withStore-function, but a dispatch-call directly on the store would not have the expected behavior. As such, full integration tests would probably not succeed...
Right now it does indeed look like users would have to modify their app code to get this to work properly.

I'll give it some more thought and maybe play around with the code a little bit when I have the time myself.

stinodes avatar Feb 06 '18 14:02 stinodes

@stinodes I am having the same need, that is, a global store with namespaced stores across different reducers. In the end, did you succeed testing such use cases?

nick13jaremek avatar Jul 04 '19 10:07 nick13jaremek

You should be able to use redux' combineReducers function to combine the reducers you depend on and have them in your saga test.

I don't quite remember what I did here since it's been a while and don't use redux as much anymore, quite honestly.

On Thu, 4 Jul 2019, 12:59 Nick Jaremek, [email protected] wrote:

@stinodes https://github.com/stinodes I am having the same need, that is, a global store with namespaced stores across different reducers. In the end, did you succeed testing such use cases?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jfairbank/redux-saga-test-plan/issues/167?email_source=notifications&email_token=AEFKF35EHVBGFCF5WZOMVRLP5XJX3A5CNFSM4EMZVXPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZHDBKQ#issuecomment-508440746, or mute the thread https://github.com/notifications/unsubscribe-auth/AEFKF36XX6SPOOMSK56ARLTP5XJX3ANCNFSM4EMZVXPA .

stinodes avatar Jul 04 '19 13:07 stinodes

@stinodes Thank you for your reply! I managed to use the combineReducers function from the redux package.

Basically, for the expectSaga test I had to do the following:

const reducer = combinReducers({ auth: authReducer, cart: cartReducer });
await expectSaga(someSaga)
  .withReducer(reducer)
  .withState({
    auth: {
      /* Auth store */
    },
    cart: {
      /* Cart store */
    })
  })
  .dispatch({ type: 'ACTION_TRIGGERING_SAGA', data: { some: 'data' } })
  .run();

It comes down to passing the combined reducer as the reducer to be used in the expectSaga test.

This way it is possible to test sagas that tackle multiple stores without having to use hacks to force Redux selectors to work.

Thanks @jfairbank and all the contributors for this awesome library! It makes testing redux sagas a breeze 😄

nick13jaremek avatar Jul 10 '19 15:07 nick13jaremek

@nick13jaremek jep, that's exactly how I did it as well, and really all you need.

stinodes avatar Jul 22 '19 11:07 stinodes