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

usePullRequestFilesViewed.test.tsx

154 lines4.4 KB

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