1
1
import { act , renderHook } from '@testing-library/react-hooks' ;
2
+ import { replaceRaf } from 'raf-stub' ;
2
3
import useRafLoop from '../useRafLoop' ;
3
4
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
+
4
13
describe ( 'useRafLoop' , ( ) => {
5
- it ( 'should be defined' , ( ) => {
6
- expect ( useRafLoop ) . toBeDefined ( ) ;
14
+ beforeAll ( ( ) => {
15
+ replaceRaf ( ) ;
7
16
} ) ;
8
17
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
+ } ) ;
18
21
19
- done ( ) ;
20
- } , 120 ) ;
22
+ it ( 'should be defined' , ( ) => {
23
+ expect ( useRafLoop ) . toBeDefined ( ) ;
21
24
} ) ;
22
25
23
26
it ( 'should return stop function, start function and loop state' , ( ) => {
@@ -28,26 +31,31 @@ describe('useRafLoop', () => {
28
31
expect ( typeof hook . result . current [ 2 ] ) . toEqual ( 'function' ) ;
29
32
} ) ;
30
33
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 ( ) ;
34
46
const hook = renderHook ( ( ) => useRafLoop ( spy ) , { initialProps : false } ) ;
35
47
36
- // stop the loop
48
+ expect ( spy ) . not . toBeCalled ( ) ;
49
+
37
50
act ( ( ) => {
38
51
hook . result . current [ 0 ] ( ) ;
39
52
} ) ;
40
-
41
- setTimeout ( ( ) => {
42
- expect ( calls ) . toEqual ( 0 ) ;
43
-
44
- done ( ) ;
45
- } , 50 ) ;
53
+ requestAnimationFrame . step ( ) ;
54
+ expect ( spy ) . not . toBeCalled ( ) ;
46
55
} ) ;
47
56
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 ( ) ;
51
59
const hook = renderHook ( ( ) => useRafLoop ( spy ) , { initialProps : false } ) ;
52
60
53
61
expect ( hook . result . current [ 1 ] ) . toBe ( true ) ;
@@ -56,56 +64,39 @@ describe('useRafLoop', () => {
56
64
act ( ( ) => {
57
65
hook . result . current [ 0 ] ( ) ;
58
66
} ) ;
59
-
60
67
expect ( hook . result . current [ 1 ] ) . toBe ( false ) ;
61
- setTimeout ( ( ) => {
62
- expect ( calls ) . toEqual ( 0 ) ;
63
-
64
- done ( ) ;
65
- } , 120 ) ;
66
68
} ) ;
67
69
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 ( ) ;
71
72
const hook = renderHook ( ( ) => useRafLoop ( spy ) , { initialProps : false } ) ;
72
73
73
- expect ( hook . result . current [ 1 ] ) . toBe ( true ) ;
74
-
74
+ expect ( spy ) . not . toBeCalled ( ) ;
75
75
// stop the loop
76
76
act ( ( ) => {
77
77
hook . result . current [ 0 ] ( ) ;
78
78
} ) ;
79
+ requestAnimationFrame . step ( ) ;
80
+ expect ( spy ) . not . toBeCalled ( ) ;
79
81
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
+ } ) ;
92
86
93
- done ( ) ;
94
- } , 120 ) ;
95
- } , 50 ) ;
87
+ requestAnimationFrame . step ( ) ;
88
+ requestAnimationFrame . step ( ) ;
89
+ expect ( spy ) . toBeCalledTimes ( 2 ) ;
96
90
} ) ;
97
91
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 ( ) ;
101
94
const hook = renderHook ( ( ) => useRafLoop ( spy ) , { initialProps : false } ) ;
102
95
103
96
hook . unmount ( ) ;
104
97
105
- setTimeout ( ( ) => {
106
- expect ( calls ) . toEqual ( 0 ) ;
98
+ requestAnimationFrame . step ( ) ;
107
99
108
- done ( ) ;
109
- } , 50 ) ;
100
+ expect ( spy ) . not . toBeCalled ( ) ;
110
101
} ) ;
111
102
} ) ;
0 commit comments