-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vanilla): setters do not freeze new values (#2476)
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
- Loading branch information
1 parent
2ca8339
commit 93a28f4
Showing
2 changed files
with
74 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,69 @@ | ||
import { StrictMode } from 'react' | ||
import { render } from '@testing-library/react' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import { it } from 'vitest' | ||
import { useAtom } from 'jotai/react' | ||
import { atom } from 'jotai/vanilla' | ||
import { freezeAtom, freezeAtomCreator } from 'jotai/vanilla/utils' | ||
|
||
it('freezeAtom basic test', async () => { | ||
const objAtom = atom({ count: 0 }) | ||
const objAtom = atom({ deep: {} }, (_get, set, _ignored?) => { | ||
set(objAtom, { deep: {} }) | ||
}) | ||
|
||
const Component = () => { | ||
const [obj] = useAtom(freezeAtom(objAtom)) | ||
const [obj, setObj] = useAtom(freezeAtom(objAtom)) | ||
|
||
return <div>isFrozen: {`${Object.isFrozen(obj)}`}</div> | ||
return ( | ||
<> | ||
<button onClick={setObj}>change</button> | ||
<div> | ||
isFrozen: {`${Object.isFrozen(obj) && Object.isFrozen(obj.deep)}`} | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
const { findByText } = render( | ||
const { getByText, findByText } = render( | ||
<StrictMode> | ||
<Component /> | ||
</StrictMode>, | ||
) | ||
|
||
await findByText('isFrozen: true') | ||
|
||
fireEvent.click(getByText('change')) | ||
|
||
await findByText('isFrozen: true') | ||
}) | ||
|
||
it('freezeAtomCreator basic test', async () => { | ||
const createFrozenAtom = freezeAtomCreator(atom) | ||
const objAtom = createFrozenAtom({ count: 0 }) | ||
const objAtom = createFrozenAtom({ deep: {} }, (_get, set, _ignored?) => { | ||
set(objAtom, { deep: {} }) | ||
}) | ||
|
||
const Component = () => { | ||
const [obj] = useAtom(objAtom) | ||
const [obj, setObj] = useAtom(objAtom) | ||
|
||
return <div>isFrozen: {`${Object.isFrozen(obj)}`}</div> | ||
return ( | ||
<> | ||
<button onClick={setObj}>change</button> | ||
<div> | ||
isFrozen: {`${Object.isFrozen(obj) && Object.isFrozen(obj.deep)}`} | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
const { findByText } = render( | ||
const { getByText, findByText } = render( | ||
<StrictMode> | ||
<Component /> | ||
</StrictMode>, | ||
) | ||
|
||
await findByText('isFrozen: true') | ||
|
||
fireEvent.click(getByText('change')) | ||
|
||
await findByText('isFrozen: true') | ||
}) |