1
1
import React , { forwardRef } from 'react'
2
2
import { render , cleanup } from '@testing-library/react'
3
3
import '@testing-library/jest-dom/extend-expect'
4
- import { AnimatedValue , AnimatedArray } from '@react-spring/animated '
5
- import { SpringValue } from '@react-spring/core'
4
+ import createMockRaf , { MockRaf } from 'mock-raf '
5
+ import { FrameLoop , SpringValue , Animatable } from '@react-spring/core'
6
6
import { a } from '.'
7
+ import * as Globals from 'shared/globals'
7
8
8
9
afterEach ( cleanup )
9
10
11
+ let mockRaf : MockRaf
12
+ beforeEach ( ( ) => {
13
+ mockRaf = createMockRaf ( )
14
+ Globals . assign ( {
15
+ performanceNow : mockRaf . now ,
16
+ requestAnimationFrame : mockRaf . raf ,
17
+ cancelAnimationFrame : mockRaf . cancel ,
18
+ frameLoop : new FrameLoop ( ) ,
19
+ } )
20
+ } )
21
+
10
22
describe ( 'animated component' , ( ) => {
11
23
it ( 'creates an HTML element from a tag name' , ( ) => {
12
24
const AnimatedH1 = a ( 'h1' )
@@ -28,10 +40,10 @@ describe('animated component', () => {
28
40
</ h2 >
29
41
) )
30
42
const AnimatedName = a ( Name )
31
- const child = new AnimatedValue ( 'Animated Text' )
32
- const name = new AnimatedValue ( 'name' )
43
+ const child = spring ( 'Animated Text' )
44
+ const name = spring ( 'name' )
33
45
const { queryByTitle } = render (
34
- < AnimatedName name = { name as SpringValue < string > } other = "test" >
46
+ < AnimatedName name = { name } other = "test" >
35
47
{ child }
36
48
</ AnimatedName >
37
49
)
@@ -41,14 +53,15 @@ describe('animated component', () => {
41
53
} )
42
54
43
55
it ( 'accepts Animated values in style prop' , ( ) => {
44
- const opacity = new AnimatedValue < number > ( 0 )
56
+ const opacity = spring ( 0 )
45
57
const { queryByText } = render (
46
58
< a . div style = { { opacity, color : 'red' } } > Text</ a . div >
47
59
)
48
60
const div : any = queryByText ( 'Text' ) !
49
61
expect ( div ) . toBeTruthy ( )
50
62
expect ( div . style . opacity ) . toBe ( '0' )
51
- opacity . setValue ( 1 )
63
+ opacity . set ( 1 )
64
+ mockRaf . step ( )
52
65
expect ( div . style . opacity ) . toBe ( '1' )
53
66
} )
54
67
@@ -62,7 +75,7 @@ describe('animated component', () => {
62
75
</ h2 >
63
76
) )
64
77
const AnimatedName = a ( Name )
65
- const opacity = new AnimatedValue ( 0.5 )
78
+ const opacity = spring ( 0.5 )
66
79
const { queryByText } = render (
67
80
< AnimatedName
68
81
style = { {
@@ -75,16 +88,17 @@ describe('animated component', () => {
75
88
const div : any = queryByText ( 'Text' ) !
76
89
expect ( div ) . toBeTruthy ( )
77
90
expect ( div . style . opacity ) . toBe ( '0.5' )
78
- opacity . setValue ( 1 )
91
+ opacity . set ( 1 )
92
+ mockRaf . step ( )
79
93
expect ( div . style . opacity ) . toBe ( '1' )
80
94
} )
81
95
82
96
it ( 'accepts scrollTop and scrollLeft properties' , ( ) => {
83
- const scrollTop = new AnimatedValue ( 0 )
97
+ const scrollTop = spring ( 0 )
84
98
const { queryByTestId } = render (
85
99
< a . div
86
- scrollTop = { scrollTop as SpringValue < number > }
87
- scrollLeft = { new AnimatedValue ( 0 ) as SpringValue < number > }
100
+ scrollTop = { scrollTop }
101
+ scrollLeft = { 0 }
88
102
style = { { height : 100 } }
89
103
data-testid = "wrapper" >
90
104
< div style = { { height : 200 } } />
@@ -93,7 +107,8 @@ describe('animated component', () => {
93
107
const wrapper : any = queryByTestId ( 'wrapper' ) !
94
108
expect ( wrapper . scrollTop ) . toBe ( 0 )
95
109
expect ( wrapper . scrollLeft ) . toBe ( 0 )
96
- scrollTop . setValue ( 20 )
110
+ scrollTop . set ( 20 )
111
+ mockRaf . step ( )
97
112
expect ( wrapper . scrollTop ) . toBe ( 20 )
98
113
} )
99
114
@@ -122,16 +137,9 @@ describe('animated component', () => {
122
137
} )
123
138
124
139
it ( 'accepts Animated values or Animated arrays as attributes' , ( ) => {
125
- const scale = new AnimatedValue ( 2 )
126
- const translate : any = new AnimatedArray ( [
127
- new AnimatedValue ( 10 ) ,
128
- new AnimatedValue ( 20 ) ,
129
- ] )
130
- const translate3d : [
131
- AnimatedValue < number > ,
132
- AnimatedValue < number > ,
133
- string
134
- ] = [ new AnimatedValue ( 30 ) , new AnimatedValue ( 40 ) , '50px' ]
140
+ const scale = spring ( 2 )
141
+ const translate = spring ( [ 10 , 20 ] as const )
142
+ const translate3d = [ spring ( 30 ) , spring ( 40 ) , '50px' ] as const
135
143
136
144
const { queryByTestId } = render (
137
145
< a . div style = { { scale, translate, translate3d } } data-testid = "wrapper" />
@@ -170,7 +178,7 @@ describe('animated component', () => {
170
178
} )
171
179
172
180
it ( 'applies `transform:none` when identity transform is detected' , ( ) => {
173
- const z = new AnimatedValue ( 0 )
181
+ const z = spring ( 0 )
174
182
const { queryByTestId } = render (
175
183
< a . div
176
184
style = { {
@@ -207,3 +215,7 @@ describe('animated component', () => {
207
215
expect ( wrapper . style . transform ) . toBe ( 'translateX(40px) scale(1,2)' )
208
216
} )
209
217
} )
218
+
219
+ function spring < T > ( value : T ) : SpringValue < Animatable < T > > {
220
+ return new SpringValue ( 'value' ) . update ( { from : { value } } )
221
+ }
0 commit comments