11import { render } from "@testing-library/react" ;
2- import { describe , expect , it , vi } from "vitest" ;
2+ import { beforeEach , describe , expect , it , vi } from "vitest" ;
3+ import type { MermaidConfig } from "mermaid" ;
34import { Mermaid } from "../lib/mermaid" ;
45
56// Mock mermaid
7+ const mockInitialize = vi . fn ( ) ;
8+ const mockRender = vi . fn ( ) . mockResolvedValue ( { svg : "<svg>Test SVG</svg>" } ) ;
9+
610vi . mock ( "mermaid" , ( ) => ( {
711 default : {
8- initialize : vi . fn ( ) ,
9- render : vi . fn ( ) ,
12+ initialize : mockInitialize ,
13+ render : mockRender ,
1014 } ,
1115} ) ) ;
1216
1317describe ( "Mermaid" , ( ) => {
18+ beforeEach ( ( ) => {
19+ // Clear mock calls before each test
20+ mockInitialize . mockClear ( ) ;
21+ mockRender . mockClear ( ) ;
22+ } ) ;
23+
1424 it ( "renders without crashing" , ( ) => {
1525 const { container } = render ( < Mermaid chart = "graph TD; A-->B" /> ) ;
1626 expect ( container . firstChild ) . toBeDefined ( ) ;
@@ -24,4 +34,98 @@ describe("Mermaid", () => {
2434 const mermaidContainer = container . firstChild as HTMLElement ;
2535 expect ( mermaidContainer . className ) . toContain ( "custom-class" ) ;
2636 } ) ;
37+
38+ it ( "initializes with custom config" , async ( ) => {
39+ const customConfig : MermaidConfig = {
40+ theme : "dark" ,
41+ themeVariables : {
42+ primaryColor : "#ff0000" ,
43+ primaryTextColor : "#ffffff" ,
44+ } ,
45+ fontFamily : "Arial, sans-serif" ,
46+ } as MermaidConfig ;
47+
48+ render ( < Mermaid chart = "graph TD; A-->B" config = { customConfig } /> ) ;
49+
50+ // Wait for initialization
51+ await vi . waitFor ( ( ) => {
52+ expect ( mockInitialize ) . toHaveBeenCalled ( ) ;
53+ } ) ;
54+
55+ // Check that initialize was called with the custom config
56+ const initializeCall = mockInitialize . mock . calls [ 0 ] [ 0 ] ;
57+ expect ( initializeCall . theme ) . toBe ( "dark" ) ;
58+ expect ( initializeCall . themeVariables ?. primaryColor ) . toBe ( "#ff0000" ) ;
59+ expect ( initializeCall . fontFamily ) . toBe ( "Arial, sans-serif" ) ;
60+ } ) ;
61+
62+ it ( "initializes with default config when none provided" , async ( ) => {
63+ render ( < Mermaid chart = "graph TD; A-->B" /> ) ;
64+
65+ // Wait for initialization
66+ await vi . waitFor ( ( ) => {
67+ expect ( mockInitialize ) . toHaveBeenCalled ( ) ;
68+ } ) ;
69+
70+ // Check that initialize was called with default config
71+ const initializeCall = mockInitialize . mock . calls [ 0 ] [ 0 ] ;
72+ expect ( initializeCall . theme ) . toBe ( "default" ) ;
73+ expect ( initializeCall . securityLevel ) . toBe ( "strict" ) ;
74+ expect ( initializeCall . fontFamily ) . toBe ( "monospace" ) ;
75+ } ) ;
76+
77+ it ( "accepts different config values" , ( ) => {
78+ const config1 : MermaidConfig = {
79+ theme : "forest" ,
80+ } as MermaidConfig ;
81+
82+ const { rerender } = render ( < Mermaid chart = "graph TD; A-->B" config = { config1 } /> ) ;
83+
84+ // Should render without error
85+ expect ( mockRender ) . toBeDefined ( ) ;
86+
87+ const config2 : MermaidConfig = {
88+ theme : "dark" ,
89+ fontFamily : "Arial" ,
90+ } as MermaidConfig ;
91+
92+ // Should be able to rerender with different config
93+ rerender ( < Mermaid chart = "graph TD; A-->B" config = { config2 } /> ) ;
94+
95+ // Should still render without error
96+ expect ( mockRender ) . toBeDefined ( ) ;
97+ } ) ;
98+
99+ it ( "handles complex config objects with functions" , ( ) => {
100+ const config : MermaidConfig = {
101+ theme : "dark" ,
102+ themeVariables : {
103+ primaryColor : "#ff0000" ,
104+ primaryTextColor : "#ffffff" ,
105+ } ,
106+ fontFamily : "Arial" ,
107+ } as MermaidConfig ;
108+
109+ const { container } = render ( < Mermaid chart = "graph TD; A-->B" config = { config } /> ) ;
110+
111+ // Should render without error even with complex config
112+ expect ( container . firstChild ) . toBeTruthy ( ) ;
113+ } ) ;
114+
115+ it ( "supports multiple components with different configs" , async ( ) => {
116+ const config1 : MermaidConfig = { theme : "forest" } as MermaidConfig ;
117+ const config2 : MermaidConfig = { theme : "dark" } as MermaidConfig ;
118+
119+ // Render first component
120+ const { rerender } = render ( < Mermaid chart = "graph TD; A-->B" config = { config1 } /> ) ;
121+
122+ await vi . waitFor ( ( ) => expect ( mockInitialize ) . toHaveBeenCalledTimes ( 1 ) ) ;
123+ expect ( mockInitialize . mock . calls [ 0 ] [ 0 ] . theme ) . toBe ( "forest" ) ;
124+
125+ // Render second component with different config
126+ rerender ( < Mermaid chart = "graph TD; X-->Y" config = { config2 } /> ) ;
127+
128+ await vi . waitFor ( ( ) => expect ( mockInitialize ) . toHaveBeenCalledTimes ( 2 ) ) ;
129+ expect ( mockInitialize . mock . calls [ 1 ] [ 0 ] . theme ) . toBe ( "dark" ) ;
130+ } ) ;
27131} ) ;
0 commit comments