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

PullRequestMarkdownEditor.browser.test.tsx

90 lines2.9 KB

1import { act, render, type Screen } from "../../../test/render";
2import { AuthSourceControlWriteScope, EnvironmentId } from "@t3tools/contracts";
3import { expect, it, vi } from "vite-plus/test";
4import { 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​
29import { PullRequestMarkdownEditor } from "./PullRequestMarkdownEditor";
30​
31const secondary = EnvironmentId.make("secondary");
32const onSave = vi.fn<(next: string) => void>();
33const onCancel = vi.fn<() => void>();
34​
35function setup() {
36 permissions.allowed.clear();
37 permissions.listeners.clear();
38 onSave.mockReset();
39 onCancel.mockReset();
41​
42async function openEditor(options?: { value?: string; allowEmpty?: boolean }) {
43 return render(
44 <PullRequestMarkdownEditor
45 value={options?.value ?? "Original description"}
46 cwd="/repo"
47 environmentId={secondary}
48 label="Pull request description"
49 saving={false}
50 allowEmpty={options?.allowEmpty ?? false}
51 onSave={onSave}
52 onCancel={onCancel}
53 />,
54 );
56​
57function button(screen: Screen, label: string) {
58 const match = [...screen.container.querySelectorAll("button")].find(
59 (element) => element.textContent === label,
60 );
61 expect(match, `button ${label}`).toBeDefined();
62 return match!;
64​
65const clickSave = (screen: Screen) => act(async () => button(screen, "Save").click());
66​
67async function grant(allowed: boolean) {
68 await act(async () => {
69 if (allowed) permissions.allowed.add(secondary);
70 else permissions.allowed.delete(secondary);
71 permissions.listeners.forEach((listener) => listener());
72 });
74​
75it("keeps an edited draft through revocation and saves it after access returns without remounting", async () => {
76 setup();
77 permissions.allowed.add(secondary);
78 await using screen = await openEditor();
79 const textarea = screen.container.querySelector("textarea")!;
80 await userEvent.clear(textarea);
81 await userEvent.type(textarea, "Keep these unsaved edits");
82​
83 await grant(false);
84 expect(button(screen, "Save").disabled).toBe(true);
85 expect(screen.container.querySelector("textarea")).toBe(textarea);
86​
87 await grant(true);
88 await clickSave(screen);
89 expect(onSave).toHaveBeenCalledExactlyOnceWith("Keep these unsaved edits");
90});