Test Usage example
Bwvolleyball opened this issue · 2 comments
The docs mention that the createFetcherStore
/ createMutatorStore
global config should help with test setup and mocking of data, but, there's no practical example of what this looks like.
Do you happen to have a small example that shows how to do this in a test?
@Bwvolleyball Hi there!
Honest answer: we do have examples, but they are temporary/0.1 version/we're not really sure if that's the best way forward.
It's very much related to #8 (initial states and SSR), and the real answer is that current way of doing tests/SSR/stories is painful.
The broad idea is this:
nanoquery
returns a third argument, which has a function (not part of the stable API) called__unsafeOverruleSettings
- essentially, this function overrules everything you provided for all fetcher stores, including the fetcher function itself
- so, basic guidance is this:
- call this function, provide it with a mocked version of the fetcher like this:
__unsafeOverruleSettings({fetcher: jest.fn().mockImplementation(() => ...)})
- reset it after each test like this:
__unsafeOverruleSettings()
- call this function, provide it with a mocked version of the fetcher like this:
Another way of doing the same is to use MSW, which is a much more robust and thoughtful way of testing network-related stuff, but that's out of the scope for nanoquery doc.
I'll share our vision for testing in near future.
We have a WIP PR for a new entity in the core of nanostores called Context. Context is essentially a place where all state for all atoms is stored. Once we land it in the main nanostores repo (I suspect it'll happen in August), I'll adapt nanoquery to this concept, and it'll simplify things a lot.
Using this new concept you'll be able to explicitly set a value for any atom by its reference for a certain isolated context (e.g., a single test unit!). I'll probably move fetcher functions inside atoms, which will give you a granular tool to change a fetcher function for a single fetcher/mutator store for a single test. That should probably be the better way that won't be nanoquery specific at all and will be the new way to test everything nanostores-related.
In the meantime—yeah, gotta use what we have.
Thank you, this is incredibly helpful, and I really appreciate the tips and tools you are able to document, and I'm excited to see the vision!