Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestMarkdownEditor.browser.test.tsx

145 lines5.0 KB

1import { act, render, type Screen } from "../../../test/render";
2import { AuthSourceControlWriteScope, EnvironmentId, ProjectId } from "@t3tools/contracts";
3import { expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5​
6const permissions = vi.hoisted(() => ({
7 allowed: new Set<string>(),
8 listeners: new Set<() => void>(),
9}));
10​
11vi.mock(import("~/state/session"), async (original) => {
12 const { useSyncExternalStore } = await import("react");
13 const canWrite = (environmentId: string | null, scope: string) =>
14 scope === AuthSourceControlWriteScope && permissions.allowed.has(environmentId ?? "");
15 return {
16 ...(await original()),
17 readEnvironmentScope: canWrite,
18 useEnvironmentScope: (environmentId, scope) =>
19 useSyncExternalStore(
20 (listener) => {
21 permissions.listeners.add(listener);
22 return () => permissions.listeners.delete(listener);
23 },
24 () => canWrite(environmentId, scope),
25 ),
26 };
27});
28​
29vi.mock(import("~/state/pullRequests"), async (original) => ({
30 ...(await original()),
31 usePullRequestFilterOptions: (targets: ReadonlyArray<unknown>) => ({
32 labels: [],
33 authors:
34 targets.length === 0
35 ? []
36 : [
37 { login: "julius", name: "Julius", avatarUrl: null },
38 { login: "theo", name: "Theo", avatarUrl: null },
39 ],
40 references:
41 targets.length === 0
42 ? []
43 : [
44 { reference: "#12", kind: "issue", number: 12, title: "Crash", state: "open" },
45 { reference: "!12", kind: "change-request", number: 12, title: "Fix", state: "open" },
46 ],
47 error: null,
48 isPending: false,
49 }),
50}));
51​
52import { PullRequestMarkdownEditor } from "./PullRequestMarkdownEditor";
53​
54const secondary = EnvironmentId.make("secondary");
55const onSave = vi.fn<(next: string) => void>();
56const onCancel = vi.fn<() => void>();
57​
58function setup() {
59 permissions.allowed.clear();
60 permissions.listeners.clear();
61 onSave.mockReset();
62 onCancel.mockReset();
64​
65async function openEditor(options?: { value?: string; allowEmpty?: boolean }) {
66 return render(
67 <PullRequestMarkdownEditor
68 value={options?.value ?? "Original description"}
69 cwd="/repo"
70 environmentId={secondary}
71 completionScope={{
72 environmentId: secondary,
73 projectId: ProjectId.make("project"),
74 support: { mentions: true, referenceSigils: ["#", "!"] },
75 }}
76 label="Pull request description"
77 saving={false}
78 allowEmpty={options?.allowEmpty ?? false}
79 onSave={onSave}
80 onCancel={onCancel}
81 />,
82 );
84​
85function button(screen: Screen, label: string) {
86 const match = [...screen.container.querySelectorAll("button")].find(
87 (element) => element.textContent === label,
88 );
89 expect(match, `button ${label}`).toBeDefined();
90 return match!;
92​
93const clickSave = (screen: Screen) => act(async () => button(screen, "Save").click());
94​
95async function grant(allowed: boolean) {
96 await act(async () => {
97 if (allowed) permissions.allowed.add(secondary);
98 else permissions.allowed.delete(secondary);
99 permissions.listeners.forEach((listener) => listener());
100 });
102​
103it("keeps an edited draft through revocation and saves it after access returns without remounting", async () => {
104 setup();
105 permissions.allowed.add(secondary);
106 await using screen = await openEditor();
107 const textarea = screen.container.querySelector("textarea")!;
108 await userEvent.clear(textarea);
109 await userEvent.type(textarea, "Keep these unsaved edits");
110​
111 await grant(false);
112 expect(button(screen, "Save").disabled).toBe(true);
113 expect(screen.container.querySelector("textarea")).toBe(textarea);
114​
115 await grant(true);
116 await clickSave(screen);
117 expect(onSave).toHaveBeenCalledExactlyOnceWith("Keep these unsaved edits");
118});
119​
120it("completes @mentions and !references on Enter, and Escape closes the list until the next keystroke", async () => {
121 setup();
122 permissions.allowed.add(secondary);
123 await using screen = await openEditor({ value: "" });
124 const textarea = screen.container.querySelector("textarea")!;
125 await userEvent.type(textarea, "Thanks @ju");
126 await expect.element(page.getByRole("option", { name: /julius/ })).toBeVisible();
127​
128 await userEvent.keyboard("{Enter}");
129 expect(textarea.value).toBe("Thanks @julius ");
130 await expect.element(page.getByRole("listbox")).not.toBeInTheDocument();
131​
132 await userEvent.type(textarea, "and @");
133 await expect.element(page.getByRole("option", { name: /theo/ })).toBeVisible();
134 await userEvent.keyboard("{Escape}");
135 await expect.element(page.getByRole("listbox")).not.toBeInTheDocument();
136 await userEvent.type(textarea, "t");
137 await expect.element(page.getByRole("option", { name: /theo/ })).toBeVisible();
138​
139 await userEvent.type(textarea, " fixes !1");
140 await expect.element(page.getByRole("option", { name: /Fix/ })).toBeVisible();
141 await userEvent.keyboard("{Enter}");
142 expect(textarea.value).toBe("Thanks @julius and @t fixes !12 ");
143 expect(onCancel).not.toHaveBeenCalled();
144 expect(onSave).not.toHaveBeenCalled();
145});