File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -279,6 +279,46 @@ describe('React', () => {
279
279
expect ( numCalls ) . toBe ( 2 )
280
280
expect ( renderedItems . length ) . toEqual ( 2 )
281
281
} )
282
+
283
+ it ( 'calls selector twice once on mount when state changes during render' , ( ) => {
284
+ store = createStore ( ( { count } = { count : 0 } ) => ( {
285
+ count : count + 1 ,
286
+ } ) )
287
+
288
+ let numCalls = 0
289
+ const selector = ( s ) => {
290
+ numCalls += 1
291
+ return s . count
292
+ }
293
+ const renderedItems = [ ]
294
+
295
+ const Child = ( ) => {
296
+ useLayoutEffect ( ( ) => {
297
+ store . dispatch ( { type : '' , count : 1 } )
298
+ } , [ ] )
299
+ return < div />
300
+ }
301
+
302
+ const Comp = ( ) => {
303
+ const value = useSelector ( selector )
304
+ renderedItems . push ( value )
305
+ return (
306
+ < div >
307
+ < Child />
308
+ </ div >
309
+ )
310
+ }
311
+
312
+ rtl . render (
313
+ < ProviderMock store = { store } >
314
+ < Comp />
315
+ </ ProviderMock >
316
+ )
317
+
318
+ // Selector first called on Comp mount, and then re-invoked after mount due to useLayoutEffect dispatching event
319
+ expect ( numCalls ) . toBe ( 2 )
320
+ expect ( renderedItems . length ) . toEqual ( 2 )
321
+ } )
282
322
} )
283
323
284
324
it ( 'uses the latest selector' , ( ) => {
You can’t perform that action at this time.
0 commit comments