Skip to content

Commit 09167df

Browse files
authored
fix: succeed useRafLoop tests
2 parents 6e5cfac + be950bb commit 09167df

File tree

3 files changed

+56
-59
lines changed

3 files changed

+56
-59
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"lint-staged": "9.2.3",
8787
"markdown-loader": "5.1.0",
8888
"prettier": "1.18.2",
89+
"raf-stub": "^3.0.0",
8990
"react": "16.9.0",
9091
"react-dom": "16.9.0",
9192
"react-spring": "8.0.27",

src/__tests__/useRafLoop.test.tsx

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import { act, renderHook } from '@testing-library/react-hooks';
2+
import { replaceRaf } from 'raf-stub';
23
import useRafLoop from '../useRafLoop';
34

5+
declare var requestAnimationFrame: {
6+
add: (cb: Function) => number;
7+
remove: (id: number) => void;
8+
flush: (duration?: number) => void;
9+
reset: () => void;
10+
step: (steps?: number, duration?: number) => void;
11+
};
12+
413
describe('useRafLoop', () => {
5-
it('should be defined', () => {
6-
expect(useRafLoop).toBeDefined();
14+
beforeAll(() => {
15+
replaceRaf();
716
});
817

9-
it('should call a callback constantly inside the raf loop', done => {
10-
let calls = 0;
11-
const spy = () => calls++;
12-
renderHook(() => useRafLoop(spy), { initialProps: false });
13-
14-
expect(calls).toEqual(0);
15-
16-
setTimeout(() => {
17-
expect(calls).toBeGreaterThanOrEqual(2);
18+
afterEach(() => {
19+
requestAnimationFrame.reset();
20+
});
1821

19-
done();
20-
}, 120);
22+
it('should be defined', () => {
23+
expect(useRafLoop).toBeDefined();
2124
});
2225

2326
it('should return stop function, start function and loop state', () => {
@@ -28,26 +31,31 @@ describe('useRafLoop', () => {
2831
expect(typeof hook.result.current[2]).toEqual('function');
2932
});
3033

31-
it('first element call should stop the loop', done => {
32-
let calls = 0;
33-
const spy = () => calls++;
34+
it('should call a callback constantly inside the raf loop', () => {
35+
const spy = jest.fn();
36+
renderHook(() => useRafLoop(spy), { initialProps: false });
37+
38+
expect(spy).not.toBeCalled();
39+
requestAnimationFrame.step();
40+
requestAnimationFrame.step();
41+
expect(spy).toBeCalledTimes(2);
42+
});
43+
44+
it('first element call should stop the loop', () => {
45+
const spy = jest.fn();
3446
const hook = renderHook(() => useRafLoop(spy), { initialProps: false });
3547

36-
// stop the loop
48+
expect(spy).not.toBeCalled();
49+
3750
act(() => {
3851
hook.result.current[0]();
3952
});
40-
41-
setTimeout(() => {
42-
expect(calls).toEqual(0);
43-
44-
done();
45-
}, 50);
53+
requestAnimationFrame.step();
54+
expect(spy).not.toBeCalled();
4655
});
4756

48-
it('second element should represent loop state', done => {
49-
let calls = 0;
50-
const spy = () => calls++;
57+
it('second element should represent loop state', () => {
58+
const spy = jest.fn();
5159
const hook = renderHook(() => useRafLoop(spy), { initialProps: false });
5260

5361
expect(hook.result.current[1]).toBe(true);
@@ -56,56 +64,39 @@ describe('useRafLoop', () => {
5664
act(() => {
5765
hook.result.current[0]();
5866
});
59-
6067
expect(hook.result.current[1]).toBe(false);
61-
setTimeout(() => {
62-
expect(calls).toEqual(0);
63-
64-
done();
65-
}, 120);
6668
});
6769

68-
it('third element call should restart loop', done => {
69-
let calls = 0;
70-
const spy = () => calls++;
70+
it('third element call should restart loop', () => {
71+
const spy = jest.fn();
7172
const hook = renderHook(() => useRafLoop(spy), { initialProps: false });
7273

73-
expect(hook.result.current[1]).toBe(true);
74-
74+
expect(spy).not.toBeCalled();
7575
// stop the loop
7676
act(() => {
7777
hook.result.current[0]();
7878
});
79+
requestAnimationFrame.step();
80+
expect(spy).not.toBeCalled();
7981

80-
setTimeout(() => {
81-
expect(hook.result.current[1]).toBe(false);
82-
expect(calls).toEqual(0);
83-
84-
// start the loop
85-
act(() => {
86-
hook.result.current[2]();
87-
});
88-
89-
setTimeout(() => {
90-
expect(hook.result.current[1]).toBe(true);
91-
expect(calls).toBeGreaterThanOrEqual(2);
82+
// start the loop
83+
act(() => {
84+
hook.result.current[2]();
85+
});
9286

93-
done();
94-
}, 120);
95-
}, 50);
87+
requestAnimationFrame.step();
88+
requestAnimationFrame.step();
89+
expect(spy).toBeCalledTimes(2);
9690
});
9791

98-
it('loop should stop itself on unmount', done => {
99-
let calls = 0;
100-
const spy = () => calls++;
92+
it('loop should stop itself on unmount', () => {
93+
const spy = jest.fn();
10194
const hook = renderHook(() => useRafLoop(spy), { initialProps: false });
10295

10396
hook.unmount();
10497

105-
setTimeout(() => {
106-
expect(calls).toEqual(0);
98+
requestAnimationFrame.step();
10799

108-
done();
109-
}, 50);
100+
expect(spy).not.toBeCalled();
110101
});
111102
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9973,6 +9973,11 @@ raf-schd@^4.0.0:
99739973
resolved "https://registry.yarnpkg.com/raf-schd/-/raf-schd-4.0.2.tgz#bd44c708188f2e84c810bf55fcea9231bcaed8a0"
99749974
integrity sha512-VhlMZmGy6A6hrkJWHLNTGl5gtgMUm+xfGza6wbwnE914yeQ5Ybm18vgM734RZhMgfw4tacUrWseGZlpUrrakEQ==
99759975

9976+
raf-stub@^3.0.0:
9977+
version "3.0.0"
9978+
resolved "https://registry.yarnpkg.com/raf-stub/-/raf-stub-3.0.0.tgz#40e53dc3ad3b241311f914bbd41dc11a2c9ee0a9"
9979+
integrity sha512-64wjDTI8NAkplC3WYF3DUBXmdx8AZF0ubxiicZi83BKW5hcdvMtbwDe6gpFBngTo6+XIJbfwmUP8lMa85UPK6A==
9980+
99769981
raf@^3.4.0:
99779982
version "3.4.0"
99789983
resolved "https://registry.yarnpkg.com/raf/-/raf-3.4.0.tgz#a28876881b4bc2ca9117d4138163ddb80f781575"

0 commit comments

Comments
 (0)