11import { act , renderHook } from '@testing-library/react-hooks' ;
2+ import { replaceRaf } from 'raf-stub' ;
23import 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+
413describe ( '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} ) ;
0 commit comments