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

PullRequestMarkdownEditor.browser.test.tsx

159 lines4.8 KB

1import { AuthSourceControlWriteScope, EnvironmentId } from "@t3tools/contracts";
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5import { userEvent } from "vite-plus/test/browser";
6​
7const permissions = vi.hoisted(() => ({
8 allowed: new Set<string>(),
9 listeners: new Set<() => void>(),
10}));
11​
12vi.mock(import("~/state/session"), async (original) => {
13 const { useSyncExternalStore } = await import("react");
14 const canWrite = (environmentId: string | null, scope: string) =>
15 scope === AuthSourceControlWriteScope && permissions.allowed.has(environmentId ?? "");
16 return {
17 ...(await original()),
18 readEnvironmentScope: canWrite,
19 useEnvironmentScope: (environmentId, scope) =>
20 useSyncExternalStore(
21 (listener) => {
22 permissions.listeners.add(listener);
23 return () => permissions.listeners.delete(listener);
24 },
25 () => canWrite(environmentId, scope),
26 ),
27 };
28});
29​
30import { PullRequestMarkdownEditor } from "./PullRequestMarkdownEditor";
31​
32const primary = EnvironmentId.make("primary");
33const secondary = EnvironmentId.make("secondary");
34const onSave = vi.fn<(next: string) => void>();
35const onCancel = vi.fn<() => void>();
36let root: Root;
37let container: HTMLDivElement;
38​
39beforeEach(() => {
40 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
41 permissions.allowed.clear();
42 permissions.listeners.clear();
43 onSave.mockReset();
44 onCancel.mockReset();
45 container = document.createElement("div");
46 document.body.append(container);
47 root = createRoot(container);
48});
49​
50afterEach(async () => {
51 await act(async () => root.unmount());
52 container.remove();
53 vi.unstubAllGlobals();
54});
55​
56async function openEditor(options?: { value?: string; allowEmpty?: boolean }) {
57 await act(async () =>
58 root.render(
59 <PullRequestMarkdownEditor
60 value={options?.value ?? "Original description"}
61 cwd="/repo"
62 environmentId={secondary}
63 label="Pull request description"
64 saving={false}
65 allowEmpty={options?.allowEmpty ?? false}
66 onSave={onSave}
67 onCancel={onCancel}
68 />,
69 ),
70 );
72​
73function button(label: string) {
74 const match = [...container.querySelectorAll("button")].find(
75 (element) => element.textContent === label,
76 );
77 expect(match, `button ${label}`).toBeDefined();
78 return match!;
80​
81const clickSave = () => act(async () => button("Save").click());
82​
83async function grant(allowed: boolean) {
84 await act(async () => {
85 if (allowed) permissions.allowed.add(secondary);
86 else permissions.allowed.delete(secondary);
87 permissions.listeners.forEach((listener) => listener());
88 });
90​
91it("does not save a secondary environment's description using the primary grant", async () => {
92 permissions.allowed.add(primary);
93 await openEditor();
94​
95 expect(button("Save").disabled).toBe(true);
96 await clickSave();
97​
98 expect(onSave).not.toHaveBeenCalled();
99});
100​
101it("rejects a Save click when the grant changes before React updates", async () => {
102 permissions.allowed.add(secondary);
103 await openEditor();
104 permissions.allowed.delete(secondary);
105​
106 expect(button("Save").disabled).toBe(false);
107 await clickSave();
108​
109 expect(onSave).not.toHaveBeenCalled();
110});
111​
112it("keeps an edited draft through revocation and saves it after access returns without remounting", async () => {
113 permissions.allowed.add(secondary);
114 await openEditor();
115 const textarea = container.querySelector("textarea")!;
116 await userEvent.clear(textarea);
117 await userEvent.type(textarea, "Keep these unsaved edits");
118​
119 await grant(false);
120 expect(button("Save").disabled).toBe(true);
121 expect(container.querySelector("textarea")).toBe(textarea);
122​
123 await grant(true);
124 await clickSave();
125 expect(onSave).toHaveBeenCalledExactlyOnceWith("Keep these unsaved edits");
126});
127​
128it("saves with source-control permission alone, from the button or Cmd/Ctrl+Enter", async () => {
129 permissions.allowed.add(secondary);
130 await openEditor();
131​
132 await clickSave();
133 expect(onSave).toHaveBeenCalledExactlyOnceWith("Original description");
134​
135 container.querySelector("textarea")!.focus();
136 await userEvent.keyboard("{Control>}{Enter}{/Control}");
137 expect(onSave).toHaveBeenCalledTimes(2);
138});
139​
140it("allows clearing a description only after a pending grant becomes available", async () => {
141 await openEditor({ value: "", allowEmpty: true });
142 await clickSave();
143 expect(onSave).not.toHaveBeenCalled();
144​
145 await grant(true);
146 await clickSave();
147 expect(onSave).toHaveBeenCalledExactlyOnceWith("");
148});
149​
150it("keeps cancelling a local edit available without a write grant", async () => {
151 await openEditor();
152​
153 await act(async () => button("Cancel").click());
154 container.querySelector("textarea")!.focus();
155 await userEvent.keyboard("{Escape}");
156​
157 expect(onCancel).toHaveBeenCalledTimes(2);
158 expect(onSave).not.toHaveBeenCalled();
159});