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

highlights.test.ts

46 lines1.5 KB

1import { codeToTokens } from "@pierre/highlights";
2import { pierreDark, pierreLight } from "@pierre/highlights/themes";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5import { highlightCode } from "./highlights";
6​
7vi.mock("expo", async () => {
8 const { createWasmHighlightsModule } = await import("./highlights.testing");
9 const highlights = createWasmHighlightsModule();
10 return { requireOptionalNativeModule: () => highlights };
11});
12​
13describe("highlightCode", () => {
14 it("produces @pierre/highlights' own tokens and colors", () => {
15 const code = [
16 "/** 🎉 docs\r",
17 " */",
18 "export const greet = (name: string) => <b title={`hi ${name}`}>é</b>;",
19 "const multi = `first",
20 "second`; // done",
21 "",
22 ].join("\n");
23​
24 for (const [scheme, theme] of [
25 ["light", pierreLight],
26 ["dark", pierreDark],
27 ] as const) {
28 const expected = codeToTokens(code, { lang: "tsx", theme }).tokens.map((line) =>
29 line.map((token) => ({
30 content: token.content,
31 color: token.color ?? null,
32 fontStyle: token.fontStyle ?? null,
33 })),
34 );
35 expect(highlightCode(code, "tsx", scheme)).toEqual(expected);
36 }
37 });
38​
39 it("keeps an over-long line as one plain token", () => {
40 const longLine = `const data = "${"x".repeat(1_000)}";`;
41 const [first, second] = highlightCode(`let a = 1;\n${longLine}`, "typescript", "dark");
42​
43 expect(first?.length).toBeGreaterThan(1);
44 expect(second).toEqual([{ content: longLine, color: null, fontStyle: null }]);
45 });
46});