Файли
openidea/src/ui/Select.test.tsx
T
Bert Verhelstтаbertyhell d84d8829c4 fix(vcs): branch pickers in the Git dialogs filter as you type (#3816)
Merge, Rebase (upstream and --onto), Pull's remote branch and New Worktree's checkout used plain non-searchable Selects, so repositories with hundreds of branches could only be scrolled. They now share a BranchSelect that is always searchable, and a searchable Select opens its list with the typed key as the query when a key is pressed on the closed trigger. The agents' base-branch picker is searchable too.

Closes #3788

Co-authored-by: bertyhell <[email protected]>
2026-11-17 01:58:49 +08:00

218 рядки
8.4 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen, within, waitFor } from "@testing-library/react";
import { StrictMode, useState } from "react";
import { Select, SelectListbox, type SelectOption } from "./Select";
import { useI18n } from "@/platform/i18n";
import { Dialog } from "./Dialog";
const options: SelectOption[] = [
{ value: "9router", label: "9router", section: true },
{ value: "9router/cmc/Beam", label: "Beam", detail: "cmc/Beam · Fast", keywords: "9router" },
{ value: "9router/blocked", label: "Blocked", disabled: true, keywords: "9router" },
{ value: "other", label: "Other", section: true },
{ value: "other/BlueMoon", label: "Blue Moon", keywords: "other" },
{ value: "__section__:real", label: "Plain" },
];
function setup(searchable = true, rows = options) {
const onChange = vi.fn();
render(<Select aria-label="Model" options={rows} value={null} searchable={searchable} onChange={onChange} />);
fireEvent.click(screen.getByRole("button", { name: "Model" }));
return onChange;
}
const list = () => screen.getByRole("listbox");
const labels = () =>
within(list())
.queryAllByRole("option")
.map((o) => o.textContent);
const query = (value: string) => fireEvent.change(screen.getByRole("textbox"), { target: { value } });
const key = (key: string, target = list()) => fireEvent.keyDown(target, { key });
describe("Select", () => {
it("focuses the search input when opened", async () => {
setup();
await waitFor(() => expect(screen.getByRole("textbox")).toHaveFocus());
});
it("never selects sections by click, Enter or type-ahead, and preserves full model values", () => {
const changed = setup(false);
fireEvent.click(within(list()).getByText("9router"));
expect(changed).not.toHaveBeenCalled();
key("9");
key("Enter");
expect(changed).toHaveBeenCalledExactlyOnceWith("9router/cmc/Beam");
});
it("does not select a list containing only headings", () => {
const changed = setup(false, [{ value: "a", label: "A", section: true }]);
key("a");
key("Enter");
expect(changed).not.toHaveBeenCalled();
});
it("skips sections and disabled options with arrows, Home and End", () => {
const changed = setup(false);
key("ArrowDown");
expect(within(list()).getByRole("option", { name: "Blue Moon" })).toHaveAttribute("aria-selected", "true");
key("End");
key("Home");
key("ArrowUp");
key("Enter");
expect(changed).toHaveBeenCalledExactlyOnceWith("__section__:real");
});
it("renders disabled models as options, retains them in search, and rejects selection", () => {
const changed = setup();
query("blocked");
const blocked = within(list()).getByRole("option", { name: "Blocked" });
expect(within(list()).getByText("9router")).toBeInTheDocument();
fireEvent.click(blocked);
key("Enter");
expect(changed).not.toHaveBeenCalled();
});
it("matches provider, model, nested path, description, and multiple case-insensitive tokens", () => {
setup();
for (const search of ["9router Beam", "CMC fast", "9ROUTER bm"]) {
query(search);
expect(labels()).toEqual(["Beamcmc/Beam · Fast"]);
expect(within(list()).getByText("9router")).toBeInTheDocument();
expect(within(list()).queryByText("Other")).not.toBeInTheDocument();
}
});
it("navigates filtered results and lets search input handle spaces and caret movement", () => {
const changed = setup();
query("other");
const input = screen.getByRole("textbox");
expect(fireEvent.keyDown(input, { key: " " })).toBe(true);
expect(fireEvent.keyDown(input, { key: "Home" })).toBe(true);
key("ArrowDown", input);
key("Enter", input);
expect(changed).toHaveBeenCalledExactlyOnceWith("other/BlueMoon");
});
it("keeps exact substrings alongside fuzzy matches in original order", () => {
setup(true, [
{ value: "exact", label: "Beam" },
{ value: "fuzzy", label: "Beta amber" },
]);
query("beam");
expect(labels()).toEqual(["Beam", "Beta amber"]);
});
it("keeps non-searchable type-ahead prefix-only and shows every option", () => {
const changed = setup(false);
expect(screen.queryByRole("textbox")).not.toBeInTheDocument();
key("b");
key("m");
key("Enter");
expect(changed).toHaveBeenCalledExactlyOnceWith("other/BlueMoon");
});
it("opens a searchable list from a key typed on the closed trigger, with that key as the query", async () => {
const onChange = vi.fn();
render(
<Select
aria-label="Branch"
options={[
{ value: "main", label: "main" },
{ value: "develop", label: "develop" },
{ value: "feature/dev-tools", label: "feature/dev-tools" },
]}
value="main"
searchable
onChange={onChange}
/>,
);
const trigger = screen.getByRole("button", { name: "Branch" });
fireEvent.keyDown(trigger, { key: "d", ctrlKey: true });
fireEvent.keyDown(trigger, { key: "Shift" });
expect(screen.queryByRole("listbox")).toBeNull();
fireEvent.keyDown(trigger, { key: "d" });
const search = screen.getByRole("textbox");
expect(search).toHaveValue("d");
fireEvent.change(search, { target: { value: "dev" } });
expect(labels()).toEqual(["develop", "feature/dev-tools"]);
key("Enter", search);
expect(onChange).toHaveBeenCalledExactlyOnceWith("develop");
expect(screen.queryByRole("listbox")).toBeNull();
});
it("leaves typed keys to type-ahead when the list is not searchable", () => {
render(<Select aria-label="Model" options={options} value={null} onChange={() => {}} />);
fireEvent.keyDown(screen.getByRole("button", { name: "Model" }), { key: "b" });
expect(screen.queryByRole("listbox")).toBeNull();
});
it("does not confuse a heading value with a real option value", () => {
const changed = setup(false, [
{ value: "a", label: "Provider", section: true },
{ value: "a", label: "Real" },
]);
key("Enter");
expect(changed).toHaveBeenCalledExactlyOnceWith("a");
});
});
describe("Select preview lifecycle", () => {
const rows = [
{ value: "a", label: "A" },
{ value: "b", label: "B" },
];
it("restores using the newly selected value when closing", () => {
const preview = vi.fn();
function Picker() {
const [value, setValue] = useState("a");
return (
<Select
aria-label="Theme"
value={value}
options={rows}
onChange={setValue}
onPreview={(next) => preview(next, value)}
/>
);
}
render(<Picker />);
fireEvent.click(screen.getByRole("button", { name: "Theme" }));
expect(preview).toHaveBeenLastCalledWith("a", "a");
key("ArrowDown");
expect(preview).toHaveBeenLastCalledWith("b", "a");
key("Enter");
expect(preview).toHaveBeenLastCalledWith(null, "b");
});
it("keeps the initial preview active under Strict Mode and clears it on unmount", () => {
const preview = vi.fn();
const { unmount } = render(
<StrictMode>
<SelectListbox value="b" options={rows} onChoose={vi.fn()} onPreview={preview} />
</StrictMode>,
);
expect(preview).toHaveBeenLastCalledWith("b");
unmount();
expect(preview).toHaveBeenLastCalledWith(null);
});
it("closes only the list on Escape inside a dialog and restores trigger focus", () => {
const close = vi.fn();
render(
<Dialog open title="Settings" onClose={close}>
<Select aria-label="Theme" value="b" options={rows} onChange={vi.fn()} />
</Dialog>,
);
const trigger = screen.getByRole("button", { name: "Theme" });
trigger.focus();
fireEvent.click(trigger);
expect(within(list()).getByRole("option", { name: "B" })).toHaveAttribute("aria-selected", "true");
key("Escape");
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
expect(close).not.toHaveBeenCalled();
expect(trigger).toHaveFocus();
});
});
it("preserves user values when their labels match translated UI text", () => {
const previous = useI18n.getState().texts;
useI18n.setState({ texts: { Cancel: "取消" } });
try {
const changed = setup(false, [{ value: "Cancel", label: "Cancel", verbatim: true }]);
expect(within(list()).getByRole("option", { name: "Cancel" })).toBeInTheDocument();
key("c");
key("Enter");
expect(changed).toHaveBeenCalledWith("Cancel");
} finally {
useI18n.setState({ texts: previous });
}
});