// : the ARIA combobox wiring, the debounced and aborted // fetch, the options from the server's fragment, the keys, closing, and the // field restored on disconnect. fetch is a fake; real layout is the smoke's // job (examples/combobox.html). import { afterAll, afterEach, beforeAll, beforeEach, expect, test } from "bun:test"; import { DEBOUNCE_MS, optionsOf } from "../packages/combobox/index"; import styles from "../packages/combobox/combobox.css"; const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)); const settle = () => sleep(DEBOUNCE_MS + 30); const FRAGMENT = ``; let calls: { url: string; init: RequestInit }[] = []; let answer: (url: string, init: RequestInit) => Promise; const realFetch = globalThis.fetch; const happy = (window as unknown as { happyDOM: { setURL(url: string): void } }).happyDOM; beforeAll(() => happy.setURL("https://cine.example/proposer/film")); afterAll(() => happy.setURL("about:blank")); beforeEach(() => { calls = []; answer = async () => new Response(FRAGMENT, { headers: { "content-type": "text/html" } }); globalThis.fetch = (async (url: string | URL, init: RequestInit = {}) => { calls.push({ url: String(url), init }); return answer(String(url), init); }) as typeof fetch; }); afterEach(async () => { globalThis.fetch = realFetch; document.body.textContent = ""; await sleep(0); }); const mount = (attrs = 'src="/proposer/film/suggestions" min="2"') => { const wrap = document.createElement("div"); wrap.innerHTML = `

ailleurs

