forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseRafLoop.test.tsx
151 lines (114 loc) Β· 4.01 KB
/
useRafLoop.test.tsx
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
import { renderHook } from '@testing-library/react-hooks';
import { replaceRaf } from 'raf-stub';
import useRafLoop from '../src/useRafLoop';
declare var requestAnimationFrame: {
add: (cb: Function) => number;
remove: (id: number) => void;
flush: (duration?: number) => void;
reset: () => void;
step: (steps?: number, duration?: number) => void;
};
describe('useRafLoop', () => {
beforeAll(() => {
replaceRaf();
});
afterEach(() => {
requestAnimationFrame.reset();
});
it('should be defined', () => {
expect(useRafLoop).toBeDefined();
});
it('should return object with start, stop and isActive functions', () => {
const hook = renderHook(() => useRafLoop(() => false), { initialProps: false });
expect(hook.result.current).toStrictEqual([
expect.any(Function),
expect.any(Function),
expect.any(Function),
]);
});
it('should constantly call callback inside the raf loop', () => {
const spy = jest.fn();
renderHook(() => useRafLoop(spy), { initialProps: false });
expect(spy).not.toBeCalled();
requestAnimationFrame.step(2);
expect(spy).toBeCalledTimes(2);
requestAnimationFrame.step(2);
expect(spy).toBeCalledTimes(4);
});
it('should not start the loop if 2nd hook parameter is falsy', () => {
const spy = jest.fn();
renderHook(() => useRafLoop(spy, false), { initialProps: false });
expect(spy).not.toBeCalled();
requestAnimationFrame.step(2);
expect(spy).not.toBeCalled();
});
it('should pass the time argument to given callback', () => {
const spy = jest.fn();
renderHook(() => useRafLoop(spy), { initialProps: false });
expect(spy).not.toBeCalled();
requestAnimationFrame.step();
expect(typeof spy.mock.calls[0][0]).toBe('number');
});
it('should stop the loop on component unmount', () => {
const spy = jest.fn();
const hook = renderHook(() => useRafLoop(spy), { initialProps: false });
expect(spy).not.toBeCalled();
requestAnimationFrame.step(2);
expect(spy).toBeCalledTimes(2);
hook.unmount();
requestAnimationFrame.step(2);
expect(spy).toBeCalledTimes(2);
});
it('should call the actual callback when it changed', () => {
const spy1 = jest.fn();
const spy2 = jest.fn();
const hook = renderHook(({ cb }) => useRafLoop(cb), { initialProps: { cb: spy1 } });
expect(spy1).not.toBeCalled();
requestAnimationFrame.step(2);
expect(spy1).toBeCalledTimes(2);
hook.rerender({ cb: spy2 });
requestAnimationFrame.step(2);
expect(spy1).toBeCalledTimes(2);
expect(spy2).toBeCalledTimes(2);
});
describe('returned methods', () => {
it('stop method should stop the loop', () => {
const spy = jest.fn();
const hook = renderHook(() => useRafLoop(spy), { initialProps: false });
const [stop] = hook.result.current;
expect(spy).not.toBeCalled();
requestAnimationFrame.step(2);
expect(spy).toBeCalledTimes(2);
stop();
requestAnimationFrame.step(2);
expect(spy).toBeCalledTimes(2);
});
it('start method should start stopped loop', () => {
const spy = jest.fn();
const hook = renderHook(() => useRafLoop(spy, false), { initialProps: false });
const [stop, start] = hook.result.current;
expect(spy).not.toBeCalled();
requestAnimationFrame.step(2);
expect(spy).not.toBeCalled();
start();
requestAnimationFrame.step(2);
expect(spy).toBeCalledTimes(2);
stop();
requestAnimationFrame.step(2);
expect(spy).toBeCalledTimes(2);
start();
requestAnimationFrame.step(2);
expect(spy).toBeCalledTimes(4);
});
it('isActive method should return current loop state', () => {
const spy = jest.fn();
const hook = renderHook(() => useRafLoop(spy, false), { initialProps: false });
const [stop, start, isActive] = hook.result.current;
expect(isActive()).toBe(false);
start();
expect(isActive()).toBe(true);
stop();
expect(isActive()).toBe(false);
});
});
});