Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { timeoutManager } from '..'
3import { defaultTimeoutProvider, systemSetTimeoutZero } from '../timeoutManager'
4import type {
5 ManagedTimerId,
6 TimeoutCallback,
7 TimeoutManager,
8 TimeoutProvider,
9} from '..'
10β
11describe('timeoutManager', () => {
12 describe('TimeoutCallback', () => {
13 it('should take a single void argument and return void', () => {
14 expectTypeOf<TimeoutCallback>().toEqualTypeOf<(_: void) => void>()
15 })
16β
17 it('should be callable without an argument', () => {
18 // A single `void` parameter is what lets `setTimeout(resolve, n)` and a
19 // zero-argument callback both type check.
20 const callback: TimeoutCallback = () => {}
21β
22 expectTypeOf(callback).toEqualTypeOf<TimeoutCallback>()
23 // A lone `void` parameter is optional at the call site, so the tuple is
24 // `[] | [void]` and the callback may be called with no argument at all.
25 expectTypeOf<Parameters<TimeoutCallback>['length']>().toEqualTypeOf<
26 0 | 1
27 >()
28 })
29β
30 it('should reject a callback that requires a value argument', () => {
31 // @ts-expect-error a timeout callback cannot require a value argument
32 const invalid: TimeoutCallback = (_: number) => {}
33β
34 expectTypeOf(invalid).toBeFunction()
35 expectTypeOf<TimeoutCallback>().parameter(0).toBeVoid()
36 expectTypeOf<ReturnType<TimeoutCallback>>().toEqualTypeOf<void>()
37 })
38 })
39β
40 describe('ManagedTimerId', () => {
41 it('should be a number or a primitive-coercible object', () => {
42 expectTypeOf<ManagedTimerId>().toEqualTypeOf<
43 number | { [Symbol.toPrimitive]: () => number }
44 >()
45 })
46β
47 it('should accept a number and a primitive-coercible object', () => {
48 expectTypeOf<Extract<ManagedTimerId, number>>().toEqualTypeOf<number>()
49 expectTypeOf<Exclude<ManagedTimerId, number>>().toEqualTypeOf<{
50 [Symbol.toPrimitive]: () => number
51 }>()
52 })
53β
54 it('should reject a value that cannot be coerced to a number', () => {
55 // @ts-expect-error a timer id cannot be a string
56 const invalid: ManagedTimerId = 'timer'
57β
58 expectTypeOf(invalid).toEqualTypeOf<
59 number | { [Symbol.toPrimitive]: () => number }
60 >()
61 expectTypeOf<Extract<ManagedTimerId, string>>().toEqualTypeOf<never>()
62 })
63 })
64β
65 describe('TimeoutProvider', () => {
66 describe('setTimeout', () => {
67 it('should take a callback and a delay and return the timer id', () => {
68 expectTypeOf<TimeoutProvider['setTimeout']>().toEqualTypeOf<
69 (callback: TimeoutCallback, delay: number) => ManagedTimerId
70 >()
71 })
72 })
73β
74 describe('clearTimeout', () => {
75 it('should take the timer id or undefined and return void', () => {
76 expectTypeOf<TimeoutProvider['clearTimeout']>().toEqualTypeOf<
77 (timeoutId: ManagedTimerId | undefined) => void
78 >()
79 })
80 })
81β
82 describe('setInterval', () => {
83 it('should take a callback and a delay and return the timer id', () => {
84 expectTypeOf<TimeoutProvider['setInterval']>().toEqualTypeOf<
85 (callback: TimeoutCallback, delay: number) => ManagedTimerId
86 >()
87 })
88 })
89β
90 describe('clearInterval', () => {
91 it('should take the timer id or undefined and return void', () => {
92 expectTypeOf<TimeoutProvider['clearInterval']>().toEqualTypeOf<
93 (intervalId: ManagedTimerId | undefined) => void
94 >()
95 })
96 })
97β
98 it('should have every member readonly', () => {
99 // `Pick` preserves the `readonly` modifier that an indexed access strips.
100 expectTypeOf<TimeoutProvider>().toEqualTypeOf<{
101 readonly setTimeout: (
102 callback: TimeoutCallback,
103 delay: number,
104 ) => ManagedTimerId
105 readonly clearTimeout: (timeoutId: ManagedTimerId | undefined) => void
106 readonly setInterval: (
107 callback: TimeoutCallback,
108 delay: number,
109 ) => ManagedTimerId
110 readonly clearInterval: (intervalId: ManagedTimerId | undefined) => void
111 }>()
112 })
113β
114 it('should default its timer id to a managed timer id', () => {
115 expectTypeOf<TimeoutProvider>().toEqualTypeOf<
116 TimeoutProvider<ManagedTimerId>
117 >()
118 })
119β
120 it('should carry a narrowed timer id through its members', () => {
121 expectTypeOf<TimeoutProvider<number>['setTimeout']>().toEqualTypeOf<
122 (callback: TimeoutCallback, delay: number) => number
123 >()
124 expectTypeOf<TimeoutProvider<number>['clearTimeout']>().toEqualTypeOf<
125 (timeoutId: number | undefined) => void
126 >()
127 })
128β
129 it('should constrain its timer id to a managed timer id', () => {
130 // @ts-expect-error a string is not a managed timer id
131 expectTypeOf<TimeoutProvider<string>>().toBeObject()
132β
133 expectTypeOf<TimeoutProvider<number>>().toBeObject()
134 })
135β
136 it('should only expose its documented members', () => {
137 expectTypeOf<keyof TimeoutProvider>().toEqualTypeOf<
138 'setTimeout' | 'clearTimeout' | 'setInterval' | 'clearInterval'
139 >()
140 })
141 })
142β
143 describe('defaultTimeoutProvider', () => {
144 it('should be a timeout provider of the default timer id', () => {
145 expectTypeOf(defaultTimeoutProvider).toEqualTypeOf<TimeoutProvider>()
146 })
147β
148 it('should not narrow its timer id to the platform one', () => {
149 // The provider is declared as `TimeoutProvider`, not
150 // `TimeoutProvider<ReturnType<typeof setTimeout>>`, so swapping it for a
151 // custom provider stays type safe across environments.
152 expectTypeOf(
153 defaultTimeoutProvider.setTimeout,
154 ).returns.toEqualTypeOf<ManagedTimerId>()
155 expectTypeOf(
156 defaultTimeoutProvider.setInterval,
157 ).returns.toEqualTypeOf<ManagedTimerId>()
158 })
159β
160 it('should be accepted by setTimeoutProvider', () => {
161 expectTypeOf(
162 timeoutManager.setTimeoutProvider(defaultTimeoutProvider),
163 ).toEqualTypeOf<void>()
164 })
165 })
166β
167 describe('TimeoutManager', () => {
168 describe('setTimeoutProvider', () => {
169 it('should take a provider and return void', () => {
170 expectTypeOf(timeoutManager.setTimeoutProvider).toEqualTypeOf<
171 <TTimerId extends ManagedTimerId>(
172 provider: TimeoutProvider<TTimerId>,
173 ) => void
174 >()
175 })
176β
177 it('should reject a provider that is not a timeout provider', () => {
178 // @ts-expect-error a plain object is not a timeout provider
179 timeoutManager.setTimeoutProvider({})
180β
181 expectTypeOf(
182 timeoutManager.setTimeoutProvider({
183 setTimeout: (_callback, _delay) => 1,
184 clearTimeout: (_timeoutId) => {},
185 setInterval: (_callback, _delay) => 1,
186 clearInterval: (_intervalId) => {},
187 }),
188 ).toEqualTypeOf<void>()
189 })
190β
191 it('should infer the timer id from the provider', () => {
192 timeoutManager.setTimeoutProvider({
193 setTimeout: (callback, delay) => {
194 expectTypeOf(callback).toEqualTypeOf<TimeoutCallback>()
195 expectTypeOf(delay).toEqualTypeOf<number>()
196 return 1
197 },
198 clearTimeout: (timeoutId) => {
199 // The timer id is inferred from `setTimeout`'s literal return.
200 expectTypeOf(timeoutId).toEqualTypeOf<1 | undefined>()
201 },
202 setInterval: (_callback, _delay) => 1,
203 clearInterval: (_intervalId) => {},
204 })
205 })
206 })
207β
208 describe('setTimeout', () => {
209 it('should take a callback and a delay and return the timer id', () => {
210 expectTypeOf(timeoutManager.setTimeout).toEqualTypeOf<
211 (callback: TimeoutCallback, delay: number) => ManagedTimerId
212 >()
213 })
214β
215 it('should require the delay', () => {
216 // @ts-expect-error the delay is required
217 timeoutManager.setTimeout(() => {})
218β
219 expectTypeOf(
220 timeoutManager.setTimeout(() => {}, 0),
221 ).toEqualTypeOf<ManagedTimerId>()
222 })
223 })
224β
225 describe('clearTimeout', () => {
226 it('should take the timer id or undefined and return void', () => {
227 expectTypeOf(timeoutManager.clearTimeout).toEqualTypeOf<
228 (timeoutId: ManagedTimerId | undefined) => void
229 >()
230 })
231 })
232β
233 describe('setInterval', () => {
234 it('should take a callback and a delay and return the timer id', () => {
235 expectTypeOf(timeoutManager.setInterval).toEqualTypeOf<
236 (callback: TimeoutCallback, delay: number) => ManagedTimerId
237 >()
238 })
239 })
240β
241 describe('clearInterval', () => {
242 it('should take the timer id or undefined and return void', () => {
243 expectTypeOf(timeoutManager.clearInterval).toEqualTypeOf<
244 (intervalId: ManagedTimerId | undefined) => void
245 >()
246 })
247 })
248β
249 it('should be the type of the exported singleton', () => {
250 expectTypeOf(timeoutManager).toEqualTypeOf<TimeoutManager>()
251 })
252β
253 it('should only expose its documented members', () => {
254 expectTypeOf<keyof TimeoutManager>().toEqualTypeOf<
255 | 'setTimeoutProvider'
256 | 'setTimeout'
257 | 'clearTimeout'
258 | 'setInterval'
259 | 'clearInterval'
260 >()
261 })
262 })
263β
264 describe('systemSetTimeoutZero', () => {
265 it('should take a callback and return void', () => {
266 expectTypeOf(systemSetTimeoutZero).toEqualTypeOf<
267 (callback: TimeoutCallback) => void
268 >()
269 })
270β
271 it('should require the callback', () => {
272 // @ts-expect-error the callback is required
273 systemSetTimeoutZero()
274β
275 expectTypeOf(systemSetTimeoutZero(() => {})).toEqualTypeOf<void>()
276 })
277β
278 it('should reject a callback that requires a value argument', () => {
279 // @ts-expect-error a timeout callback cannot require a value argument
280 systemSetTimeoutZero((_: number) => {})
281β
282 expectTypeOf(systemSetTimeoutZero(() => {})).toEqualTypeOf<void>()
283 })
284 })
285})