Skip to content

Commit df0e214

Browse files
authored
test(react): add tests for useKatexStyles hook (CopilotKit#3397)
## Summary Follow-up to CopilotKit#3314. Adds test coverage for the `useKatexStyles` hook: 1. **Renders without error** — dynamic CSS import succeeds 2. **Graceful failure** — doesn't throw when katex CSS import fails 3. **Regression guard** — reads the component source to ensure the static `import 'katex/dist/katex.min.css'` doesn't creep back in ## Test plan - [x] `nx run @copilotkitnext/react:test -- use-katex-styles` — 3/3 pass
2 parents e2b2dd9 + 729a053 commit df0e214

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { renderHook } from "@testing-library/react";
2+
import { describe, it, expect, vi, beforeEach } from "vitest";
3+
4+
// Mock the katex CSS import globally
5+
vi.mock("katex/dist/katex.min.css", () => ({}));
6+
7+
describe("useKatexStyles", () => {
8+
beforeEach(() => {
9+
// Reset module state (the singleton `injected` flag) between tests
10+
vi.resetModules();
11+
});
12+
13+
async function loadHook() {
14+
const mod = await import("../useKatexStyles");
15+
return mod.useKatexStyles;
16+
}
17+
18+
it("renders without error (dynamic import succeeds)", async () => {
19+
const useKatexStyles = await loadHook();
20+
21+
expect(() => {
22+
renderHook(() => useKatexStyles());
23+
}).not.toThrow();
24+
});
25+
26+
it("does not throw when katex CSS import fails", async () => {
27+
vi.doMock("katex/dist/katex.min.css", () => {
28+
throw new Error("CSS not found");
29+
});
30+
31+
const useKatexStyles = await loadHook();
32+
33+
expect(() => {
34+
renderHook(() => useKatexStyles());
35+
}).not.toThrow();
36+
});
37+
38+
it("does not use a static katex CSS import in the component", async () => {
39+
// Regression guard: ensure the static import doesn't creep back.
40+
// Read the component source and verify no static katex CSS import.
41+
const fs = await import("fs");
42+
const path = await import("path");
43+
const componentPath = path.resolve(
44+
__dirname,
45+
"../../components/chat/CopilotChatAssistantMessage.tsx",
46+
);
47+
const source = fs.readFileSync(componentPath, "utf-8");
48+
49+
// Should NOT have a static import of katex CSS
50+
expect(source).not.toMatch(
51+
/^import\s+['"]katex\/dist\/katex\.min\.css['"]/m,
52+
);
53+
// Should use the hook instead
54+
expect(source).toMatch(/useKatexStyles/);
55+
});
56+
});

0 commit comments

Comments
 (0)