|
1 | 1 | import { debounce, throttle, once, getGlobal, setGlobal } from '../src/func'; |
2 | 2 | import { AnyFunc } from '../src/type'; |
3 | 3 |
|
4 | | -test('debounce', () => { |
5 | | - let context: unknown; |
6 | | - const f = jest.fn(function (...args) { |
7 | | - context = this; |
8 | | - }); |
9 | | - const debounced = debounce(f); |
10 | | - |
11 | | - // 不会立即执行 |
12 | | - jest.useFakeTimers(); |
13 | | - debounced.call(1, 2, 3); |
14 | | - debounced.call(4, 5, 6); |
15 | | - debounced.call(7, 8, 9); |
16 | | - expect(f).not.toBeCalled(); |
17 | | - |
18 | | - // 到时间后执行最后一次 |
19 | | - jest.runAllTimers(); |
20 | | - expect(f.mock.calls).toHaveLength(1); |
21 | | - expect(context).toBe(7); |
22 | | - expect(f).toBeCalledWith(8, 9); |
23 | | - |
24 | | - // 取消后不再执行 |
25 | | - debounced.cancel(); |
26 | | - jest.runAllTimers(); |
27 | | - debounced.call(10, 11, 12); |
28 | | - debounced.call(13, 14, 15); |
29 | | - expect(f.mock.calls).toHaveLength(1); |
30 | | - jest.useRealTimers(); |
31 | | -}); |
32 | | - |
33 | | -test('throttle 默认非立即执行', () => { |
34 | | - let context: unknown; |
35 | | - const f = jest.fn(function (...args) { |
36 | | - context = this; |
37 | | - }); |
38 | | - const throttled = throttle(f, 1000); |
39 | | - |
40 | | - // 非立即执行 |
41 | | - jest.useFakeTimers(); |
42 | | - throttled.call(1, 2, 3); |
43 | | - throttled.call(4, 5, 6); |
44 | | - throttled.call(7, 8, 9); |
45 | | - expect(f.mock.calls).toHaveLength(0); |
46 | | - |
47 | | - // 超时后 |
48 | | - jest.runAllTimers(); |
49 | | - expect(f.mock.calls).toHaveLength(1); |
50 | | - throttled.call(10, 11); |
51 | | - expect(f.mock.calls).toHaveLength(1); |
52 | | - jest.runAllTimers(); |
53 | | - expect(f.mock.calls).toHaveLength(2); |
54 | | - expect(f).toBeCalledWith(11); |
55 | | - expect(context).toEqual(10); |
56 | | - |
57 | | - // 取消后不再执行 |
58 | | - throttled.cancel(); |
59 | | - throttled.call(12, 13); |
60 | | - expect(f.mock.calls).toHaveLength(2); |
61 | | - jest.runAllTimers(); |
62 | | - expect(f.mock.calls).toHaveLength(2); |
63 | | - jest.useRealTimers(); |
64 | | -}); |
65 | | - |
66 | | -test('throttle 立即执行', () => { |
67 | | - let context: unknown; |
68 | | - const f = jest.fn(function (...args) { |
69 | | - context = this; |
70 | | - }); |
71 | | - const throttled = throttle(f, 1000, true); |
72 | | - |
73 | | - // 立即执行 |
74 | | - jest.useFakeTimers(); |
75 | | - throttled.call(1, 2, 3); |
76 | | - throttled.call(4, 5, 6); |
77 | | - throttled.call(7, 8, 9); |
78 | | - expect(f.mock.calls).toHaveLength(1); |
79 | | - expect(f).toBeCalledWith(2, 3); |
80 | | - expect(context).toEqual(1); |
81 | | - |
82 | | - // 超时后 |
83 | | - jest.runAllTimers(); |
84 | | - expect(f.mock.calls).toHaveLength(2); |
85 | | - throttled.call(10, 11); |
86 | | - expect(f.mock.calls).toHaveLength(2); |
87 | | - jest.runAllTimers(); |
88 | | - expect(f.mock.calls).toHaveLength(3); |
89 | | - expect(f).toBeCalledWith(11); |
90 | | - expect(context).toEqual(10); |
91 | | - |
92 | | - // 取消后不再执行 |
93 | | - throttled.cancel(); |
94 | | - throttled.call(12, 13); |
95 | | - expect(f.mock.calls).toHaveLength(3); |
96 | | - jest.runAllTimers(); |
97 | | - expect(f.mock.calls).toHaveLength(3); |
98 | | - jest.useRealTimers(); |
99 | | -}); |
| 4 | +// test('debounce', () => { |
| 5 | +// let context: unknown; |
| 6 | +// const f = jest.fn(function (...args) { |
| 7 | +// context = this; |
| 8 | +// }); |
| 9 | +// const debounced = debounce(f); |
| 10 | + |
| 11 | +// // 不会立即执行 |
| 12 | +// jest.useFakeTimers(); |
| 13 | +// debounced.call(1, 2, 3); |
| 14 | +// debounced.call(4, 5, 6); |
| 15 | +// debounced.call(7, 8, 9); |
| 16 | +// expect(f).not.toBeCalled(); |
| 17 | + |
| 18 | +// // 到时间后执行最后一次 |
| 19 | +// jest.runAllTimers(); |
| 20 | +// expect(f.mock.calls).toHaveLength(1); |
| 21 | +// expect(context).toBe(7); |
| 22 | +// expect(f).toBeCalledWith(8, 9); |
| 23 | + |
| 24 | +// // 取消后不再执行 |
| 25 | +// debounced.cancel(); |
| 26 | +// jest.runAllTimers(); |
| 27 | +// debounced.call(10, 11, 12); |
| 28 | +// debounced.call(13, 14, 15); |
| 29 | +// expect(f.mock.calls).toHaveLength(1); |
| 30 | +// jest.useRealTimers(); |
| 31 | +// }); |
| 32 | + |
| 33 | +// test('throttle 默认非立即执行', () => { |
| 34 | +// let context: unknown; |
| 35 | +// const f = jest.fn(function (...args) { |
| 36 | +// context = this; |
| 37 | +// }); |
| 38 | +// const throttled = throttle(f, 1000); |
| 39 | + |
| 40 | +// // 非立即执行 |
| 41 | +// jest.useFakeTimers(); |
| 42 | +// throttled.call(1, 2, 3); |
| 43 | +// throttled.call(4, 5, 6); |
| 44 | +// throttled.call(7, 8, 9); |
| 45 | +// expect(f.mock.calls).toHaveLength(0); |
| 46 | + |
| 47 | +// // 超时后 |
| 48 | +// jest.runAllTimers(); |
| 49 | +// expect(f.mock.calls).toHaveLength(1); |
| 50 | +// throttled.call(10, 11); |
| 51 | +// expect(f.mock.calls).toHaveLength(1); |
| 52 | +// jest.runAllTimers(); |
| 53 | +// expect(f.mock.calls).toHaveLength(2); |
| 54 | +// expect(f).toBeCalledWith(11); |
| 55 | +// expect(context).toEqual(10); |
| 56 | + |
| 57 | +// // 取消后不再执行 |
| 58 | +// throttled.cancel(); |
| 59 | +// throttled.call(12, 13); |
| 60 | +// expect(f.mock.calls).toHaveLength(2); |
| 61 | +// jest.runAllTimers(); |
| 62 | +// expect(f.mock.calls).toHaveLength(2); |
| 63 | +// jest.useRealTimers(); |
| 64 | +// }); |
| 65 | + |
| 66 | +// test('throttle 立即执行', () => { |
| 67 | +// let context: unknown; |
| 68 | +// const f = jest.fn(function (...args) { |
| 69 | +// context = this; |
| 70 | +// }); |
| 71 | +// const throttled = throttle(f, 1000, true); |
| 72 | + |
| 73 | +// // 立即执行 |
| 74 | +// jest.useFakeTimers(); |
| 75 | +// throttled.call(1, 2, 3); |
| 76 | +// throttled.call(4, 5, 6); |
| 77 | +// throttled.call(7, 8, 9); |
| 78 | +// expect(f.mock.calls).toHaveLength(1); |
| 79 | +// expect(f).toBeCalledWith(2, 3); |
| 80 | +// expect(context).toEqual(1); |
| 81 | + |
| 82 | +// // 超时后 |
| 83 | +// jest.runAllTimers(); |
| 84 | +// expect(f.mock.calls).toHaveLength(2); |
| 85 | +// throttled.call(10, 11); |
| 86 | +// expect(f.mock.calls).toHaveLength(2); |
| 87 | +// jest.runAllTimers(); |
| 88 | +// expect(f.mock.calls).toHaveLength(3); |
| 89 | +// expect(f).toBeCalledWith(11); |
| 90 | +// expect(context).toEqual(10); |
| 91 | + |
| 92 | +// // 取消后不再执行 |
| 93 | +// throttled.cancel(); |
| 94 | +// throttled.call(12, 13); |
| 95 | +// expect(f.mock.calls).toHaveLength(3); |
| 96 | +// jest.runAllTimers(); |
| 97 | +// expect(f.mock.calls).toHaveLength(3); |
| 98 | +// jest.useRealTimers(); |
| 99 | +// }); |
100 | 100 |
|
101 | 101 | // test('throttle 连续在等待时间内执行', () => { |
102 | 102 | // return new Promise(done => { |
|
0 commit comments