Open History to see the last commit
1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { getTranslationsSnapshot, GTProvider } from "gt-react";
3import { expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5
6const linking = vi.hoisted(() => ({
7 changeLink: vi.fn<(...args: unknown[]) => Promise<void>>(),
8}));
9
10vi.mock(import("~/hooks/usePullRequestLinking"), async (original) => ({
11 ...(await original()),
12 usePullRequestLinking: (() => ({
13 mode: "multiple",
14 canLink: () => true,
15 changeLink: linking.changeLink,
16 })) as never,
17}));
18vi.mock(import("~/state/entities"), async (original) => ({
19 ...(await original()),
20 useProjects: () => [],
21 useThreadShell: () => null,
22}));
23
24import { RegistryContext } from "@effect/atom-react";
25
26import { initializeI18n } from "~/i18n/initializeI18n";
27import { appAtomRegistry } from "~/rpc/atomRegistry";
28import { act, render } from "../../../test/render";
29import { LinkPullRequestDialogHost, openLinkPullRequestDialog } from "./LinkPullRequestDialog";
30
31const PR_URL = "https://github.com/acme/demo-app/pull/7";
32
33const enterEvent = (init?: KeyboardEventInit) =>
34 new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...init });
35
36async function openDialog() {
37 linking.changeLink.mockReset().mockResolvedValue(undefined);
38 appAtomRegistry.reset();
39 await using stack = new AsyncDisposableStack();
40 stack.defer(() => appAtomRegistry.reset());
41 const screen = stack.use(
42 await render(
43 <RegistryContext.Provider value={appAtomRegistry}>
44 <LinkPullRequestDialogHost />
45 </RegistryContext.Provider>,
46 ),
47 );
48 await act(() =>
49 openLinkPullRequestDialog({
50 environmentId: EnvironmentId.make("environment"),
51 threadId: ThreadId.make("thread"),
52 }),
53 );
54 const owned = stack.move();
55 return {
56 ...screen,
57 input: page.getByPlaceholder("Pull request URL or #42"),
58 [Symbol.asyncDispose]: () => owned.disposeAsync(),
59 };
60}
61
62it("focuses the reference input when the dialog opens", async () => {
63 await using dialog = await openDialog();
64 await expect.element(dialog.input).toHaveFocus();
65});
66
67it("starts only one link request on repeated Enter presses while pending and retries after failure", async () => {
68 await using dialog = await openDialog();
69 const pending = Promise.withResolvers<void>();
70 linking.changeLink.mockReturnValueOnce(pending.promise);
71 await dialog.input.fill(PR_URL);
72
73 await act(() => {
74 const element = dialog.input.element();
75 element.dispatchEvent(enterEvent());
76 element.dispatchEvent(enterEvent());
77 });
78 await userEvent.keyboard("{Enter}");
79
80 expect(linking.changeLink).toHaveBeenCalledOnce();
81 await expect.element(dialog.input).toHaveAttribute("readonly");
82
83 await act(() => pending.reject(new Error("link failed")));
84 await expect.element(page.getByText("link failed")).toBeVisible();
85 await expect.element(dialog.input).not.toHaveAttribute("readonly");
86
87 await userEvent.keyboard("{Enter}");
88 expect(linking.changeLink).toHaveBeenCalledTimes(2);
89 await expect.element(dialog.input).not.toBeInTheDocument();
90});
91
92it.each([{ isComposing: true }, { keyCode: 229 }, { repeat: true }])(
93 "ignores Enter during IME composition or auto-repeat without validating or submitting: %j",
94 async (ignoredEnter) => {
95 await using dialog = await openDialog();
96
97 await act(() => dialog.input.element().dispatchEvent(enterEvent(ignoredEnter)));
98 await expect
99 .element(page.getByText("Paste a pull request URL or enter 123 / #123."))
100 .not.toBeInTheDocument();
101 expect(linking.changeLink).not.toHaveBeenCalled();
102
103 await dialog.input.fill(PR_URL);
104 await act(() => dialog.input.element().dispatchEvent(enterEvent(ignoredEnter)));
105 expect(linking.changeLink).not.toHaveBeenCalled();
106
107 await userEvent.keyboard("{Enter}");
108 expect(linking.changeLink).toHaveBeenCalledOnce();
109 await expect.element(dialog.input).not.toBeInTheDocument();
110 },
111);
112
113it("shows the number error in the UI language when the thread has no repository", async () => {
114 // GTProvider leaves its locale in a cookie; this sets it back to English.
115 initializeI18n(() => {});
116 linking.changeLink.mockReset().mockResolvedValue(undefined);
117 appAtomRegistry.reset();
118 const translations = await getTranslationsSnapshot("de");
119 await using _screen = await render(
120 <RegistryContext.Provider value={appAtomRegistry}>
121 <GTProvider locale="de" translations={translations}>
122 <LinkPullRequestDialogHost />
123 </GTProvider>
124 </RegistryContext.Provider>,
125 );
126 await act(() =>
127 openLinkPullRequestDialog({
128 environmentId: EnvironmentId.make("environment"),
129 threadId: ThreadId.make("thread"),
130 }),
131 );
132
133 await page.getByRole("textbox").fill("123");
134 await userEvent.keyboard("{Enter}");
135
136 await expect
137 .element(
138 page.getByText(
139 "Füge die vollständige Pull-Request-URL ein. Dieser Thread hat kein Repository, in dem die Nummer aufgelöst werden kann.",
140 ),
141 )
142 .toBeVisible();
143 expect(page.getByText(/this thread has no repository/).query()).toBeNull();
144 initializeI18n(() => {});
145 appAtomRegistry.reset();
146});