/* Copyright 2026 Marimo. All rights reserved. */ import { describe, expect, test } from "vitest"; import { Events } from "../events"; /** * Create a minimal fake event with just a target (no composedPath). * Simulates synthetic events from libraries like React Aria. */ function fakeEvent(target: EventTarget): Pick { return { target }; } /** * Create a fake native-like KeyboardEvent with composedPath() returning * a different element than target — the key scenario for shadow DOM * retargeting where target is the shadow host but composedPath()[0] * is the real focused element inside the shadow root. */ function fakeNativeEvent( retargetedHost: EventTarget, realTarget: EventTarget, ): KeyboardEvent { const event = new KeyboardEvent("keydown"); Object.defineProperty(event, "target", { value: retargetedHost }); Object.defineProperty(event, "composedPath", { value: () => [realTarget, retargetedHost, document.body, document], }); return event; } describe("Events.composedTarget", () => { test("returns composedPath()[0] for native events", () => { const input = document.createElement("input"); const host = document.createElement("div"); const event = fakeNativeEvent(host, input); expect(Events.composedTarget(event)).toBe(input); }); test("falls back to e.target when composedPath is absent", () => { const div = document.createElement("div"); const event = fakeEvent(div); expect(Events.composedTarget(event)).toBe(div); }); test("falls back to e.target when composedPath returns empty array", () => { const div = document.createElement("div"); const event = new KeyboardEvent("keydown"); Object.defineProperty(event, "target", { value: div }); Object.defineProperty(event, "composedPath", { value: () => [] }); expect(Events.composedTarget(event)).toBe(div); }); }); describe("Events.shouldIgnoreKeyboardEvent", () => { test("ignores events from ", () => { const input = document.createElement("input"); const event = fakeNativeEvent(input, input); expect(Events.shouldIgnoreKeyboardEvent(event)).toBe(true); }); test("ignores events from