|
1 | | -import { render, screen, waitFor, within } from "@testing-library/react"; |
| 1 | +import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; |
2 | 2 | import userEvent from "@testing-library/user-event"; |
3 | 3 | import { createMemoryRouter, MemoryRouter, RouterProvider } from "react-router"; |
4 | 4 | import { renderToString } from "react-dom/server"; |
5 | | -import { describe, expect, it } from "vite-plus/test"; |
| 5 | +import { describe, expect, it, vi } from "vite-plus/test"; |
6 | 6 | import { Footer, Header, Layout } from "~/components/Layout"; |
7 | 7 | import { CommandPalette } from "~/components/CommandPalette"; |
8 | 8 | import { AdminLayout } from "~/components/AdminLayout"; |
@@ -109,7 +109,87 @@ describe("公开导航收束", () => { |
109 | 109 | expect(await screen.findByRole("status")).toHaveTextContent("没有匹配的快捷命令"); |
110 | 110 | }); |
111 | 111 |
|
112 | | - it.each([375, 1280])( |
| 112 | + it.each([ |
| 113 | + ["绝对不存在的命令", "绝对不存在的命令"], |
| 114 | + ["node", "node"], |
| 115 | + [" node js ", "node js"], |
| 116 | + ])( |
| 117 | + "searches for %p with Enter instead of activating an implicit command", |
| 118 | + async (query, expected) => { |
| 119 | + const user = userEvent.setup(); |
| 120 | + const onOpenChange = vi.fn(); |
| 121 | + const { router } = renderWithRoot(<CommandPalette open onOpenChange={onOpenChange} />); |
| 122 | + const input = screen.getByRole("combobox", { name: "搜索命令" }); |
| 123 | + |
| 124 | + await user.type(input, query); |
| 125 | + await user.keyboard("{Enter}"); |
| 126 | + |
| 127 | + await waitFor(() => expect(router.state.location.pathname).toBe("/search")); |
| 128 | + expect(new URLSearchParams(router.state.location.search).get("q")).toBe(expected); |
| 129 | + expect(onOpenChange).toHaveBeenCalledWith(false); |
| 130 | + }, |
| 131 | + ); |
| 132 | + |
| 133 | + it("resets explicit command selection when the query changes", async () => { |
| 134 | + const user = userEvent.setup(); |
| 135 | + const { router } = renderWithRoot(<CommandPalette open onOpenChange={() => {}} />); |
| 136 | + const input = screen.getByRole("combobox", { name: "搜索命令" }); |
| 137 | + |
| 138 | + await user.keyboard("{ArrowDown}"); |
| 139 | + await user.type(input, "node"); |
| 140 | + await user.keyboard("{Enter}"); |
| 141 | + |
| 142 | + await waitFor(() => expect(router.state.location.pathname).toBe("/search")); |
| 143 | + expect(new URLSearchParams(router.state.location.search).get("q")).toBe("node"); |
| 144 | + }); |
| 145 | + |
| 146 | + it("keeps Enter available for search when arrow navigation has no matching command", async () => { |
| 147 | + const user = userEvent.setup(); |
| 148 | + const { router } = renderWithRoot(<CommandPalette open onOpenChange={() => {}} />); |
| 149 | + const input = screen.getByRole("combobox", { name: "搜索命令" }); |
| 150 | + |
| 151 | + await user.type(input, "绝对不存在的命令"); |
| 152 | + await user.keyboard("{ArrowDown}{Enter}"); |
| 153 | + |
| 154 | + await waitFor(() => expect(router.state.location.pathname).toBe("/search")); |
| 155 | + expect(new URLSearchParams(router.state.location.search).get("q")).toBe("绝对不存在的命令"); |
| 156 | + }); |
| 157 | + |
| 158 | + it.each(["", " "])("ignores an empty command search %p", async (query) => { |
| 159 | + const user = userEvent.setup(); |
| 160 | + const onOpenChange = vi.fn(); |
| 161 | + const { router } = renderWithRoot(<CommandPalette open onOpenChange={onOpenChange} />); |
| 162 | + const input = screen.getByRole("combobox", { name: "搜索命令" }); |
| 163 | + |
| 164 | + if (query) await user.type(input, query); |
| 165 | + await user.keyboard("{Enter}"); |
| 166 | + |
| 167 | + expect(router.state.location.pathname).toBe("/"); |
| 168 | + expect(router.state.location.search).toBe(""); |
| 169 | + expect(onOpenChange).not.toHaveBeenCalledWith(false); |
| 170 | + expect(input).toHaveValue(query); |
| 171 | + expect(input).toBeEnabled(); |
| 172 | + }); |
| 173 | + |
| 174 | + it("does not submit or activate a command while an IME composition is active", async () => { |
| 175 | + const user = userEvent.setup(); |
| 176 | + const onOpenChange = vi.fn(); |
| 177 | + const { router } = renderWithRoot(<CommandPalette open onOpenChange={onOpenChange} />); |
| 178 | + const input = screen.getByRole("combobox", { name: "搜索命令" }); |
| 179 | + await user.type(input, "节点"); |
| 180 | + |
| 181 | + fireEvent.keyDown(input, { key: "Enter", code: "Enter", isComposing: true }); |
| 182 | + |
| 183 | + expect(router.state.location.pathname).toBe("/"); |
| 184 | + expect(onOpenChange).not.toHaveBeenCalledWith(false); |
| 185 | + expect(input).toHaveValue("节点"); |
| 186 | + |
| 187 | + await user.keyboard("{Enter}"); |
| 188 | + await waitFor(() => expect(router.state.location.pathname).toBe("/search")); |
| 189 | + expect(new URLSearchParams(router.state.location.search).get("q")).toBe("节点"); |
| 190 | + }); |
| 191 | + |
| 192 | + it.each([375, 768, 1280, 1440])( |
113 | 193 | "allocates a non-overlapping search row and touch-safe close control at %ipx", |
114 | 194 | (width) => { |
115 | 195 | window.innerWidth = width; |
|
0 commit comments