mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 03:47:50 +00:00
Avoid setTimeout inconsistencies in the unit tests.
Former-commit-id: fdbe08fcb381bf7771a1a7e474882d82e5bdbdf2
This commit is contained in:
85
test/test.js
85
test/test.js
@@ -431,21 +431,21 @@
|
|||||||
QUnit.module('lodash.debounce');
|
QUnit.module('lodash.debounce');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
test('subsequent "immediate" debounced calls return the last `func` result', function() {
|
||||||
|
var debounced = _.debounce(function(value) { return value; }, 32, true),
|
||||||
|
result = [debounced('x'), debounced('y')];
|
||||||
|
|
||||||
|
deepEqual(result, ['x', 'x']);
|
||||||
|
});
|
||||||
|
|
||||||
asyncTest('subsequent debounced calls return the last `func` result', function() {
|
asyncTest('subsequent debounced calls return the last `func` result', function() {
|
||||||
var debounced = _.debounce(function(value) { return value; }, 90);
|
var debounced = _.debounce(function(value) { return value; }, 32);
|
||||||
debounced('x');
|
debounced('x');
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
equal(debounced('y'), 'x');
|
equal(debounced('y'), 'x');
|
||||||
QUnit.start();
|
QUnit.start();
|
||||||
}, 120);
|
}, 64);
|
||||||
});
|
|
||||||
|
|
||||||
test('subsequent "immediate" debounced calls return the last `func` result', function() {
|
|
||||||
var debounced = _.debounce(function(value) { return value; }, 90, true),
|
|
||||||
result = [debounced('x'), debounced('y')];
|
|
||||||
|
|
||||||
deepEqual(result, ['x', 'x']);
|
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -1785,46 +1785,37 @@
|
|||||||
deepEqual(result, ['x', 'x']);
|
deepEqual(result, ['x', 'x']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should clear timeout when `func` is called', function() {
|
||||||
|
var counter = 0,
|
||||||
|
oldDate = Date,
|
||||||
|
throttled = _.throttle(function() { counter++; }, 32);
|
||||||
|
|
||||||
|
throttled();
|
||||||
|
throttled();
|
||||||
|
|
||||||
|
window.Date = function() { return Object(Infinity); };
|
||||||
|
throttled();
|
||||||
|
window.Date = oldDate;
|
||||||
|
|
||||||
|
equal(counter, 2);
|
||||||
|
});
|
||||||
|
|
||||||
asyncTest('supports recursive calls', function() {
|
asyncTest('supports recursive calls', function() {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var throttled = _.throttle(function() {
|
var throttled = _.throttle(function() {
|
||||||
counter++;
|
counter++;
|
||||||
if (counter < 4) {
|
if (counter < 10) {
|
||||||
throttled();
|
throttled();
|
||||||
}
|
}
|
||||||
}, 90);
|
|
||||||
|
|
||||||
setTimeout(function() {
|
|
||||||
ok(counter > 1);
|
|
||||||
QUnit.start();
|
|
||||||
}, 180);
|
|
||||||
|
|
||||||
throttled();
|
|
||||||
});
|
|
||||||
|
|
||||||
asyncTest('should clear timeout when `func` is called', function() {
|
|
||||||
var now = new Date,
|
|
||||||
times = [];
|
|
||||||
|
|
||||||
var throttled = _.throttle(function() {
|
|
||||||
times.push(new Date - now);
|
|
||||||
}, 32);
|
}, 32);
|
||||||
|
|
||||||
setTimeout(throttled, 32);
|
throttled();
|
||||||
setTimeout(throttled, 32);
|
equal(counter, 1);
|
||||||
setTimeout(throttled, 64);
|
|
||||||
setTimeout(throttled, 64);
|
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
var actual = _.every(times, function(value, index) {
|
ok(counter < 3)
|
||||||
return index
|
|
||||||
? (value - times[index - 1]) > 2
|
|
||||||
: true;
|
|
||||||
});
|
|
||||||
|
|
||||||
ok(actual);
|
|
||||||
QUnit.start();
|
QUnit.start();
|
||||||
}, 260);
|
}, 32);
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest('should not trigger a trailing call when invoked once', function() {
|
asyncTest('should not trigger a trailing call when invoked once', function() {
|
||||||
@@ -1837,12 +1828,11 @@
|
|||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
equal(counter, 1);
|
equal(counter, 1);
|
||||||
QUnit.start();
|
QUnit.start();
|
||||||
}, 64);
|
}, 96);
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest('should trigger trailing call when invoked repeatedly', function() {
|
asyncTest('should trigger trailing call when invoked repeatedly', function() {
|
||||||
var actual,
|
var counter = 0,
|
||||||
counter = 0,
|
|
||||||
limit = 80,
|
limit = 80,
|
||||||
throttled = _.throttle(function() { counter++; }, 32),
|
throttled = _.throttle(function() { counter++; }, 32),
|
||||||
start = new Date;
|
start = new Date;
|
||||||
@@ -1850,18 +1840,13 @@
|
|||||||
while ((new Date - start) < limit) {
|
while ((new Date - start) < limit) {
|
||||||
throttled();
|
throttled();
|
||||||
}
|
}
|
||||||
setTimeout(function() {
|
var lastCount = counter;
|
||||||
actual = counter + 2;
|
ok(lastCount > 1);
|
||||||
throttled();
|
|
||||||
throttled();
|
|
||||||
}, 128);
|
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
equal(counter, actual);
|
ok(counter > lastCount);
|
||||||
QUnit.start();
|
QUnit.start();
|
||||||
}, 256);
|
}, 96);
|
||||||
|
|
||||||
ok(counter > 1);
|
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user