|
| 1 | +import { waitFor } from "@testing-library/dom" |
1 | 2 | import { supportsFlags } from "motion-dom" |
2 | | -import { act, createRef, useState } from "react" |
| 3 | +import { act, createRef, useEffect, useRef, useState } from "react" |
3 | 4 | import { motion, useMotionValue } from "../../" |
4 | 5 | import "../../animation/animators/waapi/__tests__/setup" |
5 | 6 | import { nextFrame } from "../../gestures/__tests__/utils" |
@@ -979,4 +980,79 @@ describe("WAAPI animations", () => { |
979 | 980 | ) |
980 | 981 | supportsFlags.linearEasing = undefined |
981 | 982 | }) |
| 983 | + |
| 984 | + test("Finishes a transform that can't be accelerated when the main thread is blocked past its end", async () => { |
| 985 | + supportsFlags.linearEasing = true |
| 986 | + const ref = createRef<HTMLDivElement>() |
| 987 | + const completed: string[] = [] |
| 988 | + let setVisible: (isVisible: boolean) => void |
| 989 | + |
| 990 | + const variants = { |
| 991 | + visible: { |
| 992 | + visibility: "visible", |
| 993 | + opacity: 1, |
| 994 | + scale: 1, |
| 995 | + transition: { duration: 0.1 }, |
| 996 | + }, |
| 997 | + hidden: { |
| 998 | + opacity: 0, |
| 999 | + scale: 0.5, |
| 1000 | + transition: { duration: 0.1 }, |
| 1001 | + transitionEnd: { visibility: "hidden" }, |
| 1002 | + }, |
| 1003 | + } |
| 1004 | + |
| 1005 | + const Component = () => { |
| 1006 | + const [isVisible, set] = useState(false) |
| 1007 | + const isMounted = useRef(false) |
| 1008 | + setVisible = set |
| 1009 | + |
| 1010 | + useEffect(() => { |
| 1011 | + /** |
| 1012 | + * Block the main thread for longer than the transition, |
| 1013 | + * before the animation's first frame. |
| 1014 | + */ |
| 1015 | + if (isMounted.current && !isVisible) { |
| 1016 | + const start = performance.now() |
| 1017 | + while (performance.now() - start < 300) {} |
| 1018 | + } |
| 1019 | + isMounted.current = true |
| 1020 | + }, [isVisible]) |
| 1021 | + |
| 1022 | + return ( |
| 1023 | + <motion.div |
| 1024 | + ref={ref} |
| 1025 | + initial={false} |
| 1026 | + variants={variants} |
| 1027 | + animate={isVisible ? "visible" : "hidden"} |
| 1028 | + onUpdate={() => {}} |
| 1029 | + onAnimationComplete={(definition) => |
| 1030 | + completed.push(definition as string) |
| 1031 | + } |
| 1032 | + /> |
| 1033 | + ) |
| 1034 | + } |
| 1035 | + |
| 1036 | + render(<Component />) |
| 1037 | + try { |
| 1038 | + act(() => setVisible(true)) |
| 1039 | + await waitFor(() => expect(completed).toEqual(["visible"])) |
| 1040 | + |
| 1041 | + act(() => setVisible(false)) |
| 1042 | + await waitFor(() => |
| 1043 | + expect(completed).toEqual(["visible", "hidden"]) |
| 1044 | + ) |
| 1045 | + expect(ref.current!.style.visibility).toBe("hidden") |
| 1046 | + expect(ref.current!.style.transform).toBe("scale(0.5)") |
| 1047 | + |
| 1048 | + act(() => setVisible(true)) |
| 1049 | + await waitFor(() => |
| 1050 | + expect(completed).toEqual(["visible", "hidden", "visible"]) |
| 1051 | + ) |
| 1052 | + expect(ref.current!.style.visibility).toBe("visible") |
| 1053 | + expect(ref.current!.style.opacity).toBe("1") |
| 1054 | + } finally { |
| 1055 | + supportsFlags.linearEasing = undefined |
| 1056 | + } |
| 1057 | + }) |
982 | 1058 | }) |
0 commit comments