Skip to main content
Module

x/jotai/tests/optics/focus-traversal.test.tsx

👻 Primitive and flexible state management for React
Go to Latest
File
import { StrictMode } from 'react'import { fireEvent, render } from '@testing-library/react'import * as O from 'optics-ts'import { atom, useAtom } from 'jotai'import { focusAtom } from 'jotai/optics'import { getTestProvider } from '../testUtils'
const Provider = getTestProvider()
it('updates traversals', async () => { const bigAtom = atom<{ a?: number }[]>([{ a: 5 }, {}, { a: 6 }]) const focusFunction = (optic: O.OpticFor<{ a?: number }[]>) => optic.elems().prop('a').optional()
const Counter = () => { const [count, setCount] = useAtom(focusAtom(bigAtom, focusFunction)) const [bigAtomValue] = useAtom(bigAtom) return ( <> <div>bigAtom: {JSON.stringify(bigAtomValue)}</div> <div>count: {count.join(',')}</div> <button onClick={() => setCount((c) => c + 1)}>button</button> </> ) }
const { getByText, findByText } = render( <StrictMode> <Provider> <Counter /> </Provider> </StrictMode> )
await findByText('count: 5,6') await findByText('bigAtom: [{"a":5},{},{"a":6}]')
fireEvent.click(getByText('button')) await findByText('count: 6,7') await findByText('bigAtom: [{"a":6},{},{"a":7}]')})