-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
115 lines (107 loc) · 2.9 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
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
105
106
107
108
109
110
111
112
113
114
115
'use strict';
var dh = require("./index");
var test = require("tape");
test("Basic usage", function(t) {
var done = function() {
t.end();
};
var doneBounced = dh(done, function() { return "hash" }, 5);
doneBounced();
});
test("It should only call the original function once after debounce time", function(t) {
var callCount = 0;
var done = function() {
callCount++;
};
var doneBounced = dh(done, function() { return "hash" }, 5);
doneBounced();
doneBounced();
doneBounced();
t.equal(callCount, 0);
setTimeout(function() {
t.equal(callCount, 1);
t.end();
}, 10);
});
test("It should have different debounces for different hashes", function(t) {
var callCount = {};
var done = function(user) {
callCount[user.name] = callCount[user.name] || 0;
callCount[user.name]++;
};
var generateHash = function(user) {
return user.name;
};
var doneBounced = dh(done, generateHash, 5);
doneBounced({name: "alice"});
doneBounced({name: "bob"});
doneBounced({name: "alice"});
doneBounced({name: "alice"});
doneBounced({name: "bob"});
setTimeout(function() {
t.equal(callCount["alice"], 1);
t.equal(callCount["bob"], 1);
t.end();
}, 10);
});
test("It should call immediately if immediate is true, and ignore further calls", function(t) {
var callCount = 0;
var done = function() {
callCount++;
};
var doneBounced = dh(done, function() { return "hash" }, 5, true);
doneBounced();
t.equal(callCount, 1);
doneBounced();
t.equal(callCount, 1);
doneBounced();
t.equal(callCount, 1);
t.end();
});
test("It should work with options.immediate", function(t) {
var callCount = 0;
var done = function() {
callCount++;
};
var doneBounced = dh(done, function() { return "hash" }, 5, {immediate: true});
doneBounced();
t.equal(callCount, 1);
doneBounced();
t.equal(callCount, 1);
doneBounced();
t.equal(callCount, 1);
t.end();
});
test("It should not call immediate if it's not in the options", function(t) {
var callCount = 0;
var done = function() {
callCount++;
};
var doneBounced = dh(done, function() { return "hash" }, 5, {});
doneBounced();
doneBounced();
doneBounced();
t.equal(callCount, 0);
setTimeout(function() {
t.equal(callCount, 1);
t.end();
}, 10);
});
test("It should work with maxWait", function(t) {
var callCount = 0;
var timeout;
var done = function() {
callCount++;
};
var doneBounced = dh(done, function() { return "hash" }, 10, {maxWait: 20});
function doBounce() {
doneBounced();
timeout = setTimeout(doBounce, 5);
}
doBounce();
setTimeout(function() {
t.equal(callCount, 1);
clearTimeout(timeout);
t.end();
}, 25);
});