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 { createRef } from "react"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | import { page, userEvent } from "vite-plus/test/browser"; |
| 4 | |
| 5 | import { requestConfirmDialog, resetConfirmDialogForTests } from "~/confirmDialog"; |
| 6 | import { act, render } from "../../../test/render"; |
| 7 | import { ConfirmDialogHost } from "../ConfirmDialogHost"; |
| 8 | import { Button } from "./button"; |
| 9 | import { |
| 10 | Dialog, |
| 11 | DialogAction, |
| 12 | DialogClose, |
| 13 | DialogPopup, |
| 14 | DialogTitle, |
| 15 | DialogTrigger, |
| 16 | } from "./dialog"; |
| 17 | |
| 18 | describe("dialog initial focus", () => { |
| 19 | it.each([ |
| 20 | { label: "generic", options: undefined, focused: "Cancel", confirmed: false }, |
| 21 | { |
| 22 | label: "destructive opt-in", |
| 23 | options: { variant: "destructive", initialFocus: "confirm" }, |
| 24 | focused: "Cancel", |
| 25 | confirmed: false, |
| 26 | }, |
| 27 | { label: "opt-in", options: { initialFocus: "confirm" }, focused: "Confirm", confirmed: true }, |
| 28 | ] as const)( |
| 29 | "focuses $focused for a $label confirmation", |
| 30 | async ({ options, focused, confirmed }) => { |
| 31 | resetConfirmDialogForTests(); |
| 32 | await using _screen = await render(<ConfirmDialogHost />); |
| 33 | let confirmation: Promise<boolean> | undefined; |
| 34 | await act(() => { |
| 35 | confirmation = requestConfirmDialog("Continue?", options); |
| 36 | }); |
| 37 | |
| 38 | await expect.element(page.getByRole("button", { name: focused, exact: true })).toHaveFocus(); |
| 39 | await userEvent.keyboard("{Enter}"); |
| 40 | await expect(confirmation).resolves.toBe(confirmed); |
| 41 | }, |
| 42 | ); |
| 43 | |
| 44 | it("focuses the dialog action and keeps the caller's popup ref", async () => { |
| 45 | const popup = createRef<HTMLDivElement>(); |
| 46 | await using _screen = await render( |
| 47 | <Dialog> |
| 48 | <DialogTrigger>Open retry</DialogTrigger> |
| 49 | <DialogPopup ref={popup}> |
| 50 | <DialogTitle>Import failed</DialogTitle> |
| 51 | <DialogClose>Cancel</DialogClose> |
| 52 | <DialogAction>Try again</DialogAction> |
| 53 | </DialogPopup> |
| 54 | </Dialog>, |
| 55 | ); |
| 56 | await act(() => page.getByRole("button", { name: "Open retry" }).element().focus()); |
| 57 | await userEvent.keyboard("{Enter}"); |
| 58 | |
| 59 | await expect.element(page.getByRole("button", { name: "Try again" })).toHaveFocus(); |
| 60 | expect(popup.current).toBe(page.getByRole("dialog").element()); |
| 61 | }); |
| 62 | |
| 63 | it.each(["disabled action", "caller initialFocus"] as const)( |
| 64 | "falls back to Cancel for a %s", |
| 65 | async (reason) => { |
| 66 | const cancel = createRef<HTMLButtonElement>(); |
| 67 | await using _screen = await render( |
| 68 | <Dialog defaultOpen> |
| 69 | <DialogPopup {...(reason === "caller initialFocus" ? { initialFocus: cancel } : {})}> |
| 70 | <DialogTitle>Install update?</DialogTitle> |
| 71 | <DialogClose ref={cancel} render={<Button variant="outline" />}> |
| 72 | Cancel |
| 73 | </DialogClose> |
| 74 | <DialogAction disabled={reason === "disabled action"}>Install</DialogAction> |
| 75 | </DialogPopup> |
| 76 | </Dialog>, |
| 77 | ); |
| 78 | |
| 79 | await expect.element(page.getByRole("button", { name: "Cancel", exact: true })).toHaveFocus(); |
| 80 | }, |
| 81 | ); |
| 82 | |
| 83 | it("focuses the popup instead of the action when opened by touch", async () => { |
| 84 | await using _screen = await render( |
| 85 | <Dialog> |
| 86 | <DialogTrigger>Open installer</DialogTrigger> |
| 87 | <DialogPopup> |
| 88 | <DialogTitle>Install relay client</DialogTitle> |
| 89 | <DialogClose>Cancel</DialogClose> |
| 90 | <DialogAction>Install</DialogAction> |
| 91 | </DialogPopup> |
| 92 | </Dialog>, |
| 93 | ); |
| 94 | |
| 95 | const trigger = page.getByRole("button", { name: "Open installer" }).element(); |
| 96 | await act(() => { |
| 97 | trigger.dispatchEvent( |
| 98 | new PointerEvent("pointerdown", { bubbles: true, pointerType: "touch" }), |
| 99 | ); |
| 100 | trigger.dispatchEvent(new PointerEvent("pointerup", { bubbles: true, pointerType: "touch" })); |
| 101 | trigger.dispatchEvent( |
| 102 | new PointerEvent("click", { bubbles: true, pointerType: "touch", detail: 1 }), |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | await expect.element(page.getByRole("dialog")).toHaveFocus(); |
| 107 | }); |
| 108 | }); |