`; document.body.appendChild(wrap); const host = wrap.querySelector("pebble-combobox") as HTMLElement; const input = host.querySelector("input")!; const listbox = host.querySelector("[role=listbox]") as HTMLElement; return { host, input, listbox, popup: host.querySelector(".popup") as HTMLElement }; }; const type = (input: HTMLInputElement, value: string) => { input.value = value; input.dispatchEvent(new Event("input", { bubbles: true })); }; const key = (input: HTMLInputElement, k: string) => { const event = new KeyboardEvent("keydown", { key: k, bubbles: true, cancelable: true }); input.dispatchEvent(event); return event; }; test("the field becomes a combobox controlling a labelled, hidden listbox", () => { const { input, listbox, popup } = mount(); expect(input.getAttribute("role")).toBe("combobox"); expect(input.getAttribute("aria-autocomplete")).toBe("list"); expect(input.getAttribute("aria-expanded")).toBe("false"); expect(input.getAttribute("aria-controls")).toBe(listbox.id); expect(input.getAttribute("autocomplete")).toBe("off"); expect(listbox.getAttribute("aria-labelledby")).toBe(document.querySelector("label")!.id); expect(popup.hidden).toBe(true); }); test("fetches after the pause, with at least min characters, and shows the options", async () => { const { host, input, listbox, popup } = mount(); type(input, "m"); await settle(); expect(calls.length).toBe(0); type(input, "ma"); await sleep(DEBOUNCE_MS - 100); expect(calls.length).toBe(0); await sleep(130); expect(calls.length).toBe(1); expect(calls[0]!.url).toBe("https://cine.example/proposer/film/suggestions?q=ma"); expect((calls[0]!.init.headers as Record).Accept).toBe("text/html"); await sleep(5); const options = [...listbox.querySelectorAll("li")]; expect(options.length).toBe(3); expect(options.map(o => o.getAttribute("role"))).toEqual(["option", "option", "option"]); expect(options.every(o => o.id && o.getAttribute("aria-selected") === "false")).toBe(true); expect(options[0]!.querySelector("a")!.getAttribute("tabindex")).toBe("-1"); expect(popup.hidden).toBe(false); expect(input.getAttribute("aria-expanded")).toBe("true"); expect(host.hasAttribute("data-open")).toBe(true); }); test("typing on aborts the request in flight; only the last query's answer shows", async () => { const signals: AbortSignal[] = []; answer = (url, init) => new Promise((resolve, reject) => { signals.push(init.signal!); init.signal!.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError"))); setTimeout(() => resolve(new Response(url.endsWith("q=elf") ? `` : FRAGMENT)), 50); }); const { input, listbox } = mount(); type(input, "maman"); await sleep(DEBOUNCE_MS + 10); // its request is in flight type(input, "elf"); expect(signals[0]!.aborted).toBe(true); await sleep(DEBOUNCE_MS + 80); expect(calls.map(c => new URL(c.url).searchParams.get("q"))).toEqual(["maman", "elf"]); expect([...listbox.querySelectorAll("li")].map(li => li.textContent)).toEqual(["Elfe"]); }); test("arrows move the active option and wrap; Enter follows its link", async () => { const { input, listbox } = mount(); type(input, "noel"); await settle(); const options = [...listbox.querySelectorAll("li")]; expect(key(input, "ArrowDown").defaultPrevented).toBe(true); expect(input.getAttribute("aria-activedescendant")).toBe(options[0]!.id); expect(options[0]!.getAttribute("aria-selected")).toBe("true"); expect(options[0]!.hasAttribute("data-active")).toBe(true); key(input, "ArrowUp"); expect(input.getAttribute("aria-activedescendant")).toBe(options[2]!.id); key(input, "ArrowDown"); expect(input.getAttribute("aria-activedescendant")).toBe(options[0]!.id); key(input, "ArrowDown"); expect(options[0]!.getAttribute("aria-selected")).toBe("false"); let followed = ""; options[1]!.querySelector("a")!.addEventListener("click", event => { followed = (event.currentTarget as HTMLAnchorElement).getAttribute("href")!; event.preventDefault(); }); expect(key(input, "Enter").defaultPrevented).toBe(true); expect(followed).toBe("/titres/elf"); }); test("Enter with no active option is the form's", async () => { const { input } = mount(); type(input, "noel"); await settle(); expect(key(input, "Enter").defaultPrevented).toBe(false); }); test("Escape closes; ArrowDown opens again on the results already there", async () => { const { input, popup } = mount(); type(input, "noel"); await settle(); key(input, "ArrowDown"); expect(key(input, "Escape").defaultPrevented).toBe(true); expect(popup.hidden).toBe(true); expect(input.getAttribute("aria-expanded")).toBe("false"); expect(input.hasAttribute("aria-activedescendant")).toBe(false); expect(key(input, "Escape").defaultPrevented).toBe(false); // nothing to close key(input, "ArrowDown"); expect(popup.hidden).toBe(false); }); test("a press outside closes; a press inside keeps focus in the field", async () => { const { input, popup } = mount(); type(input, "noel"); await settle(); const inside = new MouseEvent("mousedown", { bubbles: true, cancelable: true }); popup.querySelector("a")!.dispatchEvent(inside); expect(inside.defaultPrevented).toBe(true); popup.querySelector("a")!.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); expect(popup.hidden).toBe(false); document.getElementById("dehors")!.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); expect(popup.hidden).toBe(true); }); test("going under min, an empty answer or a failed request closes the popup", async () => { const { input, popup } = mount(); type(input, "noel"); await settle(); type(input, "n"); await sleep(5); expect(popup.hidden).toBe(true); answer = async () => new Response("
    "); type(input, "zzz"); await settle(); expect(popup.hidden).toBe(true); answer = async () => new Response("boom", { status: 500 }); type(input, "zzzz"); await settle(); expect(popup.hidden).toBe(true); }); test("disconnecting puts the field back as it was", async () => { const { host, input } = mount(); host.remove(); await sleep(5); expect(input.hasAttribute("role")).toBe(false); expect(input.hasAttribute("aria-expanded")).toBe(false); expect(input.hasAttribute("autocomplete")).toBe(false); }); test("optionsOf takes the first list's items", () => { expect(optionsOf(FRAGMENT).map(li => li.querySelector("a")!.getAttribute("href"))).toEqual([ "/titres/home-alone", "/titres/elf", "/titres/grinch", ]); expect(optionsOf("

    rien

    ")).toEqual([]); }); test("the sheet: 44px rows, dark defaults", () => { expect(styles).toContain("44px"); expect(styles).toContain("prefers-color-scheme:dark"); expect(styles).toContain("--pebble-combobox-"); });