diff --git a/src/features/vcs/CombinedDiff.module.css b/src/features/vcs/CombinedDiff.module.css index ebc9ae969..04250953f 100644 --- a/src/features/vcs/CombinedDiff.module.css +++ b/src/features/vcs/CombinedDiff.module.css @@ -71,6 +71,9 @@ display: flex; flex-direction: column; } +.body[hidden] { + display: none; +} /* Space a block takes before its diff loads, so the scrollbar is roughly right. */ .placeholder { height: 160px; diff --git a/src/features/vcs/CombinedDiff.tsx b/src/features/vcs/CombinedDiff.tsx index d68c8f910..436a0d77e 100644 --- a/src/features/vcs/CombinedDiff.tsx +++ b/src/features/vcs/CombinedDiff.tsx @@ -98,14 +98,15 @@ function Block({ /> )} - {!collapsed && - (near ? ( -
- -
- ) : ( -
- ))} + {/* A collapsed block keeps its loaded diff, hidden: unmounting it would release the + working-tree document and, with autosave off, drop its unsaved edits. */} + {near ? ( + + ) : ( + !collapsed &&
+ )} ); } diff --git a/src/features/vcs/__tests__/combinedDiff.test.tsx b/src/features/vcs/__tests__/combinedDiff.test.tsx index 5eaf8874c..f34cd002d 100644 --- a/src/features/vcs/__tests__/combinedDiff.test.tsx +++ b/src/features/vcs/__tests__/combinedDiff.test.tsx @@ -83,20 +83,24 @@ describe("the all-in-one diff tab", () => { expect(screen.getAllByText("src")).toHaveLength(2); }); - it("collapses a file and all files", () => { + it("collapses a file and all files, keeping their diffs (and unsaved edits) loaded", () => { render(); + const shown = () => screen.queryAllByTestId("block-diff").filter((b) => !b.closest("[hidden]")); + const first = screen.getAllByTestId("block-diff")[0]!; act(() => { fireEvent.click(screen.getAllByRole("button", { name: "Collapse File" })[0]!); }); - expect(screen.getAllByTestId("block-diff")).toHaveLength(2); + expect(shown()).toHaveLength(2); act(() => { fireEvent.click(screen.getByRole("button", { name: "Collapse All Files" })); }); - expect(screen.queryAllByTestId("block-diff")).toHaveLength(0); + expect(shown()).toHaveLength(0); act(() => { fireEvent.click(screen.getByRole("button", { name: "Expand All Files" })); }); - expect(screen.getAllByTestId("block-diff")).toHaveLength(3); + expect(shown()).toHaveLength(3); + // The same diff all along: collapsing never released its document. + expect(screen.getAllByTestId("block-diff")[0]).toBe(first); }); it("switches every block between side-by-side and unified", () => {