Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import {
3 TimeoutManager,
4 defaultTimeoutProvider,
5 systemSetTimeoutZero,
6 timeoutManager,
7} from '../timeoutManager'
8import type { MockInstance } from 'vitest'
9β
10describe('timeoutManager', () => {
11 function createMockProvider(name: string = 'custom') {
12 return {
13 __TEST_ONLY__name: name,
14 setTimeout: vi.fn(() => 123),
15 clearTimeout: vi.fn(),
16 setInterval: vi.fn(() => 456),
17 clearInterval: vi.fn(),
18 }
19 }
20β
21 let consoleErrorSpy: MockInstance<typeof console.error>
22β
23 beforeEach(() => {
24 consoleErrorSpy = vi.spyOn(console, 'error')
25 })
26β
27 afterEach(() => {
28 vi.restoreAllMocks()
29 })
30β
31 describe('TimeoutManager', () => {
32 let manager: TimeoutManager
33β
34 beforeEach(() => {
35 manager = new TimeoutManager()
36 })
37β
38 it('by default proxies calls to globalThis setTimeout/clearTimeout', () => {
39 const setTimeoutSpy = vi.spyOn(globalThis, 'setTimeout')
40 const clearTimeoutSpy = vi.spyOn(globalThis, 'clearTimeout')
41 const setIntervalSpy = vi.spyOn(globalThis, 'setInterval')
42 const clearIntervalSpy = vi.spyOn(globalThis, 'clearInterval')
43β
44 const callback = vi.fn()
45 const timeoutId = manager.setTimeout(callback, 100)
46 expect(setTimeoutSpy).toHaveBeenCalledWith(callback, 100)
47 clearTimeout(Number(timeoutId))
48β
49 manager.clearTimeout(200)
50 expect(clearTimeoutSpy).toHaveBeenCalledWith(200)
51β
52 const intervalId = manager.setInterval(callback, 300)
53 expect(setIntervalSpy).toHaveBeenCalledWith(callback, 300)
54 clearInterval(Number(intervalId))
55β
56 manager.clearInterval(400)
57 expect(clearIntervalSpy).toHaveBeenCalledWith(400)
58 })
59β
60 describe('setTimeoutProvider', () => {
61 it('proxies calls to the configured timeout provider', () => {
62 const customProvider = createMockProvider()
63 manager.setTimeoutProvider(customProvider)
64β
65 const callback = vi.fn()
66β
67 manager.setTimeout(callback, 100)
68 expect(customProvider.setTimeout).toHaveBeenCalledWith(callback, 100)
69β
70 manager.clearTimeout(999)
71 expect(customProvider.clearTimeout).toHaveBeenCalledWith(999)
72β
73 manager.setInterval(callback, 200)
74 expect(customProvider.setInterval).toHaveBeenCalledWith(callback, 200)
75β
76 manager.clearInterval(888)
77 expect(customProvider.clearInterval).toHaveBeenCalledWith(888)
78 })
79β
80 it('warns when switching providers after making call', () => {
81 // 1. switching before making any calls does not warn
82 const customProvider = createMockProvider()
83 manager.setTimeoutProvider(customProvider)
84 expect(consoleErrorSpy).not.toHaveBeenCalled()
85β
86 // Make a call. The next switch should warn
87 manager.setTimeout(vi.fn(), 100)
88β
89 // 2. switching after making a call should warn
90 const customProvider2 = createMockProvider('custom2')
91 manager.setTimeoutProvider(customProvider2)
92 expect(consoleErrorSpy).toHaveBeenCalledWith(
93 expect.stringMatching(
94 /\[timeoutManager\]: Switching .* might result in unexpected behavior\..*/,
95 ),
96 expect.anything(),
97 )
98β
99 // 3. Switching again with no intermediate calls should not warn
100 vi.mocked(consoleErrorSpy).mockClear()
101 const customProvider3 = createMockProvider('custom3')
102 manager.setTimeoutProvider(customProvider3)
103 expect(consoleErrorSpy).not.toHaveBeenCalled()
104 })
105 })
106 })
107β
108 describe('globalThis timeoutManager instance', () => {
109 it('should be an instance of TimeoutManager', () => {
110 expect(timeoutManager).toBeInstanceOf(TimeoutManager)
111 })
112 })
113β
114 describe('exported functions', () => {
115 let provider: ReturnType<typeof createMockProvider>
116 beforeEach(() => {
117 provider = createMockProvider()
118 timeoutManager.setTimeoutProvider(provider)
119 })
120 afterEach(() => {
121 timeoutManager.setTimeoutProvider(defaultTimeoutProvider)
122 })
123β
124 describe('systemSetTimeoutZero', () => {
125 it('should use globalThis setTimeout with 0 delay', () => {
126 const spy = vi.spyOn(globalThis, 'setTimeout')
127β
128 const callback = vi.fn()
129 systemSetTimeoutZero(callback)
130β
131 expect(spy).toHaveBeenCalledWith(callback, 0)
132 clearTimeout(spy.mock.results[0]?.value)
133 })
134 })
135 })
136})