Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/solid-motion-experiments.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196import { 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( () => ( <motion.div id="box" class="card" ref={(node) => { 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( () => ( <motion.div as="section" data-testid="panel" animate={{ opacity: 1 }} transition={{ duration: 0 }} /> ), 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<string, unknown>[] = []; const Card: Component<Record<string, unknown>> = (props) => { seen.push(props); return <div data-testid="card" data-id={String(props.id)} />; }; const MotionCard = motion.create(Card);
const dispose = render( () => ( <MotionCard id="a" animate={{ opacity: 1 }} initial={{ opacity: 0 }} transition={{ duration: 0 }} /> ), 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<string, unknown>[] = []; const Card: Component<Record<string, unknown>> = (props) => { seen.push(props); return <div data-testid="card" />; }; const MotionCard = motion.create(Card, { forwardMotionProps: true });
const dispose = render( () => ( <MotionCard animate={{ opacity: 1 }} initial={{ opacity: 0 }} transition={{ duration: 0 }} /> ), 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<string, unknown>[] = []; const Card: Component<Record<string, unknown>> = (props) => { seen.push(props); return <div data-testid="card" />; }; const MotionCard = motion.create(Card);
const dispose = render( () => ( <MotionCard forwardMotionProps animate={{ opacity: 1 }} initial={{ opacity: 0 }} transition={{ duration: 0 }} /> ), 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( () => ( <motion.div data-testid="box" animate={() => ({ 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(); });});