);
}
// `FileContextMenuSurface` defers `onStartRename` to `onCloseAutoFocus` and calls
// `preventDefault()` there so Radix does not pull focus back to the trigger and
// steal it from the freshly mounted input. Both halves of that are only correct
// if the deferral is scoped to a *pending* rename: any other menu item, and any
// dismissal, must leave Radix's default focus restoration alone and must never
// enter edit mode. A pending flag that outlives its close is the failure mode.
// The anchor stands in for whatever held focus when the user opened the menu.
// Radix restores focus to it on close, so asserting against it distinguishes a
// real restoration from focus falling back to `document.body`.
function PendingRenameHarness({ onStartRename }: { onStartRename: () => void }) {
const [tree, setTree] = React.useState(FILE_NODE);
return (
<>
row
>
);
}
describe("FileContextMenu rename", () => {
it("does not start rename when a different menu item is selected", async () => {
const onStartRename = vi.fn();
render();
openMenu(RENAME_ROW);
fireEvent.click(screen.getByText("Download"));
await waitFor(() => expect(screen.queryByText("Download")).toBeNull());
expect(onStartRename).not.toHaveBeenCalled();
expect(screen.queryByRole("textbox")).toBeNull();
});
it("does not start rename when the menu is dismissed without a selection", async () => {
const onStartRename = vi.fn();
render();
openMenu(RENAME_ROW);
fireEvent.keyDown(document, { key: "Escape" });
await waitFor(() => expect(screen.queryByText("Rename")).toBeNull());
expect(onStartRename).not.toHaveBeenCalled();
});
it("does not replay a consumed rename on the next menu close", async () => {
const onStartRename = vi.fn();
render();
openMenu(RENAME_ROW);
fireEvent.click(screen.getByText("Rename"));
await waitFor(() => expect(onStartRename).toHaveBeenCalledTimes(1));
// Reopening and dismissing must not re-fire the rename the previous close
// already consumed.
openMenu(RENAME_ROW);
fireEvent.keyDown(document, { key: "Escape" });
await waitFor(() => expect(screen.queryByText("Rename")).toBeNull());
expect(onStartRename).toHaveBeenCalledTimes(1);
});
// The three tests above prove `onStartRename` stays unfired, which is only
// half of the contract. `preventDefault()` suppresses Radix's focus
// restoration, and hoisting it above the pending-rename guard would keep every
// assertion above green while silently dropping the user back on
// `document.body` after Download or Escape. These two pin the other half.
it("restores focus to the opener when a different menu item is selected", async () => {
render();
const anchor = screen.getByTestId(FOCUS_ANCHOR);
anchor.focus();
openMenu(RENAME_ROW);
fireEvent.click(screen.getByText("Download"));
await waitFor(() => expect(document.activeElement).toBe(anchor));
});
it("restores focus to the opener when the menu is dismissed without a selection", async () => {
render();
const anchor = screen.getByTestId(FOCUS_ANCHOR);
anchor.focus();
openMenu(RENAME_ROW);
fireEvent.keyDown(document, { key: "Escape" });
await waitFor(() => expect(document.activeElement).toBe(anchor));
});
it("starts rename after the menu closes and immediately focuses the selected filename", async () => {
vi.useFakeTimers();
render();
openMenu(RENAME_ROW);
fireEvent.click(screen.getByText("Rename"));
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
});
const input = screen.getByRole("textbox");
expect(document.activeElement).toBe(input);
expect((input as HTMLInputElement).selectionStart).toBe(0);
expect((input as HTMLInputElement).selectionEnd).toBe(FILE_NODE.name.length);
});
});
describe("FileContextMenu Download item", () => {
it("shows a Download item for a file when onDownloadFile is provided", () => {
const onDownloadFile = vi.fn().mockResolvedValue(true);
render(
{}}
onDownloadFile={onDownloadFile}
onStartRename={() => {}}
>
row
,
);
openMenu("file-row");
const item = screen.getByText("Download");
expect(item).toBeTruthy();
});
it("calls onDownloadFile with the node path when Download is selected", () => {
const onDownloadFile = vi.fn().mockResolvedValue(true);
render(
{}}
onDownloadFile={onDownloadFile}
onStartRename={() => {}}
>
row
,
);
openMenu("file-row");
fireEvent.click(screen.getByText("Download"));
expect(onDownloadFile).toHaveBeenCalledWith("README.md");
});
it("does not show a Download item for directories", () => {
const onDownloadFile = vi.fn().mockResolvedValue(true);
render(
{}}
onDeleteFile={vi.fn().mockResolvedValue(true)}
onDownloadFile={onDownloadFile}
onStartRename={() => {}}
>
row
,
);
openMenu("dir-row");
expect(screen.queryByText("Download")).toBeNull();
});
it("does not show a Download item when a bulk selection is active", () => {
const onDownloadFile = vi.fn().mockResolvedValue(true);
render(
{}}
onDeleteFile={vi.fn().mockResolvedValue(true)}
onDownloadFile={onDownloadFile}
onStartRename={() => {}}
selectedCount={3}
selectedPaths={new Set(["a", "b", "c"])}
>
row
,
);
openMenu("file-row");
expect(screen.queryByText("Download")).toBeNull();
});
});
describe("FileContextMenu chat context item", () => {
it.each([
["file", FILE_NODE],
["directory", DIR_NODE],
] as const)("shows and selects Add to chat context for a single %s", (_kind, node) => {
const onAddToChatContext = vi.fn();
render(
{}}
onAddToChatContext={onAddToChatContext}
onStartRename={() => {}}
>