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

PullRequestMarkdown.browser.test.tsx

89 lines3.0 KB

1import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2import { type AssetResource, EnvironmentId } from "@t3tools/contracts";
3import { expect, it, vi } from "vite-plus/test";
4​
5import { render } from "../../../test/render";
6import { PullRequestMarkdown, PullRequestMarkdownContext } from "./PullRequestMarkdown";
7​
8const requested = vi.hoisted(() => [] as Array<AssetResource | null>);
9vi.mock(import("~/assets/assetUrls"), async (original) => ({
10 ...(await original()),
11 useAssetUrlState: (_environmentId, resource) => {
12 requested.push(resource);
13 return { _tag: "Loading" };
14 },
15}));
16​
17function setup() {
18 requested.length = 0;
20​
21async function renderBody(
22 host: string,
23 options: { lineBreaks?: boolean; repository?: string; text?: string } = {},
24) {
25 const { repository = null, text = "first line\nsecond line", ...props } = options;
26 const context = {
27 remarkPlugins: [],
28 lineBreaks: sourceControlClients.get(host).hardLineBreaks ?? false,
29 threadRef: null,
30 repository,
31 };
32 return render(
33 <PullRequestMarkdownContext value={context}>
34 <PullRequestMarkdown
35 text={text}
36 cwd="/repo"
37 environmentId={EnvironmentId.make("local")}
38 {...props}
39 />
40 </PullRequestMarkdownContext>,
41 );
43​
44it.each(["github", "forgejo", "azure-devops"])("%s breaks a single newline", async (host) => {
45 await using screen = await renderBody(host);
46 await expect.element(screen.getByText(/first line/)).toBeInTheDocument();
47 expect(screen.container.querySelectorAll("p br")).toHaveLength(1);
48});
49​
50it.each(["gitlab", "bitbucket", "gitcafe", "unknown"])(
51 "%s joins a single newline",
52 async (host) => {
53 await using screen = await renderBody(host);
54 await expect.element(screen.getByText(/first line/)).toBeInTheDocument();
55 expect(screen.container.querySelectorAll("p br")).toHaveLength(0);
56 },
57);
58​
59it("lets text the host did not write opt out of the host's breaks", async () => {
60 await using screen = await renderBody("github", { lineBreaks: false });
61 await expect.element(screen.getByText(/first line/)).toBeInTheDocument();
62 expect(screen.container.querySelectorAll("p br")).toHaveLength(0);
63});
64​
65it("loads the body's host media in the pull request's repository, not the checkout's", async () => {
66 setup();
67 const image = "https://github.com/user-attachments/assets/7195f963-51a9-4331-be74-3e06be760422";
68 const video = "https://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456";
69 await using _screen = await renderBody("github", {
70 repository: "enterprise/private",
71 text: `![shot](${image})\n\n${video}`,
72 });
73 await vi.waitFor(() =>
74 expect(requested).toEqual(
75 expect.arrayContaining([
76 expect.objectContaining({
77 cwd: "/repo",
78 url: expect.stringContaining("7195f963"),
79 repository: "enterprise/private",
80 }),
81 expect.objectContaining({
82 cwd: "/repo",
83 url: expect.stringContaining("2f8c1a90"),
84 repository: "enterprise/private",
85 }),
86 ]),
87 ),
88 );
89});