Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { sleep } from '@tanstack/query-test-utils'
3import { asyncThrottle } from '../asyncThrottle'
4β
5describe('asyncThrottle', () => {
6 beforeEach(() => {
7 vi.useFakeTimers()
8 })
9β
10 afterEach(() => {
11 vi.useRealTimers()
12 })
13β
14 it('should throttle calls to run at most once per interval with the latest arguments', async () => {
15 const interval = 10
16 const execTimeStamps: Array<number> = []
17 const mockFunc = vi.fn<
18 (id: number, complete?: (value?: unknown) => void) => Promise<void>
19 >(async (id: number, complete?: (value?: unknown) => void) => {
20 await sleep(1)
21 execTimeStamps.push(Date.now())
22 if (complete) {
23 complete(id)
24 }
25 })
26 const testFunc = asyncThrottle(mockFunc, { interval })
27β
28 testFunc(1)
29 await vi.advanceTimersByTimeAsync(1)
30β
31 testFunc(2)
32 await vi.advanceTimersByTimeAsync(1)
33β
34 new Promise((resolve) => testFunc(3, resolve))
35β
36 await vi.advanceTimersToNextTimerAsync()
37 await vi.advanceTimersByTimeAsync(interval)
38 expect(mockFunc).toHaveBeenCalledTimes(2)
39 expect(mockFunc.mock.calls[1]?.[0]).toBe(3)
40 expect(execTimeStamps).toHaveLength(2)
41 expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
42 interval,
43 )
44 })
45β
46 it('should handle special timing (Bug #3331 case 1)', async () => {
47 const interval = 1000
48 const execTimeStamps: Array<number> = []
49 const mockFunc = vi.fn<
50 (id: number, complete?: (value?: unknown) => void) => Promise<void>
51 >(async (id: number, complete?: (value?: unknown) => void) => {
52 await sleep(30)
53 execTimeStamps.push(Date.now())
54 if (complete) {
55 complete(id)
56 }
57 })
58 const testFunc = asyncThrottle(mockFunc, { interval })
59β
60 testFunc(1)
61 testFunc(2)
62 await vi.advanceTimersByTimeAsync(35)
63 testFunc(3)
64 await vi.advanceTimersByTimeAsync(35)
65 new Promise((resolve) => testFunc(4, resolve))
66β
67 await vi.advanceTimersToNextTimerAsync()
68 await vi.advanceTimersByTimeAsync(interval)
69 expect(mockFunc).toHaveBeenCalledTimes(2)
70 expect(mockFunc.mock.calls[1]?.[0]).toBe(4)
71 expect(execTimeStamps).toHaveLength(2)
72 expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
73 interval,
74 )
75 })
76β
77 it('should handle "func" execution time greater than the interval (Bug #3331 case 2)', async () => {
78 const interval = 1000
79 const execTimeStamps: Array<number> = []
80 const mockFunc = vi.fn<
81 (id: number, complete?: (value?: unknown) => void) => Promise<void>
82 >(async (id: number, complete?: (value?: unknown) => void) => {
83 await sleep(interval + 10)
84 execTimeStamps.push(Date.now())
85 if (complete) {
86 complete(id)
87 }
88 })
89 const testFunc = asyncThrottle(mockFunc, { interval })
90β
91 testFunc(1)
92 testFunc(2)
93 new Promise((resolve) => testFunc(3, resolve))
94β
95 await vi.advanceTimersToNextTimerAsync()
96 await vi.advanceTimersByTimeAsync(interval + 10)
97 await vi.advanceTimersByTimeAsync(interval + 10)
98 expect(mockFunc).toHaveBeenCalledTimes(2)
99 expect(mockFunc.mock.calls[1]?.[0]).toBe(3)
100 expect(execTimeStamps).toHaveLength(2)
101 expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
102 interval,
103 )
104 })
105β
106 it('should not break next invoke when "func" throws error', async () => {
107 const interval = 10
108β
109 const mockFunc = vi.fn<
110 (id: number, complete?: (value?: unknown) => void) => Promise<void>
111 >(async (id: number, complete?: (value?: unknown) => void) => {
112 if (id === 1) throw new Error('error')
113 await sleep(1)
114 if (complete) {
115 complete(id)
116 }
117 })
118 const testFunc = asyncThrottle(mockFunc, { interval })
119β
120 testFunc(1)
121 await vi.advanceTimersByTimeAsync(1)
122β
123 new Promise((resolve) => testFunc(2, resolve))
124 await vi.advanceTimersByTimeAsync(interval)
125 expect(mockFunc).toHaveBeenCalledTimes(2)
126 expect(mockFunc.mock.calls[1]?.[0]).toBe(2)
127 })
128β
129 it('should call "onError" when "func" throws error', () => {
130 const err = new Error('error')
131 const handleError = (e: unknown) => {
132 expect(e).toBe(err)
133 }
134β
135 const testFunc = asyncThrottle(
136 () => {
137 throw err
138 },
139 { onError: handleError },
140 )
141 testFunc()
142 })
143β
144 it('should throw error when "func" is not a function', () => {
145 expect(() => asyncThrottle(1 as any)).toThrow('argument is not function.')
146 })
147})