@@ -7,7 +7,8 @@ import * as rtl from 'react-testing-library'
77import {
88 Provider as ProviderMock ,
99 useSelector ,
10- shallowEqual
10+ shallowEqual ,
11+ connect
1112} from '../../src/index.js'
1213import { useReduxContext } from '../../src/hooks/useReduxContext'
1314
@@ -302,6 +303,78 @@ describe('React', () => {
302303
303304 spy . mockRestore ( )
304305 } )
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+ } )
305378 } )
306379
307380 describe ( 'error handling for invalid arguments' , ( ) => {
0 commit comments