-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathuseRaf.test.ts
More file actions
184 lines (148 loc) Β· 5.42 KB
/
useRaf.test.ts
File metadata and controls
184 lines (148 loc) Β· 5.42 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { act, renderHook } from '@testing-library/react-hooks';
import { replaceRaf } from 'raf-stub';
import useRaf from '../src/useRaf';
/**
* New requestAnimationFrame after being replaced with raf-stub for testing purposes.
*/
interface RequestAnimationFrame {
reset(): void;
step(): void;
}
declare var requestAnimationFrame: RequestAnimationFrame;
replaceRaf();
const fixedStart = 1564949709496;
const spyDateNow = jest.spyOn(Date, 'now').mockImplementation(() => fixedStart);
beforeEach(() => {
jest.useFakeTimers();
requestAnimationFrame.reset();
});
afterEach(() => {
jest.clearAllTimers();
requestAnimationFrame.reset();
});
it('should init percentage of time elapsed', () => {
const { result } = renderHook(() => useRaf());
const timeElapsed = result.current;
expect(timeElapsed).toBe(0);
});
it('should return corresponding percentage of time elapsed for default ms', () => {
const { result } = renderHook(() => useRaf());
expect(result.current).toBe(0);
act(() => {
jest.runOnlyPendingTimers(); // start after delay
spyDateNow.mockImplementationOnce(() => fixedStart + 1e12 * 0.25); // 25%
requestAnimationFrame.step();
});
expect(result.current).toBe(0.25);
act(() => {
spyDateNow.mockImplementationOnce(() => fixedStart + 1e12 * 0.5); // 50%
requestAnimationFrame.step();
});
expect(result.current).toBe(0.5);
act(() => {
spyDateNow.mockImplementationOnce(() => fixedStart + 1e12 * 0.75); // 75%
requestAnimationFrame.step();
});
expect(result.current).toBe(0.75);
act(() => {
spyDateNow.mockImplementationOnce(() => fixedStart + 1e12); // 100%
requestAnimationFrame.step();
});
expect(result.current).toBe(1);
});
it('should return corresponding percentage of time elapsed for custom ms', () => {
const customMs = 2000;
const { result } = renderHook(() => useRaf(customMs));
expect(result.current).toBe(0);
act(() => {
jest.runOnlyPendingTimers(); // start after delay
spyDateNow.mockImplementationOnce(() => fixedStart + customMs * 0.25); // 25%
requestAnimationFrame.step();
});
expect(result.current).toBe(0.25);
act(() => {
spyDateNow.mockImplementationOnce(() => fixedStart + customMs * 0.5); // 50%
requestAnimationFrame.step();
});
expect(result.current).toBe(0.5);
act(() => {
spyDateNow.mockImplementationOnce(() => fixedStart + customMs * 0.75); // 75%
requestAnimationFrame.step();
});
expect(result.current).toBe(0.75);
act(() => {
spyDateNow.mockImplementationOnce(() => fixedStart + customMs); // 100%
requestAnimationFrame.step();
});
expect(result.current).toBe(1);
});
it('should return always 1 after corresponding ms reached', () => {
const { result } = renderHook(() => useRaf());
expect(result.current).toBe(0);
act(() => {
jest.runOnlyPendingTimers(); // start after delay
spyDateNow.mockImplementationOnce(() => fixedStart + 1e12); // 100%
requestAnimationFrame.step();
});
expect(result.current).toBe(1);
act(() => {
spyDateNow.mockImplementationOnce(() => fixedStart + 1e12 * 1.1); // 110%
requestAnimationFrame.step();
});
expect(result.current).toBe(1);
act(() => {
spyDateNow.mockImplementationOnce(() => fixedStart + 1e12 * 3); // 300%
requestAnimationFrame.step();
});
expect(result.current).toBe(1);
});
it('should wait until delay reached to start calculating elapsed percentage', () => {
const customMs = 2000;
const { result } = renderHook(() => useRaf(customMs, 500));
expect(result.current).toBe(0);
act(() => {
jest.advanceTimersByTime(250); // fast-forward only half of custom delay
});
expect(result.current).toBe(0);
act(() => {
jest.advanceTimersByTime(249); // fast-forward 1ms less than custom delay
});
expect(result.current).toBe(0);
act(() => {
jest.advanceTimersByTime(1); // fast-forward exactly to custom delay
// After delay is reached, onStart fires and begins the rAF loop.
// Step one animation frame to see elapsed progress.
spyDateNow.mockImplementationOnce(() => fixedStart + customMs * 0.5);
requestAnimationFrame.step();
});
expect(result.current).not.toBe(0);
});
it('should not immediately complete when ms exceeds setTimeout max (issue #779)', () => {
// setTimeout fires immediately if delay > 2^31-1 (2147483647ms).
// With the default ms=1e12, the stop timer must NOT fire immediately.
const { result } = renderHook(() => useRaf());
// After starting (run the delay timer), elapsed should still be 0
// because no animation frames have run yet.
act(() => {
jest.runOnlyPendingTimers(); // start after delay=0
});
// If the bug is present, the stop setTimeout(cb, 1e12) fires immediately
// and sets elapsed to 1. With the fix, it should still be 0.
expect(result.current).toBe(0);
// Stepping one frame with a small time elapsed should give a tiny fraction, not 1
act(() => {
spyDateNow.mockImplementationOnce(() => fixedStart + 100);
requestAnimationFrame.step();
});
expect(result.current).toBeGreaterThan(0);
expect(result.current).toBeLessThan(1);
});
it('should clear pending timers on unmount', () => {
const spyRafStop = jest.spyOn(global, 'cancelAnimationFrame' as any);
const { unmount } = renderHook(() => useRaf());
expect(clearTimeout).not.toHaveBeenCalled();
expect(spyRafStop).not.toHaveBeenCalled();
unmount();
expect(clearTimeout).toHaveBeenCalledTimes(2);
expect(spyRafStop).toHaveBeenCalledTimes(1);
});