ctrlplusb / easy-peasy

Vegetarian friendly state for React

Home Page:https://easy-peasy.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mocking thunk calls from another thunk

DanielPe05 opened this issue · comments

Testing a thunk that calls an action is very simple, using the mockActions helper, however, this becomes very difficult when testing that a thunk calls another thunk.
It is common practice for large stores to dispatch other thunks when something happens inside your thunk. This is possible using getStoreActions. When testing this behavior since mockActions doesn't prevent the call to the other thunk I'm having to include other store models to satisfy my test. This feels bad every time I run into it, like the boundaries of my test are not well defined. for example:

const { getActions, getMockedActions } = createStore(
  myModel,
  { mockActions: true }
)

await getActions().someThunk()

expect(getMockedActions()).toContainEqual({
  payload: undefined,
  type: '@thunk.someOtherModel.someOtherThunk(start)'
})

I would expect the above to pass but it fails because the model actually calls the thunk. You end up having to do something like this:

const { getActions, getState } = createStore(
  { myModel, someOtherModel }
)

await getActions().myModel.someThunk()

expect(getState().someOtherModel.expectedState).toEqual('...')

As more models depend on each other this becomes increasingly difficult to maintain. Not sure if there is a workaround for this in place or if there is a plan to support this in the future. I'd be happy to contribute something like this if I get some high-level direction on how to accomplish it.