import { createSignal, type Component, type JSX } from "solid-js"; import { render } from "solid-js/web"; import { afterEach, describe, expect, it, vi } from "vitest"; vi.mock("motion-dom", () => import("../test/motion-dom-mock.ts")); import { motion } from "./motion.ts"; function createRoot() { const root = document.createElement("div"); document.body.append(root); return root; } async function flushEffects() { await Promise.resolve(); await Promise.resolve(); } afterEach(() => { document.body.textContent = ""; vi.useRealTimers(); }); describe("createMotionComponent", () => { it("renders Solid JSX member components and forwards DOM props/ref", async () => { const root = createRoot(); let refElement: HTMLDivElement | undefined; const dispose = render( () => ( { refElement = node; }} animate={{ opacity: 1 }} transition={{ duration: 0 }} /> ), root, ); await flushEffects(); const element = root.querySelector("#box") as HTMLDivElement; expect(element).toBeInstanceOf(HTMLDivElement); expect(element.className).toBe("card"); expect(refElement).toBe(element); dispose(); }); it("supports the Solid-local as prop without leaking it to the DOM", async () => { const root = createRoot(); const dispose = render( () => ( ), root, ); await flushEffects(); const element = root.querySelector("[data-testid='panel']") as HTMLElement; expect(element.tagName).toBe("SECTION"); expect(element.hasAttribute("as")).toBe(false); dispose(); }); it("filters Motion props from custom components by default", async () => { const root = createRoot(); const seen: Record[] = []; const Card: Component> = (props) => { seen.push(props); return
; }; const MotionCard = motion.create(Card); const dispose = render( () => ( ), root, ); await flushEffects(); expect(seen).toHaveLength(1); expect(seen[0]?.id).toBe("a"); expect("animate" in seen[0]!).toBe(false); expect("initial" in seen[0]!).toBe(false); expect("transition" in seen[0]!).toBe(false); dispose(); }); it("forwards Motion props to custom components when requested", async () => { const root = createRoot(); const seen: Record[] = []; const Card: Component> = (props) => { seen.push(props); return
; }; const MotionCard = motion.create(Card, { forwardMotionProps: true }); const dispose = render( () => ( ), root, ); await flushEffects(); expect(seen[0]?.animate).toEqual({ opacity: 1 }); expect(seen[0]?.initial).toEqual({ opacity: 0 }); expect(seen[0]?.transition).toEqual({ duration: 0 }); dispose(); }); it("forwards Motion props to custom components per instance when requested", async () => { const root = createRoot(); const seen: Record[] = []; const Card: Component> = (props) => { seen.push(props); return
; }; const MotionCard = motion.create(Card); const dispose = render( () => ( ), root, ); await flushEffects(); expect(seen[0]?.animate).toEqual({ opacity: 1 }); expect(seen[0]?.initial).toEqual({ opacity: 0 }); expect(seen[0]?.transition).toEqual({ duration: 0 }); dispose(); }); it("tracks accessor-based animate targets without component rerender assumptions", async () => { const root = createRoot(); const [open, setOpen] = createSignal(false); const dispose = render( () => ( ({ opacity: open() ? 1 : 0.25 })} transition={{ duration: 0 }} /> ), root, ); await flushEffects(); const element = root.querySelector("[data-testid='box']") as HTMLElement; expect(element.style.opacity).toBe("0.25"); setOpen(true); await flushEffects(); expect(element.style.opacity).toBe("1"); dispose(); }); });