|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { act, fireEvent, render, waitFor } from "@testing-library/react" |
|
4
|
|
|
5
|
// mock components |
|
6
|
vi.mock("@/components/Loading", () => ({ |
|
7
|
Loading: () => <div data-testid="loading">Loading...</div>, |
|
8
|
})) |
|
9
|
|
|
10
|
vi.mock("react-medium-image-zoom", () => ({ |
|
11
|
Controlled: ({ |
|
12
|
children, |
|
13
|
isZoomed, |
|
14
|
onZoomChange, |
|
15
|
classDialog, |
|
16
|
}: { |
|
17
|
children: React.ReactNode |
|
18
|
isZoomed: boolean |
|
19
|
onZoomChange: (shouldZoom: boolean) => void |
|
20
|
classDialog?: string |
|
21
|
}) => ( |
|
22
|
<div |
|
23
|
data-testid="controlled-zoom" |
|
24
|
data-is-zoomed={isZoomed} |
|
25
|
data-class-dialog={classDialog} |
|
26
|
onClick={() => onZoomChange(!isZoomed)} |
|
27
|
> |
|
28
|
{children} |
|
29
|
</div> |
|
30
|
), |
|
31
|
})) |
|
32
|
|
|
33
|
// Mock Suspense to avoid act warnings - render children directly |
|
34
|
vi.mock("react", async () => { |
|
35
|
const actual = await vi.importActual<typeof React>("react") |
|
36
|
return { |
|
37
|
...actual, |
|
38
|
Suspense: ({ children }: { children: React.ReactNode }) => <>{children}</>, |
|
39
|
} |
|
40
|
}) |
|
41
|
|
|
42
|
// mock mermaid |
|
43
|
const mockRender = vi.fn() |
|
44
|
const mockInitialize = vi.fn() |
|
45
|
const mockRegisterIconPacks = vi.fn() |
|
46
|
|
|
47
|
vi.mock("mermaid", () => ({ |
|
48
|
default: { |
|
49
|
initialize: () => mockInitialize(), |
|
50
|
registerIconPacks: (packs: unknown) => mockRegisterIconPacks(packs), |
|
51
|
render: (id: string, content: string) => mockRender(id, content), |
|
52
|
}, |
|
53
|
})) |
|
54
|
|
|
55
|
import { MermaidDiagram } from "../../MermaidDiagram" |
|
56
|
|
|
57
|
beforeEach(() => { |
|
58
|
vi.clearAllMocks() |
|
59
|
mockRender.mockResolvedValue({ |
|
60
|
svg: `<svg viewBox="0 0 100 200"><rect width="100" height="200" /></svg>`, |
|
61
|
}) |
|
62
|
}) |
|
63
|
|
|
64
|
describe("rendering", () => { |
|
65
|
test("renders mermaid diagram container", async () => { |
|
66
|
const { container } = render( |
|
67
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
68
|
) |
|
69
|
const zoom = container.querySelector("[data-testid='controlled-zoom']") |
|
70
|
expect(zoom).toBeInTheDocument() |
|
71
|
}) |
|
72
|
|
|
73
|
test("initializes mermaid API", async () => { |
|
74
|
render(<MermaidDiagram diagramContent="graph TD; A-->B" />) |
|
75
|
await waitFor(() => { |
|
76
|
expect(mockInitialize).toHaveBeenCalledTimes(1) |
|
77
|
}) |
|
78
|
}) |
|
79
|
|
|
80
|
test("registers the Medusa icon pack", async () => { |
|
81
|
render(<MermaidDiagram diagramContent="graph TD; A-->B" />) |
|
82
|
await waitFor(() => { |
|
83
|
expect(mockRegisterIconPacks).toHaveBeenCalledWith([ |
|
84
|
expect.objectContaining({ name: "medusa" }), |
|
85
|
]) |
|
86
|
}) |
|
87
|
}) |
|
88
|
|
|
89
|
test("calls mermaid.render with diagram content", async () => { |
|
90
|
const diagramContent = "graph TD; A-->B" |
|
91
|
render(<MermaidDiagram diagramContent={diagramContent} />) |
|
92
|
await waitFor(() => { |
|
93
|
expect(mockRender).toHaveBeenCalled() |
|
94
|
}) |
|
95
|
expect(mockRender).toHaveBeenCalledWith( |
|
96
|
expect.stringContaining("mermaid-svg-"), |
|
97
|
diagramContent |
|
98
|
) |
|
99
|
}) |
|
100
|
|
|
101
|
test("renders SVG when result is available", async () => { |
|
102
|
const { container } = render( |
|
103
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
104
|
) |
|
105
|
await waitFor(() => { |
|
106
|
const svg = container.querySelector("svg") |
|
107
|
expect(svg).toBeInTheDocument() |
|
108
|
}) |
|
109
|
}) |
|
110
|
|
|
111
|
test("renders SVG with correct content", async () => { |
|
112
|
const mockSvg = `<svg viewBox="0 0 100 200"><rect width="100" height="200" /></svg>` |
|
113
|
mockRender.mockResolvedValue({ |
|
114
|
svg: mockSvg, |
|
115
|
}) |
|
116
|
const { container } = render( |
|
117
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
118
|
) |
|
119
|
await waitFor(() => { |
|
120
|
// Query the inner SVG (the mermaid content) |
|
121
|
const svgs = container.querySelectorAll("svg") |
|
122
|
expect(svgs.length).toBeGreaterThan(0) |
|
123
|
// The outer SVG contains the mermaid SVG as innerHTML |
|
124
|
const outerSvg = svgs[0] |
|
125
|
expect(outerSvg).toBeInTheDocument() |
|
126
|
// Check that the inner SVG content is present (browser normalizes self-closing tags) |
|
127
|
expect(outerSvg.innerHTML).toContain(`viewBox="0 0 100 200"`) |
|
128
|
expect(outerSvg.innerHTML).toContain("<rect") |
|
129
|
expect(outerSvg.innerHTML).toContain(`width="100"`) |
|
130
|
expect(outerSvg.innerHTML).toContain(`height="200"`) |
|
131
|
}) |
|
132
|
}) |
|
133
|
|
|
134
|
test("renders the diagram in a 16:9 container when not zoomed", async () => { |
|
135
|
const { container } = render( |
|
136
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
137
|
) |
|
138
|
await waitFor(() => { |
|
139
|
const wrapper = container.querySelector("svg")?.parentElement |
|
140
|
expect(wrapper).toHaveClass("aspect-video") |
|
141
|
expect(wrapper).toHaveClass("items-center") |
|
142
|
expect(wrapper).toHaveClass("justify-center") |
|
143
|
}) |
|
144
|
}) |
|
145
|
|
|
146
|
test("renders SVG with the diagram's viewBox and width", async () => { |
|
147
|
const mockSvg = `<svg viewBox="0 0 100 200"><rect width="100" height="200" /></svg>` |
|
148
|
mockRender.mockResolvedValue({ |
|
149
|
svg: mockSvg, |
|
150
|
}) |
|
151
|
const { container } = render( |
|
152
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
153
|
) |
|
154
|
await waitFor(() => { |
|
155
|
const svg = container.querySelector("svg") |
|
156
|
expect(svg).toHaveAttribute("viewBox", "0 0 100 200") |
|
157
|
expect(svg).toHaveAttribute("preserveAspectRatio", "xMidYMid meet") |
|
158
|
expect(svg).toHaveStyle({ width: "100px" }) |
|
159
|
}) |
|
160
|
}) |
|
161
|
|
|
162
|
test("renders SVG with a full width when viewBox is not available", async () => { |
|
163
|
const mockSvg = `<svg><rect width="100" height="200" /></svg>` |
|
164
|
mockRender.mockResolvedValue({ |
|
165
|
svg: mockSvg, |
|
166
|
}) |
|
167
|
const { container } = render( |
|
168
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
169
|
) |
|
170
|
await waitFor(() => { |
|
171
|
const svg = container.querySelector("svg") |
|
172
|
expect(svg).not.toHaveAttribute("viewBox") |
|
173
|
expect(svg).toHaveStyle({ width: "100%" }) |
|
174
|
}) |
|
175
|
}) |
|
176
|
}) |
|
177
|
|
|
178
|
describe("zoom functionality", () => { |
|
179
|
test("fills the viewport when zoomed", async () => { |
|
180
|
const { container } = render( |
|
181
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
182
|
) |
|
183
|
await waitFor(() => { |
|
184
|
const svg = container.querySelector("svg") |
|
185
|
expect(svg).toBeInTheDocument() |
|
186
|
}) |
|
187
|
|
|
188
|
const zoom = container.querySelector("[data-testid='controlled-zoom']") |
|
189
|
await act(async () => { |
|
190
|
fireEvent.click(zoom!) |
|
191
|
}) |
|
192
|
|
|
193
|
await waitFor(() => { |
|
194
|
const wrapper = container.querySelector("svg")?.parentElement |
|
195
|
expect(wrapper).toHaveClass("h-screen") |
|
196
|
expect(wrapper).toHaveClass("w-screen") |
|
197
|
expect(wrapper).not.toHaveClass("aspect-video") |
|
198
|
}) |
|
199
|
}) |
|
200
|
|
|
201
|
test("updates zoom state when onZoomChange is called", async () => { |
|
202
|
const { container } = render( |
|
203
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
204
|
) |
|
205
|
await waitFor(() => { |
|
206
|
const zoom = container.querySelector("[data-testid='controlled-zoom']") |
|
207
|
expect(zoom).toHaveAttribute("data-is-zoomed", "false") |
|
208
|
}) |
|
209
|
|
|
210
|
const zoom = container.querySelector("[data-testid='controlled-zoom']") |
|
211
|
await act(async () => { |
|
212
|
fireEvent.click(zoom!) |
|
213
|
}) |
|
214
|
|
|
215
|
await waitFor(() => { |
|
216
|
const updatedZoom = container.querySelector( |
|
217
|
"[data-testid='controlled-zoom']" |
|
218
|
) |
|
219
|
expect(updatedZoom).toHaveAttribute("data-is-zoomed", "true") |
|
220
|
}) |
|
221
|
}) |
|
222
|
}) |
|
223
|
|
|
224
|
describe("error handling", () => { |
|
225
|
test("handles mermaid render error gracefully", async () => { |
|
226
|
const consoleErrorSpy = vi |
|
227
|
.spyOn(console, "error") |
|
228
|
.mockImplementation(() => {}) |
|
229
|
const error = new Error("Mermaid render failed") |
|
230
|
mockRender.mockRejectedValue(error) |
|
231
|
|
|
232
|
const diagramContent = "invalid mermaid syntax" |
|
233
|
render(<MermaidDiagram diagramContent={diagramContent} />) |
|
234
|
|
|
235
|
await waitFor(() => { |
|
236
|
expect(consoleErrorSpy).toHaveBeenCalledWith( |
|
237
|
expect.stringContaining( |
|
238
|
"An error occurred while rendering Mermaid.js diagram" |
|
239
|
) |
|
240
|
) |
|
241
|
}) |
|
242
|
|
|
243
|
consoleErrorSpy.mockRestore() |
|
244
|
}) |
|
245
|
|
|
246
|
test("does not render SVG when render fails", async () => { |
|
247
|
mockRender.mockRejectedValue(new Error("Render failed")) |
|
248
|
const { container } = render( |
|
249
|
<MermaidDiagram diagramContent="invalid syntax" /> |
|
250
|
) |
|
251
|
|
|
252
|
await waitFor(() => { |
|
253
|
const svg = container.querySelector("svg") |
|
254
|
expect(svg).toBeInTheDocument() |
|
255
|
expect(svg?.innerHTML).toBe("") |
|
256
|
}) |
|
257
|
}) |
|
258
|
}) |
|
259
|
|
|
260
|
describe("viewBox regex matching", () => { |
|
261
|
test("extracts viewBox values correctly", async () => { |
|
262
|
const mockSvg = `<svg viewBox="0 0 300 400"><rect /></svg>` |
|
263
|
mockRender.mockResolvedValue({ |
|
264
|
svg: mockSvg, |
|
265
|
}) |
|
266
|
const { container } = render( |
|
267
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
268
|
) |
|
269
|
await waitFor(() => { |
|
270
|
const svg = container.querySelector("svg") |
|
271
|
expect(svg).toBeInTheDocument() |
|
272
|
expect(svg).toHaveAttribute("viewBox", "0 0 300 400") |
|
273
|
}) |
|
274
|
}) |
|
275
|
|
|
276
|
test("handles viewBox with negative values", async () => { |
|
277
|
const mockSvg = `<svg viewBox="-10 -20 300 400"><rect /></svg>` |
|
278
|
mockRender.mockResolvedValue({ |
|
279
|
svg: mockSvg, |
|
280
|
}) |
|
281
|
const { container } = render( |
|
282
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
283
|
) |
|
284
|
await waitFor(() => { |
|
285
|
const svg = container.querySelector("svg") |
|
286
|
expect(svg).toBeInTheDocument() |
|
287
|
expect(svg).toHaveAttribute("viewBox", "-10 -20 300 400") |
|
288
|
}) |
|
289
|
}) |
|
290
|
}) |
|
291
|
|
|
292
|
describe("diagram content changes", () => { |
|
293
|
test("re-renders when diagramContent changes", async () => { |
|
294
|
const { rerender } = render( |
|
295
|
<MermaidDiagram diagramContent="graph TD; A-->B" /> |
|
296
|
) |
|
297
|
|
|
298
|
await waitFor(() => { |
|
299
|
expect(mockRender).toHaveBeenCalledTimes(1) |
|
300
|
}) |
|
301
|
|
|
302
|
rerender(<MermaidDiagram diagramContent="graph LR; C-->D" />) |
|
303
|
|
|
304
|
await waitFor(() => { |
|
305
|
expect(mockRender).toHaveBeenCalledTimes(2) |
|
306
|
}) |
|
307
|
}) |
|
308
|
}) |