11import { afterAll , beforeAll , describe , expect , it , vi } from "vitest" ;
2- import { TimeoutManager } from "../../../src/common/timeoutManager .js" ;
2+ import { setManagedTimeout } from "../../../src/common/managedTimeout .js" ;
33
4- describe ( "TimeoutManager " , ( ) => {
4+ describe ( "setManagedTimeout " , ( ) => {
55 beforeAll ( ( ) => {
66 vi . useFakeTimers ( ) ;
77 } ) ;
@@ -13,7 +13,7 @@ describe("TimeoutManager", () => {
1313 it ( "calls the timeout callback" , ( ) => {
1414 const callback = vi . fn ( ) ;
1515
16- new TimeoutManager ( callback , 1000 ) ;
16+ setManagedTimeout ( callback , 1000 ) ;
1717
1818 vi . advanceTimersByTime ( 1000 ) ;
1919 expect ( callback ) . toHaveBeenCalled ( ) ;
@@ -22,10 +22,10 @@ describe("TimeoutManager", () => {
2222 it ( "does not call the timeout callback if the timeout is cleared" , ( ) => {
2323 const callback = vi . fn ( ) ;
2424
25- const timeoutManager = new TimeoutManager ( callback , 1000 ) ;
25+ const timeout = setManagedTimeout ( callback , 1000 ) ;
2626
2727 vi . advanceTimersByTime ( 500 ) ;
28- timeoutManager . clear ( ) ;
28+ timeout . cancel ( ) ;
2929 vi . advanceTimersByTime ( 500 ) ;
3030
3131 expect ( callback ) . not . toHaveBeenCalled ( ) ;
@@ -34,44 +34,32 @@ describe("TimeoutManager", () => {
3434 it ( "does not call the timeout callback if the timeout is reset" , ( ) => {
3535 const callback = vi . fn ( ) ;
3636
37- const timeoutManager = new TimeoutManager ( callback , 1000 ) ;
37+ const timeout = setManagedTimeout ( callback , 1000 ) ;
3838
3939 vi . advanceTimersByTime ( 500 ) ;
40- timeoutManager . reset ( ) ;
40+ timeout . restart ( ) ;
4141 vi . advanceTimersByTime ( 500 ) ;
4242 expect ( callback ) . not . toHaveBeenCalled ( ) ;
4343 } ) ;
4444
45- it ( "calls the onerror callback" , ( ) => {
46- const onerrorCallback = vi . fn ( ) ;
47-
48- const tm = new TimeoutManager ( ( ) => {
49- throw new Error ( "test" ) ;
50- } , 1000 ) ;
51- tm . onerror = onerrorCallback ;
52-
53- vi . advanceTimersByTime ( 1000 ) ;
54- expect ( onerrorCallback ) . toHaveBeenCalled ( ) ;
55- } ) ;
56-
5745 describe ( "if timeout is reset" , ( ) => {
5846 it ( "does not call the timeout callback within the timeout period" , ( ) => {
5947 const callback = vi . fn ( ) ;
6048
61- const timeoutManager = new TimeoutManager ( callback , 1000 ) ;
49+ const timeout = setManagedTimeout ( callback , 1000 ) ;
6250
6351 vi . advanceTimersByTime ( 500 ) ;
64- timeoutManager . reset ( ) ;
52+ timeout . restart ( ) ;
6553 vi . advanceTimersByTime ( 500 ) ;
6654 expect ( callback ) . not . toHaveBeenCalled ( ) ;
6755 } ) ;
6856 it ( "calls the timeout callback after the timeout period" , ( ) => {
6957 const callback = vi . fn ( ) ;
7058
71- const timeoutManager = new TimeoutManager ( callback , 1000 ) ;
59+ const timeout = setManagedTimeout ( callback , 1000 ) ;
7260
7361 vi . advanceTimersByTime ( 500 ) ;
74- timeoutManager . reset ( ) ;
62+ timeout . restart ( ) ;
7563 vi . advanceTimersByTime ( 1000 ) ;
7664 expect ( callback ) . toHaveBeenCalled ( ) ;
7765 } ) ;
0 commit comments