From 02068525759c6ee4cbc2178c04ceb30372f2ac40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Gro=CC=88nke?= Date: Thu, 30 Jul 2015 20:17:35 +0200 Subject: [PATCH] add more debouncer tests --- test/unit/utils.html | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/unit/utils.html b/test/unit/utils.html index bebd5ca94d..9e142d8935 100644 --- a/test/unit/utils.html +++ b/test/unit/utils.html @@ -94,6 +94,71 @@ }); + test('debounce flushing', function(done) { + + var called = 0; + var cb = function() { + called++; + }; + + window.el1.debounce('foo', cb); + window.el1.flushDebouncer('foo'); + window.el1.debounce('foo', cb); + + setTimeout(function() { + assert.equal(called, 2, 'debounce should be called twice'); + done(); + }, 100); + + }); + + test('debounce state', function(done) { + + window.el1.debounce('foo', function() { + assert.equal(window.el1.isDebouncerActive('foo'), false, 'debouncer is inactive after resolution'); + done(); + }); + + assert.equal(window.el1.isDebouncerActive('foo'), true, 'debouncer is active immediately after triggering'); + + }); + + test('debounce cancelling', function(done) { + + var triggered = false; + + window.el1.debounce('foo', function() { + triggered = true; + }); + window.el1.cancelDebouncer('foo'); + assert.equal(window.el1.isDebouncerActive('foo'), false, 'debouncer is inactive after cancelling'); + + setTimeout(function() { + assert.equal(triggered, false, 'debounce never fired'); + done(); + }, 100); + + }); + + test('duplicate debouncer assignment', function(done) { + + var a, b; + + window.el1.debounce('foo', function() { + a = 'foo'; + }); + window.el1.debounce('foo', function() { + b = 'bar'; + }); + + setTimeout(function() { + assert.equal(a, undefined, 'first debouncer was never fired'); + assert.equal(b, 'bar', 'only the second debouncer callback was executed'); + done(); + }, 100); + + }); + });