-
-
Notifications
You must be signed in to change notification settings - Fork 567
/
focus-prism.test.tsx
74 lines (63 loc) 路 1.97 KB
/
focus-prism.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 prisms', async () => {
const bigAtom = atom<{ a?: number }>({ a: 5 })
const focusFunction = (optic: O.OpticFor<{ a?: number }>) =>
optic.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}</div>
<button onClick={() => setCount((c) => c + 1)}>button</button>
</>
)
}
const { getByText, findByText } = render(
<StrictMode>
<Provider>
<Counter />
</Provider>
</StrictMode>
)
await findByText('count: 5')
await findByText('bigAtom: {"a":5}')
fireEvent.click(getByText('button'))
await findByText('count: 6')
await findByText('bigAtom: {"a":6}')
})
it('atoms that focus on no values are not updated', async () => {
const bigAtom = atom<{ a?: number }>({})
const focusFunction = (optic: O.OpticFor<{ a?: number }>) =>
optic.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: {JSON.stringify(count)}</div>
<button onClick={() => setCount((c) => c + 1)}>button</button>
</>
)
}
const { getByText, findByText } = render(
<StrictMode>
<Provider>
<Counter />
</Provider>
</StrictMode>
)
await findByText('count:')
await findByText('bigAtom: {}')
fireEvent.click(getByText('button'))
await findByText('count:')
await findByText('bigAtom: {}')
})