Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/solid-motion-experiments.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185import { hydrate } from "solid-js/web";import { createServer, type ViteDevServer } from "vite";import solidPlugin from "vite-plugin-solid";import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { SSR_HYDRATION_CONTROL_CASES, SSR_HYDRATION_MOTION_CASES, SsrHydrationFixture, renderIdForHydrationCase, type SsrHydrationCaseName,} from "./ssr-hydration-cases.client.tsx";
let server: ViteDevServer;
beforeAll(async () => { server = await createServer({ appType: "custom", configFile: false, logLevel: "error", plugins: [ solidPlugin({ ssr: true, hot: false, solid: { generate: "ssr", hydratable: true, }, }), ], server: { middlewareMode: true }, });});
afterAll(async () => { await server?.close();});
afterEach(() => { document.body.textContent = ""; delete (window as typeof window & { _$HY?: unknown })._$HY; vi.restoreAllMocks();});
type ServerFixtureModule = typeof import("./ssr-hydration-cases.server.tsx");
interface ServerHydrationMarkup { html: string; hydrationScript: string;}
function flushHydrationEffects(): Promise<void> { return Promise.resolve().then(() => Promise.resolve());}
async function renderServerHtml(name: SsrHydrationCaseName): Promise<string> { return (await renderServerHydrationMarkup(name)).html;}
async function renderServerHydrationMarkup(name: SsrHydrationCaseName): Promise<ServerHydrationMarkup> { const mod = await server.ssrLoadModule("/src/test/ssr-hydration-cases.server.tsx") as ServerFixtureModule; return mod.renderSsrHydrationFixtureMarkup(name);}
function captureConsoleDiagnostics() { const messages: string[] = []; const collect = (level: "error" | "warn") => (...args: unknown[]) => { messages.push(`${level}: ${args.map(String).join(" ")}`); };
vi.spyOn(console, "error").mockImplementation(collect("error")); vi.spyOn(console, "warn").mockImplementation(collect("warn"));
return messages;}
function hydrationDiagnostics(messages: readonly string[]): string[] { return messages.filter((message) => /hydration|mismatch|hydratable/i.test(message));}
function createHydrationRoot(serverHtml: string): HTMLElement { const root = document.createElement("main"); root.innerHTML = serverHtml; document.body.append(root); return root;}
function executeHydrationScript(scriptHtml: string): void { const template = document.createElement("template"); template.innerHTML = scriptHtml.trim(); const code = template.content.querySelector("script")?.textContent;
if (!code) { throw new Error("Expected Solid hydration script to contain executable code."); }
window.eval(code);}
function createHydratableRoot(markup: ServerHydrationMarkup): HTMLElement { executeHydrationScript(markup.hydrationScript); return createHydrationRoot(markup.html);}
function expectTransformToContainTranslateX(element: HTMLElement | SVGElement, value: number): void { expect(element.getAttribute("style") ?? "").toContain(`translateX(${value}px)`);}
async function expectCaseHydratesWithoutDiagnostics(name: SsrHydrationCaseName): Promise<void> { const markup = await renderServerHydrationMarkup(name); expect( markup.html, `Expected server markup for ${name} to include Solid hydration markers before hydrate() runs.`, ).toContain("data-hk=");
const root = createHydratableRoot(markup); const element = root.querySelector(`[data-testid='${name}']`) as HTMLElement | SVGElement | null;
expect(element).toBeTruthy();
const diagnostics = captureConsoleDiagnostics(); const dispose = hydrate( () => <SsrHydrationFixture name={name} />, root, { renderId: renderIdForHydrationCase(name) }, );
await flushHydrationEffects(); dispose();
expect(hydrationDiagnostics(diagnostics)).toEqual([]);}
describe("SSR + hydration", () => { it.each(SSR_HYDRATION_CONTROL_CASES)("hydrates control case %s without mismatch diagnostics", async (name) => { await expectCaseHydratesWithoutDiagnostics(name); });
it.each(SSR_HYDRATION_MOTION_CASES)("hydrates motion case %s without mismatch diagnostics", async (name) => { await expectCaseHydratesWithoutDiagnostics(name); });
it("serializes explicit initial styles before hydration", async () => { const root = createHydrationRoot(await renderServerHtml("explicit-initial")); const element = root.querySelector<HTMLElement>("[data-testid='explicit-initial']");
expect(element?.style.opacity).toBe("0"); expectTransformToContainTranslateX(element!, 4); });
it("serializes animate styles when initial is false", async () => { const root = createHydrationRoot(await renderServerHtml("initial-false")); const element = root.querySelector<HTMLElement>("[data-testid='initial-false']");
expect(element?.style.opacity).toBe("1"); expectTransformToContainTranslateX(element!, 12); });
it("serializes animate styles when initial is omitted", async () => { const root = createHydrationRoot(await renderServerHtml("omitted-initial")); const element = root.querySelector<HTMLElement>("[data-testid='omitted-initial']");
expect(element?.style.opacity).toBe("1"); expectTransformToContainTranslateX(element!, 12); });
it("serializes SVG styles without breaking hydration", async () => { const root = createHydrationRoot(await renderServerHtml("svg-style")); const element = root.querySelector<SVGElement>("[data-testid='svg-style']");
expect(element?.tagName.toLowerCase()).toBe("svg"); expect(element?.style.opacity).toBe("0.5"); expect(element?.style.getPropertyValue("--svg-alpha")).toBe("0.4"); });
it("serializes CSS variables and transform styles together", async () => { const root = createHydrationRoot(await renderServerHtml("css-vars-transform")); const element = root.querySelector<HTMLElement>("[data-testid='css-vars-transform']");
expect(element?.style.opacity).toBe("1"); expect(element?.style.getPropertyValue("--brand-alpha")).toBe("0.75"); expectTransformToContainTranslateX(element!, 12); });});