question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How do you unit test Jotai atoms?

See original GitHub issue

Considering 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
Noitidartcommented, Jan 22, 2021

Yes totally I’ll write a test for the above and share here and add it into docs if everyone likes it.

2reactions
dai-shicommented, Jan 10, 2021

It seems to fit all of our needs perfectly, but I haven’t been able to find any docs on testing Jotai atoms.

It would be really nice to have docs/testing.md for sure.

Since Jotai only functions inside “React” do I have to write a dummy component with a dummy button to test my state management atoms?

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.

Also interested in how I would test components that use useAtom.

Not sure what you mean by this. If you are testing a component behavior, you don’t need to mock useAtom. 🤔

it would be very helpful to have a guide/docs on the recommended way to do this.

Yeah. I’m not super experienced on testing best practices. Hope someone to volunteer. Otherwise, I’d try my best…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found