Skip to main content
Module

x/jotai/tests/utils/useResetAtom.test.tsx

👻 Primitive and flexible state management for React
Go to Latest
File
import { fireEvent, render } from '@testing-library/react'import { atom, useAtom } from 'jotai'import { RESET, atomWithReducer, atomWithReset, useResetAtom,} from 'jotai/utils'import { getTestProvider } from '../testUtils'
const Provider = getTestProvider()
it('atomWithReset resets to its first value', async () => { const countAtom = atomWithReset(0)
const Parent = () => { const [count, setValue] = useAtom(countAtom) const resetAtom = useResetAtom(countAtom) return ( <> <div>count: {count}</div> <button onClick={resetAtom}>reset</button> <button onClick={() => setValue((oldValue) => oldValue + 1)}> increment </button> <button onClick={() => setValue(10)}>set to 10</button> </> ) }
const { findByText, getByText } = render( <Provider> <Parent /> </Provider> )
await findByText('count: 0')
fireEvent.click(getByText('increment')) await findByText('count: 1') fireEvent.click(getByText('increment')) await findByText('count: 2') fireEvent.click(getByText('increment')) await findByText('count: 3')
fireEvent.click(getByText('reset')) await findByText('count: 0')
fireEvent.click(getByText('set to 10')) await findByText('count: 10')
fireEvent.click(getByText('increment')) await findByText('count: 11') fireEvent.click(getByText('increment')) await findByText('count: 12') fireEvent.click(getByText('increment')) await findByText('count: 13')})
it('atomWithReset through read-write atom', async () => { const primitiveAtom = atomWithReset(0) const countAtom = atom( (get) => get(primitiveAtom), (_get, set, newValue: number | typeof RESET) => set(primitiveAtom, newValue) )
const Parent = () => { const [count, setValue] = useAtom(countAtom) const resetAtom = useResetAtom(countAtom) return ( <> <div>count: {count}</div> <button onClick={resetAtom}>reset</button> <button onClick={() => setValue(10)}>set to 10</button> </> ) }
const { findByText, getByText } = render( <Provider> <Parent /> </Provider> )
await findByText('count: 0')
fireEvent.click(getByText('set to 10')) await findByText('count: 10')
fireEvent.click(getByText('reset')) await findByText('count: 0')})
it('useResetAtom with custom atom', async () => { const reducer = (state: number, action: 'INCREASE' | typeof RESET) => { switch (action) { case 'INCREASE': return state + 1 case RESET: return 0 } }
const countAtom = atomWithReducer(0, reducer)
const Parent = () => { const [count, dispatch] = useAtom(countAtom) const resetAtom = useResetAtom(countAtom) return ( <> <div>count: {count}</div> <button onClick={resetAtom}>reset</button> <button onClick={() => dispatch('INCREASE')}>increment</button> </> ) }
const { findByText, getByText } = render( <Provider> <Parent /> </Provider> )
await findByText('count: 0')
fireEvent.click(getByText('increment')) await findByText('count: 1') fireEvent.click(getByText('increment')) await findByText('count: 2') fireEvent.click(getByText('increment')) await findByText('count: 3')
fireEvent.click(getByText('reset')) await findByText('count: 0')})
it('useResetAtom with scope', async () => { const scope = Symbol() const countAtom = atomWithReset(0)
const Parent = () => { const [count, setValue] = useAtom(countAtom, scope) const resetAtom = useResetAtom(countAtom, scope) return ( <> <div>count: {count}</div> <button onClick={resetAtom}>reset</button> <button onClick={() => setValue((oldValue) => oldValue + 1)}> increment </button> </> ) }
const { findByText, getByText } = render( <Provider scope={scope}> <Parent /> </Provider> )
await findByText('count: 0')
fireEvent.click(getByText('increment')) await findByText('count: 1')
fireEvent.click(getByText('reset')) await findByText('count: 0')})