@@ -7,7 +7,8 @@ import * as rtl from 'react-testing-library'
7
7
import {
8
8
Provider as ProviderMock ,
9
9
useSelector ,
10
- shallowEqual
10
+ shallowEqual ,
11
+ connect
11
12
} from '../../src/index.js'
12
13
import { useReduxContext } from '../../src/hooks/useReduxContext'
13
14
@@ -302,6 +303,78 @@ describe('React', () => {
302
303
303
304
spy . mockRestore ( )
304
305
} )
306
+
307
+ it ( 're-throws errors from the selector that only occur during rendering' , ( ) => {
308
+ const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
309
+
310
+ const Parent = ( ) => {
311
+ const count = useSelector ( s => s . count )
312
+ return < Child parentCount = { count } />
313
+ }
314
+
315
+ const Child = ( { parentCount } ) => {
316
+ const result = useSelector ( ( { count } ) => {
317
+ if ( parentCount > 0 ) {
318
+ throw new Error ( )
319
+ }
320
+
321
+ return count + parentCount
322
+ } )
323
+
324
+ return < div > { result } </ div >
325
+ }
326
+
327
+ rtl . render (
328
+ < ProviderMock store = { store } >
329
+ < Parent />
330
+ </ ProviderMock >
331
+ )
332
+
333
+ expect ( ( ) => store . dispatch ( { type : '' } ) ) . toThrowError ( )
334
+
335
+ spy . mockRestore ( )
336
+ } )
337
+
338
+ it ( 'allows dealing with stale props by putting a specific connected component above the hooks component' , ( ) => {
339
+ const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
340
+
341
+ const Parent = ( ) => {
342
+ const count = useSelector ( s => s . count )
343
+ return < ConnectedWrapper parentCount = { count } />
344
+ }
345
+
346
+ const ConnectedWrapper = connect ( ( { count } ) => ( { count } ) ) (
347
+ ( { parentCount } ) => {
348
+ return < Child parentCount = { parentCount } />
349
+ }
350
+ )
351
+
352
+ let sawInconsistentState = false
353
+
354
+ const Child = ( { parentCount } ) => {
355
+ const result = useSelector ( ( { count } ) => {
356
+ if ( count !== parentCount ) {
357
+ sawInconsistentState = true
358
+ }
359
+
360
+ return count + parentCount
361
+ } )
362
+
363
+ return < div > { result } </ div >
364
+ }
365
+
366
+ rtl . render (
367
+ < ProviderMock store = { store } >
368
+ < Parent />
369
+ </ ProviderMock >
370
+ )
371
+
372
+ store . dispatch ( { type : '' } )
373
+
374
+ expect ( sawInconsistentState ) . toBe ( false )
375
+
376
+ spy . mockRestore ( )
377
+ } )
305
378
} )
306
379
307
380
describe ( 'error handling for invalid arguments' , ( ) => {
0 commit comments