|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { fireEvent, render } from "@testing-library/react" |
|
4
|
import { CollapsibleProps } from "../../../hooks/use-collapsible" |
|
5
|
|
|
6
|
// mock hooks |
|
7
|
const mockSetCollapsed = vi.fn() |
|
8
|
const mockGetCollapsibleElms = vi.fn( |
|
9
|
(children: React.ReactNode): React.ReactNode => children |
|
10
|
) |
|
11
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
|
12
|
const mockUseCollapsible = vi.fn((_options: CollapsibleProps) => ({ |
|
13
|
getCollapsibleElms: mockGetCollapsibleElms, |
|
14
|
setCollapsed: mockSetCollapsed, |
|
15
|
})) |
|
16
|
|
|
17
|
// mock components |
|
18
|
vi.mock("@/hooks/use-collapsible", () => ({ |
|
19
|
useCollapsible: (options: CollapsibleProps) => mockUseCollapsible(options), |
|
20
|
})) |
|
21
|
|
|
22
|
vi.mock("@/components/Loading", () => ({ |
|
23
|
Loading: ({ className }: { className?: string }) => ( |
|
24
|
<div data-testid="loading" className={className}> |
|
25
|
Loading... |
|
26
|
</div> |
|
27
|
), |
|
28
|
})) |
|
29
|
|
|
30
|
vi.mock("@/components/Details/Summary", () => ({ |
|
31
|
DetailsSummary: ({ title, onClick }: DetailsSummaryProps) => ( |
|
32
|
<summary data-testid="details-summary-title" onClick={onClick}> |
|
33
|
{title} |
|
34
|
</summary> |
|
35
|
), |
|
36
|
})) |
|
37
|
|
|
38
|
// Mock Suspense to avoid act warnings - render children directly |
|
39
|
vi.mock("react", async () => { |
|
40
|
const actual = await vi.importActual<typeof React>("react") |
|
41
|
return { |
|
42
|
...actual, |
|
43
|
Suspense: ({ children }: { children: React.ReactNode }) => <>{children}</>, |
|
44
|
} |
|
45
|
}) |
|
46
|
|
|
47
|
import { Details } from "../../Details" |
|
48
|
import { DetailsSummary, DetailsSummaryProps } from "../Summary" |
|
49
|
|
|
50
|
beforeEach(() => { |
|
51
|
mockUseCollapsible.mockClear() |
|
52
|
mockUseCollapsible.mockReturnValue({ |
|
53
|
getCollapsibleElms: mockGetCollapsibleElms, |
|
54
|
setCollapsed: mockSetCollapsed, |
|
55
|
}) |
|
56
|
mockSetCollapsed.mockClear() |
|
57
|
mockGetCollapsibleElms.mockImplementation( |
|
58
|
(children: React.ReactNode): React.ReactNode => children |
|
59
|
) |
|
60
|
}) |
|
61
|
|
|
62
|
describe("rendering", () => { |
|
63
|
test("renders details element", () => { |
|
64
|
const { container } = render( |
|
65
|
<Details> |
|
66
|
<div>Content</div> |
|
67
|
</Details> |
|
68
|
) |
|
69
|
const details = container.querySelector("details") |
|
70
|
expect(details).toBeInTheDocument() |
|
71
|
expect(details).toHaveTextContent("Content") |
|
72
|
}) |
|
73
|
|
|
74
|
test("renders with summaryContent", () => { |
|
75
|
const { container } = render( |
|
76
|
<Details summaryContent="Summary Title"> |
|
77
|
<div>Content</div> |
|
78
|
</Details> |
|
79
|
) |
|
80
|
const summary = container.querySelector("summary") |
|
81
|
expect(summary).toBeInTheDocument() |
|
82
|
const title = container.querySelector( |
|
83
|
"[data-testid='details-summary-title']" |
|
84
|
) |
|
85
|
expect(title).toBeInTheDocument() |
|
86
|
expect(title).toHaveTextContent("Summary Title") |
|
87
|
}) |
|
88
|
|
|
89
|
test("renders with summaryElm", () => { |
|
90
|
const summaryElm = <DetailsSummary title="Custom Summary" /> |
|
91
|
const { container } = render( |
|
92
|
<Details summaryElm={summaryElm}> |
|
93
|
<div>Content</div> |
|
94
|
</Details> |
|
95
|
) |
|
96
|
const summary = container.querySelector("summary") |
|
97
|
expect(summary).toBeInTheDocument() |
|
98
|
const title = container.querySelector( |
|
99
|
"[data-testid='details-summary-title']" |
|
100
|
) |
|
101
|
expect(title).toBeInTheDocument() |
|
102
|
expect(title).toHaveTextContent("Custom Summary") |
|
103
|
}) |
|
104
|
|
|
105
|
test("renders with className", () => { |
|
106
|
const { container } = render( |
|
107
|
<Details className="custom-class"> |
|
108
|
<div>Content</div> |
|
109
|
</Details> |
|
110
|
) |
|
111
|
const details = container.querySelector("details") |
|
112
|
expect(details).toHaveClass("custom-class") |
|
113
|
}) |
|
114
|
}) |
|
115
|
|
|
116
|
describe("initial state", () => { |
|
117
|
test("renders closed by default when openInitial is false", () => { |
|
118
|
const { container } = render( |
|
119
|
<Details openInitial={false}> |
|
120
|
<div>Content</div> |
|
121
|
</Details> |
|
122
|
) |
|
123
|
const details = container.querySelector("details") |
|
124
|
expect(details).not.toHaveAttribute("open") |
|
125
|
}) |
|
126
|
|
|
127
|
test("renders open when openInitial is true", () => { |
|
128
|
const { container } = render( |
|
129
|
<Details openInitial={true}> |
|
130
|
<div>Content</div> |
|
131
|
</Details> |
|
132
|
) |
|
133
|
const details = container.querySelector("details") |
|
134
|
expect(details).toHaveAttribute("open") |
|
135
|
}) |
|
136
|
|
|
137
|
test("passes correct initialValue to useCollapsible when openInitial is false", () => { |
|
138
|
render( |
|
139
|
<Details openInitial={false}> |
|
140
|
<div>Content</div> |
|
141
|
</Details> |
|
142
|
) |
|
143
|
expect(mockUseCollapsible).toHaveBeenCalledWith( |
|
144
|
expect.objectContaining({ |
|
145
|
initialValue: true, // !openInitial = !false = true |
|
146
|
}) |
|
147
|
) |
|
148
|
}) |
|
149
|
|
|
150
|
test("passes correct initialValue to useCollapsible when openInitial is true", () => { |
|
151
|
render( |
|
152
|
<Details openInitial={true}> |
|
153
|
<div>Content</div> |
|
154
|
</Details> |
|
155
|
) |
|
156
|
expect(mockUseCollapsible).toHaveBeenCalledWith( |
|
157
|
expect.objectContaining({ |
|
158
|
initialValue: false, // !openInitial = !true = false |
|
159
|
}) |
|
160
|
) |
|
161
|
}) |
|
162
|
}) |
|
163
|
|
|
164
|
describe("height animation", () => { |
|
165
|
test("passes heightAnimation to useCollapsible when true", () => { |
|
166
|
render( |
|
167
|
<Details heightAnimation={true}> |
|
168
|
<div>Content</div> |
|
169
|
</Details> |
|
170
|
) |
|
171
|
expect(mockUseCollapsible).toHaveBeenCalledWith( |
|
172
|
expect.objectContaining({ |
|
173
|
heightAnimation: true, |
|
174
|
}) |
|
175
|
) |
|
176
|
}) |
|
177
|
|
|
178
|
test("passes heightAnimation to useCollapsible when false", () => { |
|
179
|
render( |
|
180
|
<Details heightAnimation={false}> |
|
181
|
<div>Content</div> |
|
182
|
</Details> |
|
183
|
) |
|
184
|
expect(mockUseCollapsible).toHaveBeenCalledWith( |
|
185
|
expect.objectContaining({ |
|
186
|
heightAnimation: false, |
|
187
|
}) |
|
188
|
) |
|
189
|
}) |
|
190
|
}) |
|
191
|
|
|
192
|
describe("interactions", () => { |
|
193
|
test("toggles open state when summary is clicked", () => { |
|
194
|
const { container } = render( |
|
195
|
<Details summaryContent="Summary"> |
|
196
|
<div>Content</div> |
|
197
|
</Details> |
|
198
|
) |
|
199
|
const details = container.querySelector("details") |
|
200
|
const summary = container.querySelector("summary") |
|
201
|
expect(details).not.toHaveAttribute("open") |
|
202
|
|
|
203
|
fireEvent.click(summary!) |
|
204
|
expect(details).toHaveAttribute("open") |
|
205
|
expect(mockSetCollapsed).toHaveBeenCalledWith(false) |
|
206
|
}) |
|
207
|
|
|
208
|
test("closes when summary is clicked and already open", () => { |
|
209
|
mockUseCollapsible.mockReturnValue({ |
|
210
|
getCollapsibleElms: mockGetCollapsibleElms, |
|
211
|
setCollapsed: mockSetCollapsed, |
|
212
|
}) |
|
213
|
const { container } = render( |
|
214
|
<Details summaryContent="Summary" openInitial={true}> |
|
215
|
<div>Content</div> |
|
216
|
</Details> |
|
217
|
) |
|
218
|
const details = container.querySelector("details") |
|
219
|
const summary = container.querySelector("summary") |
|
220
|
expect(details).toHaveAttribute("open") |
|
221
|
|
|
222
|
fireEvent.click(summary!) |
|
223
|
expect(mockSetCollapsed).toHaveBeenCalledWith(true) |
|
224
|
}) |
|
225
|
|
|
226
|
test("prevents default on details click", () => { |
|
227
|
const { container } = render( |
|
228
|
<Details summaryContent="Summary"> |
|
229
|
<div>Content</div> |
|
230
|
</Details> |
|
231
|
) |
|
232
|
const details = container.querySelector("details") |
|
233
|
const clickEvent = new MouseEvent("click", { |
|
234
|
bubbles: true, |
|
235
|
cancelable: true, |
|
236
|
}) |
|
237
|
const preventDefaultSpy = vi.spyOn(clickEvent, "preventDefault") |
|
238
|
|
|
239
|
fireEvent(details!, clickEvent) |
|
240
|
expect(preventDefaultSpy).toHaveBeenCalled() |
|
241
|
}) |
|
242
|
|
|
243
|
test("stops propagation on toggle event", () => { |
|
244
|
const { container } = render( |
|
245
|
<Details summaryContent="Summary"> |
|
246
|
<div>Content</div> |
|
247
|
</Details> |
|
248
|
) |
|
249
|
const details = container.querySelector("details") |
|
250
|
const toggleEvent = new Event("toggle", { bubbles: true, cancelable: true }) |
|
251
|
const stopPropagationSpy = vi.spyOn(toggleEvent, "stopPropagation") |
|
252
|
|
|
253
|
fireEvent(details!, toggleEvent) |
|
254
|
expect(stopPropagationSpy).toHaveBeenCalled() |
|
255
|
}) |
|
256
|
|
|
257
|
test("navigates to link when link is clicked in summary", () => { |
|
258
|
const originalLocation = window.location |
|
259
|
delete (window as unknown as { location?: Location }).location |
|
260
|
Object.defineProperty(window, "location", { |
|
261
|
value: { href: "http://localhost" }, |
|
262
|
writable: true, |
|
263
|
configurable: true, |
|
264
|
}) |
|
265
|
|
|
266
|
const { container } = render( |
|
267
|
<Details summaryContent={<a href="/test">Link</a>}> |
|
268
|
<div>Content</div> |
|
269
|
</Details> |
|
270
|
) |
|
271
|
const link = container.querySelector("a") |
|
272
|
expect(link).toBeInTheDocument() |
|
273
|
|
|
274
|
fireEvent.click(link!) |
|
275
|
expect(window.location.href).toBe("/test") |
|
276
|
|
|
277
|
Object.defineProperty(window, "location", { |
|
278
|
value: originalLocation, |
|
279
|
writable: true, |
|
280
|
configurable: true, |
|
281
|
}) |
|
282
|
}) |
|
283
|
|
|
284
|
test("does not toggle when code element is clicked", () => { |
|
285
|
const { container } = render( |
|
286
|
<Details summaryContent={<code>Code</code>}> |
|
287
|
<div>Content</div> |
|
288
|
</Details> |
|
289
|
) |
|
290
|
const details = container.querySelector("details") |
|
291
|
const code = container.querySelector("code") |
|
292
|
expect(details).not.toHaveAttribute("open") |
|
293
|
|
|
294
|
mockSetCollapsed.mockClear() |
|
295
|
fireEvent.click(code!) |
|
296
|
// Should not toggle when code is clicked |
|
297
|
expect(mockSetCollapsed).not.toHaveBeenCalled() |
|
298
|
}) |
|
299
|
}) |
|
300
|
|
|
301
|
describe("summary element cloning", () => { |
|
302
|
test("clones summaryElm with open and onClick props", () => { |
|
303
|
const summaryElm = <DetailsSummary title="Custom Summary" /> |
|
304
|
const { container } = render( |
|
305
|
<Details summaryElm={summaryElm} openInitial={true}> |
|
306
|
<div>Content</div> |
|
307
|
</Details> |
|
308
|
) |
|
309
|
const summary = container.querySelector("summary") |
|
310
|
expect(summary).toBeInTheDocument() |
|
311
|
// The cloned element should receive open prop |
|
312
|
const title = container.querySelector( |
|
313
|
"[data-testid='details-summary-title']" |
|
314
|
) |
|
315
|
expect(title).toBeInTheDocument() |
|
316
|
}) |
|
317
|
|
|
318
|
test("clones summaryElm with onClick handler", () => { |
|
319
|
const summaryElm = <DetailsSummary title="Custom Summary" /> |
|
320
|
const { container } = render( |
|
321
|
<Details summaryElm={summaryElm}> |
|
322
|
<div>Content</div> |
|
323
|
</Details> |
|
324
|
) |
|
325
|
const summary = container.querySelector("summary") |
|
326
|
expect(summary).toBeInTheDocument() |
|
327
|
|
|
328
|
fireEvent.click(summary!) |
|
329
|
expect(mockSetCollapsed).toHaveBeenCalled() |
|
330
|
}) |
|
331
|
}) |
|
332
|
|
|
333
|
describe("collapsible integration", () => { |
|
334
|
test("calls onClose callback when collapsed", () => { |
|
335
|
mockUseCollapsible.mockReturnValue({ |
|
336
|
getCollapsibleElms: mockGetCollapsibleElms, |
|
337
|
setCollapsed: mockSetCollapsed, |
|
338
|
}) |
|
339
|
|
|
340
|
render( |
|
341
|
<Details summaryContent="Summary"> |
|
342
|
<div>Content</div> |
|
343
|
</Details> |
|
344
|
) |
|
345
|
|
|
346
|
expect(mockUseCollapsible).toHaveBeenCalledWith( |
|
347
|
expect.objectContaining({ |
|
348
|
onClose: expect.any(Function), |
|
349
|
}) |
|
350
|
) |
|
351
|
}) |
|
352
|
}) |