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

usePullRequestDiff.test.tsx

118 lines4.0 KB

1import { RegistryContext } from "@effect/atom-react";
2import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
3import type { PullRequestDiffResult } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import { Atom, AtomRegistry } from "effect/reactivity";
6import { act, useLayoutEffect } from "react";
7import { createRoot } from "react-dom/client";
8import { expect, it, vi } from "vite-plus/test";
9​
10const mocks = vi.hoisted(() => ({ diff: vi.fn() }));
11vi.mock("../../state/pull-requests", () => ({ composerPullRequests: { diff: mocks.diff } }));
12​
13import { usePullRequestDiff } from "./usePullRequestDiff";
14​
15function patch(path: string, nextCursor: string | null = null): PullRequestDiffResult {
16 return {
17 patch: `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+new\n`,
18 truncated: false,
19 nextCursor,
20 };
22​
23function setup() {
24 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
25 const registry = AtomRegistry.make();
26 const requests: {
27 cursor: string | null;
28 resolve: (value: PullRequestDiffResult) => void;
29 reject: (error: Error) => void;
30 }[] = [];
31 const queries = Atom.family((cursor: string | null) =>
32 Atom.make(
33 Effect.tryPromise(
34 () =>
35 new Promise<PullRequestDiffResult>((resolve, reject) => {
36 requests.push({ cursor, resolve, reject });
37 }),
38 ),
39 ).pipe(Atom.keepAlive),
40 );
41 mocks.diff.mockImplementation((target: { input: { cursor?: string } }) =>
42 queries(target.input.cursor ?? null),
43 );
44 const root = createRoot(document.createElement("div"));
45 let current: ReturnType<typeof usePullRequestDiff>;
46 function Probe() {
47 const result = usePullRequestDiff(
48 {
49 environmentId: "environment",
50 projectId: "project",
51 repository: "repo",
52 number: 1,
53 },
54 sourceControlClients.get("github").changeRequest,
55 );
56 useLayoutEffect(() => {
57 current = result;
58 });
59 return null;
60 }
61 act(() =>
62 root.render(
63 <RegistryContext.Provider value={registry}>
64 <Probe />
65 </RegistryContext.Provider>,
66 ),
67 );
68 return {
69 requests,
70 get current() {
71 return current;
72 },
73 async succeed(index: number, result: PullRequestDiffResult) {
74 await act(async () => {
75 requests[index]!.resolve(result);
76 });
77 },
78 async fail(index: number) {
79 await act(async () => {
80 requests[index]!.reject(new Error("Connection lost"));
81 });
82 },
83 [Symbol.dispose]() {
84 act(() => root.unmount());
85 registry.dispose();
86 },
87 };
89​
90it("retries a failed continuation without discarding loaded files or restarting page one", async () => {
91 using screen = setup();
92 await screen.succeed(0, patch("first.ts", "second"));
93 act(() => screen.current.loadMore());
94 await screen.fail(1);
95 expect(screen.current.error).not.toBeNull();
96 expect(screen.current.patch).toContain("first.ts");
97​
98 act(() => screen.current.refresh());
99 expect(screen.requests.map((request) => request.cursor)).toEqual([null, "second", "second"]);
100 expect(screen.current.patch).toContain("first.ts");
101 await screen.succeed(2, patch("second.ts"));
102 expect(screen.current.error).toBeNull();
103 expect(screen.current.parsed.kind).toBe("files");
104 if (screen.current.parsed.kind !== "files") throw new Error("Expected files");
105 expect(screen.current.parsed.files.map((file) => file.path)).toEqual(["first.ts", "second.ts"]);
106 expect(screen.current.nextCursor).toBeNull();
107});
108​
109it("still refreshes from page one after successfully loading a continuation", async () => {
110 using screen = setup();
111 await screen.succeed(0, patch("first.ts", "second"));
112 act(() => screen.current.loadMore());
113 await screen.succeed(1, patch("second.ts"));
114 act(() => screen.current.refresh());
115 await screen.succeed(2, patch("refreshed.ts"));
116 if (screen.current.parsed.kind !== "files") throw new Error("Expected files");
117 expect(screen.current.parsed.files.map((file) => file.path)).toEqual(["refreshed.ts"]);
118});