@@ -29,10 +29,14 @@ describe('createAtomStore', () => {
29
29
arr : INITIAL_ARR ,
30
30
} ;
31
31
32
- const { useMyTestStoreStore, MyTestStoreProvider } = createAtomStore (
33
- initialTestStoreValue ,
34
- { name : 'myTestStore' as const }
35
- ) ;
32
+ const {
33
+ myTestStoreStore,
34
+ useMyTestStoreStore,
35
+ MyTestStoreProvider,
36
+ useMyTestStoreValue,
37
+ } = createAtomStore ( initialTestStoreValue , {
38
+ name : 'myTestStore' as const ,
39
+ } ) ;
36
40
37
41
let numRenderCount = 0 ;
38
42
const NumRenderer = ( ) => {
@@ -92,6 +96,24 @@ describe('createAtomStore', () => {
92
96
) ;
93
97
} ;
94
98
99
+ let arrNumRenderCountWithOneHook = 0 ;
100
+ const ArrNumRendererWithOneHook = ( ) => {
101
+ arrNumRenderCountWithOneHook += 1 ;
102
+ const num = useMyTestStoreValue ( 'num' ) ;
103
+ const arrNum = useMyTestStoreValue (
104
+ 'arr' ,
105
+ {
106
+ selector : ( v ) => v [ num ] ,
107
+ } ,
108
+ [ num ]
109
+ ) ;
110
+ return (
111
+ < div >
112
+ < div > arrNumWithOneHook: { arrNum } </ div >
113
+ </ div >
114
+ ) ;
115
+ } ;
116
+
95
117
let arrNumRenderWithDepsCount = 0 ;
96
118
const ArrNumRendererWithDeps = ( ) => {
97
119
arrNumRenderWithDepsCount += 1 ;
@@ -105,11 +127,31 @@ describe('createAtomStore', () => {
105
127
) ;
106
128
} ;
107
129
130
+ let arrNumRenderWithDepsAndAtomCount = 0 ;
131
+ const ArrNumRendererWithDepsAndAtom = ( ) => {
132
+ arrNumRenderWithDepsAndAtomCount += 1 ;
133
+ const store = useMyTestStoreStore ( ) ;
134
+ const numAtom = myTestStoreStore . atom . num ;
135
+ const num = store . useAtomValue ( numAtom ) ;
136
+ const arrAtom = myTestStoreStore . atom . arr ;
137
+ const arrNum = store . useAtomValue ( arrAtom , ( v ) => v [ num ] , [ num ] ) ;
138
+ return (
139
+ < div >
140
+ < div > arrNumWithDepsAndAtom: { arrNum } </ div >
141
+ </ div >
142
+ ) ;
143
+ } ;
144
+
108
145
const BadSelectorRenderer = ( ) => {
109
146
const arr0 = useMyTestStoreStore ( ) . useArrValue ( ( v ) => v [ 0 ] ) ;
110
147
return < div > { arr0 } </ div > ;
111
148
} ;
112
149
150
+ const BadSelectorRenderer2 = ( ) => {
151
+ const arr0 = useMyTestStoreValue ( 'arr' , { selector : ( v ) => v [ 0 ] } ) ;
152
+ return < div > { arr0 } </ div > ;
153
+ } ;
154
+
113
155
const Buttons = ( ) => {
114
156
const store = useMyTestStoreStore ( ) ;
115
157
return (
@@ -154,7 +196,9 @@ describe('createAtomStore', () => {
154
196
< Arr0Renderer />
155
197
< Arr1Renderer />
156
198
< ArrNumRenderer />
199
+ < ArrNumRendererWithOneHook />
157
200
< ArrNumRendererWithDeps />
201
+ < ArrNumRendererWithDepsAndAtom />
158
202
< Buttons />
159
203
</ MyTestStoreProvider >
160
204
) ;
@@ -166,7 +210,9 @@ describe('createAtomStore', () => {
166
210
expect ( arr0RenderCount ) . toBe ( 2 ) ;
167
211
expect ( arr1RenderCount ) . toBe ( 2 ) ;
168
212
expect ( arrNumRenderCount ) . toBe ( 2 ) ;
213
+ expect ( arrNumRenderCountWithOneHook ) . toBe ( 2 ) ;
169
214
expect ( arrNumRenderWithDepsCount ) . toBe ( 2 ) ;
215
+ expect ( arrNumRenderWithDepsAndAtomCount ) . toBe ( 2 ) ;
170
216
expect ( getByText ( 'arrNum: alice' ) ) . toBeInTheDocument ( ) ;
171
217
expect ( getByText ( 'arrNumWithDeps: alice' ) ) . toBeInTheDocument ( ) ;
172
218
@@ -177,7 +223,9 @@ describe('createAtomStore', () => {
177
223
expect ( arr0RenderCount ) . toBe ( 2 ) ;
178
224
expect ( arr1RenderCount ) . toBe ( 2 ) ;
179
225
expect ( arrNumRenderCount ) . toBe ( 5 ) ;
226
+ expect ( arrNumRenderCountWithOneHook ) . toBe ( 5 ) ;
180
227
expect ( arrNumRenderWithDepsCount ) . toBe ( 5 ) ;
228
+ expect ( arrNumRenderWithDepsAndAtomCount ) . toBe ( 5 ) ;
181
229
expect ( getByText ( 'arrNum: bob' ) ) . toBeInTheDocument ( ) ;
182
230
expect ( getByText ( 'arrNumWithDeps: bob' ) ) . toBeInTheDocument ( ) ;
183
231
@@ -188,7 +236,9 @@ describe('createAtomStore', () => {
188
236
expect ( arr0RenderCount ) . toBe ( 2 ) ;
189
237
expect ( arr1RenderCount ) . toBe ( 2 ) ;
190
238
expect ( arrNumRenderCount ) . toBe ( 5 ) ;
239
+ expect ( arrNumRenderCountWithOneHook ) . toBe ( 5 ) ;
191
240
expect ( arrNumRenderWithDepsCount ) . toBe ( 5 ) ;
241
+ expect ( arrNumRenderWithDepsAndAtomCount ) . toBe ( 5 ) ;
192
242
expect ( getByText ( 'arrNum: bob' ) ) . toBeInTheDocument ( ) ;
193
243
expect ( getByText ( 'arrNumWithDeps: bob' ) ) . toBeInTheDocument ( ) ;
194
244
@@ -199,7 +249,9 @@ describe('createAtomStore', () => {
199
249
expect ( arr0RenderCount ) . toBe ( 2 ) ;
200
250
expect ( arr1RenderCount ) . toBe ( 2 ) ;
201
251
expect ( arrNumRenderCount ) . toBe ( 5 ) ;
252
+ expect ( arrNumRenderCountWithOneHook ) . toBe ( 5 ) ;
202
253
expect ( arrNumRenderWithDepsCount ) . toBe ( 5 ) ;
254
+ expect ( arrNumRenderWithDepsAndAtomCount ) . toBe ( 5 ) ;
203
255
expect ( getByText ( 'arrNum: bob' ) ) . toBeInTheDocument ( ) ;
204
256
expect ( getByText ( 'arrNumWithDeps: bob' ) ) . toBeInTheDocument ( ) ;
205
257
@@ -210,7 +262,9 @@ describe('createAtomStore', () => {
210
262
expect ( arr0RenderCount ) . toBe ( 3 ) ;
211
263
expect ( arr1RenderCount ) . toBe ( 2 ) ;
212
264
expect ( arrNumRenderCount ) . toBe ( 8 ) ;
265
+ expect ( arrNumRenderCountWithOneHook ) . toBe ( 8 ) ;
213
266
expect ( arrNumRenderWithDepsCount ) . toBe ( 8 ) ;
267
+ expect ( arrNumRenderWithDepsAndAtomCount ) . toBe ( 8 ) ;
214
268
expect ( getByText ( 'arrNum: ava' ) ) . toBeInTheDocument ( ) ;
215
269
expect ( getByText ( 'arrNumWithDeps: ava' ) ) . toBeInTheDocument ( ) ;
216
270
} ) ;
@@ -224,6 +278,16 @@ describe('createAtomStore', () => {
224
278
)
225
279
) . toThrow ( ) ;
226
280
} ) ;
281
+
282
+ it ( 'Throw error is user does memoize selector 2' , ( ) => {
283
+ expect ( ( ) =>
284
+ render (
285
+ < MyTestStoreProvider >
286
+ < BadSelectorRenderer2 />
287
+ </ MyTestStoreProvider >
288
+ )
289
+ ) . toThrow ( ) ;
290
+ } ) ;
227
291
} ) ;
228
292
229
293
describe ( 'single provider' , ( ) => {
0 commit comments