Effector: how to reset all domain stores before each test?

639 Views Asked by At

I want to reset all domain stores before each test case. Is there some way to do it with Effector?

1

There are 1 best solutions below

0
Alexander Khoroshikh On

There is no such API in effector. You can create separate event and subscribe every store to it:

const resetForm = createEvent()

formDomain.onCreateStore(store => store.reset(resetForm))

But in general you shouldn't manually reset stores in tests. Prefer Fork API usage instead

https://effector.dev/docs/api/effector/fork - docs

https://dev.to/effector/the-best-part-of-effector-4c27 - article

Example:

test('stuff', async () => {
  // create new forked scope, which is completly independent
  const scope = fork({ 
    // apply modifications like initial store values in this scope
    values: [[$myStore, "value"], [$myOtherStore, 0]], // changed value in $myStore specifically for this scope
    handlers: [[myFx, mockHandler)]] // changed effect handler to mock one for this scope
  });
 
   // launching event or effect, which triggers the logic we want to test
   // we doing it just in our forked scope
  await allSettled(startEvent, {
   scope,
   params: // params of startEvent
  })
 
  // check states of stores in this scope after all calculations ended
  expect(scope.getState($myStore)).toEqual(...)
})