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
298 lignes · 12750 octetsBrut · Historique
1// <pebble-tablet> on its own engine. The engine's correctness is proven in
2// packages/tablet/engine/*.test.ts; these tests drive the ELEMENT through
3// synthetic events (happy-dom has no editing engine of its own — which the
4// architecture makes irrelevant: the engine owns editing, so a canceled
5// beforeinput + the mirror IS the typing path). True native typing is the
6// headless-Chromium smoke's job (examples/tablet-smoke.ts).
7import { afterEach, expect, test } from "bun:test";
8import { define } from "@pebble/pebble";
9import { CHANGE_DEBOUNCE, Tablet, type TabletElement } from "../packages/tablet/index";
10
11const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
12
13/** Mounts a <pebble-tablet>; with `text`, a server-rendered textarea child named "body". */
14const mount = (text?: string): { el: TabletElement; field: HTMLTextAreaElement } => {
15 const el = document.createElement("pebble-tablet") as TabletElement;
16 if (text !== undefined) {
17 const field = document.createElement("textarea");
18 field.name = "body";
19 field.value = text;
20 el.appendChild(field);
21 }
22 document.body.appendChild(el);
23 return { el, field: el.querySelector("textarea")! };
24};
25
26/** A cancelable beforeinput, as the browser would fire it. */
27const beforeinput = (inputType: string, data: string | null = null) =>
28 new InputEvent("beforeinput", { inputType, data, bubbles: true, cancelable: true });
29
30/** Focus the field and put the caret at `pos`. */
31const focusAt = (field: HTMLTextAreaElement, pos: number) => {
32 field.focus();
33 field.setSelectionRange(pos, pos);
34 document.dispatchEvent(new Event("selectionchange"));
35};
36
37afterEach(async () => {
38 document.body.textContent = "";
39 await sleep(0); // define() defers releases one macrotask; flush them
40});
41
42test("enhances a server-rendered textarea: same visible field, data-enhanced, initial doc", () => {
43 const { el, field } = mount("server text");
44 expect(field.hidden).toBe(false); // the textarea IS the surface now
45 expect(field.getAttribute("part")).toBe("editor field");
46 expect(el.getAttribute("data-enhanced")).toBe("");
47 expect(el.editor?.doc).toBe("server text");
48});
49
50test("typing: a canceled beforeinput applies through the engine to the field", () => {
51 const { el, field } = mount("hello");
52 focusAt(field, 5);
53 const event = beforeinput("insertText", " world");
54 field.dispatchEvent(event);
55 const value = field.value; // read in the listener's task, where the mirror has written
56 expect(event.defaultPrevented).toBe(true); // the engine took it
57 expect(value).toBe("hello world"); // the mirror wrote it
58 expect(el.editor?.doc).toBe("hello world");
59});
60
61test("the caret is the model's: projected back after an edit", async () => {
62 const { field } = mount("hello");
63 focusAt(field, 0);
64 field.dispatchEvent(beforeinput("insertText", "x"));
65 await sleep(0); // the projection runs once the edit's writes settle
66 expect(field.selectionStart).toBe(1);
67 expect(field.selectionEnd).toBe(1);
68});
69
70test("an inputType the engine declines is the browser's to make (not canceled)", () => {
71 const { field } = mount("hello");
72 focusAt(field, 5);
73 const event = beforeinput("insertFromPasteAsQuotation", "quoted"); // not in the engine's vocabulary
74 field.dispatchEvent(event);
75 expect(event.defaultPrevented).toBe(false); // declined: the native edit and the input reconcile own it
76 expect(field.value).toBe("hello"); // untouched by the engine
77 // (happy-dom coerces data:null to "", so the unreadable-text decline —
78 // same filter branch — is covered by the engine's carriesText tests and
79 // owned end-to-end by the Chromium smoke.)
80});
81
82test("doc → form: a plain FormData read sends what was typed", () => {
83 const form = document.createElement("form");
84 document.body.appendChild(form);
85 const el = document.createElement("pebble-tablet") as TabletElement;
86 const field = document.createElement("textarea");
87 field.name = "body";
88 field.value = "hello";
89 el.appendChild(field);
90 form.appendChild(el);
91 focusAt(field, 5);
92 field.dispatchEvent(beforeinput("insertText", " world"));
93 expect(new FormData(form).get("body")).toBe("hello world");
94});
95
96test("textarea → doc: a programmatic write to the field reconciles by diff", () => {
97 const { el, field } = mount("hello");
98 field.value = "replaced";
99 field.dispatchEvent(new Event("input"));
100 expect(el.editor?.doc).toBe("replaced");
101 // and the field was not fought: the mirror sees an equal value, writes nothing
102 expect(field.value).toBe("replaced");
103});
104
105test("undo reaches past a programmatic write into what was typed before it", () => {
106 const { el, field } = mount("");
107 focusAt(field, 0);
108 field.dispatchEvent(beforeinput("insertText", "mine"));
109 expect(field.value).toBe("mine");
110 field.value = "theirs";
111 field.dispatchEvent(new Event("input"));
112 expect(field.value).toBe("theirs");
113 // Native textarea undo would be dead here (the value write cleared it).
114 field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true }));
115 expect(field.value).toBe("mine");
116 expect(el.editor?.doc).toBe("mine");
117});
118
119test("redo restores what undo took", () => {
120 const { field } = mount("");
121 focusAt(field, 0);
122 field.dispatchEvent(beforeinput("insertText", "typed"));
123 field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true }));
124 expect(field.value).toBe("");
125 field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, shiftKey: true, bubbles: true, cancelable: true }));
126 expect(field.value).toBe("typed");
127});
128
129test("a run of typing undoes as a run, not a character at a time", () => {
130 const { field } = mount("");
131 focusAt(field, 0);
132 for (const char of "abc") field.dispatchEvent(beforeinput("insertText", char));
133 expect(field.value).toBe("abc");
134 field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true }));
135 expect(field.value).toBe("");
136});
137
138test("Mod+A selects the document through the engine", async () => {
139 const { field } = mount("hello world");
140 focusAt(field, 3);
141 field.dispatchEvent(new KeyboardEvent("keydown", { key: "a", ctrlKey: true, bubbles: true, cancelable: true }));
142 await sleep(0);
143 expect(field.selectionStart).toBe(0);
144 expect(field.selectionEnd).toBe(11);
145});
146
147test("change leaves debounced: rapid edits emit once, with the latest text", async () => {
148 const { el, field } = mount("start");
149 focusAt(field, 5);
150 const events: string[] = [];
151 el.addEventListener("change", e => events.push((e as CustomEvent<{ value: string }>).detail.value));
152 field.dispatchEvent(beforeinput("insertText", " a"));
153 await sleep(50);
154 field.dispatchEvent(beforeinput("insertText", "b"));
155 await sleep(CHANGE_DEBOUNCE - 150); // still inside the silence window
156 expect(events).toEqual([]);
157 await sleep(CHANGE_DEBOUNCE); // past it
158 expect(events).toEqual(["start ab"]);
159});
160
161test("no change event for the initial value", async () => {
162 const { el } = mount("start");
163 const events: string[] = [];
164 el.addEventListener("change", e => events.push((e as CustomEvent).type));
165 await sleep(CHANGE_DEBOUNCE + 100);
166 expect(events).toEqual([]);
167});
168
169test("Mod+S emits save with the text and prevents the browser default", () => {
170 const { el, field } = mount("abc");
171 const saves: string[] = [];
172 el.addEventListener("save", e => saves.push((e as CustomEvent<{ value: string }>).detail.value));
173 const chord = new KeyboardEvent("keydown", { key: "s", ctrlKey: true, bubbles: true, cancelable: true });
174 field.dispatchEvent(chord);
175 expect(saves).toEqual(["abc"]);
176 expect(chord.defaultPrevented).toBe(true);
177});
178
179test("no chord resolves while an IME composition is in progress", () => {
180 const { el, field } = mount("abc");
181 const saves: string[] = [];
182 el.addEventListener("save", () => saves.push("save"));
183 const chord = new KeyboardEvent("keydown", {
184 key: "s",
185 ctrlKey: true,
186 isComposing: true,
187 bubbles: true,
188 cancelable: true,
189 });
190 field.dispatchEvent(chord);
191 expect(saves).toEqual([]);
192 expect(chord.defaultPrevented).toBe(false);
193});
194
195test("IME: composing text is never canceled nor folded in; compositionend folds it as one step", async () => {
196 const { el, field } = mount("a");
197 focusAt(field, 1);
198 field.dispatchEvent(new CompositionEvent("compositionstart", { bubbles: true }));
199 // The browser writes composed text straight into the field; the engine
200 // must neither cancel insertCompositionText nor take it into the model.
201 field.value = "aか";
202 const composing = new InputEvent("beforeinput", {
203 inputType: "insertCompositionText",
204 data: "か",
205 isComposing: true,
206 bubbles: true,
207 cancelable: true,
208 });
209 field.dispatchEvent(composing);
210 expect(composing.defaultPrevented).toBe(false);
211 expect(el.editor?.doc).toBe("a"); // the model stands still
212 field.value = "aかな";
213 field.dispatchEvent(new CompositionEvent("compositionend", { bubbles: true }));
214 expect(el.editor?.doc).toBe("aかな"); // folded in whole
215 // one undo removes the composition, and only it
216 field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true }));
217 expect(field.value).toBe("a");
218});
219
220test("paste: clipboard text applies as one edit; the event is canceled", () => {
221 const { el, field } = mount("hello ");
222 focusAt(field, 6);
223 const event = new ClipboardEvent("paste", { bubbles: true, cancelable: true });
224 Object.defineProperty(event, "clipboardData", {
225 value: { getData: (type: string) => (type === "text/plain" ? "world" : "") },
226 });
227 field.dispatchEvent(event);
228 expect(event.defaultPrevented).toBe(true);
229 expect(field.value).toBe("hello world");
230 expect(el.editor?.doc).toBe("hello world");
231});
232
233test("cut: the selection goes to the clipboard and out of the document", () => {
234 const { field } = mount("hello world");
235 focusAt(field, 5);
236 field.setSelectionRange(5, 11);
237 document.dispatchEvent(new Event("selectionchange"));
238 const store = new Map<string, string>();
239 const event = new ClipboardEvent("cut", { bubbles: true, cancelable: true });
240 Object.defineProperty(event, "clipboardData", {
241 value: { getData: (t: string) => store.get(t) ?? "", setData: (t: string, v: string) => void store.set(t, v) },
242 });
243 field.dispatchEvent(event);
244 expect(event.defaultPrevented).toBe(true);
245 expect(store.get("text/plain")).toBe(" world");
246 expect(field.value).toBe("hello");
247});
248
249test("a form reset restores the default value into the model", async () => {
250 const form = document.createElement("form");
251 document.body.appendChild(form);
252 const el = document.createElement("pebble-tablet") as TabletElement;
253 const field = document.createElement("textarea");
254 field.name = "body";
255 field.textContent = "default"; // the server-rendered default: reset restores defaultValue
256 el.appendChild(field);
257 form.appendChild(el);
258 focusAt(field, 7);
259 field.dispatchEvent(beforeinput("insertText", " typed"));
260 expect(field.value).toBe("default typed");
261 form.reset();
262 await sleep(10); // the reset read runs once the browser has settled the value
263 expect(field.value).toBe("default");
264 expect(el.editor?.doc).toBe("default");
265});
266
267test("without a textarea child an empty one is rendered; the host's name passes through", () => {
268 const el = document.createElement("pebble-tablet") as TabletElement;
269 el.setAttribute("name", "note");
270 document.body.appendChild(el);
271 const field = el.querySelector("textarea")!;
272 expect(field).not.toBeNull();
273 expect(field.name).toBe("note");
274 expect(field.hidden).toBe(false);
275 expect(el.editor?.doc).toBe("");
276 focusAt(field, 0);
277 field.dispatchEvent(beforeinput("insertText", "typed"));
278 expect(field.value).toBe("typed");
279});
280
281test("once-only: a second define() of the tag returns the first constructor, registers nothing twice", () => {
282 // The double-import case (two vendored bundles, or a module graph that
283 // reaches the package twice) ends here: define() sees the tag is taken and
284 // hands back the existing constructor instead of throwing or replacing it.
285 const again = define("pebble-tablet", { shadow: false }, () => null);
286 expect(again).toBe(Tablet);
287 const el = document.createElement("pebble-tablet") as TabletElement;
288 document.body.appendChild(el);
289 expect(el.editor).toBeDefined(); // the original render still runs
290});
291
292test("disconnect releases the element's engine view", async () => {
293 const { el } = mount("hello");
294 el.remove();
295 await sleep(0); // the release runs one macrotask after disconnect
296 expect(el.editor).toBeUndefined();
297 expect(el.getAttribute("data-enhanced")).toBeNull();
298});