The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import { page, userEvent } from "vite-plus/test/browser"; |
| 3 | |
| 4 | import { render } from "../../test/render"; |
| 5 | import { useCommitOnBlur } from "../hooks/useCommitOnBlur"; |
| 6 | import { AppRouteModal } from "./AppRouteModal"; |
| 7 | |
| 8 | vi.mock("./chat/GlobalVoiceInputIndicator", () => ({ GlobalVoiceInputIndicator: () => null })); |
| 9 | |
| 10 | function CommitOnBlurField({ onCommit }: { onCommit: (next: string) => void }) { |
| 11 | return <input aria-label="Instructions" {...useCommitOnBlur("", onCommit)} />; |
| 12 | } |
| 13 | |
| 14 | describe("AppRouteModal", () => { |
| 15 | it("commits a focused field when Esc closes it", async () => { |
| 16 | const onCommit = vi.fn(); |
| 17 | await using _screen = await render( |
| 18 | <AppRouteModal label="Settings"> |
| 19 | <CommitOnBlurField onCommit={onCommit} /> |
| 20 | </AppRouteModal>, |
| 21 | { router: "/settings/general" }, |
| 22 | ); |
| 23 | |
| 24 | await page.getByRole("textbox", { name: "Instructions" }).fill("Keep titles short"); |
| 25 | await userEvent.keyboard("{Escape}"); |
| 26 | |
| 27 | expect(onCommit).toHaveBeenCalledWith("Keep titles short"); |
| 28 | }); |
| 29 | |
| 30 | it("returns focus to the opener when Esc closes it", async () => { |
| 31 | const opener = <button type="button">Open usage</button>; |
| 32 | await using screen = await render(opener, { router: "/" }); |
| 33 | const openerButton = screen.getByRole("button", { name: "Open usage" }); |
| 34 | (openerButton.element() as HTMLElement).focus(); |
| 35 | |
| 36 | await screen.rerender( |
| 37 | <> |
| 38 | {opener} |
| 39 | <AppRouteModal label="Usage"> |
| 40 | <p>Usage</p> |
| 41 | </AppRouteModal> |
| 42 | </>, |
| 43 | ); |
| 44 | await expect.element(page.getByRole("dialog", { name: "Usage" })).toHaveFocus(); |
| 45 | await userEvent.keyboard("{Escape}"); |
| 46 | |
| 47 | await expect.element(page.getByRole("dialog", { name: "Usage" })).not.toBeInTheDocument(); |
| 48 | await expect.element(openerButton).toHaveFocus(); |
| 49 | }); |
| 50 | |
| 51 | it("stays open when Esc belongs to a modal outside Base UI", async () => { |
| 52 | await using _screen = await render( |
| 53 | <AppRouteModal label="Settings"> |
| 54 | <CommitOnBlurField onCommit={() => undefined} /> |
| 55 | </AppRouteModal>, |
| 56 | { router: "/settings/general" }, |
| 57 | ); |
| 58 | const settings = page.getByRole("dialog", { name: "Settings" }); |
| 59 | // Base UI takes initial focus a frame after opening; let it land before Clerk's modal takes focus. |
| 60 | await expect.element(settings).toHaveFocus(); |
| 61 | // Clerk renders its account modal in its own root and dismisses on a document Esc listener. |
| 62 | const foreignModal = document.body.appendChild(document.createElement("input")); |
| 63 | using _foreignModal = { [Symbol.dispose]: () => foreignModal.remove() }; |
| 64 | |
| 65 | foreignModal.focus(); |
| 66 | await userEvent.keyboard("{Escape}"); |
| 67 | |
| 68 | await expect.element(settings).toHaveAttribute("data-open"); |
| 69 | }); |
| 70 | }); |