import { act, fireEvent, render, screen } from "@testing-library/react"; import { beforeEach, describe, expect, it, vi } from "vitest"; import type { EditorInput } from "@/workbench/contributions"; const editor = vi.hoisted(() => ({ input: null as EditorInput | null, follow: null as ((input: EditorInput | null) => void) | null, })); beforeEach(() => { editor.input = null; editor.follow = null; }); vi.mock("@/workbench/contributions", async (original) => ({ ...(await original()), maybeEditorService: () => ({ activeInput: () => editor.input, onActiveInputChange: (follow: typeof editor.follow) => { editor.follow = follow; return () => { editor.follow = null; }; }, }), })); vi.mock("@/platform/ipc", async (importOriginal) => { const real = await importOriginal(); return { ...real, call: vi.fn() }; }); const preview = vi.hoisted(() => ({ props: null as null | { input: { data: DiffDataT }; onNeighbour?: (next: DiffDataT) => void }, })); type DiffDataT = import("../diffInput").DiffData; vi.mock("../DiffEditor", () => ({ DiffEditor: (props: { input: { data: DiffDataT }; onNeighbour?: (next: DiffDataT) => void }) => { preview.props = props; return
; }, })); const openDiff = vi.hoisted(() => vi.fn()); vi.mock("../diffInput", async (original) => ({ ...(await original()), openDiff, })); import { CommitToolWindow, LocalChangesView } from "../CommitView"; import { setCommitDiffPreview, useCommit } from "../commitStore"; import { chainNeighbour, diffInput, workingDiffData } from "../diffInput"; import { useVcs } from "../store"; /** The splitter between the changes and the preview: the only vertical one in the layout. */ const previewHandle = () => screen.queryAllByRole("separator").find((s) => s.getAttribute("aria-orientation") === "vertical"); const modified = (path: string) => ({ path, relPath: path.slice(3), staged: null, unstaged: "modified" as const, renamedFrom: null, }); /** Selects the tree's first file the way a user does, so the tree's own state moves too. */ function selectFirstFile() { const tree = screen.getByRole("tree", { name: "Changes" }); tree.focus(); fireEvent.keyDown(tree, { key: "Home" }); fireEvent.keyDown(tree, { key: "ArrowDown" }); } describe("Commit tool window Preview Diff (#1599)", () => { beforeEach(() => { localStorage.clear(); openDiff.mockClear(); useCommit.setState({ commitDiffPreview: false, selection: [] }); useVcs.setState({ available: true, root: "/p", repo: "/p", roots: ["/p"], status: [] }); }); it("offers a Preview Diff toggle in the changes toolbar, off by default", () => { render(); const toggle = screen.getByRole("button", { name: "Preview Diff" }); expect(toggle).toHaveAttribute("aria-pressed", "false"); expect(screen.queryByTestId("diff-preview")).toBeNull(); }); it("shows an embedded diff pane beside the changes when enabled", () => { useVcs.setState({ status: [modified("/p/a.ts")] }); render(); selectFirstFile(); act(() => setCommitDiffPreview(true)); // Side by side with the changes: the splitter between the panels is vertical. expect(previewHandle()).toBeTruthy(); expect(screen.getByTestId("diff-preview")).toHaveAttribute("data-path", "/p/a.ts"); expect(screen.getByPlaceholderText("Commit Message")).toBeTruthy(); }); it("previews the selected file and follows the selection", () => { useVcs.setState({ status: ["a.ts", "b.ts"].map((name) => modified(`/p/${name}`)) }); render(); act(() => setCommitDiffPreview(true)); expect(screen.getByText("Select a file to preview its changes")).toBeTruthy(); selectFirstFile(); expect(screen.getByTestId("diff-preview")).toHaveAttribute("data-path", "/p/a.ts"); const tree = screen.getByRole("tree", { name: "Changes" }); fireEvent.keyDown(tree, { key: "ArrowDown" }); expect(screen.getByTestId("diff-preview")).toHaveAttribute("data-path", "/p/b.ts"); fireEvent.keyDown(tree, { key: "ArrowUp" }); expect(screen.getByTestId("diff-preview")).toHaveAttribute("data-path", "/p/a.ts"); }); it("keeps the files list and the commit message accessible while the preview is open", () => { useVcs.setState({ status: [modified("/p/a.ts")] }); render(); selectFirstFile(); act(() => setCommitDiffPreview(true)); expect(screen.getByTestId("diff-preview")).toBeTruthy(); expect(screen.getByRole("tree", { name: "Changes" })).toBeTruthy(); expect(screen.getByPlaceholderText("Commit Message")).toBeTruthy(); expect(screen.getByRole("button", { name: "Commit" })).toBeTruthy(); }); it("offers a resize handle between the changes and the preview", () => { useVcs.setState({ status: [modified("/p/a.ts")] }); render(); selectFirstFile(); act(() => setCommitDiffPreview(true)); const handle = previewHandle(); expect(handle).toBeTruthy(); expect(handle!.getAttribute("aria-valuenow")).not.toBeNull(); // Resizing moves the boundary and keeps both sides rendered. fireEvent.keyDown(handle!, { key: "ArrowLeft" }); expect(screen.getByTestId("diff-preview")).toBeTruthy(); expect(screen.getByRole("tree", { name: "Changes" })).toBeTruthy(); }); it("hides the pane when disabled and persists the toggle state", () => { useVcs.setState({ status: [modified("/p/a.ts")] }); render(); selectFirstFile(); const toggle = screen.getByRole("button", { name: "Preview Diff" }); act(() => toggle.click()); expect(useCommit.getState().commitDiffPreview).toBe(true); expect(localStorage.getItem("openidea.vcs.commitDiffPreview")).toBe("true"); expect(screen.getByTestId("diff-preview")).toHaveAttribute("data-path", "/p/a.ts"); act(() => toggle.click()); expect(useCommit.getState().commitDiffPreview).toBe(false); expect(localStorage.getItem("openidea.vcs.commitDiffPreview")).toBe("false"); expect(screen.queryByTestId("diff-preview")).toBeNull(); expect(previewHandle()).toBeUndefined(); // The tree keeps its selection across the toggling. expect(screen.getByText("a.ts").closest('[role="treeitem"]')).toHaveAttribute("aria-selected", "true"); }); it("shows an empty state until exactly one file is selected", () => { useVcs.setState({ status: ["a.ts", "b.ts"].map((name) => modified(`/p/${name}`)) }); render(); act(() => setCommitDiffPreview(true)); expect(screen.getByText("Select a file to preview its changes")).toBeTruthy(); act(() => useCommit.setState({ selection: ["/p/a.ts", "/p/b.ts"] })); expect(screen.getByText("Select a file to preview its changes")).toBeTruthy(); }); it("shows an empty state when there are no changes to preview", () => { render(); act(() => setCommitDiffPreview(true)); expect(screen.getByText("No changes to preview")).toBeTruthy(); expect(screen.queryByTestId("diff-preview")).toBeNull(); }); }); it("follows the active working diff without following other revisions or taking focus", () => { useCommit.setState({ commitDiffPreview: false, selection: [] }); useVcs.setState({ available: true, root: "/p", repo: "/p", roots: ["/p"], status: [modified("/p/a.ts"), modified("/p/b.ts")], }); render(); const message = screen.getByPlaceholderText("Commit Message"); message.focus(); act(() => editor.follow?.(diffInput(workingDiffData("/p/b.ts")))); expect(useCommit.getState().selection).toEqual(["/p/b.ts"]); expect(screen.getByRole("treeitem", { name: /b.ts/ })).toHaveAttribute("aria-selected", "true"); expect(document.activeElement).toBe(message); act(() => editor.follow?.( diffInput({ path: "/p/a.ts", left: { type: "rev", rev: "old" }, right: { type: "rev", rev: "new" } }), ), ); expect(useCommit.getState().selection).toEqual(["/p/b.ts"]); }); describe("following the active diff across Git status refreshes", () => { beforeEach(() => { editor.input = diffInput(workingDiffData("/p/b.ts")); useCommit.setState({ commitDiffPreview: false, selection: [], groupByDir: false, groupByStaging: false }); useVcs.setState({ available: true, root: "/p", repo: "/p", roots: ["/p"], status: [modified("/p/a.ts"), modified("/p/b.ts")], }); }); it("keeps a manually selected file when status refreshes with the same active diff", () => { render(); expect(useCommit.getState().selection).toEqual(["/p/b.ts"]); selectFirstFile(); expect(useCommit.getState().selection).toEqual(["/p/a.ts"]); act(() => useVcs.setState({ status: [modified("/p/a.ts"), modified("/p/b.ts")] })); expect(useCommit.getState().selection).toEqual(["/p/a.ts"]); act(() => editor.follow?.(editor.input)); expect(useCommit.getState().selection).toEqual(["/p/a.ts"]); }); it("keeps a manually collapsed group across status refreshes", () => { render(); const tree = screen.getByRole("tree", { name: "Changes" }); tree.focus(); fireEvent.keyDown(tree, { key: "Home" }); fireEvent.keyDown(tree, { key: "ArrowLeft" }); expect(tree.querySelector('[role="treeitem"][aria-expanded="false"]')).toBeTruthy(); expect(screen.queryByRole("treeitem", { name: /b.ts/ })).toBeNull(); act(() => useVcs.setState({ status: [modified("/p/a.ts"), modified("/p/b.ts")] })); expect(screen.queryByRole("treeitem", { name: /b.ts/ })).toBeNull(); }); it.each([ null, diffInput({ path: "/p/b.ts", left: { type: "rev", rev: "old" }, right: { type: "rev", rev: "new" } }), ])("follows the same diff again after switching away from it (%#)", (other) => { render(); selectFirstFile(); act(() => editor.follow?.(other)); act(() => editor.follow?.(editor.input)); expect(useCommit.getState().selection).toEqual(["/p/b.ts"]); }); }); it("follows a newly opened diff when its Git status arrives later", () => { editor.input = diffInput(workingDiffData("/p/new.ts")); useCommit.setState({ commitDiffPreview: false, selection: [] }); useVcs.setState({ available: true, root: "/p", repo: "/p", roots: ["/p"], status: [] }); render(); act(() => useVcs.setState({ status: [modified("/p/new.ts")] })); expect(useCommit.getState().selection).toEqual(["/p/new.ts"]); }); describe("the active diff follows the selected file (#2492)", () => { beforeEach(() => { openDiff.mockClear(); useCommit.setState({ commitDiffPreview: false, selection: [], groupByDir: false, groupByStaging: false, }); useVcs.setState({ available: true, root: "/p", repo: "/p", roots: ["/p"], status: [modified("/p/a.ts"), modified("/p/b.ts")], }); }); it("shows the newly selected file in the open diff tab, keeping focus in the tree", () => { editor.input = diffInput(workingDiffData("/p/b.ts")); render(); selectFirstFile(); expect(openDiff).toHaveBeenCalledTimes(1); const [data, opts] = openDiff.mock.calls[0]!; expect(data.path).toBe("/p/a.ts"); expect(data.chain?.map((d: { path: string }) => d.path)).toEqual(["/p/a.ts", "/p/b.ts"]); expect(opts).toEqual({ preview: true, focus: false }); }); it.each([ ["no tab is open", null], ["the active tab is a file", { kind: "text", key: "/p/b.ts", path: "/p/b.ts" } as EditorInput], [ "the active diff compares other revisions", diffInput({ path: "/p/b.ts", left: { type: "rev", rev: "old" }, right: { type: "rev", rev: "new" }, }), ], ])("opens nothing on selection when %s", (_case, input) => { editor.input = input; render(); selectFirstFile(); expect(openDiff).not.toHaveBeenCalled(); }); it("opens nothing when the selection already is the diff's file", () => { editor.input = diffInput(workingDiffData("/p/a.ts")); render(); selectFirstFile(); expect(openDiff).not.toHaveBeenCalled(); }); }); describe("moving past the preview's first or last difference (#7149)", () => { beforeEach(() => { localStorage.clear(); openDiff.mockClear(); preview.props = null; useCommit.setState({ commitDiffPreview: true, diffPreview: true, selection: [], groupByDir: false }); useVcs.setState({ available: true, root: "/p", repo: "/p", roots: ["/p"], status: ["a.ts", "b.ts", "c.ts"].map((name) => modified(`/p/${name}`)), }); }); /** F7 past the last difference: what DiffEditor does with the chain it was given. */ const move = (dir: 1 | -1) => { const { input, onNeighbour } = preview.props!; const next = chainNeighbour(input.data, dir); expect(next).not.toBeNull(); expect(onNeighbour).toBeDefined(); act(() => onNeighbour!(next!)); }; it.each([ ["Commit tool window", () => ], ["Local Changes", () => ], ])("%s preview goes on to the tree's next and previous files and selects them", (_name, view) => { render(view()); selectFirstFile(); expect(preview.props!.input.data.chain?.map((d) => d.path)).toEqual(["/p/a.ts", "/p/b.ts", "/p/c.ts"]); move(1); expect(screen.getByTestId("diff-preview")).toHaveAttribute("data-path", "/p/b.ts"); expect(preview.props!.input.data.reveal).toBe("first"); expect(useCommit.getState().selection).toEqual(["/p/b.ts"]); expect(screen.getByRole("treeitem", { name: /b.ts/ })).toHaveAttribute("aria-selected", "true"); move(-1); expect(screen.getByTestId("diff-preview")).toHaveAttribute("data-path", "/p/a.ts"); expect(preview.props!.input.data.reveal).toBe("last"); expect(useCommit.getState().selection).toEqual(["/p/a.ts"]); // The preview swaps its own content instead of opening an editor tab. expect(openDiff).not.toHaveBeenCalled(); }); it("has no next file when the tree has only one", () => { useVcs.setState({ status: [modified("/p/a.ts")] }); render(); selectFirstFile(); expect(preview.props!.input.data.chain).toBeUndefined(); }); });