@@ -172,26 +172,46 @@ describe('React', () => {
172
172
} )
173
173
174
174
it ( 'notices store updates between render and store subscription effect' , ( ) => {
175
+ const Child = ( { count } : { count : number } ) => {
176
+ // console.log('Child rendering')
177
+ useLayoutEffect ( ( ) => {
178
+ // console.log('Child layoutEffect: ', count)
179
+ if ( count === 0 ) {
180
+ // console.log('Dispatching store update')
181
+ normalStore . dispatch ( { type : '' } )
182
+ }
183
+ } , [ count ] )
184
+ return null
185
+ }
175
186
const Comp = ( ) => {
187
+ // console.log('Parent rendering, selecting state')
176
188
const count = useNormalSelector ( ( s ) => s . count )
177
- renderedItems . push ( count )
178
189
179
- // I don't know a better way to trigger a store update before the
180
- // store subscription effect happens
181
- if ( count === 0 ) {
182
- normalStore . dispatch ( { type : '' } )
183
- }
190
+ useLayoutEffect ( ( ) => {
191
+ // console.log('Parent layoutEffect: ', count)
192
+ renderedItems . push ( count )
193
+ } )
184
194
185
- return < div > { count } </ div >
195
+ return (
196
+ < div >
197
+ { count }
198
+ < Child count = { count } />
199
+ </ div >
200
+ )
186
201
}
187
202
203
+ // console.log('Starting initial render')
188
204
rtl . render (
189
205
< ProviderMock store = { normalStore } >
190
206
< Comp />
191
207
</ ProviderMock >
192
208
)
193
209
194
- expect ( renderedItems ) . toEqual ( [ 0 , 1 ] )
210
+ // With `useSyncExternalStore`, we get three renders of `<Comp>`:
211
+ // 1) Initial render, count is 0
212
+ // 2) Render due to dispatch, still sync in the initial render's commit phase
213
+ // TODO 3) ??
214
+ expect ( renderedItems ) . toEqual ( [ 0 , 1 , 1 ] )
195
215
} )
196
216
} )
197
217
@@ -358,7 +378,11 @@ describe('React', () => {
358
378
359
379
const Comp = ( ) => {
360
380
const value = useSelector ( selector )
361
- renderedItems . push ( value )
381
+
382
+ useLayoutEffect ( ( ) => {
383
+ renderedItems . push ( value )
384
+ } )
385
+
362
386
return (
363
387
< div >
364
388
< Child />
@@ -374,7 +398,9 @@ describe('React', () => {
374
398
375
399
// Selector first called on Comp mount, and then re-invoked after mount due to useLayoutEffect dispatching event
376
400
expect ( numCalls ) . toBe ( 2 )
377
- expect ( renderedItems . length ) . toEqual ( 2 )
401
+ // TODO As with "notice store updates" above, we're now getting _3_ renders here
402
+ // expect(renderedItems.length).toEqual(2)
403
+ expect ( renderedItems . length ) . toEqual ( 3 )
378
404
} )
379
405
} )
380
406
@@ -455,7 +481,8 @@ describe('React', () => {
455
481
const Comp = ( ) => {
456
482
const result = useSelector ( ( count : number ) => {
457
483
if ( count > 0 ) {
458
- throw new Error ( 'foo' )
484
+ // console.log('Throwing error')
485
+ throw new Error ( 'Panic!' )
459
486
}
460
487
461
488
return count
@@ -474,11 +501,13 @@ describe('React', () => {
474
501
475
502
rtl . render ( < App /> )
476
503
504
+ // TODO We can no longer catch errors in selectors after dispatch ourselves, as `uSES` swallows them.
505
+ // The test selector will happen to re-throw while rendering and we do see that.
477
506
expect ( ( ) => {
478
507
act ( ( ) => {
479
508
store . dispatch ( { type : '' } )
480
509
} )
481
- } ) . toThrow ( / T h e e r r o r m a y b e c o r r e l a t e d / )
510
+ } ) . toThrow ( / m a y b e c o r r e l a t e d w i t h / )
482
511
483
512
spy . mockRestore ( )
484
513
} )
@@ -571,7 +600,9 @@ describe('React', () => {
571
600
// triggers render on store change
572
601
useNormalSelector ( ( s ) => s . count )
573
602
const array = useSelector ( ( ) => [ 1 , 2 , 3 ] , alwaysEqual )
574
- renderedItems . push ( array )
603
+ useLayoutEffect ( ( ) => {
604
+ renderedItems . push ( array )
605
+ } )
575
606
return < div />
576
607
}
577
608
@@ -588,6 +619,7 @@ describe('React', () => {
588
619
} )
589
620
590
621
expect ( renderedItems . length ) . toBe ( 2 )
622
+ // TODO This is failing, and correctly so. `uSES` drops the memoized value if it gets a new selector.
591
623
expect ( renderedItems [ 0 ] ) . toBe ( renderedItems [ 1 ] )
592
624
} )
593
625
} )
0 commit comments