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