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

LinkPullRequestDialog.browser.test.tsx

203 lines7.3 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.each([{ isComposing: true }, { keyCode: 229 }, { repeat: true }])(
94 "ignores Enter during IME composition or auto-repeat without validating or submitting: %j",
95 async (ignoredEnter) => {
96 await using dialog = await openDialog();
97​
98 await act(() => dialog.input.element().dispatchEvent(enterEvent(ignoredEnter)));
99 await expect
100 .element(page.getByText("Paste a pull request URL or enter 123 / #123."))
101 .not.toBeInTheDocument();
102 expect(linking.changeLink).not.toHaveBeenCalled();
103​
104 await dialog.input.fill(PR_URL);
105 await act(() => dialog.input.element().dispatchEvent(enterEvent(ignoredEnter)));
106 expect(linking.changeLink).not.toHaveBeenCalled();
107​
108 await userEvent.keyboard("{Enter}");
109 expect(linking.changeLink).toHaveBeenCalledOnce();
110 await expect.element(dialog.input).not.toBeInTheDocument();
111 },
112);
113​
114it("shows the number error in the UI language when the thread has no repository", async () => {
115 linking.changeLink.mockReset().mockResolvedValue(undefined);
116 appAtomRegistry.reset();
117 await using _screen = await render(
118 <RegistryContext.Provider value={appAtomRegistry}>
119 <LinkPullRequestDialogHost />
120 </RegistryContext.Provider>,
121 { locale: "de" },
122 );
123 await act(() =>
124 openLinkPullRequestDialog({
125 environmentId: EnvironmentId.make("environment"),
126 threadId: ThreadId.make("thread"),
127 }),
128 );
129​
130 await page.getByRole("textbox").fill("123");
131 await userEvent.keyboard("{Enter}");
132​
133 await expect
134 .element(
135 page.getByText(
136 "Füge die vollständige Pull-Request-URL ein. Dieser Thread hat kein Repository, in dem die Nummer aufgelöst werden kann.",
137 ),
138 )
139 .toBeVisible();
140 expect(page.getByText(/this thread has no repository/).query()).toBeNull();
141 appAtomRegistry.reset();
142});
143​
144it("closes when leaving its thread, drops unsaved input, and ignores a left thread's settling submit", async () => {
145 await using dialog = await openDialog();
146 const dialogPopup = page.getByRole("dialog", { name: "Link pull request" });
147 const threadA = {
148 environmentId: EnvironmentId.make("environment"),
149 threadId: ThreadId.make("thread"),
150 };
151 const threadB = {
152 environmentId: EnvironmentId.make("environment"),
153 threadId: ThreadId.make("other-thread"),
154 };
155​
156 await expect.element(dialogPopup).toBeVisible();
157 await act(() => retainThreadOverlaysForThread(scopedThreadKey(threadA)));
158 await expect.element(dialogPopup).toBeVisible();
159​
160 await dialog.input.fill(PR_URL);
161 await act(() => retainThreadOverlaysForThread(scopedThreadKey(threadB)));
162 await expect.element(dialogPopup).not.toBeInTheDocument();
163​
164 await act(() => openLinkPullRequestDialog(threadA));
165 await expect.element(dialog.input).toHaveValue("");
166 await act(() => retainThreadOverlaysForThread(null));
167 await expect.element(dialogPopup).not.toBeInTheDocument();
168​
169 const pending = Promise.withResolvers<void>();
170 linking.changeLink.mockReturnValueOnce(pending.promise);
171 await act(() => openLinkPullRequestDialog(threadA));
172 await dialog.input.fill(PR_URL);
173 await userEvent.keyboard("{Enter}");
174 await vi.waitFor(() => expect(linking.changeLink).toHaveBeenCalledOnce());
175​
176 await act(() => retainThreadOverlaysForThread(scopedThreadKey(threadB)));
177 await expect.element(dialogPopup).not.toBeInTheDocument();
178​
179 await act(() => retainThreadOverlaysForThread(scopedThreadKey(threadA)));
180 await act(() => openLinkPullRequestDialog(threadA));
181 await expect.element(dialogPopup).toBeVisible();
182​
183 await act(async () => {
184 pending.resolve(undefined);
185 await settlePromise(() => Promise.resolve());
186 });
187 await expect.element(dialogPopup).toBeVisible();
188​
189 const sameThreadPending = Promise.withResolvers<void>();
190 linking.changeLink.mockReturnValueOnce(sameThreadPending.promise);
191 await dialog.input.fill(PR_URL);
192 await userEvent.keyboard("{Enter}");
193 await vi.waitFor(() => expect(linking.changeLink).toHaveBeenCalledTimes(2));
194​
195 await act(() => openLinkPullRequestDialog(threadA));
196 await expect.element(dialogPopup).toBeVisible();
197​
198 await act(async () => {
199 sameThreadPending.resolve(undefined);
200 await settlePromise(() => Promise.resolve());
201 });
202 await expect.element(dialogPopup).not.toBeInTheDocument();
203});