|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { fireEvent, render } from "@testing-library/react" |
|
4
|
import { Sidebar } from "types" |
|
5
|
|
|
6
|
// mock hooks |
|
7
|
const mockSetMobileSidebarOpen = vi.fn() |
|
8
|
const mockSetDesktopSidebarOpen = vi.fn() |
|
9
|
const mockSetSidebarTopHeight = vi.fn() |
|
10
|
|
|
11
|
const mockSidebar: Sidebar.Sidebar = { |
|
12
|
sidebar_id: "test-sidebar", |
|
13
|
title: "Test Sidebar", |
|
14
|
items: [ |
|
15
|
{ |
|
16
|
type: "link", |
|
17
|
path: "/test", |
|
18
|
title: "Test Link", |
|
19
|
}, |
|
20
|
], |
|
21
|
} |
|
22
|
|
|
23
|
const defaultUseSidebarReturn = { |
|
24
|
sidebars: [mockSidebar], |
|
25
|
shownSidebar: mockSidebar as |
|
26
|
| Sidebar.Sidebar |
|
27
|
| Sidebar.SidebarItemSidebar |
|
28
|
| undefined, |
|
29
|
mobileSidebarOpen: false, |
|
30
|
setMobileSidebarOpen: mockSetMobileSidebarOpen, |
|
31
|
isSidebarStatic: true, |
|
32
|
sidebarRef: React.createRef<HTMLDivElement>(), |
|
33
|
desktopSidebarOpen: true, |
|
34
|
setDesktopSidebarOpen: mockSetDesktopSidebarOpen, |
|
35
|
setSidebarTopHeight: mockSetSidebarTopHeight, |
|
36
|
sidebarHistory: ["test-sidebar"], |
|
37
|
} |
|
38
|
|
|
39
|
const mockUseSidebar = vi.fn(() => defaultUseSidebarReturn) |
|
40
|
const mockUseKeyboardShortcut = vi.fn() |
|
41
|
|
|
42
|
vi.mock("@/providers/Sidebar", () => ({ |
|
43
|
useSidebar: () => mockUseSidebar(), |
|
44
|
})) |
|
45
|
|
|
46
|
vi.mock("@/hooks/use-keyboard-shortcut", () => ({ |
|
47
|
useKeyboardShortcut: (options: unknown) => mockUseKeyboardShortcut(options), |
|
48
|
})) |
|
49
|
|
|
50
|
// mock components |
|
51
|
vi.mock("@/components/Loading", () => ({ |
|
52
|
Loading: ({ |
|
53
|
count, |
|
54
|
className, |
|
55
|
}: { |
|
56
|
count?: number |
|
57
|
className?: string |
|
58
|
barClassName?: string |
|
59
|
}) => ( |
|
60
|
<div data-testid="loading" data-count={count} className={className}> |
|
61
|
Loading |
|
62
|
</div> |
|
63
|
), |
|
64
|
})) |
|
65
|
|
|
66
|
vi.mock("@/components/Sidebar/Item", () => ({ |
|
67
|
SidebarItem: ({ |
|
68
|
item, |
|
69
|
hasNextItems, |
|
70
|
}: { |
|
71
|
item: Sidebar.SidebarItem |
|
72
|
hasNextItems: boolean |
|
73
|
}) => ( |
|
74
|
<div data-testid="sidebar-item" data-has-next={hasNextItems}> |
|
75
|
{"title" in item ? item.title : item.type} |
|
76
|
</div> |
|
77
|
), |
|
78
|
})) |
|
79
|
|
|
80
|
vi.mock("@/components/Sidebar/Top", () => ({ |
|
81
|
SidebarTop: () => <div data-testid="sidebar-top">Top</div>, |
|
82
|
})) |
|
83
|
|
|
84
|
vi.mock("react-transition-group", () => ({ |
|
85
|
CSSTransition: ({ |
|
86
|
children, |
|
87
|
nodeRef, |
|
88
|
}: { |
|
89
|
children: React.ReactNode |
|
90
|
nodeRef: React.RefObject<HTMLDivElement> |
|
91
|
}) => <div ref={nodeRef}>{children}</div>, |
|
92
|
SwitchTransition: ({ children }: { children: React.ReactNode }) => ( |
|
93
|
<div>{children}</div> |
|
94
|
), |
|
95
|
})) |
|
96
|
|
|
97
|
vi.mock("@react-hook/resize-observer", () => ({ |
|
98
|
default: vi.fn(), |
|
99
|
})) |
|
100
|
|
|
101
|
vi.mock("@/providers/BrowserProvider", () => ({ |
|
102
|
useIsBrowser: () => ({ |
|
103
|
isBrowser: true, |
|
104
|
}), |
|
105
|
})) |
|
106
|
|
|
107
|
import { Sidebar as SidebarComponent } from "../../Sidebar" |
|
108
|
|
|
109
|
beforeEach(() => { |
|
110
|
vi.clearAllMocks() |
|
111
|
mockUseSidebar.mockReturnValue(defaultUseSidebarReturn) |
|
112
|
}) |
|
113
|
|
|
114
|
describe("rendering", () => { |
|
115
|
test("renders sidebar top", () => { |
|
116
|
const { container } = render(<SidebarComponent />) |
|
117
|
const top = container.querySelector("[data-testid='sidebar-top']") |
|
118
|
expect(top).toBeInTheDocument() |
|
119
|
}) |
|
120
|
|
|
121
|
test("renders sidebar items", () => { |
|
122
|
const { container } = render(<SidebarComponent />) |
|
123
|
const items = container.querySelectorAll("[data-testid='sidebar-item']") |
|
124
|
expect(items).toHaveLength(1) |
|
125
|
expect(items[0]).toHaveTextContent("Test Link") |
|
126
|
}) |
|
127
|
|
|
128
|
test("renders loading when items are empty and not static", () => { |
|
129
|
mockUseSidebar.mockReturnValue({ |
|
130
|
...defaultUseSidebarReturn, |
|
131
|
shownSidebar: { ...mockSidebar, items: [] }, |
|
132
|
isSidebarStatic: false, |
|
133
|
}) |
|
134
|
const { container } = render(<SidebarComponent />) |
|
135
|
const loading = container.querySelector("[data-testid='loading']") |
|
136
|
expect(loading).toBeInTheDocument() |
|
137
|
}) |
|
138
|
|
|
139
|
test("does not render loading when items exist", () => { |
|
140
|
const { container } = render(<SidebarComponent />) |
|
141
|
const loading = container.querySelector("[data-testid='loading']") |
|
142
|
expect(loading).not.toBeInTheDocument() |
|
143
|
}) |
|
144
|
|
|
145
|
test("renders overlay when mobile sidebar is open", () => { |
|
146
|
mockUseSidebar.mockReturnValue({ |
|
147
|
...defaultUseSidebarReturn, |
|
148
|
mobileSidebarOpen: true, |
|
149
|
}) |
|
150
|
const { container } = render(<SidebarComponent />) |
|
151
|
const overlay = container.querySelector( |
|
152
|
"[data-testid='mobile-sidebar-overlay']" |
|
153
|
) |
|
154
|
expect(overlay).toBeInTheDocument() |
|
155
|
}) |
|
156
|
|
|
157
|
test("does not render overlay when mobile sidebar is closed", () => { |
|
158
|
const { container } = render(<SidebarComponent />) |
|
159
|
const overlay = container.querySelector( |
|
160
|
"[data-testid='mobile-sidebar-overlay']" |
|
161
|
) |
|
162
|
expect(overlay).not.toBeInTheDocument() |
|
163
|
}) |
|
164
|
|
|
165
|
test("applies mobile sidebar open classes", () => { |
|
166
|
mockUseSidebar.mockReturnValue({ |
|
167
|
...defaultUseSidebarReturn, |
|
168
|
mobileSidebarOpen: true, |
|
169
|
}) |
|
170
|
const { container } = render(<SidebarComponent />) |
|
171
|
const aside = container.querySelector("aside") |
|
172
|
expect(aside).toHaveClass( |
|
173
|
"!left-docs_0.5 !top-docs_0.5 z-50 shadow-elevation-modal dark:shadow-elevation-modal-dark" |
|
174
|
) |
|
175
|
expect(aside).toHaveClass("rounded") |
|
176
|
expect(aside).toHaveClass("lg:!left-0 lg:!top-0 lg:shadow-none") |
|
177
|
}) |
|
178
|
|
|
179
|
test("applies desktop sidebar open classes", () => { |
|
180
|
mockUseSidebar.mockReturnValue({ |
|
181
|
...defaultUseSidebarReturn, |
|
182
|
desktopSidebarOpen: true, |
|
183
|
}) |
|
184
|
const { container } = render(<SidebarComponent />) |
|
185
|
const aside = container.querySelector("aside") |
|
186
|
expect(aside).toHaveClass("lg:left-0") |
|
187
|
}) |
|
188
|
|
|
189
|
test("applies desktop sidebar closed classes", () => { |
|
190
|
mockUseSidebar.mockReturnValue({ |
|
191
|
...defaultUseSidebarReturn, |
|
192
|
desktopSidebarOpen: false, |
|
193
|
}) |
|
194
|
const { container } = render(<SidebarComponent />) |
|
195
|
const aside = container.querySelector("aside") |
|
196
|
expect(aside).toHaveClass("lg:!absolute lg:!-left-full") |
|
197
|
}) |
|
198
|
|
|
199
|
test("applies custom className", () => { |
|
200
|
const { container } = render(<SidebarComponent className="custom-class" />) |
|
201
|
const aside = container.querySelector("aside") |
|
202
|
expect(aside).toHaveClass("custom-class") |
|
203
|
}) |
|
204
|
|
|
205
|
test("uses items from shownSidebar when available", () => { |
|
206
|
const sidebarWithItems: Sidebar.Sidebar = { |
|
207
|
sidebar_id: "sidebar-with-items", |
|
208
|
title: "Sidebar With Items", |
|
209
|
items: [ |
|
210
|
{ |
|
211
|
type: "link", |
|
212
|
path: "/item1", |
|
213
|
title: "Item 1", |
|
214
|
}, |
|
215
|
{ |
|
216
|
type: "link", |
|
217
|
path: "/item2", |
|
218
|
title: "Item 2", |
|
219
|
}, |
|
220
|
], |
|
221
|
} |
|
222
|
mockUseSidebar.mockReturnValue({ |
|
223
|
...defaultUseSidebarReturn, |
|
224
|
shownSidebar: sidebarWithItems, |
|
225
|
}) |
|
226
|
const { container } = render(<SidebarComponent />) |
|
227
|
const items = container.querySelectorAll("[data-testid='sidebar-item']") |
|
228
|
expect(items).toHaveLength(2) |
|
229
|
}) |
|
230
|
|
|
231
|
test("uses children from shownSidebar when items not available", () => { |
|
232
|
const sidebarWithChildren: Sidebar.SidebarItemSidebar = { |
|
233
|
type: "sidebar", |
|
234
|
sidebar_id: "sidebar-with-children", |
|
235
|
title: "Sidebar With Children", |
|
236
|
children: [ |
|
237
|
{ |
|
238
|
type: "link", |
|
239
|
path: "/child1", |
|
240
|
title: "Child 1", |
|
241
|
}, |
|
242
|
], |
|
243
|
} |
|
244
|
mockUseSidebar.mockReturnValue({ |
|
245
|
...defaultUseSidebarReturn, |
|
246
|
shownSidebar: sidebarWithChildren, |
|
247
|
}) |
|
248
|
const { container } = render(<SidebarComponent />) |
|
249
|
const items = container.querySelectorAll("[data-testid='sidebar-item']") |
|
250
|
expect(items).toHaveLength(1) |
|
251
|
expect(items[0]).toHaveTextContent("Child 1") |
|
252
|
}) |
|
253
|
|
|
254
|
test("passes hasNextItems correctly", () => { |
|
255
|
const sidebarWithMultipleItems: Sidebar.Sidebar = { |
|
256
|
sidebar_id: "sidebar-multiple", |
|
257
|
title: "Multiple Items", |
|
258
|
items: [ |
|
259
|
{ |
|
260
|
type: "link", |
|
261
|
path: "/item1", |
|
262
|
title: "Item 1", |
|
263
|
}, |
|
264
|
{ |
|
265
|
type: "link", |
|
266
|
path: "/item2", |
|
267
|
title: "Item 2", |
|
268
|
}, |
|
269
|
], |
|
270
|
} |
|
271
|
mockUseSidebar.mockReturnValue({ |
|
272
|
...defaultUseSidebarReturn, |
|
273
|
shownSidebar: sidebarWithMultipleItems, |
|
274
|
}) |
|
275
|
const { container } = render(<SidebarComponent />) |
|
276
|
const items = container.querySelectorAll("[data-testid='sidebar-item']") |
|
277
|
expect(items[0]).toHaveAttribute("data-has-next", "true") |
|
278
|
expect(items[1]).toHaveAttribute("data-has-next", "false") |
|
279
|
}) |
|
280
|
}) |
|
281
|
|
|
282
|
describe("interactions", () => { |
|
283
|
test("closes mobile sidebar when clicking outside", () => { |
|
284
|
mockUseSidebar.mockReturnValue({ |
|
285
|
...defaultUseSidebarReturn, |
|
286
|
mobileSidebarOpen: true, |
|
287
|
}) |
|
288
|
const { container } = render( |
|
289
|
<div> |
|
290
|
<div data-testid="outside-element">Outside Element</div> |
|
291
|
<SidebarComponent /> |
|
292
|
</div> |
|
293
|
) |
|
294
|
const outsideElement = container.querySelector( |
|
295
|
"[data-testid='outside-element']" |
|
296
|
) |
|
297
|
fireEvent.click(outsideElement!) |
|
298
|
expect(mockSetMobileSidebarOpen).toHaveBeenCalledWith(false) |
|
299
|
}) |
|
300
|
|
|
301
|
test("sets up keyboard shortcut for toggle", () => { |
|
302
|
render(<SidebarComponent />) |
|
303
|
expect(mockUseKeyboardShortcut).toHaveBeenCalledWith({ |
|
304
|
metakey: true, |
|
305
|
shortcutKeys: ["\\"], |
|
306
|
action: expect.any(Function), |
|
307
|
}) |
|
308
|
}) |
|
309
|
|
|
310
|
test("toggles desktop sidebar when keyboard shortcut is triggered", () => { |
|
311
|
render(<SidebarComponent />) |
|
312
|
const lastCall = |
|
313
|
mockUseKeyboardShortcut.mock.calls[ |
|
314
|
mockUseKeyboardShortcut.mock.calls.length - 1 |
|
315
|
] |
|
316
|
const action = lastCall[0].action |
|
317
|
action() |
|
318
|
expect(mockSetDesktopSidebarOpen).toHaveBeenCalledWith(expect.any(Function)) |
|
319
|
}) |
|
320
|
}) |