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

LinkPullRequestDialog.browser.test.tsx

216 lines7.9 KB

1import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2import { settlePromise } from "@t3tools/client-runtime/state/runtime";
3import { EnvironmentId, ThreadId } from "@t3tools/contracts";
4import { expect, it, vi } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6​
7const linking = vi.hoisted(() => ({
8 changeLink: vi.fn<(...args: unknown[]) => Promise<void>>(),
9}));
10​
11vi.mock(import("~/hooks/usePullRequestLinking"), async (original) => ({
12 ...(await original()),
13 usePullRequestLinking: (() => ({
14 mode: "multiple",
15 canLink: () => true,
16 changeLink: linking.changeLink,
17 })) as never,
18}));
19vi.mock(import("~/state/entities"), async (original) => ({
20 ...(await original()),
21 useProjects: () => [],
22 useThreadShell: () => null,
23}));
24​
25import { RegistryContext } from "@effect/atom-react";
26​
27import { retainThreadOverlaysForThread } from "~/components/chat/threadOverlayRetention";
28import { appAtomRegistry } from "~/rpc/atomRegistry";
29import { act, render } from "../../../test/render";
30import { LinkPullRequestDialogHost, openLinkPullRequestDialog } from "./LinkPullRequestDialog";
31​
32const PR_URL = "https://github.com/acme/demo-app/pull/7";
33​
34const enterEvent = (init?: KeyboardEventInit) =>
35 new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...init });
36​
37async function openDialog() {
38 linking.changeLink.mockReset().mockResolvedValue(undefined);
39 appAtomRegistry.reset();
40 await using stack = new AsyncDisposableStack();
41 stack.defer(() => appAtomRegistry.reset());
42 const screen = stack.use(
43 await render(
44 <RegistryContext.Provider value={appAtomRegistry}>
45 <LinkPullRequestDialogHost />
46 </RegistryContext.Provider>,
47 ),
48 );
49 await act(() =>
50 openLinkPullRequestDialog({
51 environmentId: EnvironmentId.make("environment"),
52 threadId: ThreadId.make("thread"),
53 }),
54 );
55 const owned = stack.move();
56 return {
57 ...screen,
58 input: page.getByPlaceholder("Pull request URL or #42"),
59 [Symbol.asyncDispose]: () => owned.disposeAsync(),
60 };
62​
63it("focuses the reference input when the dialog opens", async () => {
64 await using dialog = await openDialog();
65 await expect.element(dialog.input).toHaveFocus();
66});
67​
68it("starts only one link request on repeated Enter presses while pending and retries after failure", async () => {
69 await using dialog = await openDialog();
70 const pending = Promise.withResolvers<void>();
71 linking.changeLink.mockReturnValueOnce(pending.promise);
72 await dialog.input.fill(PR_URL);
73​
74 await act(() => {
75 const element = dialog.input.element();
76 element.dispatchEvent(enterEvent());
77 element.dispatchEvent(enterEvent());
78 });
79 await userEvent.keyboard("{Enter}");
80​
81 expect(linking.changeLink).toHaveBeenCalledOnce();
82 await expect.element(dialog.input).toHaveAttribute("readonly");
83​
84 await act(() => pending.reject(new Error("link failed")));
85 await expect.element(page.getByText("link failed")).toBeVisible();
86 await expect.element(dialog.input).not.toHaveAttribute("readonly");
87​
88 await userEvent.keyboard("{Enter}");
89 expect(linking.changeLink).toHaveBeenCalledTimes(2);
90 await expect.element(dialog.input).not.toBeInTheDocument();
91});
92​
93it("clears the submit error when editing the reference after a failed link", async () => {
94 await using dialog = await openDialog();
95 linking.changeLink.mockRejectedValueOnce(new Error("link failed"));
96​
97 await dialog.input.fill(PR_URL);
98 await userEvent.keyboard("{Enter}");
99 await expect.element(page.getByText("link failed")).toBeVisible();
100​
101 await dialog.input.fill("https://github.com/acme/demo-app/pull/42");
102 await expect.element(page.getByText("github.com/acme/demo-app #42")).toBeVisible();
103 await expect.element(page.getByText("link failed")).not.toBeInTheDocument();
104});
105​
106it.each([{ isComposing: true }, { keyCode: 229 }, { repeat: true }])(
107 "ignores Enter during IME composition or auto-repeat without validating or submitting: %j",
108 async (ignoredEnter) => {
109 await using dialog = await openDialog();
110​
111 await act(() => dialog.input.element().dispatchEvent(enterEvent(ignoredEnter)));
112 await expect
113 .element(page.getByText("Paste a pull request URL or enter 123 / #123."))
114 .not.toBeInTheDocument();
115 expect(linking.changeLink).not.toHaveBeenCalled();
116​
117 await dialog.input.fill(PR_URL);
118 await act(() => dialog.input.element().dispatchEvent(enterEvent(ignoredEnter)));
119 expect(linking.changeLink).not.toHaveBeenCalled();
120​
121 await userEvent.keyboard("{Enter}");
122 expect(linking.changeLink).toHaveBeenCalledOnce();
123 await expect.element(dialog.input).not.toBeInTheDocument();
124 },
125);
126​
127it("shows the number error in the UI language when the thread has no repository", async () => {
128 linking.changeLink.mockReset().mockResolvedValue(undefined);
129 appAtomRegistry.reset();
130 await using _screen = await render(
131 <RegistryContext.Provider value={appAtomRegistry}>
132 <LinkPullRequestDialogHost />
133 </RegistryContext.Provider>,
134 { locale: "de" },
135 );
136 await act(() =>
137 openLinkPullRequestDialog({
138 environmentId: EnvironmentId.make("environment"),
139 threadId: ThreadId.make("thread"),
140 }),
141 );
142​
143 await page.getByRole("textbox").fill("123");
144 await userEvent.keyboard("{Enter}");
145​
146 await expect
147 .element(
148 page.getByText(
149 "Füge die vollständige Pull-Request-URL ein. Dieser Thread hat kein Repository, in dem die Nummer aufgelöst werden kann.",
150 ),
151 )
152 .toBeVisible();
153 expect(page.getByText(/this thread has no repository/).query()).toBeNull();
154 appAtomRegistry.reset();
155});
156​
157it("closes when leaving its thread, drops unsaved input, and ignores a left thread's settling submit", async () => {
158 await using dialog = await openDialog();
159 const dialogPopup = page.getByRole("dialog", { name: "Link pull request" });
160 const threadA = {
161 environmentId: EnvironmentId.make("environment"),
162 threadId: ThreadId.make("thread"),
163 };
164 const threadB = {
165 environmentId: EnvironmentId.make("environment"),
166 threadId: ThreadId.make("other-thread"),
167 };
168​
169 await expect.element(dialogPopup).toBeVisible();
170 await act(() => retainThreadOverlaysForThread(scopedThreadKey(threadA)));
171 await expect.element(dialogPopup).toBeVisible();
172​
173 await dialog.input.fill(PR_URL);
174 await act(() => retainThreadOverlaysForThread(scopedThreadKey(threadB)));
175 await expect.element(dialogPopup).not.toBeInTheDocument();
176​
177 await act(() => openLinkPullRequestDialog(threadA));
178 await expect.element(dialog.input).toHaveValue("");
179 await act(() => retainThreadOverlaysForThread(null));
180 await expect.element(dialogPopup).not.toBeInTheDocument();
181​
182 const pending = Promise.withResolvers<void>();
183 linking.changeLink.mockReturnValueOnce(pending.promise);
184 await act(() => openLinkPullRequestDialog(threadA));
185 await dialog.input.fill(PR_URL);
186 await userEvent.keyboard("{Enter}");
187 await vi.waitFor(() => expect(linking.changeLink).toHaveBeenCalledOnce());
188​
189 await act(() => retainThreadOverlaysForThread(scopedThreadKey(threadB)));
190 await expect.element(dialogPopup).not.toBeInTheDocument();
191​
192 await act(() => retainThreadOverlaysForThread(scopedThreadKey(threadA)));
193 await act(() => openLinkPullRequestDialog(threadA));
194 await expect.element(dialogPopup).toBeVisible();
195​
196 await act(async () => {
197 pending.resolve(undefined);
198 await settlePromise(() => Promise.resolve());
199 });
200 await expect.element(dialogPopup).toBeVisible();
201​
202 const sameThreadPending = Promise.withResolvers<void>();
203 linking.changeLink.mockReturnValueOnce(sameThreadPending.promise);
204 await dialog.input.fill(PR_URL);
205 await userEvent.keyboard("{Enter}");
206 await vi.waitFor(() => expect(linking.changeLink).toHaveBeenCalledTimes(2));
207​
208 await act(() => openLinkPullRequestDialog(threadA));
209 await expect.element(dialogPopup).toBeVisible();
210​
211 await act(async () => {
212 sameThreadPending.resolve(undefined);
213 await settlePromise(() => Promise.resolve());
214 });
215 await expect.element(dialogPopup).not.toBeInTheDocument();
216});