Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/solid-motion-experiments.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123import { describe, expect, it } from "vitest";
import { createActiveTargets, createSnapshot, resolveInitialStyles, resolveStateOptions, resolveTargetRecord,} from "./_motion-state-options.ts";
function plain<T extends Record<string, unknown>>(value: T): Record<string, unknown> { return Object.assign({}, value);}
describe("motion state target resolution", () => { it("keeps only scalar target values for the current Milestone 0 renderer", () => { expect( plain(resolveTargetRecord({ opacity: 1, x: 12, filter: "blur(4px)", transition: { duration: 0.2 }, keyframes: [0, 1], nullish: null, })), ).toEqual({ opacity: 1, x: 12, filter: "blur(4px)" }); });
it("resolves zero-argument accessors lazily", () => { let reads = 0; const target = () => { reads += 1; return { opacity: 0.5 }; };
expect(reads).toBe(0); expect(plain(resolveTargetRecord(target))).toEqual({ opacity: 0.5 }); expect(reads).toBe(1); });
it("resolves variant labels and custom variant functions", () => { const resolved = resolveStateOptions({ initial: "closed", animate: "open", custom: 24, variants: { closed: { opacity: 0, x: -8 }, open: (custom: unknown) => ({ opacity: 1, x: Number(custom) }), }, });
const activeTargets = createActiveTargets(); const initialSnapshot = createSnapshot(resolved, activeTargets, true);
expect(plain(initialSnapshot.styleTarget)).toEqual({ opacity: 0, x: -8 }); expect(plain(initialSnapshot.animateTarget)).toEqual({ opacity: 1, x: 24 }); });
it("chooses the highest-priority active lane", () => { const resolved = resolveStateOptions({ animate: { opacity: 0.4 }, whileHover: { opacity: 0.6 }, whileTap: { opacity: 0.8 }, exit: { opacity: 0 }, }); const activeTargets = createActiveTargets();
activeTargets.whileHover = true; activeTargets.whileTap = true;
let snapshot = createSnapshot(resolved, activeTargets); expect(snapshot.activeTarget).toBe("whileTap"); expect(plain(snapshot.animateTarget)).toEqual({ opacity: 0.8 });
activeTargets.exit = true; snapshot = createSnapshot(resolved, activeTargets); expect(snapshot.activeTarget).toBe("exit"); expect(plain(snapshot.animateTarget)).toEqual({ opacity: 0 }); });});
describe("resolveInitialStyles", () => { it("serializes the explicit initial target for SSR/hydration", () => { const style = resolveInitialStyles({ style: { "--brand-alpha": 0.75 }, initial: { opacity: 0, x: 8 }, animate: { opacity: 1, x: 0 }, });
expect(style.opacity).toBe(0); expect(style["--brand-alpha"]).toBe(0.75); expect(String(style.transform)).toContain("translateX(8px)"); });
it("serializes the animate target when initial is false", () => { const style = resolveInitialStyles({ initial: false, animate: { opacity: 1, x: 12 }, });
expect(style.opacity).toBe(1); expect(String(style.transform)).toContain("translateX(12px)"); });
it("serializes the animate target when initial is omitted", () => { const style = resolveInitialStyles({ animate: { opacity: 1, x: 12 }, });
expect(style.opacity).toBe(1); expect(String(style.transform)).toContain("translateX(12px)"); });
it("does not serialize exit-only targets into the first paint", () => { const style = resolveInitialStyles({ style: { opacity: 0.9 }, exit: { opacity: 0 }, });
expect(plain(style)).toEqual({ opacity: 0.9 }); });});