11import React , { forwardRef } from 'react'
22import { render , cleanup } from '@testing-library/react'
33import '@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'
66import { a } from '.'
7+ import * as Globals from 'shared/globals'
78
89afterEach ( cleanup )
910
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+
1022describe ( 'animated component' , ( ) => {
1123 it ( 'creates an HTML element from a tag name' , ( ) => {
1224 const AnimatedH1 = a ( 'h1' )
@@ -28,10 +40,10 @@ describe('animated component', () => {
2840 </ h2 >
2941 ) )
3042 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' )
3345 const { queryByTitle } = render (
34- < AnimatedName name = { name as SpringValue < string > } other = "test" >
46+ < AnimatedName name = { name } other = "test" >
3547 { child }
3648 </ AnimatedName >
3749 )
@@ -41,14 +53,15 @@ describe('animated component', () => {
4153 } )
4254
4355 it ( 'accepts Animated values in style prop' , ( ) => {
44- const opacity = new AnimatedValue < number > ( 0 )
56+ const opacity = spring ( 0 )
4557 const { queryByText } = render (
4658 < a . div style = { { opacity, color : 'red' } } > Text</ a . div >
4759 )
4860 const div : any = queryByText ( 'Text' ) !
4961 expect ( div ) . toBeTruthy ( )
5062 expect ( div . style . opacity ) . toBe ( '0' )
51- opacity . setValue ( 1 )
63+ opacity . set ( 1 )
64+ mockRaf . step ( )
5265 expect ( div . style . opacity ) . toBe ( '1' )
5366 } )
5467
@@ -62,7 +75,7 @@ describe('animated component', () => {
6275 </ h2 >
6376 ) )
6477 const AnimatedName = a ( Name )
65- const opacity = new AnimatedValue ( 0.5 )
78+ const opacity = spring ( 0.5 )
6679 const { queryByText } = render (
6780 < AnimatedName
6881 style = { {
@@ -75,16 +88,17 @@ describe('animated component', () => {
7588 const div : any = queryByText ( 'Text' ) !
7689 expect ( div ) . toBeTruthy ( )
7790 expect ( div . style . opacity ) . toBe ( '0.5' )
78- opacity . setValue ( 1 )
91+ opacity . set ( 1 )
92+ mockRaf . step ( )
7993 expect ( div . style . opacity ) . toBe ( '1' )
8094 } )
8195
8296 it ( 'accepts scrollTop and scrollLeft properties' , ( ) => {
83- const scrollTop = new AnimatedValue ( 0 )
97+ const scrollTop = spring ( 0 )
8498 const { queryByTestId } = render (
8599 < a . div
86- scrollTop = { scrollTop as SpringValue < number > }
87- scrollLeft = { new AnimatedValue ( 0 ) as SpringValue < number > }
100+ scrollTop = { scrollTop }
101+ scrollLeft = { 0 }
88102 style = { { height : 100 } }
89103 data-testid = "wrapper" >
90104 < div style = { { height : 200 } } />
@@ -93,7 +107,8 @@ describe('animated component', () => {
93107 const wrapper : any = queryByTestId ( 'wrapper' ) !
94108 expect ( wrapper . scrollTop ) . toBe ( 0 )
95109 expect ( wrapper . scrollLeft ) . toBe ( 0 )
96- scrollTop . setValue ( 20 )
110+ scrollTop . set ( 20 )
111+ mockRaf . step ( )
97112 expect ( wrapper . scrollTop ) . toBe ( 20 )
98113 } )
99114
@@ -122,16 +137,9 @@ describe('animated component', () => {
122137 } )
123138
124139 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
135143
136144 const { queryByTestId } = render (
137145 < a . div style = { { scale, translate, translate3d } } data-testid = "wrapper" />
@@ -170,7 +178,7 @@ describe('animated component', () => {
170178 } )
171179
172180 it ( 'applies `transform:none` when identity transform is detected' , ( ) => {
173- const z = new AnimatedValue ( 0 )
181+ const z = spring ( 0 )
174182 const { queryByTestId } = render (
175183 < a . div
176184 style = { {
@@ -207,3 +215,7 @@ describe('animated component', () => {
207215 expect ( wrapper . style . transform ) . toBe ( 'translateX(40px) scale(1,2)' )
208216 } )
209217} )
218+
219+ function spring < T > ( value : T ) : SpringValue < Animatable < T > > {
220+ return new SpringValue ( 'value' ) . update ( { from : { value } } )
221+ }
0 commit comments