deno.land / x / jotai@v1.8.4 / tests / optics / focus-traversal.test.tsx

focus-traversal.test.tsx
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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}]')})
jotai

Version Info

Tagged at
a year ago