import { describe, expect, it } from "vitest"; import { apply, transform } from "./testids.ts"; const run = (source: string, options?: Parameters[1]) => { const { inserts, seen } = transform(source, options); return { out: apply(source, inserts), seen }; }; const idOf = (markup: string) => run(markup).seen[0]?.id ?? null; describe("handlers", () => { it("names a plain handler", () => expect(idOf("")).toBe("deleteIssue")); it("names the call inside an arrow", () => { expect(idOf("")).toBe("archive"); expect(idOf("")).toBe("copy"); expect(idOf("")).toBe("upload"); }); it("names an action() call after the action, not .run", () => expect(idOf("")).toBe("save")); it("reads assignments as what they do", () => { expect(idOf("")).toBe("close"); expect(idOf("")).toBe("openDrawer"); expect(idOf("")).toBe("toggleMenu"); }); it("drops the on prefix a prop carries", () => { expect(idOf("")).toBe("cancel"); expect(idOf("")).toBe("confirm"); }); it("leaves a forwarded onclick for the call site", () => expect(idOf("")).toBeNull()); it("gives up on a handler with no single action", () => expect(idOf("")).toBeNull()); }); describe("links", () => { it("names a literal href by its last word", () => { expect(idOf('x')).toBe("keys"); expect(idOf('x')).toBe("signup"); }); it("skips the parts only known at runtime", () => { expect(idOf("x")).toBe("issues"); expect(idOf("x")).toBeNull(); }); it("names a variable href after the variable", () => expect(idOf("x")).toBe("newRepoHref")); it("ignores external links and a forwarded href", () => { expect(idOf('x')).toBeNull(); expect(idOf("x")).toBeNull(); }); }); describe("fields", () => { it("names an input after what it binds", () => expect(idOf("")).toBe("formTitle")); it("names a submit button submit", () => expect(idOf('')).toBe("submit")); it("prefers a handler over a name", () => expect(idOf('')).toBe("go")); }); describe("injection", () => { it("puts the id on a component call site", () => expect(run("").out).toBe( '' )); it("leaves a hand-written id alone", () => { const { out, seen } = run(''); expect(out).toBe(''); expect(seen.map((item) => [item.id, item.source])).toEqual([["mine", "hand"]]); }); it("does not touch elements that are not interactive", () => expect(run("

hi

").out).toBe("

hi

")); it("does not touch a component it has no rule for", () => expect(run("y").out).toBe( "y" )); it("marks the root elements with the component, through blocks too", () => { const { out } = run("{#if a}
x
{/if}", { component: "Panel" }); expect(out).toBe( '{#if a}
x
{/if}' ); }); it("does not mark elements nested in another", () => expect(run("
x
", { component: "Panel" }).out).toBe( '
x
' )); it("copes with typescript in the script", () => expect( run( '' ).out ).toContain("