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

pullRequestMarkdown.logic.test.ts

46 lines1.5 KB

1import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-markdown";
2import remarkParse from "remark-parse";
3import { unified } from "unified";
4import { describe, expect, it } from "vite-plus/test";
5​
6import { remarkPullRequestAutolinks } from "./pullRequestMarkdown.logic";
7​
8type Node = {
9 type: string;
10 value?: string;
11 url?: string;
12 data?: { hProperties?: Record<string, unknown> };
13 children?: Node[];
14};
15​
16function links(markdown: string) {
17 const autolinks = changeRequestReferenceAutolinker({
18 provider: "github",
19 url: "https://github.com/team/repo/pull/8",
20 })!;
21 const tree = unified().use(remarkParse).parse(markdown) as Node;
22 remarkPullRequestAutolinks({ autolinks })(tree as never);
23 const found: Array<[string | undefined, string | undefined, unknown]> = [];
24 const visit = (node: Node) => {
25 if (node.type === "link") {
26 found.push([
27 node.children?.[0]?.value,
28 node.url,
29 node.data?.hProperties?.["dataPullRequestAutolink"],
30 ]);
31 }
32 node.children?.forEach(visit);
33 };
34 visit(tree);
35 return found;
37​
38describe("remarkPullRequestAutolinks", () => {
39 it("links the host's references in prose and leaves links and code alone", () => {
40 expect(links("Fixes #12 in **abcdef1**. [#13](https://example.com) `#14`")).toEqual([
41 ["#12", "https://github.com/team/repo/issues/12", "reference"],
42 ["abcdef1", "https://github.com/team/repo/commit/abcdef1", "commit"],
43 ["#13", "https://example.com", undefined],
44 ]);
45 });
46});