/* Copyright 2026 Marimo. All rights reserved. */ import { afterEach, describe, expect, it } from "vitest"; import { isInteractiveTarget } from "../use-cell-range-selection"; /** * Dispatch a real `mousedown` from `target` (going through real DOM event * dispatch so `composedPath()` traverses any open shadow roots) and call * `isInteractiveTarget` from inside the listener, where the event's target * and composed path are still live. */ function isInteractive(target: Element, cell: Element): boolean { let result: boolean | undefined; const handler = (event: Event) => { const path = event.composedPath(); result = isInteractiveTarget({ target: event.target, currentTarget: cell, nativeEvent: { composedPath: () => path, }, } as unknown as React.MouseEvent); }; cell.addEventListener("mousedown", handler); target.dispatchEvent( new MouseEvent("mousedown", { bubbles: true, composed: true }), ); cell.removeEventListener("mousedown", handler); if (result === undefined) { throw new Error("mousedown did not bubble to the cell"); } return result; } let mounted: HTMLElement[] = []; function makeCell(): HTMLTableCellElement { const table = document.createElement("table"); const tbody = document.createElement("tbody"); const row = document.createElement("tr"); const cell = document.createElement("td"); row.append(cell); tbody.append(row); table.append(tbody); document.body.append(table); mounted.push(table); return cell; } afterEach(() => { for (const el of mounted) { el.remove(); } mounted = []; }); describe("isInteractiveTarget", () => { it("returns false when target is the cell itself", () => { const cell = makeCell(); expect(isInteractive(cell, cell)).toBe(false); }); it("returns false when clicking plain text inside a cell", () => { const cell = makeCell(); const span = document.createElement("span"); cell.append(span); expect(isInteractive(span, cell)).toBe(false); }); it.each(["input", "button", "select", "textarea"])( "returns true when clicking a <%s>", (tag) => { const cell = makeCell(); const el = document.createElement(tag); cell.append(el); expect(isInteractive(el, cell)).toBe(true); }, ); it("returns true when clicking an link", () => { const cell = makeCell(); const a = document.createElement("a"); a.href = "#"; cell.append(a); expect(isInteractive(a, cell)).toBe(true); }); it("returns true when clicking a