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

PullRequestLinkPreview.browser.test.tsx

153 lines4.6 KB

1import { EnvironmentId, ProjectId, type PullRequestSummary } from "@t3tools/contracts";
2import { getTranslationsSnapshot, GTProvider } from "gt-react";
3import { expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5​
6const known = vi.hoisted(
7 () =>
8 ({
9 provider: "github",
10 projectId: "project",
11 repository: "owner/repo",
12 number: 1,
13 title: "Known title",
14 url: "https://github.com/owner/repo/pull/1",
15 state: "open",
16 headBranch: "feature",
17 baseBranch: "main",
18 updatedAt: "2026-09-01T00:00:00Z",
19 }) as PullRequestSummary,
20);
21const state = vi.hoisted(() => ({ restoredPreview: null as unknown, activity: null as unknown }));
22vi.mock(import("~/state/query"), async (original) => ({
23 ...(await original()),
24 // The host has not answered the preview read yet.
25 useEnvironmentQuery: ((atom: unknown) => ({
26 data:
27 atom === null
28 ? null
29 : typeof atom === "object" && "activity" in atom
30 ? state.activity
31 : state.restoredPreview,
32 dataUpdatedAt: 0,
33 error: null,
34 failure: null,
35 isPending: atom !== null,
36 isSuccess: false,
37 refresh: () => undefined,
38 })) as never,
39}));
40vi.mock(import("~/state/pullRequests"), async (original) => {
41 const actual = await original();
42 return {
43 ...actual,
44 useSharedPullRequestSummary: (() => known) as never,
45 pullRequestEnvironment: {
46 ...actual.pullRequestEnvironment,
47 activity: ((target: unknown) => ({ activity: target })) as never,
48 },
49 };
50});
51​
52import { initializeI18n } from "~/i18n/initializeI18n";
53import { render } from "../../../test/render";
54import { PullRequestLinkPreview } from "./PullRequestLinkPreview";
55​
56function setup(next: Partial<typeof state> = {}) {
57 Object.assign(state, { restoredPreview: null, activity: null }, next);
58 // GTProvider leaves its locale in a cookie; this sets it back to English.
59 initializeI18n(() => {});
61​
62function renderPreview() {
63 return render(
64 <PullRequestLinkPreview
65 link={<a href={known.url}>#1</a>}
66 originalUrl={known.url}
67 target={{
68 environmentId: EnvironmentId.make("environment"),
69 input: { projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 },
70 }}
71 />,
72 );
74​
75it("keeps a refreshing preview's state and title over an older known summary", async () => {
76 setup({
77 restoredPreview: {
78 projectId: "project",
79 repository: "owner/repo",
80 number: 1,
81 title: "Renamed title",
82 url: known.url,
83 author: { login: "octocat" },
84 state: "closed",
85 isDraft: false,
86 createdAt: "2026-08-01T00:00:00Z",
87 },
88 });
89 await using screen = await renderPreview();
90​
91 await userEvent.hover(screen.getByRole("link"));
92​
93 await expect.element(page.getByText("Renamed title")).toBeVisible();
94 await expect.element(page.getByText("Closed", { exact: true })).toBeVisible();
95 expect(page.getByText("Known title").query()).toBeNull();
96});
97​
98it("shows the known summary while the preview loads", async () => {
99 setup();
100 await using screen = await render(
101 <PullRequestLinkPreview
102 link={<a href={known.url}>#1</a>}
103 originalUrl={known.url}
104 target={{
105 environmentId: EnvironmentId.make("environment"),
106 input: { projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 },
107 }}
108 />,
109 );
110​
111 await userEvent.hover(screen.getByRole("link"));
112​
113 await expect.element(page.getByText("Known title")).toBeVisible();
114});
115​
116it("shows who commented on a linked comment in the active language", async () => {
117 const commentUrl = `${known.url}#issuecomment-2`;
118 setup({
119 activity: {
120 comments: [
121 {
122 id: "comment-2",
123 kind: "issue-comment",
124 author: { login: "octocat" },
125 body: "Looks good",
126 createdAt: "2026-09-01T00:00:00Z",
127 url: commentUrl,
128 path: null,
129 reviewState: null,
130 },
131 ],
132 reviewThreads: [],
133 commits: [],
134 },
135 });
136 const translations = await getTranslationsSnapshot("ja");
137 await using screen = await render(
138 <GTProvider locale="ja" translations={translations}>
139 <PullRequestLinkPreview
140 link={<a href={commentUrl}>#1</a>}
141 originalUrl={commentUrl}
142 target={{
143 environmentId: EnvironmentId.make("environment"),
144 input: { projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 },
145 }}
146 />
147 </GTProvider>,
148 );
149​
150 await userEvent.hover(screen.getByRole("link"));
151​
152 await expect.element(page.getByText(/octocat\s*がコメントしました/)).toBeVisible();
153});