|
1 | 1 | import { motionValue, MotionValue } from "motion-dom" |
2 | 2 | import { useEffect } from "react" |
3 | | -import { motion } from "../../" |
| 3 | +import { motion, useMotionValueEvent } from "../../" |
4 | 4 | import { syncDriver } from "../../animation/animators/__tests__/utils" |
5 | 5 | import { render } from "../../jest.setup" |
6 | 6 | import { useMotionValue } from "../use-motion-value" |
@@ -225,3 +225,57 @@ const runSpringTests = (unit?: string | undefined) => { |
225 | 225 | // Run tests for both number values and percentage values |
226 | 226 | runSpringTests() |
227 | 227 | runSpringTests("%") |
| 228 | + |
| 229 | +describe("useSpring animation events", () => { |
| 230 | + test("triggers animationStart event when spring animation begins", async () => { |
| 231 | + const promise = new Promise<boolean>((resolve) => { |
| 232 | + const Component = () => { |
| 233 | + const x = useMotionValue(0) |
| 234 | + const springX = useSpring(x, { |
| 235 | + stiffness: 100, |
| 236 | + damping: 10, |
| 237 | + }) |
| 238 | + |
| 239 | + useMotionValueEvent(springX, "animationStart", () => { |
| 240 | + resolve(true) |
| 241 | + }) |
| 242 | + |
| 243 | + useEffect(() => { |
| 244 | + x.set(100) |
| 245 | + }, [x]) |
| 246 | + |
| 247 | + return null |
| 248 | + } |
| 249 | + |
| 250 | + render(<Component />) |
| 251 | + }) |
| 252 | + |
| 253 | + await expect(promise).resolves.toBe(true) |
| 254 | + }) |
| 255 | + |
| 256 | + test("triggers animationComplete event when spring animation finishes", async () => { |
| 257 | + const promise = new Promise<boolean>((resolve) => { |
| 258 | + const Component = () => { |
| 259 | + const x = useMotionValue(0) |
| 260 | + const springX = useSpring(x, { |
| 261 | + stiffness: 1000, |
| 262 | + damping: 50, |
| 263 | + }) |
| 264 | + |
| 265 | + useMotionValueEvent(springX, "animationComplete", () => { |
| 266 | + resolve(true) |
| 267 | + }) |
| 268 | + |
| 269 | + useEffect(() => { |
| 270 | + x.set(100) |
| 271 | + }, [x]) |
| 272 | + |
| 273 | + return null |
| 274 | + } |
| 275 | + |
| 276 | + render(<Component />) |
| 277 | + }) |
| 278 | + |
| 279 | + await expect(promise).resolves.toBe(true) |
| 280 | + }) |
| 281 | +}) |
0 commit comments