From 016de80ce756a535a69a4bf0b51a1c66538ced2c Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 24 Mar 2016 00:35:22 -0700 Subject: [PATCH] Add debounce/throttle test for calling `cancel` and `flush` without anything queued. --- lodash.js | 7 +++---- test/test.js | 23 +++++++++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lodash.js b/lodash.js index 9a23ca301..2e77f23c0 100644 --- a/lodash.js +++ b/lodash.js @@ -9137,10 +9137,9 @@ } function trailingEdge(time) { - if (timerId !== undefined) { - clearTimeout(timerId); - timerId = undefined; - } + clearTimeout(timerId); + timerId = undefined; + // Only invoke if we have `lastArgs` which means `func` has been // debounced at least once. if (trailing && lastArgs) { diff --git a/test/test.js b/test/test.js index a7e6eac41..0a6bf1f92 100644 --- a/test/test.js +++ b/test/test.js @@ -21782,14 +21782,33 @@ }, 32, { 'leading': false }); funced(); - var actual = funced.flush(); + assert.strictEqual(funced.flush(), 1); setTimeout(function() { - assert.strictEqual(actual, 1); assert.strictEqual(callCount, 1); done(); }, 64); }); + + QUnit.test('_.' + methodName + ' should noop `cancel` and `flush` when nothing is queued', function(assert) { + assert.expect(2); + + var done = assert.async(); + + var callCount = 0; + + var funced = func(function() { + callCount++; + }, 32); + + funced.cancel(); + assert.strictEqual(funced.flush(), undefined); + + setTimeout(function() { + assert.strictEqual(callCount, 0); + done(); + }, 64); + }); }); /*--------------------------------------------------------------------------*/