-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.js
310 lines (250 loc) · 8.12 KB
/
tests.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*global describe, it, expect, mergebounce */
describe("mergebounce", function () {
it("merges passed in parameters before invoking the function", function (done) {
let invokedData;
const mergebounced = mergebounce(data => (invokedData = data), 10);
mergebounced({'a': [{ 'b': 2 }, { 'd': 4 }]});
mergebounced({'a': [{ 'c': 3 }, { 'e': 5 }]});
setTimeout(() => {
expect(invokedData).toEqual({ 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] })
done();
}, 15);
});
it("replaces arrays that don't contain objects", function (done) {
let invokedData;
const mergebounced = mergebounce(data => (invokedData = data), 10);
mergebounced([{a: 2}, 4, 5]);
mergebounced([{a: 1}, 2, 3, 4]);
setTimeout(() => {
expect(invokedData).toEqual([{a: 1}, 2, 3, 4]);
done();
}, 15);
});
it("concatenates arrays if concatArrays option is set", function (done) {
let invokedData;
const mergebounced = mergebounce(data => (invokedData = data), 10, {'concatArrays': true});
mergebounced([{a: 2}, 4, 5]);
mergebounced([{a: 1}, 2, 3, 4]);
setTimeout(() => {
expect(invokedData).toEqual([{a: 2}, 4, 5, {a: 1}, 2, 3, 4]);
done();
}, 15);
});
it('should dedupe concatenated arrays if "dedupeArrays" is set to true', function (done) {
let callCount = 0;
const values = [];
let mergebounced = mergebounce((value) => {
++callCount;
values.push(value);
}, 32, {'dedupeArrays': true});
mergebounced(['a']);
mergebounced(['a']);
mergebounced(['b']);
expect(values.length).toBe(0);
expect(callCount).toBe(0);
setTimeout(() => {
expect(callCount).toBe(1);
expect(values).toEqual([['a', 'b']]);
done();
}, 128);
});
it("returns a promise if the promise option is set to true", function (done) {
const mergebounced = mergebounce(() => {}, 10, {'promise': true});
const result = mergebounced([{a: 2}, 4, 5]);
expect(result instanceof Promise).toBe(true);
expect(result.isResolved).toBe(false);
setTimeout(() => {
expect(result.isResolved).toBe(true);
done();
}, 15);
});
it("merges nested arrays uniquely", function (done) {
let invokedData;
const mergebounced = mergebounce(data => (invokedData = data), 10);
mergebounced({'a': [{ 'b': 2 }, { 'd': [{a: 4}, {b: 6}] }]});
mergebounced({'a': [{ 'c': 3 }, { 'd': [{a: 5}] }]});
setTimeout(() => {
expect(invokedData).toEqual({ 'a': [{ 'b': 2, 'c': 3 }, { 'd': [{a: 5}, {b: 6}] }] })
done();
}, 15);
});
it("gracefully handles no arguments being passed in", function (done) {
let invokedData;
const mergebounced = mergebounce(data => (invokedData = data), 10);
mergebounced({'a': [{ 'b': 2 }, { 'd': [4] }]});
mergebounced();
setTimeout(() => {
expect(invokedData).toEqual({'a': [{ 'b': 2 }, { 'd': [4] }]})
done();
}, 15);
});
it("handles multple arguments being passed in", function (done) {
let args;
const mergebounced = mergebounce(function () {
args = Array.from(arguments);
}, 10);
mergebounced({ 'b': 2 }, { 'd': [4] });
mergebounced({ 'c': 3 }, { 'd': [4, 5]});
setTimeout(() => {
expect(args).toEqual([{ 'b': 2, 'c': 3 }, { 'd': [4, 5] }])
done();
}, 15);
});
it("handles multple arguments of variable length", function (done) {
let args;
const mergebounced = mergebounce(function () {
args = Array.from(arguments);
}, 10);
mergebounced({ 'b': 2 }, { 'd': [4] });
mergebounced({ 'c': 3 });
setTimeout(() => {
expect(args).toEqual([{ 'b': 2, 'c': 3 }, { 'd': [4] }])
done();
}, 15);
});
it("keeps track of multple mergebounce functions", function (done) {
let args1, args2;
const mergebounced = mergebounce(function () {
args1 = Array.from(arguments);
}, 10);
const mergebounced2 = mergebounce(function () {
args2 = Array.from(arguments);
}, 10);
mergebounced({ 'b': 2 }, { 'd': [4] });
mergebounced2({'a': [{ 'b': 2 }, { 'd': [4] }]});
mergebounced({ 'c': 3 });
mergebounced2({'a': [{ 'c': 3 }, { 'd': [4, 5] }]});
setTimeout(() => {
expect(args1).toEqual([{ 'b': 2, 'c': 3 }, { 'd': [4] }])
expect(args2).toEqual([{ 'a': [{ 'b': 2, 'c': 3 }, { 'd': [4, 5] }] }])
done();
}, 15);
});
it('should debounce a function', function (done) {
let callCount = 0;
let mergebounced = mergebounce(function (value) {
++callCount;
return value;
}, 32);
let results = [mergebounced('a'), mergebounced('b'), mergebounced('c')];
expect(results).toEqual([undefined, undefined, undefined]);
expect(callCount).toBe(0);
setTimeout(function() {
expect(callCount).toBe(1);
const results = [mergebounced(['d']), mergebounced(['e']), mergebounced(['f'])];
expect(results).toEqual(['c', 'c', 'c']);
expect(callCount).toBe(1);
}, 128);
setTimeout(function() {
expect(callCount).toBe(2);
done();
}, 256);
});
it("should not flush if it wasn't called at least once", function (done) {
let callCount = 0;
const mergebounced = mergebounce(() => ++callCount, 15);
mergebounced.flush();
expect(callCount).toBe(0);
done();
});
it('should resolve when flushing', function(done) {
let callCount = 0;
const mergebounced = mergebounce(() => ++callCount, 0, {promise: true});
mergebounced();
mergebounced();
expect(callCount).toBe(0);
mergebounced.flush().then(() => {
expect(callCount).toBe(1);
done();
});
});
it('should not immediately call `func` when `wait` is `0`', function(done) {
let callCount = 0;
const mergebounced = mergebounce(function() { ++callCount; }, 0);
mergebounced();
mergebounced();
expect(callCount).toBe(0);
setTimeout(function() {
expect(callCount).toBe(1);
done();
}, 5);
});
it('should apply default options', function(done) {
let callCount = 0;
const mergebounced = mergebounce(() => callCount++, 32, {});
mergebounced();
expect(callCount).toEqual(0);
setTimeout(function() {
expect(callCount).toEqual(1);
done();
}, 64);
});
it('should support a `maxWait` option', function(done) {
let callCount = 0;
const mergebounced = mergebounce(function(value) {
++callCount;
return value;
}, 32, { 'maxWait': 64 });
mergebounced();
mergebounced();
expect(callCount).toEqual(0);
setTimeout(function() {
expect(callCount).toEqual(1);
mergebounced();
mergebounced();
expect(callCount).toEqual(1);
}, 128);
setTimeout(function() {
expect(callCount).toEqual(2);
done();
}, 256);
});
it('should support `maxWait` in a tight loop', function(done) {
let limit = 320,
withCount = 0,
withoutCount = 0;
const withMaxWait = mergebounce(function() {
withCount++;
}, 64, { 'maxWait': 128 });
let withoutMaxWait = mergebounce(function() {
withoutCount++;
}, 96);
let start = +new Date;
while ((new Date - start) < limit) {
withMaxWait();
withoutMaxWait();
}
let actual = [Boolean(withoutCount), Boolean(withCount)];
setTimeout(function() {
expect(actual).toEqual([false, true]);
done();
}, 1);
});
it('should queue a trailing call for subsequent mergebounced calls after `maxWait`', function(done) {
let callCount = 0;
let mergebounced = mergebounce(() => ++callCount, 200, { 'maxWait': 200 });
mergebounced();
setTimeout(mergebounced, 190);
setTimeout(mergebounced, 200);
setTimeout(mergebounced, 210);
setTimeout(function() {
expect(callCount).toEqual(2);
done();
}, 500);
});
it('should cancel `maxDelayed` when `delayed` is invoked', function(done) {
let callCount = 0;
let mergebounced = mergebounce(function() {
callCount++;
}, 32, { 'maxWait': 64 });
mergebounced();
setTimeout(function() {
mergebounced();
expect(callCount).toEqual(1);
}, 128);
setTimeout(function() {
expect(callCount).toEqual(2);
done();
}, 192);
});
});