|
1 |
| -import { frame, motion, useMotionValue, useWillChange } from "../../.." |
| 1 | +import { |
| 2 | + frame, |
| 3 | + motion, |
| 4 | + MotionGlobalConfig, |
| 5 | + useMotionValue, |
| 6 | + useWillChange, |
| 7 | +} from "../../.." |
2 | 8 | import { render } from "../../../../jest.setup"
|
3 | 9 | import { nextFrame } from "../../../gestures/__tests__/utils"
|
4 | 10 | import { WillChangeMotionValue } from "../WillChangeMotionValue"
|
@@ -159,3 +165,145 @@ describe("willChange", () => {
|
159 | 165 | expect(container.firstChild).toHaveStyle("will-change: transform;")
|
160 | 166 | })
|
161 | 167 | })
|
| 168 | + |
| 169 | +describe("willChange injection", () => { |
| 170 | + beforeAll(() => { |
| 171 | + MotionGlobalConfig.WillChange = WillChangeMotionValue |
| 172 | + }) |
| 173 | + |
| 174 | + afterAll(() => { |
| 175 | + MotionGlobalConfig.WillChange = undefined |
| 176 | + }) |
| 177 | + |
| 178 | + test("Renders values defined in animate on initial render", async () => { |
| 179 | + const Component = () => { |
| 180 | + const opacity = useMotionValue(0) |
| 181 | + return ( |
| 182 | + <motion.div |
| 183 | + animate={{ x: 100, backgroundColor: "#000" }} |
| 184 | + style={{ opacity }} |
| 185 | + /> |
| 186 | + ) |
| 187 | + } |
| 188 | + |
| 189 | + const { container, rerender } = render(<Component />) |
| 190 | + rerender(<Component />) |
| 191 | + |
| 192 | + await nextFrame() |
| 193 | + |
| 194 | + expect(container.firstChild).toHaveStyle("will-change: transform;") |
| 195 | + }) |
| 196 | + |
| 197 | + test("Doesn't render CSS variables or non-hardware accelerated values", async () => { |
| 198 | + const Component = () => { |
| 199 | + return ( |
| 200 | + <motion.div |
| 201 | + animate={ |
| 202 | + { |
| 203 | + filter: "blur(10px)", |
| 204 | + background: "#000", |
| 205 | + "--test": "#000", |
| 206 | + } as any |
| 207 | + } |
| 208 | + /> |
| 209 | + ) |
| 210 | + } |
| 211 | + |
| 212 | + const { container } = render(<Component />) |
| 213 | + |
| 214 | + await nextFrame() |
| 215 | + |
| 216 | + expect(container.firstChild).toHaveStyle("will-change: filter;") |
| 217 | + }) |
| 218 | + |
| 219 | + test("Externally-provided motion values are not added to will-change", async () => { |
| 220 | + const Component = () => { |
| 221 | + const opacity = useMotionValue(0) |
| 222 | + const height = useMotionValue(100) |
| 223 | + return <motion.div style={{ opacity, height }} /> |
| 224 | + } |
| 225 | + |
| 226 | + const { container } = render(<Component />) |
| 227 | + |
| 228 | + expect(container.firstChild).not.toHaveStyle("will-change: opacity;") |
| 229 | + expect(container.firstChild).not.toHaveStyle( |
| 230 | + "will-change: height, opacity;" |
| 231 | + ) |
| 232 | + expect(container.firstChild).not.toHaveStyle( |
| 233 | + "will-change: opacity, height;" |
| 234 | + ) |
| 235 | + }) |
| 236 | + |
| 237 | + test("Don't remove values when they finish animating", async () => { |
| 238 | + return new Promise<void>(async (resolve) => { |
| 239 | + const Component = () => { |
| 240 | + return ( |
| 241 | + <motion.div |
| 242 | + transition={{ duration: 0.1 }} |
| 243 | + animate={{ x: 100 }} |
| 244 | + onAnimationComplete={() => { |
| 245 | + frame.postRender(() => { |
| 246 | + expect(container.firstChild).toHaveStyle( |
| 247 | + "will-change: transform;" |
| 248 | + ) |
| 249 | + resolve() |
| 250 | + }) |
| 251 | + }} |
| 252 | + /> |
| 253 | + ) |
| 254 | + } |
| 255 | + |
| 256 | + const { container } = render(<Component />) |
| 257 | + |
| 258 | + await nextFrame() |
| 259 | + |
| 260 | + expect(container.firstChild).toHaveStyle("will-change: transform;") |
| 261 | + }) |
| 262 | + }) |
| 263 | + |
| 264 | + test("Add values when they start animating", async () => { |
| 265 | + const Component = ({ animate }: any) => { |
| 266 | + return ( |
| 267 | + <motion.div |
| 268 | + initial={false} |
| 269 | + animate={animate} |
| 270 | + transition={{ duration: 0.1 }} |
| 271 | + /> |
| 272 | + ) |
| 273 | + } |
| 274 | + const { container, rerender } = render(<Component animate={{}} />) |
| 275 | + await nextFrame() |
| 276 | + |
| 277 | + expect(container.firstChild).not.toHaveStyle("will-change: transform;") |
| 278 | + rerender(<Component animate={{ x: 100 }} />) |
| 279 | + |
| 280 | + await nextFrame() |
| 281 | + |
| 282 | + expect(container.firstChild).toHaveStyle("will-change: transform;") |
| 283 | + }) |
| 284 | + |
| 285 | + test("Doesn't remove values when animation interrupted", async () => { |
| 286 | + const Component = ({ animate }: any) => { |
| 287 | + return ( |
| 288 | + <motion.div |
| 289 | + initial={{ x: 0 }} |
| 290 | + animate={animate} |
| 291 | + transition={{ duration: 0.1 }} |
| 292 | + /> |
| 293 | + ) |
| 294 | + } |
| 295 | + const { container, rerender } = render(<Component animate={{}} />) |
| 296 | + await nextFrame() |
| 297 | + |
| 298 | + expect(container.firstChild).not.toHaveStyle("will-change: transform;") |
| 299 | + rerender(<Component animate={{ x: 100 }} />) |
| 300 | + |
| 301 | + await nextFrame() |
| 302 | + |
| 303 | + expect(container.firstChild).toHaveStyle("will-change: transform;") |
| 304 | + rerender(<Component animate={{ x: 200 }} />) |
| 305 | + |
| 306 | + await nextFrame() |
| 307 | + expect(container.firstChild).toHaveStyle("will-change: transform;") |
| 308 | + }) |
| 309 | +}) |
0 commit comments