File size: 1,828 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { render, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ThemeProvider } from "../../contexts/ThemeContext";
import { MermaidCodeBlock } from "./MermaidCodeBlock";

const mermaidMocks = vi.hoisted(() => ({
  initialize: vi.fn(),
  render: vi.fn().mockResolvedValue({ svg: "<svg>diagram</svg>" }),
}));

vi.mock("mermaid", () => ({
  default: mermaidMocks,
}));

describe("MermaidCodeBlock", () => {
  beforeEach(() => {
    localStorage.clear();
    document.documentElement.classList.remove("dark-mode");
    mermaidMocks.initialize.mockClear();
    mermaidMocks.render.mockClear();
  });

  it("uses readable QwenPaw colors for light and dark themes", async () => {
    localStorage.setItem("qwenpaw-theme", "light");
    const light = render(
      <ThemeProvider>
        <MermaidCodeBlock chart="graph TD; A-->B" />
      </ThemeProvider>,
    );

    await waitFor(() => expect(mermaidMocks.initialize).toHaveBeenCalled());
    expect(mermaidMocks.initialize).toHaveBeenLastCalledWith(
      expect.objectContaining({
        theme: "base",
        themeVariables: expect.objectContaining({
          primaryColor: "#fff7f0",
          primaryTextColor: "#3b2416",
        }),
      }),
    );
    light.unmount();

    localStorage.setItem("qwenpaw-theme", "dark");
    render(
      <ThemeProvider>
        <MermaidCodeBlock chart="graph TD; A-->B" />
      </ThemeProvider>,
    );

    await waitFor(() =>
      expect(mermaidMocks.initialize).toHaveBeenLastCalledWith(
        expect.objectContaining({
          theme: "base",
          themeVariables: expect.objectContaining({
            lineColor: "#d19a78",
            primaryColor: "#2b211c",
            primaryTextColor: "#fff4ec",
          }),
        }),
      ),
    );
  });
});