// Integration tests for the element under happy-dom, ported // from the toboggan and toki iterations (jsdom there) and extended for the // convergence: both state-marker schemes (class AND data attribute), the // polite/assertive channels, and the part hooks. Real timers drive each // card's expiry and exit, so the dismissal assertions wait past EXIT_MS with // a margin. The × is the element's own button, so dismissing is a click. import { beforeAll, describe, expect, test } from "bun:test"; import { announceToast, toastError } from "../packages/toaster/index"; const flush = (ms = 0): Promise => new Promise((r) => setTimeout(r, ms)); let host: HTMLElement; const toast = (message: string, over: Record = {}) => announceToast({ message, type: "success", duration: 5000, ...over }); beforeAll(() => { host = document.createElement("pebble-toaster"); document.body.appendChild(host); }); const cards = () => host.querySelectorAll(".toast"); describe("pebble-toaster", () => { test("renders its container when connected", async () => { await flush(); expect(host.querySelector(".toaster")).not.toBeNull(); }); test("the region is the polite channel", () => { const region = host.querySelector(".toaster") as Element; expect(region.getAttribute("role")).toBe("region"); expect(region.getAttribute("aria-live")).toBe("polite"); }); test("renders a card with the message for an announced toast", async () => { toast("Hello one"); await flush(20); expect(cards().length).toBe(1); expect(host.textContent).toContain("Hello one"); }); test("a card carries both state-marker schemes and the part hooks", () => { const card = cards()[0] as Element; expect(card.classList.contains("toast")).toBe(true); expect(card.classList.contains("toast-success")).toBe(true); expect(card.getAttribute("data-kind")).toBe("success"); expect(card.getAttribute("part")).toBe("toast"); expect(card.getAttribute("role")).toBe("status"); expect(card.querySelector(".toast-close")?.getAttribute("part")).toBe("close"); expect(card.querySelector(".toast-progress")?.getAttribute("part")).toBe("progress"); expect(host.querySelector(".toaster-stack")?.getAttribute("part")).toBe("stack"); }); test("shows at most three toasts with a +N overflow chip", async () => { toast("two"); toast("three"); toast("four"); await flush(20); expect(cards().length).toBe(3); expect(host.querySelector(".toaster-overflow")?.textContent?.trim()).toBe("+1"); }); test("expands to show all toasts while hovered", async () => { const container = host.querySelector(".toaster") as Element; container.dispatchEvent(new MouseEvent("mouseenter")); await flush(20); expect(cards().length).toBe(4); expect(host.querySelector(".toaster-overflow")).toBeNull(); container.dispatchEvent(new MouseEvent("mouseleave")); await flush(20); expect(cards().length).toBe(3); }); test("pauses the progress bar on hover and resumes on leave", async () => { const card = cards()[0] as Element; card.dispatchEvent(new MouseEvent("mouseenter")); await flush(20); const bar = card.querySelector(".toast-progress") as HTMLElement; expect(bar.style.animationPlayState).toBe("paused"); card.dispatchEvent(new MouseEvent("mouseleave")); await flush(20); expect(bar.style.animationPlayState).toBe("running"); }); test("the × button is the element's own: a plain button, no invoker", () => { const close = cards()[0].querySelector(".toast-close") as HTMLButtonElement; expect(close.getAttribute("type")).toBe("button"); expect(close.hasAttribute("commandfor")).toBe(false); expect(close.hasAttribute("command")).toBe(false); }); test("the × button exits the toast; the stack refills after EXIT_MS", async () => { const card = cards()[0] as HTMLElement; const close = card.querySelector(".toast-close") as HTMLButtonElement; close.dispatchEvent(new MouseEvent("click", { bubbles: true })); await flush(20); expect(card.hasAttribute("data-exit")).toBe(true); expect(card.classList.contains("toast-exit")).toBe(true); await flush(400); expect(host.textContent).not.toContain("two"); expect(cards().length).toBe(3); expect(host.textContent).toContain("Hello one"); }); test("clicking anywhere on a card dismisses it too", async () => { const card = cards()[0] as HTMLElement; const text = card.textContent; card.dispatchEvent(new MouseEvent("click", { bubbles: true })); await flush(20); expect(card.hasAttribute("data-exit")).toBe(true); await flush(400); expect(host.textContent).not.toContain(text?.replace("×", "").trim() ?? "never"); }); test("auto-dismisses after the duration", async () => { toast("quick", { duration: 1000 }); await flush(50); expect(host.textContent).toContain("quick"); await flush(1800); expect(host.textContent).not.toContain("quick"); }); test("detaches its subtree on disconnect and re-renders empty on reconnect", async () => { host.remove(); await flush(20); expect(host.childNodes.length).toBe(0); document.body.appendChild(host); await flush(20); expect(host.querySelector(".toaster")).not.toBeNull(); expect(cards().length).toBe(0); }); test("an error toast is the assertive channel: role=alert", async () => { toastError("boom"); await flush(20); const card = [...cards()].find((c) => c.textContent?.includes("boom")) as Element; expect(card.getAttribute("role")).toBe("alert"); expect(card.classList.contains("toast-error")).toBe(true); expect(card.getAttribute("data-kind")).toBe("error"); // the polite cards are unaffected const info = [...cards()].find((c) => c !== card) as Element | undefined; expect(info?.getAttribute("role") ?? "status").toBe("status"); }); test("the queue caps at MAX_TOASTS, dropping the oldest", async () => { toast("dropped-one", { duration: 60000 }); for (let i = 2; i <= 12; i++) toast(`keep-${i}`, { duration: 60000 }); await flush(20); // 13 announced in all (the error toast above included); 10 kept, 3 visible expect(cards().length).toBe(3); expect(host.querySelector(".toaster-overflow")?.textContent?.trim()).toBe("+7"); const container = host.querySelector(".toaster") as Element; container.dispatchEvent(new MouseEvent("mouseenter")); await flush(20); expect(cards().length).toBe(10); expect(host.textContent).not.toContain("dropped-one"); expect(host.textContent).toContain("keep-12"); }); test("an unknown toast type warns in dev and falls back to info", async () => { const warnings: string[] = []; const original = console.warn; console.warn = (msg: unknown) => warnings.push(String(msg)); try { announceToast({ message: "odd", type: "bogus" as never, duration: 1000 }); } finally { console.warn = original; } await flush(20); expect(warnings.some((w) => w.includes('unknown toast type "bogus"'))).toBe(true); const card = [...cards()].find((c) => c.textContent?.includes("odd")) as Element; expect(card.getAttribute("data-kind")).toBe("info"); }); test("cleanup: removing the host stops every card timer", async () => { host.remove(); await flush(20); expect(host.childNodes.length).toBe(0); }); });