Open History to see the last commit
1import { HTML_RENDER_DEFAULT_FONTS, type HtmlRenderTheme } from "@t3tools/shared/htmlRender";
2import { BUILT_IN_THEME_IDS } from "@t3tools/shared/themePalettes";
3import { act, useEffect } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6
7import {
8 getStandardThemeColors,
9 setEnvironmentThemes,
10 type ThemeAppearance,
11 type ThemeHalves,
12 type ThemePreference,
13} from "../themePalette";
14import { useHtmlRenderTheme } from "./useHtmlRenderTheme";
15
16const selection = vi.hoisted(() => ({
17 theme: "ocean" as ThemePreference,
18 resolvedTheme: "dark" as ThemeAppearance,
19 themeHalves: null as ThemeHalves | null,
20}));
21
22vi.mock(import("./useTheme"), async (original) => ({
23 ...(await original()),
24 useTheme: (() => selection) as never,
25}));
26
27let root: Root | undefined;
28
29beforeEach(() => {
30 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
31 selection.themeHalves = null;
32});
33
34afterEach(() => {
35 act(() => root?.unmount());
36 root = undefined;
37 setEnvironmentThemes([]);
38 vi.unstubAllGlobals();
39});
40
41function readRenderTheme(): HtmlRenderTheme {
42 let theme: HtmlRenderTheme | undefined;
43 function Probe() {
44 const resolved = useHtmlRenderTheme();
45 useEffect(() => {
46 theme = resolved;
47 }, [resolved]);
48 return null;
49 }
50 act(() => root?.unmount());
51 root = createRoot(document.createElement("div"));
52 act(() => root!.render(<Probe />));
53 if (!theme) throw new Error("The HTML render theme was not resolved.");
54 return theme;
55}
56
57describe("useHtmlRenderTheme", () => {
58 for (const appearance of ["light", "dark"] as const) {
59 it.each([appearance, ...BUILT_IN_THEME_IDS])(
60 `injects hex colors for %s in ${appearance} mode`,
61 (preference) => {
62 selection.theme = preference;
63 selection.resolvedTheme = appearance;
64
65 const theme = readRenderTheme();
66
67 expect(theme.appearance).toBe(appearance);
68 for (const [variable, value] of Object.entries(theme.variables)) {
69 if (variable === "--radius" || variable.startsWith("--font-")) continue;
70 expect(value, variable).toMatch(/^#[\da-f]{6}(?:[\da-f]{2})?$/i);
71 }
72 expect(theme.variables["--font-sans"]).toBe(HTML_RENDER_DEFAULT_FONTS.sans);
73 expect(theme.variables["--font-mono"]).toBe(HTML_RENDER_DEFAULT_FONTS.mono);
74 expect(theme.variables["--radius"]).toBe("0.625rem");
75 },
76 );
77 }
78
79 it("preserves alpha when converting a published palette", () => {
80 setEnvironmentThemes([
81 {
82 id: "transparent",
83 label: "Transparent",
84 appearance: "dark",
85 colors: { ...getStandardThemeColors("dark"), accent: "oklch(0.5 0 0 / 0.5)" },
86 },
87 ]);
88 selection.theme = "transparent";
89 selection.resolvedTheme = "dark";
90
91 const theme = readRenderTheme();
92
93 expect(theme.variables["--accent"]).toBe("#63636380");
94 expect(theme.variables["--chart-1"]).toBe("#63636380");
95 });
96});