-
Notifications
You must be signed in to change notification settings - Fork 741
/
animated-values.test.tsx
158 lines (145 loc) · 5.46 KB
/
animated-values.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { render } from "../../../jest.setup"
import {
motion,
motionValue,
useMotionTemplate,
useMotionValue,
useTransform,
} from "../.."
import { degreesToRadians } from "popmotion"
import * as React from "react"
describe("values prop", () => {
test("Performs animations only on motion values provided via values", async () => {
const promise = new Promise<[number, HTMLElement]>((resolve) => {
const x = motionValue(0)
const ref = React.createRef<HTMLDivElement>()
const Component = () => (
<motion.div
ref={ref}
values={{ x }}
animate={{ x: 20 }}
transition={{ duration: 0.01 }}
onAnimationComplete={() => resolve([x.get(), ref.current!])}
/>
)
const { rerender } = render(<Component />)
rerender(<Component />)
})
await promise.then(([x, element]) => {
expect(x).toBe(20)
expect(element).not.toHaveStyle(
"transform: translateX(20px) translateZ(0)"
)
})
})
test("Still correctly renders values provided via style", async () => {
const promise = new Promise<[number, HTMLElement]>((resolve) => {
const x = motionValue(0)
const ref = React.createRef<HTMLDivElement>()
const Component = () => {
const doubleX = useTransform(x, [0, 1], [0, 2], {
clamp: false,
})
return (
<motion.div
ref={ref}
values={{ x }}
animate={{ x: 20 }}
style={{ x: doubleX }}
transition={{ duration: 0.01 }}
onAnimationComplete={() =>
resolve([x.get(), ref.current!])
}
/>
)
}
const { rerender } = render(<Component />)
rerender(<Component />)
})
await promise.then(([x, element]) => {
expect(x).toBe(20)
expect(element).toHaveStyle(
"transform: translateX(40px) translateZ(0)"
)
})
})
test("Doesn't render custom values", async () => {
const promise = new Promise<[number, HTMLElement]>((resolve) => {
const Component = () => {
const ref = React.useRef<HTMLDivElement>(null)
const distance = useMotionValue(100)
const angle = useMotionValue(45)
const x = useTransform([distance, angle], (latest) => {
return Math.floor(
Math.sin(degreesToRadians(latest[1] as number)) *
(latest[0] as number)
)
})
const y = useTransform([distance, angle], (latest) => {
return Math.floor(
Math.cos(degreesToRadians(latest[1] as number)) *
(latest[0] as number)
)
})
return (
<motion.div
ref={ref}
animate={{ distance: 50 } as any}
values={{ distance }}
style={{ x, y }}
transition={{ duration: 0.01 }}
onAnimationComplete={() =>
resolve([x.get(), ref.current!])
}
/>
)
}
const { rerender } = render(<Component />)
rerender(<Component />)
})
await promise.then(([x, element]) => {
expect(x).toBe(35)
expect(element).toHaveStyle(
"transform: translateX(35px) translateY(35px) translateZ(0)"
)
expect(element).not.toHaveStyle("distance: 50")
})
})
test("Prioritises transform over independent transforms", async () => {
const promise = new Promise<[number, HTMLElement]>((resolve) => {
const Component = () => {
const x = useMotionValue(100)
const scale = useMotionValue(2)
const transform = useMotionTemplate`scale(${scale}) translateX(${x}px)`
const ref = React.useRef<HTMLDivElement>(null)
return (
<motion.div
ref={ref}
animate={{ x: 50 }}
transition={{ duration: 0.01 }}
values={{ x, scale }}
style={{ transform, scale }}
onAnimationComplete={() =>
resolve([x.get(), ref.current!])
}
/>
)
}
const { rerender } = render(<Component />)
rerender(<Component />)
})
await promise.then(([x, element]) => {
expect(x).toBe(50)
expect(element).toHaveStyle("transform: scale(2) translateX(50px)")
})
})
test("Is defensive against undefined motion values", async () => {
render(
<motion.div
animate={{ x: 1 }}
initial={{ x: 0 }}
values={{ x: undefined } as any}
/>
)
})
})