-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseRaf.test.ts
155 lines (123 loc) Β· 4.24 KB
/
useRaf.test.ts
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
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 { result } = renderHook(() => useRaf(undefined, 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
});
expect(result.current).not.toBe(0);
});
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);
});