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

usePullRequestFilesViewed.browser.test.tsx

150 lines4.3 KB

1import {
2 EnvironmentId,
3 ProjectId,
4 type PullRequestFilesViewedResult,
5 type PullRequestRef,
6} from "@t3tools/contracts";
7import { AsyncResult } from "effect/reactivity";
8import { act, StrictMode, useEffect } from "react";
9import { createRoot, type Root } from "react-dom/client";
10import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
11​
12const { host, setFilesViewed, toastAdd } = vi.hoisted(() => ({
13 host: { data: null as unknown, refresh: vi.fn() },
14 setFilesViewed: vi.fn(),
15 toastAdd: vi.fn(),
16}));
17​
18vi.mock(import("~/state/query"), async (original) => ({
19 ...(await original()),
20 useEnvironmentQuery: (() => ({
21 data: host.data,
22 error: null,
23 isPending: false,
24 isSuccess: true,
25 refresh: host.refresh,
26 })) as never,
27}));
28vi.mock(import("~/state/use-atom-command"), async (original) => ({
29 ...(await original()),
30 useAtomCommand: (() => setFilesViewed) as never,
31}));
32vi.mock(import("../ui/toast"), async (original) => {
33 const actual = await original();
34 return { ...actual, toastManager: { ...actual.toastManager, add: toastAdd } as never };
35});
36​
37import {
38 usePullRequestFilesViewed,
39 type PullRequestFilesViewedView,
40} from "./usePullRequestFilesViewed";
41​
42const environmentId = EnvironmentId.make("pr-files-viewed-audit");
43const reference: PullRequestRef = {
44 projectId: ProjectId.make("project-a"),
45 repository: "acme/web",
46 number: 42,
47};
48​
49function answer(state: "unviewed" | "viewed" | "dismissed"): PullRequestFilesViewedResult {
50 return { files: [{ path: "a.ts", state }], truncated: false };
52​
53let root: Root;
54let current: PullRequestFilesViewedView;
55​
56function Surface() {
57 const view = usePullRequestFilesViewed({ environmentId, reference, enabled: true });
58 useEffect(() => {
59 current = view;
60 });
61 return null;
63​
64const view = () => current;
65const render = () =>
66 act(async () =>
67 root.render(
68 <StrictMode>
69 <Surface />
70 </StrictMode>,
71 ),
72 );
73​
74async function reads(state: "unviewed" | "viewed" | "dismissed") {
75 host.data = answer(state);
76 await render();
78​
79beforeEach(async () => {
80 vi.useFakeTimers();
81 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
82 host.data = answer("unviewed");
83 host.refresh.mockReset();
84 toastAdd.mockReset();
85 setFilesViewed.mockReset().mockResolvedValue(AsyncResult.success(undefined));
86 root = createRoot(document.createElement("div"));
87 await render();
88});
89​
90afterEach(async () => {
91 await act(async () => root.unmount());
92 vi.useRealTimers();
93 vi.unstubAllGlobals();
94});
95​
96describe("a mark whose file was pushed to before the read that followed it", () => {
97 it("gives way to the host and shows the file as changed", async () => {
98 view().setViewed("a.ts", true);
99 await act(async () => vi.advanceTimersByTimeAsync(500));
100 expect(setFilesViewed).toHaveBeenCalledExactlyOnceWith({
101 environmentId,
102 input: { ...reference, files: [{ path: "a.ts", viewed: true }] },
103 });
104 expect(host.refresh).toHaveBeenCalled();
105​
106 await reads("dismissed");
107​
108 expect(view().isViewed("a.ts")).toBe(false);
109 expect(view().isStale("a.ts")).toBe(true);
110 });
111​
112 it("stays given way to on every later read, having nothing left to recover", async () => {
113 view().setViewed("a.ts", true);
114 await act(async () => vi.advanceTimersByTimeAsync(500));
115 await reads("dismissed");
116​
117 view().refresh();
118 await reads("dismissed");
119​
120 expect(view().isViewed("a.ts")).toBe(false);
121 expect(view().isStale("a.ts")).toBe(true);
122 });
123});
124​
125describe("a mark the host has not answered for yet", () => {
126 it("holds the press while the write is still out", async () => {
127 let land = (_result: unknown) => {};
128 setFilesViewed.mockReturnValueOnce(new Promise((resolve) => (land = resolve)));
129​
130 view().setViewed("a.ts", true);
131 await act(async () => vi.advanceTimersByTimeAsync(500));
132​
133 await reads("unviewed");
134 expect(view().isViewed("a.ts")).toBe(true);
135​
136 await act(async () => land(AsyncResult.success(undefined)));
137 expect(view().isViewed("a.ts")).toBe(true);
138 });
139​
140 it("holds a press made since the read that would otherwise answer for it", async () => {
141 view().setViewed("a.ts", true);
142 await act(async () => vi.advanceTimersByTimeAsync(500));
143​
144 view().setViewed("a.ts", true);
145 await reads("dismissed");
146​
147 expect(view().isViewed("a.ts")).toBe(true);
148 expect(view().isStale("a.ts")).toBe(false);
149 });
150});