deno.land / x / jotai@v1.8.4 / tests / optics / focus-traversal.test.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142import { 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}]')})
Version Info