Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

asyncThrottle.test.ts

127 lines3.4 KB

1import { asyncThrottle } from '../asyncThrottle'
2import { sleep as delay } from '../../reactjs/tests/utils'
3​
4describe('asyncThrottle', () => {
5 test('basic', async () => {
6 const interval = 10
7 const execTimeStamps: number[] = []
8 const mockFunc = jest.fn(
9 async (id: number, complete?: (value?: unknown) => void) => {
10 await delay(1)
11 execTimeStamps.push(Date.now())
12 if (complete) {
13 complete(id)
14 }
15 }
16 )
17 const testFunc = asyncThrottle(mockFunc, { interval })
18​
19 testFunc(1)
20 await delay(1)
21 testFunc(2)
22 await delay(1)
23 await new Promise(resolve => testFunc(3, resolve))
24​
25 expect(mockFunc).toBeCalledTimes(2)
26 expect(mockFunc.mock.calls[1]?.[0]).toBe(3)
27 expect(execTimeStamps.length).toBe(2)
28 expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
29 interval
30 )
31 })
32​
33 test('Bug #3331 case 1: Special timing', async () => {
34 const interval = 1000
35 const execTimeStamps: number[] = []
36 const mockFunc = jest.fn(
37 async (id: number, complete?: (value?: unknown) => void) => {
38 await delay(30)
39 execTimeStamps.push(Date.now())
40 if (complete) {
41 complete(id)
42 }
43 }
44 )
45 const testFunc = asyncThrottle(mockFunc, { interval })
46​
47 testFunc(1)
48 testFunc(2)
49 await delay(35)
50 testFunc(3)
51 await delay(35)
52 await new Promise(resolve => testFunc(4, resolve))
53​
54 expect(mockFunc).toBeCalledTimes(2)
55 expect(mockFunc.mock.calls[1]?.[0]).toBe(4)
56 expect(execTimeStamps.length).toBe(2)
57 expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
58 interval
59 )
60 })
61​
62 test('Bug #3331 case 2: "func" execution time is greater than the interval.', async () => {
63 const interval = 1000
64 const execTimeStamps: number[] = []
65 const mockFunc = jest.fn(
66 async (id: number, complete?: (value?: unknown) => void) => {
67 await delay(interval + 10)
68 execTimeStamps.push(Date.now())
69 if (complete) {
70 complete(id)
71 }
72 }
73 )
74 const testFunc = asyncThrottle(mockFunc, { interval })
75​
76 testFunc(1)
77 testFunc(2)
78 await new Promise(resolve => testFunc(3, resolve))
79​
80 expect(mockFunc).toBeCalledTimes(2)
81 expect(mockFunc.mock.calls[1]?.[0]).toBe(3)
82 expect(execTimeStamps.length).toBe(2)
83 expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
84 interval
85 )
86 })
87​
88 test('"func" throw error not break next invoke', async () => {
89 const mockFunc = jest.fn(
90 async (id: number, complete?: (value?: unknown) => void) => {
91 if (id === 1) throw new Error('error')
92 await delay(1)
93 if (complete) {
94 complete(id)
95 }
96 }
97 )
98 const testFunc = asyncThrottle(mockFunc, { interval: 10 })
99​
100 testFunc(1)
101 await delay(1)
102 await new Promise(resolve => testFunc(2, resolve))
103​
104 expect(mockFunc).toBeCalledTimes(2)
105 expect(mockFunc.mock.calls[1]?.[0]).toBe(2)
106 })
107​
108 test('"onError" should be called when "func" throw error', done => {
109 const err = new Error('error')
110 const handleError = (e: unknown) => {
111 expect(e).toBe(err)
112 done()
113 }
114​
115 const testFunc = asyncThrottle(
116 () => {
117 throw err
118 },
119 { onError: handleError }
120 )
121 testFunc()
122 })
123​
124 test('should throw error when "func" is not a function', () => {
125 expect(() => asyncThrottle(1 as any)).toThrowError()
126 })
127})