Skip to content

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

Merged

Conversation

sukvvon
Copy link
Contributor

@sukvvon sukvvon commented May 23, 2025

Related Bug Reports or Discussions

Fixes #

Summary

before

image

after

image

Check List

  • pnpm run fix for formatting and linting code and docs

Copy link

vercel bot commented May 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 25, 2025 6:40am

Copy link

codesandbox-ci bot commented May 23, 2025

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.

Copy link

github-actions bot commented May 23, 2025

LiveCodes Preview in LiveCodes

Latest commit: dba579e
Last updated: May 25, 2025 6:39am (UTC)

Playground Link
React demo https://livecodes.io?x=id/2XR6ZK6EE

See documentations for usage instructions.

Copy link

pkg-pr-new bot commented May 23, 2025

More templates

npm i https://pkg.pr.new/jotai@3076

commit: dba579e

@sukvvon sukvvon requested a review from dai-shi May 23, 2025 12:17
return (
<>
<button onClick={() => setValue(456)}>set number</button>
<div>value is frozen: {`${Object.isFrozen(value)}`}</div>
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai-shi

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.

@sukvvon sukvvon force-pushed the test/react-vanilla-utils-freezeAtom-null-primitive branch from 3b09c55 to 209155f Compare May 23, 2025 16:32
Comment on lines 61 to 63
await userEvent.click(screen.getByText('set null'))

await screen.findByText('value is null: true')
Copy link
Member

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?

Copy link
Contributor Author

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?

@dai-shi

I think this should be good enough as is, but feel free to let me know if you think anything else should be added!

Copy link
Member

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.

@sukvvon sukvvon requested a review from dai-shi May 24, 2025 08:21
Comment on lines 63 to 71
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>
Copy link
Contributor Author

@sukvvon sukvvon May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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>

@dai-shi

How about aligning the format with the existing test code for consistency?

@sukvvon sukvvon force-pushed the test/react-vanilla-utils-freezeAtom-null-primitive branch from 16b8eef to eedbd42 Compare May 25, 2025 00:52
@dai-shi
Copy link
Member

dai-shi commented May 25, 2025

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', () => {

@sukvvon sukvvon force-pushed the test/react-vanilla-utils-freezeAtom-null-primitive branch from afde2e2 to dba579e Compare May 25, 2025 06:39
@sukvvon
Copy link
Contributor Author

sukvvon commented May 25, 2025

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', () => {

@dai-shi

I reflected it.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dai-shi dai-shi merged commit 6327521 into pmndrs:main May 25, 2025
45 checks passed
@sukvvon sukvvon deleted the test/react-vanilla-utils-freezeAtom-null-primitive branch May 25, 2025 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants