library
Prebuilt pebble components: drop-in custom elements, no compiler needed
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runAucun
0Étoiles
0Forks
2Issues
| 1 | // Integration tests for the <pebble-toaster> element under happy-dom, ported |
| 2 | // from the toboggan and toki iterations (jsdom there) and extended for the |
| 3 | // convergence: both state-marker schemes (class AND data attribute), the |
| 4 | // polite/assertive channels, and the part hooks. Real timers drive each |
| 5 | // card's expiry and exit, so the dismissal assertions wait past EXIT_MS with |
| 6 | // a margin. The × is the element's own button, so dismissing is a click. |
| 7 | |
| 8 | import { beforeAll, describe, expect, test } from "bun:test"; |
| 9 | import { announceToast, toastError } from "../packages/toaster/index"; |
| 10 | |
| 11 | const flush = (ms = 0): Promise<void> => new Promise((r) => setTimeout(r, ms)); |
| 12 | |
| 13 | let host: HTMLElement; |
| 14 | const toast = (message: string, over: Record<string, unknown> = {}) => |
| 15 | announceToast({ message, type: "success", duration: 5000, ...over }); |
| 16 | |
| 17 | beforeAll(() => { |
| 18 | host = document.createElement("pebble-toaster"); |
| 19 | document.body.appendChild(host); |
| 20 | }); |
| 21 | |
| 22 | const cards = () => host.querySelectorAll(".toast"); |
| 23 | |
| 24 | describe("pebble-toaster", () => { |
| 25 | test("renders its container when connected", async () => { |
| 26 | await flush(); |
| 27 | expect(host.querySelector(".toaster")).not.toBeNull(); |
| 28 | }); |
| 29 | |
| 30 | test("the region is the polite channel", () => { |
| 31 | const region = host.querySelector(".toaster") as Element; |
| 32 | expect(region.getAttribute("role")).toBe("region"); |
| 33 | expect(region.getAttribute("aria-live")).toBe("polite"); |
| 34 | }); |
| 35 | |
| 36 | test("renders a card with the message for an announced toast", async () => { |
| 37 | toast("Hello one"); |
| 38 | await flush(20); |
| 39 | expect(cards().length).toBe(1); |
| 40 | expect(host.textContent).toContain("Hello one"); |
| 41 | }); |
| 42 | |
| 43 | test("a card carries both state-marker schemes and the part hooks", () => { |
| 44 | const card = cards()[0] as Element; |
| 45 | expect(card.classList.contains("toast")).toBe(true); |
| 46 | expect(card.classList.contains("toast-success")).toBe(true); |
| 47 | expect(card.getAttribute("data-kind")).toBe("success"); |
| 48 | expect(card.getAttribute("part")).toBe("toast"); |
| 49 | expect(card.getAttribute("role")).toBe("status"); |
| 50 | expect(card.querySelector(".toast-close")?.getAttribute("part")).toBe("close"); |
| 51 | expect(card.querySelector(".toast-progress")?.getAttribute("part")).toBe("progress"); |
| 52 | expect(host.querySelector(".toaster-stack")?.getAttribute("part")).toBe("stack"); |
| 53 | }); |
| 54 | |
| 55 | test("shows at most three toasts with a +N overflow chip", async () => { |
| 56 | toast("two"); |
| 57 | toast("three"); |
| 58 | toast("four"); |
| 59 | await flush(20); |
| 60 | expect(cards().length).toBe(3); |
| 61 | expect(host.querySelector(".toaster-overflow")?.textContent?.trim()).toBe("+1"); |
| 62 | }); |
| 63 | |
| 64 | test("expands to show all toasts while hovered", async () => { |
| 65 | const container = host.querySelector(".toaster") as Element; |
| 66 | container.dispatchEvent(new MouseEvent("mouseenter")); |
| 67 | await flush(20); |
| 68 | expect(cards().length).toBe(4); |
| 69 | expect(host.querySelector(".toaster-overflow")).toBeNull(); |
| 70 | container.dispatchEvent(new MouseEvent("mouseleave")); |
| 71 | await flush(20); |
| 72 | expect(cards().length).toBe(3); |
| 73 | }); |
| 74 | |
| 75 | test("pauses the progress bar on hover and resumes on leave", async () => { |
| 76 | const card = cards()[0] as Element; |
| 77 | card.dispatchEvent(new MouseEvent("mouseenter")); |
| 78 | await flush(20); |
| 79 | const bar = card.querySelector(".toast-progress") as HTMLElement; |
| 80 | expect(bar.style.animationPlayState).toBe("paused"); |
| 81 | card.dispatchEvent(new MouseEvent("mouseleave")); |
| 82 | await flush(20); |
| 83 | expect(bar.style.animationPlayState).toBe("running"); |
| 84 | }); |
| 85 | |
| 86 | test("the × button is the element's own: a plain button, no invoker", () => { |
| 87 | const close = cards()[0].querySelector(".toast-close") as HTMLButtonElement; |
| 88 | expect(close.getAttribute("type")).toBe("button"); |
| 89 | expect(close.hasAttribute("commandfor")).toBe(false); |
| 90 | expect(close.hasAttribute("command")).toBe(false); |
| 91 | }); |
| 92 | |
| 93 | test("the × button exits the toast; the stack refills after EXIT_MS", async () => { |
| 94 | const card = cards()[0] as HTMLElement; |
| 95 | const close = card.querySelector(".toast-close") as HTMLButtonElement; |
| 96 | close.dispatchEvent(new MouseEvent("click", { bubbles: true })); |
| 97 | await flush(20); |
| 98 | expect(card.hasAttribute("data-exit")).toBe(true); |
| 99 | expect(card.classList.contains("toast-exit")).toBe(true); |
| 100 | await flush(400); |
| 101 | expect(host.textContent).not.toContain("two"); |
| 102 | expect(cards().length).toBe(3); |
| 103 | expect(host.textContent).toContain("Hello one"); |
| 104 | }); |
| 105 | |
| 106 | test("clicking anywhere on a card dismisses it too", async () => { |
| 107 | const card = cards()[0] as HTMLElement; |
| 108 | const text = card.textContent; |
| 109 | card.dispatchEvent(new MouseEvent("click", { bubbles: true })); |
| 110 | await flush(20); |
| 111 | expect(card.hasAttribute("data-exit")).toBe(true); |
| 112 | await flush(400); |
| 113 | expect(host.textContent).not.toContain(text?.replace("×", "").trim() ?? "never"); |
| 114 | }); |
| 115 | |
| 116 | test("auto-dismisses after the duration", async () => { |
| 117 | toast("quick", { duration: 1000 }); |
| 118 | await flush(50); |
| 119 | expect(host.textContent).toContain("quick"); |
| 120 | await flush(1800); |
| 121 | expect(host.textContent).not.toContain("quick"); |
| 122 | }); |
| 123 | |
| 124 | test("detaches its subtree on disconnect and re-renders empty on reconnect", async () => { |
| 125 | host.remove(); |
| 126 | await flush(20); |
| 127 | expect(host.childNodes.length).toBe(0); |
| 128 | document.body.appendChild(host); |
| 129 | await flush(20); |
| 130 | expect(host.querySelector(".toaster")).not.toBeNull(); |
| 131 | expect(cards().length).toBe(0); |
| 132 | }); |
| 133 | |
| 134 | test("an error toast is the assertive channel: role=alert", async () => { |
| 135 | toastError("boom"); |
| 136 | await flush(20); |
| 137 | const card = [...cards()].find((c) => c.textContent?.includes("boom")) as Element; |
| 138 | expect(card.getAttribute("role")).toBe("alert"); |
| 139 | expect(card.classList.contains("toast-error")).toBe(true); |
| 140 | expect(card.getAttribute("data-kind")).toBe("error"); |
| 141 | // the polite cards are unaffected |
| 142 | const info = [...cards()].find((c) => c !== card) as Element | undefined; |
| 143 | expect(info?.getAttribute("role") ?? "status").toBe("status"); |
| 144 | }); |
| 145 | |
| 146 | test("the queue caps at MAX_TOASTS, dropping the oldest", async () => { |
| 147 | toast("dropped-one", { duration: 60000 }); |
| 148 | for (let i = 2; i <= 12; i++) toast(`keep-${i}`, { duration: 60000 }); |
| 149 | await flush(20); |
| 150 | // 13 announced in all (the error toast above included); 10 kept, 3 visible |
| 151 | expect(cards().length).toBe(3); |
| 152 | expect(host.querySelector(".toaster-overflow")?.textContent?.trim()).toBe("+7"); |
| 153 | const container = host.querySelector(".toaster") as Element; |
| 154 | container.dispatchEvent(new MouseEvent("mouseenter")); |
| 155 | await flush(20); |
| 156 | expect(cards().length).toBe(10); |
| 157 | expect(host.textContent).not.toContain("dropped-one"); |
| 158 | expect(host.textContent).toContain("keep-12"); |
| 159 | }); |
| 160 | |
| 161 | test("an unknown toast type warns in dev and falls back to info", async () => { |
| 162 | const warnings: string[] = []; |
| 163 | const original = console.warn; |
| 164 | console.warn = (msg: unknown) => warnings.push(String(msg)); |
| 165 | try { |
| 166 | announceToast({ message: "odd", type: "bogus" as never, duration: 1000 }); |
| 167 | } finally { |
| 168 | console.warn = original; |
| 169 | } |
| 170 | await flush(20); |
| 171 | expect(warnings.some((w) => w.includes('unknown toast type "bogus"'))).toBe(true); |
| 172 | const card = [...cards()].find((c) => c.textContent?.includes("odd")) as Element; |
| 173 | expect(card.getAttribute("data-kind")).toBe("info"); |
| 174 | }); |
| 175 | |
| 176 | test("cleanup: removing the host stops every card timer", async () => { |
| 177 | host.remove(); |
| 178 | await flush(20); |
| 179 | expect(host.childNodes.length).toBe(0); |
| 180 | }); |
| 181 | }); |