-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy paththrottle.test.js
More file actions
41 lines (37 loc) · 860 Bytes
/
throttle.test.js
File metadata and controls
41 lines (37 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import throttle from './throttle';
import sleep from './../../shared/sleep';
describe('throttle', () => {
it('when less than interval, will throttle', async () => {
let count = 0;
const addCount = () => {
count += 1;
};
const timer = setInterval(throttle(addCount, 400), 200);
/**
* 400 -> 1
* 800 -> 2
*/
setTimeout(() => {
clearInterval(timer);
}, 1000);
await sleep(1500);
expect(count).toBe(2);
});
it('when greate than interval, normally call', async () => {
let count = 0;
const addCount = () => {
count += 1;
};
const timer = setInterval(throttle(addCount, 200), 300);
/**
* 300 -> 1
* 600 -> 2
* 900 -> 3
*/
setTimeout(() => {
clearInterval(timer);
}, 1000);
await sleep(1500);
expect(count).toBe(3);
});
});