Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { Removable } from '../removable'
3import { defaultTimeoutProvider, timeoutManager } from '../timeoutManager'
4import { setIsServer } from './utils'
5import type { TimeoutCallback, TimeoutProvider } from '../timeoutManager'
6β
7class RemovableTest extends Removable {
8 optionalRemoveSpy = vi.fn<() => void>()
9β
10 // Expose the protected members so they can be exercised directly.
11 callScheduleGc(): void {
12 this.scheduleGc()
13 }
14β
15 callUpdateGcTime(newGcTime: number | undefined): void {
16 this.updateGcTime(newGcTime)
17 }
18β
19 callClearGcTimeout(): void {
20 this.clearGcTimeout()
21 }
22β
23 protected optionalRemove(): void {
24 this.optionalRemoveSpy()
25 }
26}
27β
28describe('Removable', () => {
29 function createMockProvider() {
30 return {
31 setTimeout: vi.fn<TimeoutProvider<number>['setTimeout']>(
32 (_callback: TimeoutCallback, _delay: number) => 123,
33 ),
34 clearTimeout: vi.fn<TimeoutProvider<number>['clearTimeout']>(),
35 setInterval: vi.fn<TimeoutProvider<number>['setInterval']>(
36 (_callback: TimeoutCallback, _delay: number) => 456,
37 ),
38 clearInterval: vi.fn<TimeoutProvider<number>['clearInterval']>(),
39 }
40 }
41β
42 let provider: ReturnType<typeof createMockProvider>
43β
44 beforeEach(() => {
45 provider = createMockProvider()
46 timeoutManager.setTimeoutProvider(provider)
47 })
48β
49 afterEach(() => {
50 timeoutManager.setTimeoutProvider(defaultTimeoutProvider)
51 vi.restoreAllMocks()
52 })
53β
54 describe('updateGcTime', () => {
55 it('should default to 5 minutes when no gcTime is provided on the client', () => {
56 const removable = new RemovableTest()
57β
58 removable.callUpdateGcTime(undefined)
59β
60 expect(removable.gcTime).toBe(5 * 60 * 1000)
61 })
62β
63 it('should use the provided gcTime when it is larger than the default', () => {
64 const removable = new RemovableTest()
65β
66 removable.callUpdateGcTime(10 * 60 * 1000)
67β
68 expect(removable.gcTime).toBe(10 * 60 * 1000)
69 })
70β
71 it('should use an explicit gcTime even when it is smaller than the default', () => {
72 const removable = new RemovableTest()
73β
74 removable.callUpdateGcTime(1000)
75β
76 expect(removable.gcTime).toBe(1000)
77 })
78β
79 it('should never decrease an already larger gcTime', () => {
80 const removable = new RemovableTest()
81β
82 removable.callUpdateGcTime(10 * 60 * 1000)
83 removable.callUpdateGcTime(1000)
84β
85 expect(removable.gcTime).toBe(10 * 60 * 1000)
86 })
87β
88 it('should default to Infinity on the server', () => {
89 const resetIsServer = setIsServer(true)
90 try {
91 const removable = new RemovableTest()
92β
93 removable.callUpdateGcTime(undefined)
94β
95 expect(removable.gcTime).toBe(Infinity)
96 } finally {
97 resetIsServer()
98 }
99 })
100 })
101β
102 describe('scheduleGc', () => {
103 it('should schedule optionalRemove after a valid gcTime', () => {
104 const removable = new RemovableTest()
105 removable.callUpdateGcTime(1000)
106β
107 removable.callScheduleGc()
108β
109 expect(provider.setTimeout).toHaveBeenCalledTimes(1)
110 expect(provider.setTimeout).toHaveBeenCalledWith(
111 expect.any(Function),
112 1000,
113 )
114β
115 // The scheduled callback should invoke optionalRemove.
116 const [scheduledCallback] = provider.setTimeout.mock.lastCall ?? []
117 expect(removable.optionalRemoveSpy).toHaveBeenCalledTimes(0)
118 scheduledCallback?.()
119 expect(removable.optionalRemoveSpy).toHaveBeenCalledTimes(1)
120 })
121β
122 it('should not schedule when gcTime is not a valid timeout', () => {
123 const removable = new RemovableTest()
124 removable.gcTime = Infinity
125β
126 removable.callScheduleGc()
127β
128 expect(provider.setTimeout).not.toHaveBeenCalled()
129 })
130β
131 it('should clear a previously scheduled timeout before scheduling a new one', () => {
132 const removable = new RemovableTest()
133 removable.callUpdateGcTime(1000)
134β
135 removable.callScheduleGc()
136 removable.callScheduleGc()
137β
138 // The second schedule clears the first timer before setting a new one.
139 expect(provider.clearTimeout).toHaveBeenCalledWith(123)
140 expect(provider.setTimeout).toHaveBeenCalledTimes(2)
141 })
142 })
143β
144 describe('clearGcTimeout', () => {
145 it('should clear a scheduled timeout', () => {
146 const removable = new RemovableTest()
147 removable.callUpdateGcTime(1000)
148 removable.callScheduleGc()
149β
150 removable.callClearGcTimeout()
151β
152 expect(provider.clearTimeout).toHaveBeenCalledWith(123)
153 })
154β
155 it('should do nothing when no timeout is scheduled', () => {
156 const removable = new RemovableTest()
157β
158 removable.callClearGcTimeout()
159β
160 expect(provider.clearTimeout).not.toHaveBeenCalled()
161 })
162β
163 it('should not clear the same timeout twice', () => {
164 const removable = new RemovableTest()
165 removable.callUpdateGcTime(1000)
166 removable.callScheduleGc()
167β
168 removable.callClearGcTimeout()
169 removable.callClearGcTimeout()
170β
171 expect(provider.clearTimeout).toHaveBeenCalledTimes(1)
172 })
173 })
174β
175 describe('destroy', () => {
176 it('should clear the scheduled gc timeout', () => {
177 const removable = new RemovableTest()
178 removable.callUpdateGcTime(1000)
179 removable.callScheduleGc()
180β
181 removable.destroy()
182β
183 expect(provider.clearTimeout).toHaveBeenCalledWith(123)
184 })
185 })
186})