@@ -65,7 +65,7 @@ describe('useDebounce', () => {
6565
6666 // timeout shouldn't have been called yet after leading call was executed
6767 // @ts -ignore
68- expect ( screen . getByRole ( 'test' ) ) . toHaveTextContent ( 'Hello world ' ) ;
68+ expect ( screen . getByRole ( 'test' ) ) . toHaveTextContent ( 'Hello again ' ) ;
6969
7070 act ( ( ) => {
7171 jest . runAllTimers ( ) ;
@@ -283,13 +283,13 @@ describe('useDebounce', () => {
283283
284284 const tree = render ( < Component text = { 'Hello' } /> ) ;
285285
286- expect ( eq ) . toHaveBeenCalledTimes ( 1 ) ;
286+ expect ( eq ) . toHaveBeenCalledTimes ( 2 ) ;
287287
288288 act ( ( ) => {
289289 tree . rerender ( < Component text = "Test" /> ) ;
290290 } ) ;
291291
292- expect ( eq ) . toHaveBeenCalledTimes ( 2 ) ;
292+ expect ( eq ) . toHaveBeenCalledTimes ( 4 ) ;
293293 expect ( eq ) . toHaveBeenCalledWith ( 'Hello' , 'Test' ) ;
294294 // Since the equality function always returns true, expect the value to stay the same
295295 // @ts -ignore
@@ -414,4 +414,91 @@ describe('useDebounce', () => {
414414 // @ts -ignore
415415 expect ( screen . getByRole ( 'test' ) ) . toHaveTextContent ( 'Hello world' ) ;
416416 } ) ;
417+
418+
419+ it ( 'Handles isPending' , ( ) => {
420+ function Component ( { propValue} ) {
421+ const [ value , fns ] = useDebounce ( propValue , 1000 ) ;
422+ return (
423+ < div >
424+ < div role = "value" > { value } </ div >
425+ < div role = "pending" > { fns . isPending ( ) . toString ( ) } </ div >
426+ </ div >
427+ ) ;
428+ }
429+
430+ const tree = render ( < Component propValue = { 'Hello' } /> ) ;
431+
432+ // check inited value
433+ // @ts -ignore
434+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello' ) ;
435+ // @ts -ignore
436+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'false' ) ;
437+
438+ act ( ( ) => {
439+ tree . rerender ( < Component propValue = { 'Hello 1' } /> ) ;
440+ } ) ;
441+ // timeout shouldn't have called yet
442+ // @ts -ignore
443+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello' ) ;
444+ // @ts -ignore
445+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'true' ) ;
446+
447+ act ( ( ) => {
448+ jest . runAllTimers ( ) ;
449+ } ) ;
450+ // after runAllTimer text should be updated
451+ // @ts -ignore
452+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello 1' ) ;
453+ // @ts -ignore
454+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'false' ) ;
455+ } )
456+
457+ it ( 'Should handle isPending state correctly while switching between bounced values' , ( ) => {
458+ function Component ( { propValue} ) {
459+ const [ value , fns ] = useDebounce ( propValue , 1000 ) ;
460+ return (
461+ < div >
462+ < div role = "value" > { value } </ div >
463+ < div role = "pending" > { fns . isPending ( ) . toString ( ) } </ div >
464+ </ div >
465+ ) ;
466+ }
467+
468+ const tree = render ( < Component propValue = { 'Hello' } /> ) ;
469+
470+ // check inited value
471+ // @ts -ignore
472+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello' ) ;
473+ // @ts -ignore
474+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'false' ) ;
475+
476+ act ( ( ) => {
477+ tree . rerender ( < Component propValue = { 'Hello 1' } /> ) ;
478+ } ) ;
479+ // timeout shouldn't have called yet
480+ // @ts -ignore
481+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello' ) ;
482+ // @ts -ignore
483+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'true' ) ;
484+
485+ act ( ( ) => {
486+ tree . rerender ( < Component propValue = { 'Hello' } /> ) ;
487+ } ) ;
488+
489+ // timeout shouldn't have called yet
490+ // @ts -ignore
491+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello' ) ;
492+ // @ts -ignore
493+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'false' ) ;
494+
495+ act ( ( ) => {
496+ jest . runAllTimers ( ) ;
497+ } ) ;
498+ // after runAllTimer text should be updated
499+ // @ts -ignore
500+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello' ) ;
501+ // @ts -ignore
502+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'false' ) ;
503+ } )
417504} ) ;
0 commit comments