test(search): cover single-escape command palette close

This commit is contained in:
Jiayuan Zhang
2026-04-10 20:47:06 +08:00
parent e3c1755baa
commit a80fc44820
2 changed files with 60 additions and 0 deletions

View File

@@ -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(<SearchCommand />);
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();
});
});

View File

@@ -82,6 +82,7 @@ export function SearchCommand() {
if (!open) return;
const handleEsc = (e: KeyboardEvent) => {
if (e.key === "Escape") {
e.preventDefault();
e.stopPropagation();
setOpen(false);
}