-
-
Notifications
You must be signed in to change notification settings - Fork 671
test(react/vanilla-utils/freezeAtom): add tests for null and primitive handling #3076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(react/vanilla-utils/freezeAtom): add tests for null and primitive handling #3076
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
|
Playground | Link |
---|---|
React demo | https://livecodes.io?x=id/2XR6ZK6EE |
See documentations for usage instructions.
commit: |
Co-authored-by: Daishi Kato <[email protected]>
return ( | ||
<> | ||
<button onClick={() => setValue(456)}>set number</button> | ||
<div>value is frozen: {`${Object.isFrozen(value)}`}</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what this is testing. Does this test pass even if we omit freezeAtom
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback. I missed the fact that freeze doesn’t really apply to primitives. I’ll revise the test to make its purpose clearer.
3b09c55
to
209155f
Compare
await userEvent.click(screen.getByText('set null')) | ||
|
||
await screen.findByText('value is null: true') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we set something not null too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we set something not null too?
I think this should be good enough as is, but feel free to let me know if you think anything else should be added!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
L63 just passes if L61 is comment'd out, which doesn't feel like a good test. If you don't have any idea, remove L61-63.
const numberAtom = atom(123, (_get, set, _arg: number) => { | ||
set(numberAtom, 456) | ||
}) | ||
|
||
const Component = () => { | ||
const [value, setValue] = useAtom(freezeAtom(numberAtom)) | ||
return ( | ||
<> | ||
<button onClick={() => setValue(456)}>set number</button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const numberAtom = atom(123, (_get, set, _arg: number) => { | |
set(numberAtom, 456) | |
}) | |
const Component = () => { | |
const [value, setValue] = useAtom(freezeAtom(numberAtom)) | |
return ( | |
<> | |
<button onClick={() => setValue(456)}>set number</button> | |
const numberAtom = atom(123, (_get, set, _ignored?) => { | |
set(numberAtom, 456) | |
}) | |
const Component = () => { | |
const [value, setValue] = useAtom(freezeAtom(numberAtom)) | |
return ( | |
<> | |
<button onClick={setValue}>set number</button> |
How about aligning the format with the existing test code for consistency?
Co-authored-by: Daishi Kato <[email protected]>
16b8eef
to
eedbd42
Compare
Let's fix the existing tests: diff --git a/tests/react/vanilla-utils/freezeAtom.test.tsx b/tests/react/vanilla-utils/freezeAtom.test.tsx
index ca135bf..11169bb 100644
--- a/tests/react/vanilla-utils/freezeAtom.test.tsx
+++ b/tests/react/vanilla-utils/freezeAtom.test.tsx
@@ -7,17 +7,16 @@ import { atom } from 'jotai/vanilla'
import { freezeAtom, freezeAtomCreator } from 'jotai/vanilla/utils'
it('freezeAtom basic test', async () => {
- const objAtom = atom({ deep: {} }, (_get, set, _ignored?) => {
- set(objAtom, { deep: {} })
- })
+ const objAtom = atom({ deep: { count: 0 } })
const Component = () => {
const [obj, setObj] = useAtom(freezeAtom(objAtom))
return (
<>
- <button onClick={setObj}>change</button>
+ <button onClick={() => setObj({ deep: { count: 1 } })}>change</button>
<div>
- isFrozen: {`${Object.isFrozen(obj) && Object.isFrozen(obj.deep)}`}
+ count: {obj.deep.count}, isFrozen:{' '}
+ {`${Object.isFrozen(obj) && Object.isFrozen(obj.deep)}`}
</div>
</>
)
@@ -29,10 +28,10 @@ it('freezeAtom basic test', async () => {
</StrictMode>,
)
- await screen.findByText('isFrozen: true')
+ await screen.findByText('count: 0, isFrozen: true')
await userEvent.click(screen.getByText('change'))
- await screen.findByText('isFrozen: true')
+ await screen.findByText('count: 1, isFrozen: true')
})
describe('freezeAtomCreator', () => { |
afde2e2
to
dba579e
Compare
I reflected it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Related Bug Reports or Discussions
Fixes #
Summary
before
after
Check List
pnpm run fix
for formatting and linting code and docs