|
| 1 | +var clock, original, debounced; |
| 2 | + |
| 3 | +var firedOnce = function(message) { |
| 4 | + ok(original.calledOnce, message); |
| 5 | +}; |
| 6 | + |
| 7 | +var notFired = function(message) { |
| 8 | + ok(!original.called, message); |
| 9 | +}; |
| 10 | + |
| 11 | +module("Discourse.debounce", { |
| 12 | + setup: function() { |
| 13 | + clock = sinon.useFakeTimers(); |
| 14 | + original = sinon.spy(); |
| 15 | + debounced = Discourse.debounce(original, 100); |
| 16 | + }, |
| 17 | + |
| 18 | + teardown: function() { |
| 19 | + clock.restore(); |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +test("delays function execution till the end of the timeout", function() { |
| 24 | + debounced(); |
| 25 | + notFired("immediately after calling debounced function nothing happens"); |
| 26 | + |
| 27 | + clock.tick(99); |
| 28 | + notFired("just before the end of the timeout still nothing happens"); |
| 29 | + |
| 30 | + clock.tick(1); |
| 31 | + firedOnce("exactly at the end of the timeout the function is executed"); |
| 32 | +}); |
| 33 | + |
| 34 | +test("executes delayed function only once, no matter how many times debounced function is called during the timeout", function() { |
| 35 | + debounced(); |
| 36 | + debounced(); |
| 37 | + |
| 38 | + clock.tick(100); |
| 39 | + firedOnce("second call was supressed"); |
| 40 | +}); |
| 41 | + |
| 42 | +test("does not prolong the timeout when the debounced function is called for the second time during the timeout", function() { |
| 43 | + debounced(); |
| 44 | + |
| 45 | + clock.tick(50); |
| 46 | + debounced(); |
| 47 | + |
| 48 | + clock.tick(50); |
| 49 | + firedOnce("function is executed exactly at the end of the original timeout"); |
| 50 | +}); |
| 51 | + |
| 52 | +test("returns a JS timer handle that allows delayed execution to be cancelled before the timeout ends", function() { |
| 53 | + var timerId = debounced(); |
| 54 | + |
| 55 | + clock.tick(50); |
| 56 | + clearTimeout(timerId); |
| 57 | + |
| 58 | + clock.tick(50); |
| 59 | + notFired("timeout has ended but function was not executed"); |
| 60 | +}); |
| 61 | + |
| 62 | +test("preserves first call's context and params when executing delayed function", function() { |
| 63 | + var firstObj = {}; |
| 64 | + var secondObj = {}; |
| 65 | + |
| 66 | + debounced.call(firstObj, "first"); |
| 67 | + debounced.call(secondObj, "second"); |
| 68 | + |
| 69 | + clock.tick(100); |
| 70 | + ok(original.calledOn(firstObj), "the context of the first of two subsequent calls is preserved"); |
| 71 | + ok(original.calledWithExactly("first"), "param passed during the first of two subsequent calls is preserved"); |
| 72 | +}); |
| 73 | + |
| 74 | +test("can be called again (with a different context and params) after timeout passes", function() { |
| 75 | + var firstObj = {}; |
| 76 | + var secondObj = {}; |
| 77 | + |
| 78 | + debounced.call(firstObj, "first"); |
| 79 | + |
| 80 | + clock.tick(100); |
| 81 | + debounced.call(secondObj, "second"); |
| 82 | + |
| 83 | + clock.tick(100); |
| 84 | + ok(original.calledOn(secondObj), "function is executed with the context of the call made after the timeout has passed"); |
| 85 | + ok(original.calledWithExactly("second"), "function is executed with the param passed to the call made after the timeout has passed"); |
| 86 | +}); |
0 commit comments