|
1 | | -describe("debounce", function() { |
2 | | - before(function() { |
| 1 | +describe('debounce', function () { |
| 2 | + before(function () { |
3 | 3 | this.clock = sinon.useFakeTimers(); |
4 | 4 | }); |
5 | 5 |
|
6 | | - after(function() { |
| 6 | + after(function () { |
7 | 7 | this.clock.restore(); |
8 | 8 | }); |
9 | 9 |
|
10 | | - it("for one call - runs it after given ms", function () { |
| 10 | + it('for one call - runs it after given ms', function () { |
11 | 11 | 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'); |
16 | 16 | this.clock.tick(1000); |
17 | | - assert(f.calledOnceWith("test")); |
| 17 | + assert(f.calledOnceWith('test'), 'called after 1000ms'); |
18 | 18 | }); |
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 () { |
21 | 21 | const f = sinon.spy(); |
22 | 22 | const debounced = debounce(f, 1000); |
23 | 23 |
|
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) |
27 | 27 | this.clock.tick(1000); |
28 | 28 |
|
29 | | - assert(f.notCalled); |
| 29 | + assert(f.notCalled, 'not called after 1000ms'); |
30 | 30 |
|
31 | 31 | this.clock.tick(500); |
32 | 32 |
|
33 | | - assert(f.calledOnceWith('c')); |
| 33 | + assert(f.calledOnceWith('c'), 'called after 1500ms'); |
34 | 34 | }); |
35 | 35 |
|
36 | | - it("keeps the context of the call", function() { |
| 36 | + it('keeps the context of the call', function () { |
37 | 37 | let obj = { |
38 | 38 | f() { |
39 | 39 | assert.equal(this, obj); |
40 | | - } |
| 40 | + }, |
41 | 41 | }; |
42 | 42 |
|
43 | 43 | obj.f = debounce(obj.f, 1000); |
44 | | - obj.f("test"); |
| 44 | + obj.f('test'); |
45 | 45 | this.clock.tick(5000); |
46 | 46 | }); |
47 | | - |
48 | 47 | }); |
0 commit comments