@@ -538,4 +538,50 @@ describe('useDebounce', () => {
538538 // @ts -ignore
539539 expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'false' ) ;
540540 } ) ;
541+
542+ it ( 'Updates isPending UI when cancel is called' , ( ) => {
543+ function Component ( { text } ) {
544+ const [ value , { cancel, isPending } ] = useDebounce ( text , 1000 ) ;
545+ return (
546+ < div >
547+ < div role = "value" > { value } </ div >
548+ < div role = "pending" > { isPending ( ) . toString ( ) } </ div >
549+ < button role = "cancel" onClick = { ( ) => cancel ( ) } >
550+ Cancel
551+ </ button >
552+ </ div >
553+ ) ;
554+ }
555+
556+ const tree = render ( < Component text = { 'Hello' } /> ) ;
557+
558+ // Initially, not pending
559+ // @ts -ignore
560+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello' ) ;
561+ // @ts -ignore
562+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'false' ) ;
563+
564+ // Update text to trigger debounce
565+ act ( ( ) => {
566+ tree . rerender ( < Component text = { 'World' } /> ) ;
567+ } ) ;
568+
569+ // Should be pending now
570+ // @ts -ignore
571+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello' ) ;
572+ // @ts -ignore
573+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'true' ) ;
574+
575+ // Click cancel button
576+ act ( ( ) => {
577+ // @ts -ignore
578+ screen . getByRole ( 'cancel' ) . click ( ) ;
579+ } ) ;
580+
581+ // After cancel, should not be pending and UI should update
582+ // @ts -ignore
583+ expect ( screen . getByRole ( 'value' ) ) . toHaveTextContent ( 'Hello' ) ;
584+ // @ts -ignore
585+ expect ( screen . getByRole ( 'pending' ) ) . toHaveTextContent ( 'false' ) ;
586+ } ) ;
541587} ) ;
0 commit comments