@@ -13,12 +13,44 @@ describe(RecursiveTimeout, () => {
1313 [ 'clearInterval' , clearInterval ] ,
1414 [ 'clearTimeout' , clearTimeout ] ,
1515 ] ) ( 'should be cancellable with %s' , async ( clearName , clear ) => {
16- const timeout = createRecursiveTimeout ( callback , 100 )
16+ const recursive = createRecursiveTimeout ( callback , 100 )
1717
18- clear ( timeout )
18+ clear ( recursive )
1919
2020 await delay ( 150 )
2121
2222 expect ( callback ) . not . toHaveBeenCalled ( )
2323 } )
24+
25+ it ( 'should allow delaying the call by refreshing the timeout instance' , async ( ) => {
26+ const recursive = createRecursiveTimeout ( callback , 200 )
27+
28+ for ( let i = 0 ; i < 3 ; i += 1 ) {
29+ await delay ( 100 )
30+
31+ recursive . refresh ( )
32+ }
33+
34+ expect ( callback ) . not . toHaveBeenCalled ( )
35+
36+ await delay ( 200 + 10 )
37+
38+ clearTimeout ( recursive )
39+
40+ expect ( callback ) . toHaveBeenCalledTimes ( 1 )
41+ } )
42+
43+ it ( 'should show whether it is ref-ed or not' , ( ) => {
44+ const recursive = createRecursiveTimeout ( callback , 100 )
45+
46+ expect ( recursive . hasRef ( ) ) . toBe ( true )
47+
48+ for ( const [ methodName , hasRefExpected ] of [ [ 'unref' , false ] , [ 'ref' , true ] ] as const ) {
49+ recursive [ methodName ] ( )
50+
51+ expect ( recursive . hasRef ( ) ) . toBe ( hasRefExpected )
52+ }
53+
54+ recursive . unref ( ) // let it all go
55+ } )
2456} )
0 commit comments