-
Notifications
You must be signed in to change notification settings - Fork 110
/
debounceSpec.js
68 lines (50 loc) · 1.93 KB
/
debounceSpec.js
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
import debounce from '../utils/debounce';
describe('debounce test suite', () => {
let functionToDebounce;
beforeEach(() => {
functionToDebounce = jasmine.createSpy('My function to debounce');
jasmine.clock().install();
});
afterEach(() => {
jasmine.clock().uninstall();
});
it('functionToDebounce gets called after the time specified', () => {
const debounced = debounce(functionToDebounce, 1000);
debounced('foo', 'bar');
expect(functionToDebounce).not.toHaveBeenCalled();
jasmine.clock().tick(1001);
expect(functionToDebounce).toHaveBeenCalled();
expect(functionToDebounce.calls.first().args[0]).toBe('foo');
expect(functionToDebounce.calls.first().args[1]).toBe('bar');
});
it('functionToDebounce gets cancelled before time elapses', () => {
const debounced = debounce(functionToDebounce, 1000);
debounced('foo', 'bar');
jasmine.clock().tick(999);
debounced.cancel();
jasmine.clock().tick(2);
expect(functionToDebounce).not.toHaveBeenCalled();
});
it('functionToDebounce gets called once after the time specified', () => {
const debounced = debounce(functionToDebounce, 1000);
debounced('foo', 'bar');
jasmine.clock().tick(999);
debounced('foo', 'bar');
jasmine.clock().tick(999);
debounced('foo', 'bar');
jasmine.clock().tick(1001);
expect(functionToDebounce.calls.count()).toBe(1);
});
it('functionToDebounce gets called once after the time specified with args of last invocation', () => {
const debounced = debounce(functionToDebounce, 1000);
debounced('foo', 'bar');
jasmine.clock().tick(999);
debounced('foo2', 'bar2');
jasmine.clock().tick(999);
debounced('foo3', 'bar3');
jasmine.clock().tick(1001);
expect(functionToDebounce.calls.count()).toBe(1);
expect(functionToDebounce.calls.first().args[0]).toBe('foo3');
expect(functionToDebounce.calls.first().args[1]).toBe('bar3');
});
});