jfairbank / redux-saga-test-plan

Test Redux Saga with an easy plan.
http://redux-saga-test-plan.jeremyfairbank.com
MIT License
1.25k stars 127 forks source link

Passing store to expectSaga #167

Open stinodes opened 6 years ago

stinodes commented 6 years ago

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

jfairbank commented 6 years ago

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.

stinodes commented 6 years ago

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.

nick13jaremek commented 5 years ago

@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?

stinodes commented 5 years ago

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, notifications@github.com 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 .

nick13jaremek commented 5 years ago

@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 😄

stinodes commented 5 years ago

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