tobogganSe connecter

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
219 lignes · 9039 octetsBrut · Historique
1// <pebble-combobox>: the ARIA combobox wiring, the debounced and aborted
2// fetch, the options from the server's fragment, the keys, closing, and the
3// field restored on disconnect. fetch is a fake; real layout is the smoke's
4// job (examples/combobox.html).
5import { afterAll, afterEach, beforeAll, beforeEach, expect, test } from "bun:test";
6import { DEBOUNCE_MS, optionsOf } from "../packages/combobox/index";
7import styles from "../packages/combobox/combobox.css";
8
9const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms));
10const settle = () => sleep(DEBOUNCE_MS + 30);
11
12const FRAGMENT = `<ul>
13 <li><a href="/titres/home-alone"><img src="/a/1.jpg" alt=""> Maman, j’ai raté l’avion <small>1990</small></a></li>
14 <li><a href="/titres/elf"><img src="/a/2.jpg" alt=""> Elfe <small>2003</small></a></li>
15 <li><a href="/titres/grinch"><img src="/a/3.jpg" alt=""> Le Grincheux <small>2000</small></a></li>
16</ul>`;
17
18let calls: { url: string; init: RequestInit }[] = [];
19let answer: (url: string, init: RequestInit) => Promise<Response>;
20const realFetch = globalThis.fetch;
21const happy = (window as unknown as { happyDOM: { setURL(url: string): void } }).happyDOM;
22
23beforeAll(() => happy.setURL("https://cine.example/proposer/film"));
24afterAll(() => happy.setURL("about:blank"));
25
26beforeEach(() => {
27 calls = [];
28 answer = async () => new Response(FRAGMENT, { headers: { "content-type": "text/html" } });
29 globalThis.fetch = (async (url: string | URL, init: RequestInit = {}) => {
30 calls.push({ url: String(url), init });
31 return answer(String(url), init);
32 }) as typeof fetch;
33});
34
35afterEach(async () => {
36 globalThis.fetch = realFetch;
37 document.body.textContent = "";
38 await sleep(0);
39});
40
41const mount = (attrs = 'src="/proposer/film/suggestions" min="2"') => {
42 const wrap = document.createElement("div");
43 wrap.innerHTML = `<pebble-combobox ${attrs}>
44 <form method="get" action="/proposer/film">
45 <label for="q">Un film</label><input id="q" name="q" type="search"><button>Chercher</button>
46 </form>
47 </pebble-combobox><p id="dehors">ailleurs</p>`;
48 document.body.appendChild(wrap);
49 const host = wrap.querySelector("pebble-combobox") as HTMLElement;
50 const input = host.querySelector("input")!;
51 const listbox = host.querySelector("[role=listbox]") as HTMLElement;
52 return { host, input, listbox, popup: host.querySelector(".popup") as HTMLElement };
53};
54
55const type = (input: HTMLInputElement, value: string) => {
56 input.value = value;
57 input.dispatchEvent(new Event("input", { bubbles: true }));
58};
59const key = (input: HTMLInputElement, k: string) => {
60 const event = new KeyboardEvent("keydown", { key: k, bubbles: true, cancelable: true });
61 input.dispatchEvent(event);
62 return event;
63};
64
65test("the field becomes a combobox controlling a labelled, hidden listbox", () => {
66 const { input, listbox, popup } = mount();
67 expect(input.getAttribute("role")).toBe("combobox");
68 expect(input.getAttribute("aria-autocomplete")).toBe("list");
69 expect(input.getAttribute("aria-expanded")).toBe("false");
70 expect(input.getAttribute("aria-controls")).toBe(listbox.id);
71 expect(input.getAttribute("autocomplete")).toBe("off");
72 expect(listbox.getAttribute("aria-labelledby")).toBe(document.querySelector("label")!.id);
73 expect(popup.hidden).toBe(true);
74});
75
76test("fetches after the pause, with at least min characters, and shows the options", async () => {
77 const { host, input, listbox, popup } = mount();
78 type(input, "m");
79 await settle();
80 expect(calls.length).toBe(0);
81
82 type(input, "ma");
83 await sleep(DEBOUNCE_MS - 100);
84 expect(calls.length).toBe(0);
85 await sleep(130);
86 expect(calls.length).toBe(1);
87 expect(calls[0]!.url).toBe("https://cine.example/proposer/film/suggestions?q=ma");
88 expect((calls[0]!.init.headers as Record<string, string>).Accept).toBe("text/html");
89 await sleep(5);
90
91 const options = [...listbox.querySelectorAll("li")];
92 expect(options.length).toBe(3);
93 expect(options.map(o => o.getAttribute("role"))).toEqual(["option", "option", "option"]);
94 expect(options.every(o => o.id && o.getAttribute("aria-selected") === "false")).toBe(true);
95 expect(options[0]!.querySelector("a")!.getAttribute("tabindex")).toBe("-1");
96 expect(popup.hidden).toBe(false);
97 expect(input.getAttribute("aria-expanded")).toBe("true");
98 expect(host.hasAttribute("data-open")).toBe(true);
99});
100
101test("typing on aborts the request in flight; only the last query's answer shows", async () => {
102 const signals: AbortSignal[] = [];
103 answer = (url, init) =>
104 new Promise((resolve, reject) => {
105 signals.push(init.signal!);
106 init.signal!.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")));
107 setTimeout(() => resolve(new Response(url.endsWith("q=elf") ? `<ul><li><a href="/titres/elf">Elfe</a></li></ul>` : FRAGMENT)), 50);
108 });
109 const { input, listbox } = mount();
110 type(input, "maman");
111 await sleep(DEBOUNCE_MS + 10); // its request is in flight
112 type(input, "elf");
113 expect(signals[0]!.aborted).toBe(true);
114 await sleep(DEBOUNCE_MS + 80);
115 expect(calls.map(c => new URL(c.url).searchParams.get("q"))).toEqual(["maman", "elf"]);
116 expect([...listbox.querySelectorAll("li")].map(li => li.textContent)).toEqual(["Elfe"]);
117});
118
119test("arrows move the active option and wrap; Enter follows its link", async () => {
120 const { input, listbox } = mount();
121 type(input, "noel");
122 await settle();
123 const options = [...listbox.querySelectorAll("li")];
124 expect(key(input, "ArrowDown").defaultPrevented).toBe(true);
125 expect(input.getAttribute("aria-activedescendant")).toBe(options[0]!.id);
126 expect(options[0]!.getAttribute("aria-selected")).toBe("true");
127 expect(options[0]!.hasAttribute("data-active")).toBe(true);
128 key(input, "ArrowUp");
129 expect(input.getAttribute("aria-activedescendant")).toBe(options[2]!.id);
130 key(input, "ArrowDown");
131 expect(input.getAttribute("aria-activedescendant")).toBe(options[0]!.id);
132 key(input, "ArrowDown");
133 expect(options[0]!.getAttribute("aria-selected")).toBe("false");
134
135 let followed = "";
136 options[1]!.querySelector("a")!.addEventListener("click", event => {
137 followed = (event.currentTarget as HTMLAnchorElement).getAttribute("href")!;
138 event.preventDefault();
139 });
140 expect(key(input, "Enter").defaultPrevented).toBe(true);
141 expect(followed).toBe("/titres/elf");
142});
143
144test("Enter with no active option is the form's", async () => {
145 const { input } = mount();
146 type(input, "noel");
147 await settle();
148 expect(key(input, "Enter").defaultPrevented).toBe(false);
149});
150
151test("Escape closes; ArrowDown opens again on the results already there", async () => {
152 const { input, popup } = mount();
153 type(input, "noel");
154 await settle();
155 key(input, "ArrowDown");
156 expect(key(input, "Escape").defaultPrevented).toBe(true);
157 expect(popup.hidden).toBe(true);
158 expect(input.getAttribute("aria-expanded")).toBe("false");
159 expect(input.hasAttribute("aria-activedescendant")).toBe(false);
160 expect(key(input, "Escape").defaultPrevented).toBe(false); // nothing to close
161 key(input, "ArrowDown");
162 expect(popup.hidden).toBe(false);
163});
164
165test("a press outside closes; a press inside keeps focus in the field", async () => {
166 const { input, popup } = mount();
167 type(input, "noel");
168 await settle();
169 const inside = new MouseEvent("mousedown", { bubbles: true, cancelable: true });
170 popup.querySelector("a")!.dispatchEvent(inside);
171 expect(inside.defaultPrevented).toBe(true);
172 popup.querySelector("a")!.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
173 expect(popup.hidden).toBe(false);
174 document.getElementById("dehors")!.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
175 expect(popup.hidden).toBe(true);
176});
177
178test("going under min, an empty answer or a failed request closes the popup", async () => {
179 const { input, popup } = mount();
180 type(input, "noel");
181 await settle();
182 type(input, "n");
183 await sleep(5);
184 expect(popup.hidden).toBe(true);
185
186 answer = async () => new Response("<ul></ul>");
187 type(input, "zzz");
188 await settle();
189 expect(popup.hidden).toBe(true);
190
191 answer = async () => new Response("boom", { status: 500 });
192 type(input, "zzzz");
193 await settle();
194 expect(popup.hidden).toBe(true);
195});
196
197test("disconnecting puts the field back as it was", async () => {
198 const { host, input } = mount();
199 host.remove();
200 await sleep(5);
201 expect(input.hasAttribute("role")).toBe(false);
202 expect(input.hasAttribute("aria-expanded")).toBe(false);
203 expect(input.hasAttribute("autocomplete")).toBe(false);
204});
205
206test("optionsOf takes the first list's items", () => {
207 expect(optionsOf(FRAGMENT).map(li => li.querySelector("a")!.getAttribute("href"))).toEqual([
208 "/titres/home-alone",
209 "/titres/elf",
210 "/titres/grinch",
211 ]);
212 expect(optionsOf("<p>rien</p>")).toEqual([]);
213});
214
215test("the sheet: 44px rows, dark defaults", () => {
216 expect(styles).toContain("44px");
217 expect(styles).toContain("prefers-color-scheme:dark");
218 expect(styles).toContain("--pebble-combobox-");
219});