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

usePullRequestChecksRefresh.browser.test.tsx

88 lines2.9 KB

1import type { PullRequestCheck } from "@t3tools/contracts";
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5​
6import { usePullRequestChecksRefresh } from "./usePullRequestChecksRefresh";
7​
8let root: Root;
9let visibility: DocumentVisibilityState;
10​
11beforeEach(() => {
12 vi.useFakeTimers();
13 vi.setSystemTime(1_000_000);
14 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
15 visibility = "visible";
16 vi.spyOn(document, "visibilityState", "get").mockImplementation(() => visibility);
17 root = createRoot(document.createElement("div"));
18});
19​
20afterEach(() => {
21 act(() => root.unmount());
22 vi.restoreAllMocks();
23 vi.useRealTimers();
24 vi.unstubAllGlobals();
25});
26​
27it.each(["pending", "action-required"] as const)(
28 "polls quiet PRs each minute, speeds up for %s or missing checks, resumes from idle, and stops for closed PRs",
29 (pendingStatus) => {
30 const refresh = vi.fn();
31 function Probe({
32 status = "success",
33 enabled = true,
34 }: {
35 status?: PullRequestCheck["status"] | null;
36 enabled?: boolean;
37 }) {
38 usePullRequestChecksRefresh({
39 refresh,
40 enabled,
41 key: `test-pr-checks:${pendingStatus}`,
42 checks: status === null ? [] : [{ name: "CI", status, description: null, url: null }],
43 });
44 return null;
45 }
46 const update = (props: Parameters<typeof Probe>[0]) =>
47 act(() => root.render(<Probe {...props} />));
48 const advance = (ms: number) => act(() => vi.advanceTimersByTime(ms));
49​
50 update({});
51 advance(59_999);
52 expect(refresh).toHaveBeenCalledTimes(0);
53 advance(1);
54 expect(refresh).toHaveBeenCalledTimes(1);
55 update({ status: pendingStatus });
56 advance(44_999);
57 expect(refresh).toHaveBeenCalledTimes(1);
58 advance(1);
59 expect(refresh).toHaveBeenCalledTimes(2);
60 update({ status: "failure" });
61 advance(60_000);
62 expect(refresh).toHaveBeenCalledTimes(3);
63 update({ status: null });
64 for (let tick = 0; tick < 3; tick++) {
65 advance(45_000);
66 update({ status: null });
67 }
68 expect(refresh).toHaveBeenCalledTimes(6);
69 advance(44_999);
70 expect(refresh).toHaveBeenCalledTimes(6);
71 advance(1);
72 expect(refresh).toHaveBeenCalledTimes(7);
73 advance(6 * 60_000);
74 const beforeResume = refresh.mock.calls.length;
75 act(() => document.dispatchEvent(new Event("pointerdown")));
76 expect(refresh).toHaveBeenCalledTimes(beforeResume + 1);
77 visibility = "hidden";
78 act(() => document.dispatchEvent(new Event("visibilitychange")));
79 advance(60_000);
80 expect(refresh).toHaveBeenCalledTimes(beforeResume + 1);
81 visibility = "visible";
82 act(() => document.dispatchEvent(new Event("visibilitychange")));
83 expect(refresh).toHaveBeenCalledTimes(beforeResume + 2);
84 update({ enabled: false });
85 advance(120_000);
86 expect(refresh).toHaveBeenCalledTimes(beforeResume + 2);
87 },
88);