Skip to content

Commit 844b96a

Browse files
committed
fix debounce
1 parent 80f2bec commit 844b96a

File tree

1 file changed

+19
-20
lines changed
  • 1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/_js.view

1 file changed

+19
-20
lines changed
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
1-
describe("debounce", function() {
2-
before(function() {
1+
describe('debounce', function () {
2+
before(function () {
33
this.clock = sinon.useFakeTimers();
44
});
55

6-
after(function() {
6+
after(function () {
77
this.clock.restore();
88
});
99

10-
it("for one call - runs it after given ms", function () {
10+
it('for one call - runs it after given ms', function () {
1111
const f = sinon.spy();
12-
const debounced = debounce(f, 1000);
13-
14-
debounced("test");
15-
assert(f.notCalled);
12+
const debounced = debounce(f, 1000);
13+
14+
debounced('test');
15+
assert(f.notCalled, 'not called immediately');
1616
this.clock.tick(1000);
17-
assert(f.calledOnceWith("test"));
17+
assert(f.calledOnceWith('test'), 'called after 1000ms');
1818
});
19-
20-
it("for 3 calls - runs the last one after given ms", function() {
19+
20+
it('for 3 calls - runs the last one after given ms', function () {
2121
const f = sinon.spy();
2222
const debounced = debounce(f, 1000);
2323

24-
f("a")
25-
setTimeout(() => f("b"), 200); // ignored (too early)
26-
setTimeout(() => f("c"), 500); // runs (1000 ms passed)
24+
debounced('a');
25+
setTimeout(() => debounced('b'), 200); // ignored (too early)
26+
setTimeout(() => debounced('c'), 500); // runs (1000 ms passed)
2727
this.clock.tick(1000);
2828

29-
assert(f.notCalled);
29+
assert(f.notCalled, 'not called after 1000ms');
3030

3131
this.clock.tick(500);
3232

33-
assert(f.calledOnceWith('c'));
33+
assert(f.calledOnceWith('c'), 'called after 1500ms');
3434
});
3535

36-
it("keeps the context of the call", function() {
36+
it('keeps the context of the call', function () {
3737
let obj = {
3838
f() {
3939
assert.equal(this, obj);
40-
}
40+
},
4141
};
4242

4343
obj.f = debounce(obj.f, 1000);
44-
obj.f("test");
44+
obj.f('test');
4545
this.clock.tick(5000);
4646
});
47-
4847
});

0 commit comments

Comments
 (0)