summaryrefslogtreecommitdiffstats
path: root/tests/debounce.js
blob: 1c8bcf0cb055f135c15e8ebb22ffeeeda226ae27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { debounce } from '..';
import { expect } from 'chai';

/*global describe,it*/

describe('debounce()', () => {
	it('should debounce when used as a simple decorator', next => {
		let c = {
			calls: 0,
			args: null,

			@debounce
			foo(...args) {
				c.calls++;
				c.args = args;
				c.context = this;
			}
		};

		expect(c).to.have.property('calls', 0);
		c.foo(1);
		expect(c).to.have.property('calls', 0);
		c.foo(2);
		c.foo(3);
		setTimeout( () => {
			expect(c).to.have.property('calls', 1);
			expect(c.args).to.deep.equal([3]);
			expect(c.context).to.equal(c);

			next();
		}, 20);
	});

	it('should debounce when used as a function', next => {
		let c = debounce( (...args) => {
				m.calls++;
				m.args = args;
			}),
			m = { calls:0, args:null };

		expect(m).to.have.property('calls', 0);
		c(1);
		expect(m).to.have.property('calls', 0);
		c(2);
		c(3);
		setTimeout( () => {
			expect(m).to.have.property('calls', 1);
			expect(m.args).to.deep.equal([3]);

			next();
		}, 20);
	});

	it('should support passing a delay', next => {
		let c = debounce(5, (...args) => {
				m.calls.push(args);
			}),
			m = { calls:[] };

		c(1);
		setTimeout(()=> c(2), 1);
		setTimeout(()=> c(3), 10);
		setTimeout(()=> c(4), 14);
		setTimeout(()=> c(5), 22);
		expect(m.calls).to.have.length(0);
		setTimeout( () => {
			expect(m.calls).to.deep.equal([ [2], [4], [5] ]);
			next();
		}, 30);
	});
});