// CAPABILITY PROBE — what happy-dom 17 gives the tablet element tests. // The engine owns editing, so the tests drive synthetic events; this probe // pins down which DOM APIs the synthetic path can rely on. Anything missing // here gets the thinnest mock seam in the element tests, and the Chromium // smoke (examples/tablet-smoke.ts) owns true typing. import { expect, test } from "bun:test"; test("probe: InputEvent carries inputType/data and is cancelable", () => { const field = document.createElement("textarea"); document.body.appendChild(field); const event = new InputEvent("beforeinput", { inputType: "insertText", data: "x", bubbles: true, cancelable: true }); field.dispatchEvent(event); expect(event.inputType).toBe("insertText"); expect(event.data).toBe("x"); expect(event.defaultPrevented).toBe(false); document.body.textContent = ""; }); test("probe: preventDefault on beforeinput is visible synchronously", () => { const field = document.createElement("textarea"); document.body.appendChild(field); field.addEventListener("beforeinput", event => event.preventDefault()); const event = new InputEvent("beforeinput", { inputType: "deleteContentBackward", bubbles: true, cancelable: true }); field.dispatchEvent(event); expect(event.defaultPrevented).toBe(true); document.body.textContent = ""; }); test("probe: CompositionEvent constructs and dispatches", () => { const field = document.createElement("textarea"); document.body.appendChild(field); const seen: string[] = []; field.addEventListener("compositionstart", () => seen.push("start")); field.addEventListener("compositionend", () => seen.push("end")); field.dispatchEvent(new CompositionEvent("compositionstart", { bubbles: true })); field.dispatchEvent(new CompositionEvent("compositionend", { bubbles: true })); expect(seen).toEqual(["start", "end"]); // Gap: happy-dom's CompositionEvent ignores the `data` init. No seam // needed — the element never reads it: the compose action folds in // field.value, which happy-dom sets fine. document.body.textContent = ""; }); test("probe: textarea selection APIs (selectionStart/End, setSelectionRange, direction)", () => { const field = document.createElement("textarea"); field.value = "hello world"; document.body.appendChild(field); field.selectionStart = 6; field.selectionEnd = 11; expect(field.selectionStart).toBe(6); expect(field.selectionEnd).toBe(11); // start <= end always (the engine projects min/max + direction, never a // reversed pair — happy-dom collapses a reversed pair, Chromium swaps it). field.setSelectionRange(6, 11, "backward"); expect(field.selectionStart).toBe(6); expect(field.selectionEnd).toBe(11); expect(field.selectionDirection).toBe("backward"); document.body.textContent = ""; }); test("probe: focus() sets document.activeElement; selectionchange dispatches on document", () => { const field = document.createElement("textarea"); document.body.appendChild(field); field.focus(); expect(document.activeElement).toBe(field); let fired = 0; const listener = () => (fired += 1); document.addEventListener("selectionchange", listener); document.dispatchEvent(new Event("selectionchange")); document.removeEventListener("selectionchange", listener); expect(fired).toBe(1); document.body.textContent = ""; }); test("probe: ClipboardEvent with a clipboardData payload", () => { const field = document.createElement("textarea"); document.body.appendChild(field); // happy-dom 17 has no DataTransfer-in-ClipboardEvent constructor support; // the thinnest seam: assign a clipboardData-shaped stub before dispatch. const event = new ClipboardEvent("paste", { bubbles: true, cancelable: true }); const store = new Map([["text/plain", "pasted text"]]); Object.defineProperty(event, "clipboardData", { value: { getData: (type: string) => store.get(type) ?? "", setData: (type: string, text: string) => void store.set(type, text) }, }); field.dispatchEvent(event); expect((event as unknown as { clipboardData: { getData(t: string): string } }).clipboardData.getData("text/plain")).toBe("pasted text"); document.body.textContent = ""; }); test("probe: KeyboardEvent isComposing flag survives construction", () => { const event = new KeyboardEvent("keydown", { key: "s", ctrlKey: true, isComposing: true, bubbles: true, cancelable: true }); expect(event.isComposing).toBe(true); }); test("probe: event.timeStamp is a number the history grouper can use", () => { const event = new InputEvent("beforeinput", { inputType: "insertText", data: "x" }); expect(typeof event.timeStamp).toBe("number"); });