Open History to see the last commit
1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { expect, it, vi } from "vite-plus/test";
3import { page, userEvent } from "vite-plus/test/browser";
4
5const linking = vi.hoisted(() => ({
6 changeLink: vi.fn<(...args: unknown[]) => Promise<void>>(),
7}));
8
9vi.mock(import("~/hooks/usePullRequestLinking"), async (original) => ({
10 ...(await original()),
11 usePullRequestLinking: (() => ({
12 mode: "multiple",
13 canLink: () => true,
14 changeLink: linking.changeLink,
15 })) as never,
16}));
17vi.mock(import("~/state/entities"), async (original) => ({
18 ...(await original()),
19 useProjects: () => [],
20 useThreadShell: () => null,
21}));
22
23import { RegistryContext } from "@effect/atom-react";
24
25import { appAtomRegistry } from "~/rpc/atomRegistry";
26import { act, render } from "../../../test/render";
27import { LinkPullRequestDialogHost, openLinkPullRequestDialog } from "./LinkPullRequestDialog";
28
29const PR_URL = "https://github.com/acme/demo-app/pull/7";
30
31const enterEvent = (init?: KeyboardEventInit) =>
32 new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...init });
33
34async function openDialog() {
35 linking.changeLink.mockReset().mockResolvedValue(undefined);
36 appAtomRegistry.reset();
37 await using stack = new AsyncDisposableStack();
38 stack.defer(() => appAtomRegistry.reset());
39 const screen = stack.use(
40 await render(
41 <RegistryContext.Provider value={appAtomRegistry}>
42 <LinkPullRequestDialogHost />
43 </RegistryContext.Provider>,
44 ),
45 );
46 await act(() =>
47 openLinkPullRequestDialog({
48 environmentId: EnvironmentId.make("environment"),
49 threadId: ThreadId.make("thread"),
50 }),
51 );
52 const owned = stack.move();
53 return {
54 ...screen,
55 input: page.getByPlaceholder("Pull request URL or #42"),
56 [Symbol.asyncDispose]: () => owned.disposeAsync(),
57 };
58}
59
60it("focuses the reference input when the dialog opens", async () => {
61 await using dialog = await openDialog();
62 await expect.element(dialog.input).toHaveFocus();
63});
64
65it("starts only one link request on repeated Enter presses while pending and retries after failure", async () => {
66 await using dialog = await openDialog();
67 const pending = Promise.withResolvers<void>();
68 linking.changeLink.mockReturnValueOnce(pending.promise);
69 await dialog.input.fill(PR_URL);
70
71 await act(() => {
72 const element = dialog.input.element();
73 element.dispatchEvent(enterEvent());
74 element.dispatchEvent(enterEvent());
75 });
76 await userEvent.keyboard("{Enter}");
77
78 expect(linking.changeLink).toHaveBeenCalledOnce();
79 await expect.element(dialog.input).toHaveAttribute("readonly");
80
81 await act(() => pending.reject(new Error("link failed")));
82 await expect.element(page.getByText("link failed")).toBeVisible();
83 await expect.element(dialog.input).not.toHaveAttribute("readonly");
84
85 await userEvent.keyboard("{Enter}");
86 expect(linking.changeLink).toHaveBeenCalledTimes(2);
87 await expect.element(dialog.input).not.toBeInTheDocument();
88});
89
90it.each([{ isComposing: true }, { keyCode: 229 }, { repeat: true }])(
91 "ignores Enter during IME composition or auto-repeat without validating or submitting: %j",
92 async (ignoredEnter) => {
93 await using dialog = await openDialog();
94
95 await act(() => dialog.input.element().dispatchEvent(enterEvent(ignoredEnter)));
96 await expect
97 .element(page.getByText("Paste a pull request URL or enter 123 / #123."))
98 .not.toBeInTheDocument();
99 expect(linking.changeLink).not.toHaveBeenCalled();
100
101 await dialog.input.fill(PR_URL);
102 await act(() => dialog.input.element().dispatchEvent(enterEvent(ignoredEnter)));
103 expect(linking.changeLink).not.toHaveBeenCalled();
104
105 await userEvent.keyboard("{Enter}");
106 expect(linking.changeLink).toHaveBeenCalledOnce();
107 await expect.element(dialog.input).not.toBeInTheDocument();
108 },
109);