Файли
openidea/src/features/todo/TodoView.test.tsx
T
Qingтаqtvbwfn 2f2d3a25ae feat(todo): edit the file right in the TODO preview (#1129)
The TODO preview is an editor on the file, as in IntelliJ: a CodeMirror view attached to the real document, so edits share the buffer with editor tabs and Save All saves them; no editing switch. The caret lands on the selected item, also when another item of the same file is selected. Read-only files fall back to the read-only preview. Dirty buffers are held rather than released (releasing would auto-save or drop the edits), and previewText reads the held text.

Co-authored-by: qtvbwfn <[email protected]>
2026-11-04 08:58:53 +08:00

92 рядки
3.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { act, fireEvent, render, screen } from "@testing-library/react";
import { TodoView, useTodoView, useTodoViewOptions } from "./TodoView";
import { useTodos } from "./store";
import { setLocalChanges } from "@/workbench/contributions";
describe("TodoView", () => {
it("renders grouped items with the keyword emphasised and filters by keyword", () => {
act(() =>
useTodos.setState({
scannedOnce: true,
scanned: new Map([
[
"/p/src/a.ts",
[
{ path: "/p/src/a.ts", line: 4, col: 3, keyword: "TODO", text: "TODO: wire it" },
{ path: "/p/src/a.ts", line: 9, col: 3, keyword: "FIXME", text: "FIXME broken" },
],
],
]),
}),
);
render(<TodoView />);
expect(screen.getByText("a.ts")).toBeInTheDocument();
expect(screen.getByText("2 items")).toBeInTheDocument();
expect(screen.getByText("(5, 4)")).toBeInTheDocument();
expect(screen.getByText(": wire it")).toBeInTheDocument();
act(() => useTodoView.getState().toggleKeyword("FIXME"));
expect(screen.getByText("1 item")).toBeInTheDocument();
act(() => useTodoView.getState().showAll());
});
it("lists only changed files on the Changes tab, by change list", () => {
let clear = () => {};
act(() => {
clear = setLocalChanges("vcs", [
{ id: "default", name: "Changes", active: true, files: [{ path: "/p/src/b.ts" }] },
{ id: "ui", name: "UI work", active: false, files: [{ path: "/p/src/a.ts" }] },
]);
useTodos.setState({
scannedOnce: true,
scanned: new Map([
["/p/src/a.ts", [{ path: "/p/src/a.ts", line: 0, col: 3, keyword: "TODO", text: "TODO a" }]],
["/p/src/b.ts", [{ path: "/p/src/b.ts", line: 0, col: 3, keyword: "TODO", text: "TODO b" }]],
]),
});
useTodoView.getState().setTab("changes");
});
render(<TodoView />);
expect(screen.getByText("b.ts")).toBeInTheDocument();
expect(screen.queryByText("a.ts")).toBeNull();
fireEvent.click(screen.getByLabelText("Changelist"));
fireEvent.click(screen.getByRole("option", { name: /UI work/ }));
expect(useTodoView.getState().changelist).toBe("ui");
expect(screen.getByText("a.ts")).toBeInTheDocument();
expect(screen.queryByText("b.ts")).toBeNull();
act(() => {
useTodoView.setState({ tab: "project", changelist: null });
clear();
});
});
it("shows the preview pane and does not autoscroll by default, as IDEA", () => {
expect(useTodoViewOptions.getState()).toEqual({ preview: true, autoscroll: false });
render(<TodoView />);
expect(screen.getByText("Select an item to preview")).toBeInTheDocument();
act(() => useTodoViewOptions.setState({ preview: false }));
expect(screen.queryByText("Select an item to preview")).toBeNull();
act(() => useTodoViewOptions.setState({ preview: true }));
});
it("previews the selected item as an editable editor, without a switch", async () => {
act(() =>
useTodos.setState({
scannedOnce: true,
scanned: new Map([
["/p/src/a.ts", [{ path: "/p/src/a.ts", line: 4, col: 3, keyword: "TODO", text: "TODO: x" }]],
]),
}),
);
render(<TodoView />);
// Select the file group so the preview has an item (trees select on mousedown).
fireEvent.mouseDown(screen.getByText("a.ts").closest("[role=treeitem]")!);
await screen.findByText("(5, 4)");
// No editing switch: the preview is simply an editor, like IntelliJ's.
expect(screen.queryByLabelText("Allow editing")).toBeNull();
// No document attacher in tests: the editable pane degrades to its error
// state ("editor not started") instead of crashing.
await screen.findByText("editor not started");
});
});