[Question] How do you unit test Jotai atoms?
See original GitHub issueConsidering using Jotai for our production application.
It seems to fit all of our needs perfectly, but I haven’t been able to find any docs on testing Jotai atoms.
E.g. I created a simple themeAtom and setThemeAtom like this:
import { atom } from 'jotai'
import themes, { themeNameType } from 'src/lib/themes'
const DEFAULT_THEME = themes.light
export const themeAtom = atom(DEFAULT_THEME)
export const setThemeAtom = atom(
(get) => get(themeAtom),
(_, set, arg: themeNameType) => {
set(themeAtom, themes[arg])
})
How would I go about writing a unit test that makes sure when setTheme is called with a valid theme name, that the themeAtom updates to the expected theme?
Since Jotai only functions inside “React” do I have to write a dummy component with a dummy button to test my state management atoms?
Also interested in how I would test components that use useAtom
. I’m guessing in this case I mock the useAtom, but it would be very helpful to have a guide/docs on the recommended way to do this.
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:14 (9 by maintainers)
Top Results From Across the Web
Testing — Jotai, primitive and flexible state management for ...
If you have complex atoms, sometimes you want to test them in isolation. For that, you can use React Hooks Testing Library. Here's...
Read more >Daishi Kato on Twitter: "@iorranmc @TkDodo Testing react ...
For testing atom definitions as pure functions, it's still under discussion: github.com. [Question] How do you unit test Jotai atoms?
Read more >jotai - testing with react-scripts - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >Jotai, a New Granular State Management Library for React
Jōtai provides a state management mechanism similar to Recoil's, with a smaller API surface (three functions) and code bundle (1.3 KB) vs.
Read more >Testing components that use the Jotai atomWithHash function
The atomWithHash utility from the excellent jotai library can be used to create atoms that are connected to the hash part of the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes totally I’ll write a test for the above and share here and add it into docs if everyone likes it.
It would be really nice to have
docs/testing.md
for sure.Guess so. Do some files in
tests
help?If you were just testing the functions for atom like
read
/write
, you could test it without React, because they are just pure functions. But, to test a component/hook behavior, we’d need React.Not sure what you mean by this. If you are testing a component behavior, you don’t need to mock
useAtom
. 🤔Yeah. I’m not super experienced on testing best practices. Hope someone to volunteer. Otherwise, I’d try my best…