diff --git a/1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/_js.view/test.js b/1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/_js.view/test.js index a7a71c8f..f88d0126 100644 --- a/1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/_js.view/test.js +++ b/1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/_js.view/test.js @@ -1,48 +1,47 @@ -describe("debounce", function() { - before(function() { +describe('debounce', function () { + before(function () { this.clock = sinon.useFakeTimers(); }); - after(function() { + after(function () { this.clock.restore(); }); - it("for one call - runs it after given ms", function () { - const f = sinon.spy(); - const debounced = debounce(f, 1000); - - debounced("test"); - assert(f.notCalled); - this.clock.tick(1000); - assert(f.calledOnceWith("test")); - }); - - it("for 3 calls - runs the last one after given ms", function() { + it('for one call - runs it after given ms', function () { const f = sinon.spy(); const debounced = debounce(f, 1000); - f("a") - setTimeout(() => f("b"), 200); // ignored (too early) - setTimeout(() => f("c"), 500); // runs (1000 ms passed) + debounced('test'); + assert(f.notCalled, 'not called immediately'); + this.clock.tick(1000); + assert(f.calledOnceWith('test'), 'called after 1000ms'); + }); + + it('for 3 calls - runs the last one after given ms', function () { + const f = sinon.spy(); + const debounced = debounce(f, 1000); + + debounced('a'); + setTimeout(() => debounced('b'), 200); // ignored (too early) + setTimeout(() => debounced('c'), 500); // runs (1000 ms passed) this.clock.tick(1000); - assert(f.notCalled); + assert(f.notCalled, 'not called after 1000ms'); this.clock.tick(500); - assert(f.calledOnceWith('c')); + assert(f.calledOnceWith('c'), 'called after 1500ms'); }); - it("keeps the context of the call", function() { + it('keeps the context of the call', function () { let obj = { f() { assert.equal(this, obj); - } + }, }; obj.f = debounce(obj.f, 1000); - obj.f("test"); + obj.f('test'); this.clock.tick(5000); }); - });