/** @jsxImportSource react */ import { expect, test } from "bun:test"; import { act } from "react"; import { useDialog, type AlertContext, type DialogApi } from "../../../ui/dialog"; import { WebDialogHostProvider } from "./dialog-host"; import { moveDialogFocus } from "./key-event"; import { createDomTestHarness } from "./test-utils"; const { window, render } = createDomTestHarness({ withUi: false }); let dialog: DialogApi | null = null; function Capture() { dialog = useDialog(); return null; } function Buttons({ names }: { names: string[] }) { return
{names.map((name) => )}
; } function tab(shiftKey = false) { return moveDialogFocus({ key: "Tab", shiftKey, ctrlKey: false, metaKey: false, altKey: false, defaultPrevented: false }); } function focusedText() { return window.document.activeElement?.textContent ?? null; } test("a dialog opened from a dialog stacks, and closing it returns to the first", async () => { await render(); let dismissInner: (() => void) | null = null; await act(async () => { void dialog!.alert({ content: }); }); await act(async () => { void dialog!.alert({ content: (ctx: AlertContext) => { dismissInner = ctx.dismiss; return ; }, }); }); const texts = () => [...window.document.querySelectorAll(".gloom-dialog")].map((node) => node.textContent); expect(texts()).toEqual(["Outer", "Inner"]); await act(async () => { dismissInner!(); }); expect(texts()).toEqual(["Outer"]); }); test("Tab walks the topmost dialog's controls and wraps", async () => { await render(); await act(async () => { void dialog!.alert({ content: }); }); expect(tab()).toBe(true); expect(focusedText()).toBe("Save"); tab(); expect(focusedText()).toBe("Cancel"); tab(); expect(focusedText()).toBe("Save"); tab(true); expect(focusedText()).toBe("Cancel"); }); test("Tab is left alone when no dialog is open or a handler already took it", async () => { await render(); expect(tab()).toBe(false); await act(async () => { void dialog!.alert({ content: }); }); expect(moveDialogFocus({ key: "Tab", shiftKey: false, ctrlKey: false, metaKey: false, altKey: false, defaultPrevented: true })).toBe(false); });