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

PullRequestThreadDialog.browser.test.tsx

114 lines3.9 KB

1import {
2 EnvironmentId,
3 GitManagerError,
4 type GitResolvePullRequestResult,
5 ThreadId,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import { AsyncResult, Atom } from "effect/reactivity";
9import { expect, it, vi } from "vite-plus/test";
10import { page, userEvent } from "vite-plus/test/browser";
11​
12import { AppAtomRegistryProvider, appAtomRegistry } from "~/rpc/atomRegistry";
13import { gitEnvironment } from "~/state/git";
14import { vcsActionManager } from "~/state/vcs";
15import { act, fakeTimers, render } from "../../test/render";
16import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
17​
18it("keeps another caller's checkout failure out of the dialog", async () => {
19 const target = { environmentId: EnvironmentId.make("checkout-error-test"), cwd: "/repo" };
20 const failure = AsyncResult.failure(Cause.fail(new Error("Pull request #42 has local changes.")));
21 const reply = Promise.withResolvers<typeof failure>();
22​
23 await using _screen = await render(
24 <AppAtomRegistryProvider>
25 <PullRequestThreadDialog
26 {...target}
27 open
28 initialReference={null}
29 threadId={ThreadId.make("checkout-error-test")}
30 onOpenChange={() => {}}
31 onPrepared={() => {}}
32 />
33 </AppAtomRegistryProvider>,
34 );
35​
36 const dialog = page.getByRole("dialog");
37 const cancel = dialog.getByRole("button", { name: "Cancel" });
38 const checkout = vcsActionManager.track(
39 appAtomRegistry,
40 target,
41 { operation: "prepare_pull_request_thread", label: "Preparing pull request thread" },
42 () => reply.promise,
43 );
44 await expect.element(cancel).toBeDisabled();
45 await act(async () => {
46 reply.resolve(failure);
47 await checkout;
48 });
49 await expect.element(cancel).toBeEnabled();
50 expect(dialog.getByText(/Failed to prepare .* request thread\./).query()).toBeNull();
51});
52​
53it("hides a stale resolution error while debouncing or refetching a pull request reference", async () => {
54 const target = { environmentId: EnvironmentId.make("resolution-error-test"), cwd: "/repo" };
55 const failure = AsyncResult.failure<GitResolvePullRequestResult, GitManagerError>(
56 Cause.fail(
57 new GitManagerError({
58 operation: "resolvePullRequest",
59 cwd: target.cwd,
60 detail: "Pull request #99999 not found.",
61 }),
62 ),
63 );
64 const failedResolution =
65 Atom.make<AsyncResult.AsyncResult<GitResolvePullRequestResult, GitManagerError>>(failure);
66 const pendingResolution = Atom.make(
67 AsyncResult.initial<GitResolvePullRequestResult, never>(true),
68 );
69 using _resolve = vi
70 .spyOn(gitEnvironment, "pullRequestResolution")
71 .mockImplementation(({ input }) =>
72 input.reference === "99999" ? failedResolution : pendingResolution,
73 );
74​
75 await using _screen = await render(
76 <AppAtomRegistryProvider>
77 <PullRequestThreadDialog
78 {...target}
79 open
80 initialReference="#99999"
81 threadId={ThreadId.make("resolution-error-test")}
82 onOpenChange={() => {}}
83 onPrepared={() => {}}
84 />
85 </AppAtomRegistryProvider>,
86 );
87​
88 const dialog = page.getByRole("dialog");
89 const resolving = dialog.getByText("Resolving pull request...");
90 const staleError = dialog.getByText(/Pull request #99999 not found\./);
91 await expect.element(staleError).toBeVisible();
92​
93 await act(() => {
94 appAtomRegistry.set(failedResolution, AsyncResult.waiting(failure));
95 });
96 expect(resolving.element()).toBeVisible();
97 expect(staleError.query()).toBeNull();
98​
99 await act(() => {
100 appAtomRegistry.set(failedResolution, failure);
101 });
102 expect(staleError.element()).toBeVisible();
103​
104 using _timers = fakeTimers();
105 await userEvent.fill(dialog.getByRole("textbox"), "#123");
106​
107 expect(resolving.element()).toBeVisible();
108 expect(staleError.query()).toBeNull();
109​
110 await act(() => vi.advanceTimersByTime(450));
111 await userEvent.fill(dialog.getByRole("textbox"), "#99999");
112 await act(() => vi.advanceTimersByTime(450));
113 expect(staleError.element()).toBeVisible();
114});