From 44b9b085c0b6cbf7ea41ef4e022b34202fd03329 Mon Sep 17 00:00:00 2001 From: ben robbins Date: Wed, 13 Apr 2016 14:50:55 -0700 Subject: [PATCH] Add test for `_.debounce` with a `wait` of `0`. --- test/test.js | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/test/test.js b/test/test.js index 4739cec0a..44243ecaf 100644 --- a/test/test.js +++ b/test/test.js @@ -4106,21 +4106,26 @@ }, 128); }); - QUnit.test('subsequent "immediate" debounced calls return the last `func` result', function(assert) { - assert.expect(2); + QUnit.test('should not call immediately for `wait` of `0`', function(assert) { + assert.expect(3); var done = assert.async(); - var debounced = _.debounce(identity, 32, { 'leading': true, 'trailing': false }), - result = [debounced('x'), debounced('y')]; + var callCount = 0; - assert.deepEqual(result, ['x', 'x']); + var debounced = _.debounce(function(value) { + ++callCount; + return value; + }, 0); + + var actual = [debounced(0), debounced(1), debounced(2)]; + assert.deepEqual(actual, [undefined, undefined, undefined]); + assert.strictEqual(callCount, 0); setTimeout(function() { - var result = [debounced('a'), debounced('b')]; - assert.deepEqual(result, ['a', 'a']); + assert.strictEqual(callCount, 1); done(); - }, 64); + }, 5); }); QUnit.test('should apply default options', function(assert) { @@ -4179,6 +4184,23 @@ }, 64); }); + QUnit.test('subsequent leading debounced calls return the last `func` result', function(assert) { + assert.expect(2); + + var done = assert.async(); + + var debounced = _.debounce(identity, 32, { 'leading': true, 'trailing': false }), + result = [debounced('x'), debounced('y')]; + + assert.deepEqual(result, ['x', 'x']); + + setTimeout(function() { + var result = [debounced('a'), debounced('b')]; + assert.deepEqual(result, ['a', 'a']); + done(); + }, 64); + }); + QUnit.test('should support a `trailing` option', function(assert) { assert.expect(4); @@ -4319,7 +4341,7 @@ }, 192); }); - QUnit.test('should invoke the `trailing` call with the correct arguments and `this` binding', function(assert) { + QUnit.test('should invoke the trailing call with the correct arguments and `this` binding', function(assert) { assert.expect(2); var done = assert.async();