-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
130 lines (104 loc) · 3.16 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var debounce = require('./index.js');
var test = require('tape');
test('debauce', function (t) {
t.plan(3);
var fn = debounce(function (a, b) {
t.deepEqual(this, { call: 3 }, 'context should be preserved');
t.equal(a, 30, 'should preserve args');
t.equal(b, 300, 'should preserve args');
}, 10);
fn.call({ call: 1 }, 10, 100);
fn.call({ call: 2 }, 20, 200);
setTimeout(function () {
fn.call({ call: 3 }, 30, 300);
}, 3);
});
test('multiple calls should extend delay', function (t) {
t.plan(4);
var wasDelayed = false;
var fn = debounce(function (a, b) {
t.deepEqual(this, { call: 3 }, 'context should be preserved');
t.equal(a, 30, 'should preserve args');
t.equal(b, 300, 'should preserve args');
t.ok(wasDelayed, 'should have waited longer than debounce period');
}, 6);
setTimeout(function longer() {
wasDelayed = true;
}, 9);
fn.call({ call: 1 }, 10, 100);
setTimeout(function () {
fn.call({ call: 2 }, 20, 200);
setTimeout(function () {
fn.call({ call: 3 }, 30, 300);
}, 5);
}, 3);
});
test('multiple calls should not extend delay when guarantee is true', function (t) {
t.plan(8);
var first = true;
var wasDelayed = false;
var fn = debounce(
function (a, b) {
if (first) {
t.deepEqual(this, { call: 2 }, '1st context should be preserved');
t.equal(a, 20, '1st should preserve 1st args');
t.equal(b, 200, '1st should preserve 2nd args');
t.notOk(wasDelayed, 'should not have waited longer than debounce period');
first = false;
} else {
t.deepEqual(this, { call: 3 }, 'context should be preserved');
t.equal(a, 30, 'should preserve args');
t.equal(b, 300, 'should preserve args');
t.ok(wasDelayed, 'should have waited longer than debounce period');
}
},
6,
false,
true
);
setTimeout(function longer() {
wasDelayed = true;
}, 7);
fn.call({ call: 1 }, 10, 100);
setTimeout(function () {
fn.call({ call: 2 }, 20, 200);
setTimeout(function () {
fn.call({ call: 3 }, 30, 300);
}, 5);
}, 3);
});
test('at start', function (t) {
t.plan(9);
var callCount = 0;
var fn = debounce(
function (a, b) {
if (callCount === 0) {
t.deepEqual(this, { call: 1 }, '1st context should be preserved');
t.equal(a, 10, '1st should preserve 1st args');
t.equal(b, 100, '1st should preserve 2nd args');
} else if (callCount === 1) {
t.deepEqual(this, { call: 3 }, 'context should be preserved');
t.equal(a, 30, 'should preserve args');
t.equal(b, 300, 'should preserve args');
} else {
t.deepEqual(this, { call: 4 }, 'context should be preserved');
t.equal(a, 40, 'should preserve 1st args');
t.equal(b, 400, 'should preserve 2nd args');
}
callCount += 1;
},
6,
true
);
fn.call({ call: 1 }, 10, 100);
fn.call({ call: 2 }, 20, 200);
setTimeout(function () {
fn.call({ call: 3 }, 30, 300);
setTimeout(function () {
fn.call({ call: 4 }, 40, 400);
}, 10);
setTimeout(function () {
fn.call({ call: 5 }, 50, 500);
}, 3);
}, 10);
});