deno.land / x / jotai@v1.8.4 / tests / scope.test.tsx

scope.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { StrictMode, useState } from 'react'import { fireEvent, render, waitFor } from '@testing-library/react'import { atom, useAtom } from 'jotai'import { getTestProvider } from './testUtils'
const Provider = getTestProvider()
it('simple scoped provider with scoped atom', async () => { const scope = Symbol() const countAtom = atom(0)
const Display = () => { const [count, setCount] = useAtom(countAtom, scope)
return ( <> <p>count: {count}</p> <button onClick={() => setCount((c) => c + 1)}>dispatch</button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider scope={scope}> <Display /> </Provider> </StrictMode> ) await findByText('count: 0') fireEvent.click(getByText('dispatch')) await findByText('count: 1')})
it('default provider and atom with scoped provider and scoped atom', async () => { const scope = Symbol()
const scopedCountAtom = atom(0) const countAtom = atom(0)
const Display = () => { const [scopedCount, setScopedCount] = useAtom(scopedCountAtom, scope) const [count, setCount] = useAtom(countAtom)
return ( <> <p>scopedCount: {scopedCount}</p> <p>count: {count}</p> <button onClick={() => { setScopedCount((c) => c + 1) setCount((c) => c + 1) }}> dispatch </button> </> ) }
const { getByText } = render( <StrictMode> <Provider> <Provider scope={scope}> <Display /> </Provider> </Provider> </StrictMode> ) await waitFor(() => { getByText('count: 0') getByText('scopedCount: 0') }) fireEvent.click(getByText('dispatch')) await waitFor(() => { getByText('count: 1') getByText('scopedCount: 1') })})
it('keeps scoped atom value when default provider is removed', async () => { const scope = Symbol()
const scopedCountAtom = atom(0)
const Display = () => { const [scopedCount, setScopedCount] = useAtom(scopedCountAtom, scope)
return ( <> <p>scopedCount: {scopedCount}</p> <button onClick={() => { setScopedCount((c) => c + 1) }}> dispatch </button> </> ) }
const App = () => { const [hide, setHide] = useState(false) if (hide) { return ( <Provider scope={scope}> <Display /> </Provider> ) } return ( <Provider scope={scope}> <Provider> <button onClick={() => setHide(true)}>hide</button> <Display /> </Provider> </Provider> ) }
const { getByText, findByText } = render( <StrictMode> <App /> </StrictMode> ) await findByText('scopedCount: 0') fireEvent.click(getByText('dispatch')) await findByText('scopedCount: 1') fireEvent.click(getByText('hide')) await findByText('scopedCount: 1')})
it('two different scoped providers and scoped atoms', async () => { const scope = Symbol() const secondScope = Symbol()
const scopedCountAtom = atom(0) const secondScopedCountAtom = atom(10)
const Display = () => { const [scopedCount, setScopedCount] = useAtom(scopedCountAtom, scope) const [secondScopedCount, setSecondScopedCount] = useAtom( secondScopedCountAtom, secondScope )
return ( <> <p>scopedCount: {scopedCount}</p> <p>secondScopedCount: {secondScopedCount}</p> <button onClick={() => { setScopedCount((c) => c + 1) setSecondScopedCount((c) => c + 2) }}> dispatch </button> </> ) }
const { getByText } = render( <StrictMode> <Provider scope={scope}> <Provider scope={secondScope}> <Display /> </Provider> </Provider> </StrictMode> ) await waitFor(() => { getByText('scopedCount: 0') getByText('secondScopedCount: 10') }) fireEvent.click(getByText('dispatch')) await waitFor(() => { getByText('scopedCount: 1') getByText('secondScopedCount: 12') })})
jotai

Version Info

Tagged at
a year ago