|
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | +import { page, userEvent, type Locator } from "vite-plus/test/browser"; |
| 3 | + |
| 4 | +import { render } from "../../../test/render"; |
| 5 | + |
| 6 | +vi.mock(import("../../state/session"), async (original) => ({ |
| 7 | + ...(await original()), |
| 8 | + useEnvironmentScope: () => true, |
| 9 | + useEnvironmentsWithScope: ((environments: ReadonlyArray<{ environmentId: string }>) => |
| 10 | + new Set(environments.map(({ environmentId }) => environmentId))) as never, |
| 11 | + readEnvironmentScope: () => true, |
| 12 | +})); |
| 13 | +vi.mock(import("../../state/use-atom-command"), async (original) => ({ |
| 14 | + ...(await original()), |
| 15 | + useAtomCommand: (() => vi.fn()) as never, |
| 16 | +})); |
| 17 | +vi.mock(import("./SettingsScopeContext"), async (original) => ({ |
| 18 | + ...(await original()), |
| 19 | + useSettingsScope: (() => { |
| 20 | + const environment = { environmentId: "env", serverConfig: { keybindings: [] } }; |
| 21 | + return { environment, connectedEnvironments: [environment] }; |
| 22 | + }) as never, |
| 23 | +})); |
| 24 | + |
| 25 | +import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog"; |
| 26 | +import { KeybindingsSettingsPanel } from "./KeybindingsSettings"; |
| 27 | + |
| 28 | +// The modal dialog's inert layer swallows pointer events in this harness, so drive the DOM. |
| 29 | +async function activate(locator: Locator, action: "click" | "focus") { |
| 30 | + await expect.element(locator).toBeVisible(); |
| 31 | + (locator.element() as HTMLElement)[action](); |
| 32 | +} |
| 33 | + |
| 34 | +const existingField = page.getByRole("textbox", { name: "Keybinding for Sidebar: Toggle" }); |
| 35 | +const existingPill = page.getByRole("button", { name: /^Edit shortcut for Sidebar: Toggle:/ }); |
| 36 | +const newField = page.getByRole("textbox", { name: "Keybinding for new keybinding" }); |
| 37 | + |
| 38 | +describe("Esc in a shortcut recorder", () => { |
| 39 | + it.each<{ |
| 40 | + name: string; |
| 41 | + field: Locator; |
| 42 | + open: () => Promise<void>; |
| 43 | + expectReset: () => Promise<void>; |
| 44 | + }>([ |
| 45 | + { |
| 46 | + name: "existing binding", |
| 47 | + field: existingField, |
| 48 | + open: () => activate(existingPill, "click"), |
| 49 | + expectReset: async () => { |
| 50 | + await expect.element(existingField).not.toBeInTheDocument(); |
| 51 | + await expect.element(existingPill).toHaveFocus(); |
| 52 | + await expect.element(page.getByRole("button", { name: "Save" })).not.toBeInTheDocument(); |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "new binding", |
| 57 | + field: newField, |
| 58 | + open: async () => { |
| 59 | + await activate(page.getByRole("button", { name: "Add keybinding" }), "click"); |
| 60 | + await activate(newField, "focus"); |
| 61 | + }, |
| 62 | + expectReset: async () => { |
| 63 | + await expect.element(newField).toHaveValue(""); |
| 64 | + await expect.element(newField).toHaveAttribute("placeholder", "Unassigned"); |
| 65 | + }, |
| 66 | + }, |
| 67 | + ])("leaves the $name field without closing Settings", async ({ field, open, expectReset }) => { |
| 68 | + const onOpenChange = vi.fn(); |
| 69 | + await using _screen = await render( |
| 70 | + <Dialog open onOpenChange={onOpenChange}> |
| 71 | + <DialogPopup> |
| 72 | + <DialogTitle>Settings</DialogTitle> |
| 73 | + <KeybindingsSettingsPanel /> |
| 74 | + </DialogPopup> |
| 75 | + </Dialog>, |
| 76 | + ); |
| 77 | + |
| 78 | + await open(); |
| 79 | + await userEvent.keyboard("{Control>}k{/Control}"); |
| 80 | + await expect.element(field).not.toHaveValue(""); |
| 81 | + await userEvent.tab(); |
| 82 | + await activate(field, "focus"); |
| 83 | + await expect.element(field).toHaveAttribute("placeholder", "Press shortcut"); |
| 84 | + |
| 85 | + await userEvent.keyboard("{Escape}"); |
| 86 | + |
| 87 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 88 | + expect(document.activeElement?.matches("[data-keybinding-capture]")).toBe(false); |
| 89 | + await expectReset(); |
| 90 | + |
| 91 | + await userEvent.keyboard("{Escape}"); |
| 92 | + expect(onOpenChange).toHaveBeenCalledWith(false, expect.anything()); |
| 93 | + }); |
| 94 | +}); |
0 commit comments