Open History to see the last commit
1import { EnvironmentId, ProjectId } from "@t3tools/contracts";
2import { expect, it, vi } from "vite-plus/test";
3
4const state = vi.hoisted(() => ({ reads: [] as Array<unknown> }));
5vi.mock(import("~/state/pullRequests"), async (original) => {
6 const actual = await original();
7 return {
8 ...actual,
9 pullRequestEnvironment: {
10 ...actual.pullRequestEnvironment,
11 detail: ((target: unknown) => target) as never,
12 },
13 };
14});
15vi.mock(import("~/state/query"), async (original) => ({
16 ...(await original()),
17 useEnvironmentQuery: ((target: unknown) => {
18 if (target !== null || state.reads.length > 0) state.reads.push(target);
19 return {};
20 }) as never,
21}));
22
23import { act, fakeTimers, render } from "../../../test/render";
24import { usePullRequestIntentPrefetch } from "./usePullRequestIntentPrefetch";
25
26const environmentId = EnvironmentId.make("environment");
27const row = {
28 environmentId,
29 projectId: ProjectId.make("project"),
30 host: "GitHub.com",
31 repository: "owner/repo",
32 number: 7,
33};
34
35function Rows() {
36 const prefetch = usePullRequestIntentPrefetch(() => true);
37 return (
38 <>
39 <button type="button" onClick={() => prefetch(row)}>
40 rest
41 </button>
42 <button type="button" onClick={() => prefetch(null)}>
43 leave
44 </button>
45 </>
46 );
47}
48
49it("reads the panel's detail once intent rests on a row for 150 ms", async () => {
50 state.reads = [];
51 await using screen = await render(<Rows />, {
52 // A bare hook probe; nothing here navigates.
53 router: false,
54 });
55 using _timers = fakeTimers({ now: 0 });
56 const rest = screen.getByRole("button", { name: "rest" }).element() as HTMLElement;
57 const leave = screen.getByRole("button", { name: "leave" }).element() as HTMLElement;
58
59 await act(() => {
60 rest.click();
61 leave.click();
62 });
63 await act(() => vi.advanceTimersByTime(1_000));
64 expect(state.reads).toEqual([]);
65
66 await act(() => rest.click());
67 await act(() => vi.advanceTimersByTime(149));
68 expect(state.reads).toEqual([]);
69 await act(() => vi.advanceTimersByTime(1));
70 // Query atoms key on JSON order: this is the order the PRs page panel reads.
71 expect(JSON.stringify(state.reads.at(-1))).toBe(
72 JSON.stringify({
73 environmentId,
74 input: {
75 projectId: row.projectId,
76 repository: "owner/repo",
77 number: 7,
78 host: "github.com",
79 allowStale: false,
80 },
81 }),
82 );
83
84 await act(() => leave.click());
85 expect(state.reads.at(-1)).toBeNull();
86});