Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (89 loc) · 1.85 KB
/
Copy pathindex.js
File metadata and controls
104 lines (89 loc) · 1.85 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var test = require('../util/test')(__filename);
var debounce = require('../../packages/function-debounce');
test('waits for n ms then runs once', function(t) {
t.plan(1);
var callCounter = 0;
var fn = debounce(function() {
callCounter++;
}, 100);
for (var i = 0; i < 5; i++) {
setTimeout(fn, 50);
}
setTimeout(function() {
t.equal(callCounter, 1);
t.end();
}, 300);
});
test('when callFirst is true, runs once, waits for n ms then runs again', function(
t
) {
t.plan(2);
var callCounter = 0;
var fn = debounce(
function() {
callCounter++;
},
100,
true
);
fn();
for (var i = 0; i < 5; i++) {
setTimeout(fn, 50);
}
t.equal(callCounter, 1);
setTimeout(function() {
t.equal(callCounter, 2);
t.end();
}, 300);
});
test('invokes repeatedly when call intervals > than wait time', function(t) {
t.plan(4);
var callCounter = 0;
var fn = debounce(function() {
callCounter++;
}, 100);
var runAndTest = function(expectedCounter) {
fn();
t.equal(callCounter, expectedCounter);
};
fn();
setTimeout(function() {
runAndTest(1);
setTimeout(function() {
runAndTest(2);
setTimeout(function() {
runAndTest(3);
setTimeout(function() {
runAndTest(4);
}, 200);
}, 200);
}, 200);
}, 200);
});
test('invokes repeatedly when wait is 0', function(t) {
t.plan(3);
var callCounter = 0;
var fn1 = debounce(function() {
callCounter++;
}, 0);
fn1();
t.equal(callCounter, 1);
fn1();
t.equal(callCounter, 2);
fn1();
t.equal(callCounter, 3);
});
test('invokes repeatedly when wait is falsey', function(t) {
t.plan(1);
var callCounter = 0;
var fn1 = debounce(function() {
callCounter++;
});
fn1();
fn1();
fn1();
setTimeout(function() {
t.equal(callCounter, 3);
t.end();
}, 200);
});