forked from sindresorhus/p-debounce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
64 lines (48 loc) · 1.36 KB
/
test.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import test from 'ava';
import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import pDebounce from '.';
const fixture = Symbol('fixture');
test('single call', async t => {
const debounced = pDebounce(async value => value, 100);
t.is(await debounced(fixture), fixture);
});
test('multiple calls', async t => {
let count = 0;
const end = timeSpan();
const debounced = pDebounce(async value => {
count++;
await delay(50);
return value;
}, 100);
const results = await Promise.all([1, 2, 3, 4, 5].map(debounced));
t.deepEqual(results, [5, 5, 5, 5, 5]);
t.is(count, 1);
t.true(inRange(end(), 130, 170));
await delay(200);
t.is(await debounced(6), 6);
});
test('leading option', async t => {
let count = 0;
const debounced = pDebounce(async value => {
count++;
await delay(50);
return value;
}, 100, {leading: true});
const results = await Promise.all([1, 2, 3, 4].map(debounced));
t.deepEqual(results, [1, 1, 1, 1], 'value from the first promise is used without the timeout');
t.is(count, 1);
await delay(200);
t.is(await debounced(5), 5);
t.is(await debounced(6), 5);
});
test('leading option - does not call input function after timeout', async t => {
let count = 0;
const debounced = pDebounce(async () => {
count++;
}, 100, {leading: true});
await delay(300);
await debounced();
t.is(count, 1);
});