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, createElement, useEffect } from "react";
4import { create, type ReactTestRenderer } from "react-test-renderer";
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("./useTheme", () => ({ useTheme: () => selection }));
23vi.mock("./useSettings", () => ({
24 useClientSettings: (
25 select: (settings: { fontFamilySans: string; fontFamilyCode: string }) => string,
26 ) => select({ fontFamilySans: "", fontFamilyCode: "" }),
27}));
28
29let renderer: ReactTestRenderer | undefined;
30
31beforeEach(() => {
32 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
33 selection.themeHalves = null;
34});
35
36afterEach(() => {
37 act(() => renderer?.unmount());
38 renderer = undefined;
39 setEnvironmentThemes([]);
40 vi.unstubAllGlobals();
41});
42
43function readRenderTheme(): HtmlRenderTheme {
44 let theme: HtmlRenderTheme | undefined;
45 function Probe() {
46 const resolved = useHtmlRenderTheme();
47 useEffect(() => {
48 theme = resolved;
49 }, [resolved]);
50 return null;
51 }
52 act(() => {
53 renderer = create(createElement(Probe));
54 });
55 if (!theme) throw new Error("The HTML render theme was not resolved.");
56 return theme;
57}
58
59describe("useHtmlRenderTheme", () => {
60 for (const appearance of ["light", "dark"] as const) {
61 it.each([appearance, ...BUILT_IN_THEME_IDS])(
62 `injects hex colors for %s in ${appearance} mode`,
63 (preference) => {
64 selection.theme = preference;
65 selection.resolvedTheme = appearance;
66
67 const theme = readRenderTheme();
68
69 expect(theme.appearance).toBe(appearance);
70 for (const [variable, value] of Object.entries(theme.variables)) {
71 if (variable === "--radius" || variable.startsWith("--font-")) continue;
72 expect(value, variable).toMatch(/^#[\da-f]{6}(?:[\da-f]{2})?$/i);
73 }
74 expect(theme.variables["--font-sans"]).toBe(HTML_RENDER_DEFAULT_FONTS.sans);
75 expect(theme.variables["--font-mono"]).toBe(HTML_RENDER_DEFAULT_FONTS.mono);
76 expect(theme.variables["--radius"]).toBe("0.625rem");
77 },
78 );
79 }
80
81 it("preserves alpha when converting a published palette", () => {
82 setEnvironmentThemes([
83 {
84 id: "transparent",
85 label: "Transparent",
86 appearance: "dark",
87 colors: { ...getStandardThemeColors("dark"), accent: "oklch(0.5 0 0 / 0.5)" },
88 },
89 ]);
90 selection.theme = "transparent";
91 selection.resolvedTheme = "dark";
92
93 const theme = readRenderTheme();
94
95 expect(theme.variables["--accent"]).toBe("#63636380");
96 expect(theme.variables["--chart-1"]).toBe("#63636380");
97 });
98});