From a80fc44820de5e62cbf9ef43010afa7ac8a4fd37 Mon Sep 17 00:00:00 2001 From: Jiayuan Zhang Date: Fri, 10 Apr 2026 20:47:06 +0800 Subject: [PATCH] test(search): cover single-escape command palette close --- packages/views/search/search-command.test.tsx | 59 +++++++++++++++++++ packages/views/search/search-command.tsx | 1 + 2 files changed, 60 insertions(+) create mode 100644 packages/views/search/search-command.test.tsx diff --git a/packages/views/search/search-command.test.tsx b/packages/views/search/search-command.test.tsx new file mode 100644 index 000000000..5c6f39aab --- /dev/null +++ b/packages/views/search/search-command.test.tsx @@ -0,0 +1,59 @@ +import { act } from "react"; +import { render, screen, waitFor } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { SearchCommand } from "./search-command"; +import { useSearchStore } from "./search-store"; + +const { mockPush, mockSearchIssues } = vi.hoisted(() => ({ + mockPush: vi.fn(), + mockSearchIssues: vi.fn(), +})); + +vi.mock("@multica/core/api", () => ({ + api: { + searchIssues: mockSearchIssues, + }, +})); + +vi.mock("@multica/core/issues/stores", () => ({ + useRecentIssuesStore: (selector?: (state: { items: [] }) => unknown) => { + const state = { items: [] as [] }; + return selector ? selector(state) : state; + }, +})); + +vi.mock("../navigation", () => ({ + useNavigation: () => ({ + push: mockPush, + }), +})); + +describe("SearchCommand", () => { + beforeEach(() => { + mockPush.mockReset(); + mockSearchIssues.mockReset().mockResolvedValue({ issues: [] }); + + act(() => { + useSearchStore.setState({ open: true }); + }); + }); + + it("closes on a single Escape press from the search input", async () => { + const user = userEvent.setup(); + + render(); + + const input = screen.getByPlaceholderText("Type a command or search..."); + await user.click(input); + + expect(useSearchStore.getState().open).toBe(true); + + await user.keyboard("{Escape}"); + + await waitFor(() => { + expect(useSearchStore.getState().open).toBe(false); + }); + expect(screen.queryByPlaceholderText("Type a command or search...")).not.toBeInTheDocument(); + }); +}); diff --git a/packages/views/search/search-command.tsx b/packages/views/search/search-command.tsx index ac4c3d7eb..54729c460 100644 --- a/packages/views/search/search-command.tsx +++ b/packages/views/search/search-command.tsx @@ -82,6 +82,7 @@ export function SearchCommand() { if (!open) return; const handleEsc = (e: KeyboardEvent) => { if (e.key === "Escape") { + e.preventDefault(); e.stopPropagation(); setOpen(false); }