import { beforeEach, describe, expect, it } from "vitest"; import { drawablyAlert, drawablyChip, drawablyKbd, drawablyPager, drawablyQuote, drawablySteps, drawablyTabs, drawablyTooltip, } from "../src/composites.js"; beforeEach(() => { document.body.innerHTML = ""; }); function mount(html: string) { document.body.innerHTML = html; return document.body.firstElementChild as HTMLElement; } const svgs = (el: Element) => el.querySelectorAll("svg.drawably-svg").length; describe("drawablyChip", () => { it("badges the chip and boxes the checkbox wrapper", () => { const el = mount(``); const sketch = drawablyChip(el, { seed: 1 }); expect(el.classList.contains("drawably-chip")).toBe(true); expect(el.classList.contains("drawably-badge")).toBe(true); expect(el.querySelector("span")?.classList.contains("drawably-checkbox")).toBe(true); expect(el.querySelector("span")?.dataset.checked).toBe(""); sketch.destroy(); expect(svgs(el)).toBe(0); expect(el.className).toBe(""); }); it("throws when the checkbox has no wrapper of its own", () => { const el = mount(``); expect(() => drawablyChip(el)).toThrow(/wrapper/); }); }); describe("drawablyTabs", () => { it("underlines the aria-selected tab and moves on setActive", () => { const el = mount(`
less`); const sketch = drawablyQuote(el, { seed: 1 }); expect(el.querySelector("span")?.classList.contains("drawably-highlight")).toBe(true); expect(el.querySelector("footer")?.classList.contains("drawably-divider")).toBe(true); sketch.destroy(); expect(svgs(el)).toBe(0); }); }); describe("drawablyPager", () => { it("makes the current page solid and swaps on setPage", () => { const el = mount(``); const [a, b, c] = [...el.children] as HTMLElement[]; const sketch = drawablyPager(el, { seed: 1 }); expect(b.classList.contains("drawably-button--solid")).toBe(true); expect(a.classList.contains("drawably-button--outline")).toBe(true); sketch.setPage(2); expect(b.classList.contains("drawably-button--solid")).toBe(false); expect(c.classList.contains("drawably-button--solid")).toBe(true); expect(c.getAttribute("aria-current")).toBe("page"); expect(b.hasAttribute("aria-current")).toBe(false); sketch.destroy(); expect(svgs(el)).toBe(0); }); it("resketch is deterministic per seed", () => { const el = mount(``); const sketch = drawablyPager(el, { seed: 7 }); const d = () => el.querySelector("path")!.getAttribute("d"); const first = d(); sketch.resketch(9); expect(d()).not.toBe(first); sketch.resketch(7); expect(d()).toBe(first); }); });