Repository navigation
Expand file tree
/
Copy pathAnnotatableCodeView.browser.test.tsx
More file actions
163 lines (149 loc) · 5.41 KB
/
Copy pathAnnotatableCodeView.browser.test.tsx
File metadata and controls
163 lines (149 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import type { CodeViewDiffItem, FileDiffMetadata, SelectedLineRange } from "@pierre/diffs";
import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
import { StrictMode, type ComponentProps } from "react";
import { describe, expect, it, vi } from "vite-plus/test";
import { act, render } from "../../../test/render";
import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
import { buildFileDiffIdentityKey } from "~/lib/diffRendering";
type ViewerProps = Omit<ComponentProps<typeof StyledDiffCodeView<unknown>>, "options"> & {
options: {
enableLineSelection: boolean;
enableGutterUtility: boolean;
onGutterUtilityClick: (
range: SelectedLineRange,
context: { item: CodeViewDiffItem<unknown> },
) => void;
};
};
// Replace only the virtualized viewer; comment editing and submission use the real components.
vi.mock("./StyledDiffCodeView", () => ({
StyledDiffCodeView: (props: ViewerProps) =>
props.items
?.filter((item) => item.type === "diff")
.map((item) => (
<section key={item.id}>
<button
disabled={!props.options?.enableLineSelection || !props.options?.enableGutterUtility}
onClick={() => {
props.onSelectedLinesChange?.({ id: item.id, range });
props.options?.onGutterUtilityClick?.(range, { item });
}}
>
Comment on {item.fileDiff.name}
</button>
{props.selectedLines?.id === item.id ? (
<mark>Line {props.selectedLines.range.start}</mark>
) : null}
{item.annotations?.map((annotation) => (
<div key={`${annotation.side}:${annotation.lineNumber}`}>
{props.renderAnnotation?.(annotation, item.fileDiff, item)}
</div>
))}
</section>
)),
}));
import { AnnotatableCodeView } from "./AnnotatableCodeView";
import type { StyledDiffCodeView } from "./StyledDiffCodeView";
const target = DraftId.make("diff-draft-test");
const range: SelectedLineRange = { start: 1, end: 1, side: "additions" };
let container: HTMLDivElement;
function file(
name: string,
fileVersion = 1,
type: FileDiffMetadata["type"] = "change",
prevName?: string,
) {
const fileDiff = parsePatchFiles(
`diff --git a/${name} b/${name}\n--- a/${name}\n+++ b/${name}\n@@ -1 +1 @@\n-old\n+new\n`,
{ cacheKeyPrefix: name },
)[0]!.files[0]!;
fileDiff.type = type;
if (prevName) fileDiff.prevName = prevName;
return {
fileDiff,
filePath: name,
fileKey: buildFileDiffIdentityKey(fileDiff),
fileVersion,
collapsed: false,
};
}
function view(
files: ReturnType<typeof file>[],
codeViewKey = "initial",
pendingFiles: FileDiffMetadata[] = [],
) {
return (
<StrictMode>
<AnnotatableCodeView
codeViewKey={codeViewKey}
files={files}
pendingFiles={pendingFiles}
sectionId="unstaged"
sectionTitle="Uncommitted"
composerDraftTarget={target}
options={{}}
renderHeaderFilenameSuffix={() => null}
renderHeaderPrefix={() => null}
onRevealSearchMatch={() => {}}
/>
</StrictMode>
);
}
function click(label: string) {
const button = Array.from(container.querySelectorAll("button")).find(
(node) => node.textContent === label,
);
if (!button) throw new Error(`Missing button: ${label}`);
return act(() => button.click());
}
function editor() {
const textarea = container.querySelector("textarea");
if (!textarea) throw new Error("Comment editor is not available");
return textarea;
}
function typeComment(text: string) {
return act(() => {
Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!.call(
editor(),
text,
);
editor().dispatchEvent(new Event("input", { bubbles: true }));
});
}
function setup(): Disposable {
const reset = () => useComposerDraftStore.setState(useComposerDraftStore.getInitialState(), true);
reset();
container = document.createElement("div");
return { [Symbol.dispose]: reset };
}
describe("diff comment drafts", () => {
it("keeps the text and selected line through refresh and a provisional lazy patch", async () => {
using _drafts = setup();
const original = file("shared.ts", 1, "rename-pure", "old.ts");
await using screen = await render(view([original]), { container });
await click("Comment on shared.ts");
await typeComment("Keep this draft");
const refreshed = file("shared.ts", 2, "rename-pure", "old.ts");
await screen.rerender(view([refreshed], "refreshed"));
expect(editor().value).toBe("Keep this draft");
expect(container.querySelector("mark")?.textContent).toBe("Line 1");
await screen.rerender(
view([], "loading", [
{ ...refreshed.fileDiff, type: "rename-changed", hunks: [], cacheKey: "shared:pending" },
]),
);
expect(container.querySelector("textarea")).toBeNull();
await screen.rerender(view([refreshed], "loaded"));
expect(editor().value).toBe("Keep this draft");
expect(container.querySelector("mark")?.textContent).toBe("Line 1");
await click("Comment");
expect(useComposerDraftStore.getState().getComposerDraft(target)?.reviewComments).toEqual([
expect.objectContaining({
filePath: "shared.ts",
text: "Keep this draft",
selection: { ...range, endSide: "additions" },
}),
]);
expect(container.querySelector("p")?.textContent).toBe("Keep this draft");
});
});