-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
101 lines (68 loc) · 1.78 KB
/
test.js
File metadata and controls
101 lines (68 loc) · 1.78 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import raf from 'raf';
import easydoesit from './index';
raf.polyfill();
test('defers the invocation of the original function till the next animation frame', (done) => {
expect.assertions(2);
const func = jest.fn();
const debounced = easydoesit(func);
debounced();
expect(func).not.toHaveBeenCalled();
raf(() => {
expect(func).toHaveBeenCalled();
done();
});
});
test('subsequent calls return the result of the last invocation', (done) => {
expect.assertions(1);
let result = 0;
const func = jest.fn(() => ++result);
const debounced = easydoesit(func);
debounced();
raf(() => {
expect([
debounced(),
debounced()
]).toEqual([
1,
1
]);
done();
});
});
test('invokes the function with the most recent args', (done) => {
expect.assertions(1);
const func = jest.fn();
const debounced = easydoesit(func);
debounced(1);
debounced(2);
raf(() => {
expect(func).lastCalledWith(2);
done();
});
});
test('flush() calls the original function immediately, cancelling invocation on the next frame', (done) => {
expect.assertions(2);
const func = jest.fn();
const debounced = easydoesit(func);
debounced.flush();
expect(func).toHaveBeenCalledTimes(1);
raf(() => {
expect(func).toHaveBeenCalledTimes(1);
done();
});
});
test('flush() returns the value the original function has returned', () => {
expect.assertions(1);
const debounced = easydoesit(() => 'value');
expect(debounced.flush()).toBe('value');
});
test('allows to cancel() the invocation planned for the next frame', (done) => {
expect.assertions(1);
const func = jest.fn();
const debounced = easydoesit(func);
debounced.cancel();
raf(() => {
expect(func).not.toHaveBeenCalled();
done();
});
});