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