Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { page, userEvent } from "vite-plus/test/browser";
3
4import { render } from "../../test/render";
5import { useCommitOnBlur } from "../hooks/useCommitOnBlur";
6import { AppRouteModal } from "./AppRouteModal";
7
8function CommitOnBlurField({ onCommit }: { onCommit: (next: string) => void }) {
9 return <input aria-label="Instructions" {...useCommitOnBlur("", onCommit)} />;
10}
11
12describe("AppRouteModal", () => {
13 it("commits a focused field when Esc closes it", async () => {
14 const onCommit = vi.fn();
15 await using _screen = await render(
16 <AppRouteModal label="Settings">
17 <CommitOnBlurField onCommit={onCommit} />
18 </AppRouteModal>,
19 { router: "/settings/general" },
20 );
21
22 await page.getByRole("textbox", { name: "Instructions" }).fill("Keep titles short");
23 await userEvent.keyboard("{Escape}");
24
25 expect(onCommit).toHaveBeenCalledWith("Keep titles short");
26 });
27
28 it("stays open when Esc belongs to a modal outside Base UI", async () => {
29 await using _screen = await render(
30 <AppRouteModal label="Settings">
31 <CommitOnBlurField onCommit={() => undefined} />
32 </AppRouteModal>,
33 { router: "/settings/general" },
34 );
35 const settings = page.getByRole("dialog", { name: "Settings" });
36 // Base UI takes initial focus a frame after opening; let it land before Clerk's modal takes focus.
37 await expect.element(settings).toHaveFocus();
38 // Clerk renders its account modal in its own root and dismisses on a document Esc listener.
39 const foreignModal = document.body.appendChild(document.createElement("input"));
40 using _foreignModal = { [Symbol.dispose]: () => foreignModal.remove() };
41
42 foreignModal.focus();
43 await userEvent.keyboard("{Escape}");
44
45 await expect.element(settings).toHaveAttribute("data-open");
46 });
47});