Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, test, 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 test('basic', async () => {
15 const interval = 10
16 const execTimeStamps: Array<number> = []
17 const mockFunc = vi.fn(
18 async (id: number, complete?: (value?: unknown) => void) => {
19 await sleep(1)
20 execTimeStamps.push(Date.now())
21 if (complete) {
22 complete(id)
23 }
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β
39 expect(mockFunc).toBeCalledTimes(2)
40 expect(mockFunc.mock.calls[1]?.[0]).toBe(3)
41 expect(execTimeStamps.length).toBe(2)
42 expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
43 interval,
44 )
45 })
46β
47 test('Bug #3331 case 1: Special timing', async () => {
48 const interval = 1000
49 const execTimeStamps: Array<number> = []
50 const mockFunc = vi.fn(
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 )
59 const testFunc = asyncThrottle(mockFunc, { interval })
60β
61 testFunc(1)
62 testFunc(2)
63 await vi.advanceTimersByTimeAsync(35)
64 testFunc(3)
65 await vi.advanceTimersByTimeAsync(35)
66 new Promise((resolve) => testFunc(4, resolve))
67β
68 await vi.advanceTimersToNextTimerAsync()
69 await vi.advanceTimersByTimeAsync(interval)
70β
71 expect(mockFunc).toBeCalledTimes(2)
72 expect(mockFunc.mock.calls[1]?.[0]).toBe(4)
73 expect(execTimeStamps.length).toBe(2)
74 expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
75 interval,
76 )
77 })
78β
79 test('Bug #3331 case 2: "func" execution time is greater than the interval.', async () => {
80 const interval = 1000
81 const execTimeStamps: Array<number> = []
82 const mockFunc = vi.fn(
83 async (id: number, complete?: (value?: unknown) => void) => {
84 await sleep(interval + 10)
85 execTimeStamps.push(Date.now())
86 if (complete) {
87 complete(id)
88 }
89 },
90 )
91 const testFunc = asyncThrottle(mockFunc, { interval })
92β
93 testFunc(1)
94 testFunc(2)
95 new Promise((resolve) => testFunc(3, resolve))
96β
97 await vi.advanceTimersToNextTimerAsync()
98 await vi.advanceTimersByTimeAsync(interval + 10)
99 await vi.advanceTimersByTimeAsync(interval + 10)
100β
101 expect(mockFunc).toBeCalledTimes(2)
102 expect(mockFunc.mock.calls[1]?.[0]).toBe(3)
103 expect(execTimeStamps.length).toBe(2)
104 expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
105 interval,
106 )
107 })
108β
109 test('"func" throw error not break next invoke', async () => {
110 const interval = 10
111β
112 const mockFunc = vi.fn(
113 async (id: number, complete?: (value?: unknown) => void) => {
114 if (id === 1) throw new Error('error')
115 await sleep(1)
116 if (complete) {
117 complete(id)
118 }
119 },
120 )
121 const testFunc = asyncThrottle(mockFunc, { interval })
122β
123 testFunc(1)
124 await vi.advanceTimersByTimeAsync(1)
125β
126 new Promise((resolve) => testFunc(2, resolve))
127 await vi.advanceTimersByTimeAsync(interval)
128β
129 expect(mockFunc).toBeCalledTimes(2)
130 expect(mockFunc.mock.calls[1]?.[0]).toBe(2)
131 })
132β
133 test('"onError" should be called when "func" throw error', () => {
134 const err = new Error('error')
135 const handleError = (e: unknown) => {
136 expect(e).toBe(err)
137 }
138β
139 const testFunc = asyncThrottle(
140 () => {
141 throw err
142 },
143 { onError: handleError },
144 )
145 testFunc()
146 })
147β
148 test('should throw error when "func" is not a function', () => {
149 expect(() => asyncThrottle(1 as any)).toThrowError()
150 })
151})