|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { fireEvent, render } from "@testing-library/react" |
|
4
|
import { NavigationItemDropdown, MenuItem } from "types" |
|
5
|
import { ButtonProps } from "../../Button" |
|
6
|
|
|
7
|
// mock data |
|
8
|
const mockConfig = { |
|
9
|
baseUrl: "https://docs.medusajs.com", |
|
10
|
logo: "/logo.png", |
|
11
|
features: { |
|
12
|
aiAssistant: true, |
|
13
|
}, |
|
14
|
} |
|
15
|
|
|
16
|
const defaultUseSiteConfigReturn = { |
|
17
|
config: mockConfig, |
|
18
|
} |
|
19
|
|
|
20
|
// mock functions |
|
21
|
const mockSetMobileSidebarOpen = vi.fn() |
|
22
|
const mockUseSiteConfig = vi.fn(() => defaultUseSiteConfigReturn) |
|
23
|
|
|
24
|
const defaultUseSidebarReturn = { |
|
25
|
setMobileSidebarOpen: mockSetMobileSidebarOpen, |
|
26
|
isSidebarShown: true, |
|
27
|
} |
|
28
|
|
|
29
|
const defaultUseLayoutReturn = { |
|
30
|
showCollapsedNavbar: false, |
|
31
|
mainContentRef: React.createRef<HTMLDivElement>(), |
|
32
|
} |
|
33
|
const mockUseSidebar = vi.fn(() => defaultUseSidebarReturn) |
|
34
|
const mockUseLayout = vi.fn(() => defaultUseLayoutReturn) |
|
35
|
|
|
36
|
const defaultUseMainNavReturn = { |
|
37
|
logo: undefined as React.ReactNode | undefined, |
|
38
|
logoUrl: undefined as string | undefined, |
|
39
|
helpNavItem: undefined as |
|
40
|
| NavigationItemDropdown |
|
41
|
| undefined, |
|
42
|
additionalMenuItems: undefined as |
|
43
|
| MenuItem[] |
|
44
|
| undefined, |
|
45
|
navItems: [], |
|
46
|
} |
|
47
|
const mockUseMainNav = vi.fn(() => defaultUseMainNavReturn) |
|
48
|
|
|
49
|
// mock components |
|
50
|
vi.mock("@/providers/SiteConfig", () => ({ |
|
51
|
useSiteConfig: () => mockUseSiteConfig(), |
|
52
|
})) |
|
53
|
|
|
54
|
vi.mock("@/providers/Sidebar", () => ({ |
|
55
|
useSidebar: () => mockUseSidebar(), |
|
56
|
})) |
|
57
|
|
|
58
|
vi.mock("@/providers/Layout", () => ({ |
|
59
|
useLayout: () => mockUseLayout(), |
|
60
|
})) |
|
61
|
|
|
62
|
vi.mock("@/providers/MainNav", () => ({ |
|
63
|
useMainNav: () => mockUseMainNav(), |
|
64
|
})) |
|
65
|
|
|
66
|
vi.mock("next/link", () => ({ |
|
67
|
default: ({ |
|
68
|
href, |
|
69
|
children, |
|
70
|
...props |
|
71
|
}: { |
|
72
|
href: string |
|
73
|
children: React.ReactNode |
|
74
|
[key: string]: unknown |
|
75
|
}) => ( |
|
76
|
<a href={href} {...props}> |
|
77
|
{children} |
|
78
|
</a> |
|
79
|
), |
|
80
|
})) |
|
81
|
|
|
82
|
vi.mock("@/components/Icons", () => ({ |
|
83
|
ColoredMedusaIcon: ({ variant }: { variant?: string }) => ( |
|
84
|
<svg data-testid="logo-icon" data-variant={variant} /> |
|
85
|
), |
|
86
|
})) |
|
87
|
|
|
88
|
vi.mock("@/components/Button", () => ({ |
|
89
|
Button: (props: ButtonProps) => <button {...props} />, |
|
90
|
})) |
|
91
|
|
|
92
|
vi.mock("@/components/Search/ModalOpener", () => ({ |
|
93
|
SearchModalOpener: () => <div data-testid="search-modal-opener" />, |
|
94
|
})) |
|
95
|
|
|
96
|
vi.mock("@/components/AiAssistant/TriggerButton", () => ({ |
|
97
|
AiAssistantTriggerButton: () => <div data-testid="ai-assistant-trigger" />, |
|
98
|
})) |
|
99
|
|
|
100
|
vi.mock("@/components/Icons/SidebarLeft", () => ({ |
|
101
|
SidebarLeftIcon: () => <svg data-testid="sidebar-left-icon" />, |
|
102
|
})) |
|
103
|
|
|
104
|
vi.mock("@/components/MainNav/Items", () => ({ |
|
105
|
MainNavItems: ({ className }: { className?: string }) => ( |
|
106
|
<ul data-testid="nav-items" className={className} /> |
|
107
|
), |
|
108
|
})) |
|
109
|
|
|
110
|
vi.mock("@/components/MainNav/DesktopMenu", () => ({ |
|
111
|
MainNavDesktopMenu: () => <div data-testid="desktop-menu" />, |
|
112
|
})) |
|
113
|
|
|
114
|
vi.mock("@/components/MainNav/MobileMenu", () => ({ |
|
115
|
MainNavMobileMenu: () => <div data-testid="mobile-menu" />, |
|
116
|
})) |
|
117
|
|
|
118
|
vi.mock("@/components/MainNav/Version", () => ({ |
|
119
|
MainNavVersion: () => <div data-testid="version" />, |
|
120
|
})) |
|
121
|
|
|
122
|
vi.mock("@/components/MainNav/Items/Dropdown", () => ({ |
|
123
|
MainNavItemDropdown: ({ |
|
124
|
item, |
|
125
|
className, |
|
126
|
wrapperClassName, |
|
127
|
}: { |
|
128
|
item: unknown |
|
129
|
className?: string |
|
130
|
wrapperClassName?: string |
|
131
|
}) => ( |
|
132
|
<div |
|
133
|
data-testid="help-dropdown" |
|
134
|
className={className} |
|
135
|
data-wrapper-class={wrapperClassName} |
|
136
|
> |
|
137
|
{JSON.stringify(item)} |
|
138
|
</div> |
|
139
|
), |
|
140
|
})) |
|
141
|
|
|
142
|
import { MainNav } from "../../MainNav" |
|
143
|
|
|
144
|
beforeEach(() => { |
|
145
|
vi.clearAllMocks() |
|
146
|
mockUseSidebar.mockReturnValue(defaultUseSidebarReturn) |
|
147
|
mockUseLayout.mockReturnValue(defaultUseLayoutReturn) |
|
148
|
mockUseMainNav.mockReturnValue(defaultUseMainNavReturn) |
|
149
|
mockUseSiteConfig.mockReturnValue(defaultUseSiteConfigReturn) |
|
150
|
}) |
|
151
|
|
|
152
|
describe("rendering", () => { |
|
153
|
test("renders main nav", () => { |
|
154
|
const { container } = render(<MainNav />) |
|
155
|
const nav = container.querySelector("[data-testid='main-nav']") |
|
156
|
expect(nav).toBeInTheDocument() |
|
157
|
const logoLink = container.querySelector("[data-testid='logo-link']") |
|
158
|
expect(logoLink).toBeInTheDocument() |
|
159
|
const icon = container.querySelector("[data-testid='logo-icon']") |
|
160
|
expect(icon).toBeInTheDocument() |
|
161
|
const searchOpener = container.querySelector( |
|
162
|
"[data-testid='search-modal-opener']" |
|
163
|
) |
|
164
|
expect(searchOpener).toBeInTheDocument() |
|
165
|
const aiTrigger = container.querySelector( |
|
166
|
"[data-testid='ai-assistant-trigger']" |
|
167
|
) |
|
168
|
expect(aiTrigger).toBeInTheDocument() |
|
169
|
const desktopMenu = container.querySelector("[data-testid='desktop-menu']") |
|
170
|
expect(desktopMenu).toBeInTheDocument() |
|
171
|
const mobileMenu = container.querySelector("[data-testid='mobile-menu']") |
|
172
|
expect(mobileMenu).toBeInTheDocument() |
|
173
|
const version = container.querySelector("[data-testid='version']") |
|
174
|
const helpDropdown = container.querySelector( |
|
175
|
"[data-testid='help-dropdown']" |
|
176
|
) |
|
177
|
expect(version).toBeInTheDocument() |
|
178
|
expect(helpDropdown).toBeInTheDocument() |
|
179
|
}) |
|
180
|
|
|
181
|
test("does not render ai assistant trigger when ai assistant feature is disabled", () => { |
|
182
|
mockUseSiteConfig.mockReturnValueOnce({ |
|
183
|
config: { |
|
184
|
...mockConfig, |
|
185
|
features: { |
|
186
|
aiAssistant: false, |
|
187
|
}, |
|
188
|
}, |
|
189
|
}) |
|
190
|
const { container } = render(<MainNav />) |
|
191
|
const aiTrigger = container.querySelector( |
|
192
|
"[data-testid='ai-assistant-trigger']" |
|
193
|
) |
|
194
|
expect(aiTrigger).not.toBeInTheDocument() |
|
195
|
}) |
|
196
|
|
|
197
|
test("renders nav items when not collapsed", () => { |
|
198
|
const { container } = render(<MainNav />) |
|
199
|
const navItems = container.querySelector("[data-testid='nav-items']") |
|
200
|
expect(navItems).toBeInTheDocument() |
|
201
|
}) |
|
202
|
|
|
203
|
test("renders mobile sidebar button when sidebar is shown", () => { |
|
204
|
const { container } = render(<MainNav />) |
|
205
|
const button = container.querySelector( |
|
206
|
"[data-testid='mobile-sidebar-button']" |
|
207
|
) |
|
208
|
expect(button).toBeInTheDocument() |
|
209
|
}) |
|
210
|
|
|
211
|
test("does not render mobile sidebar button when sidebar is not shown", () => { |
|
212
|
mockUseSidebar.mockReturnValueOnce({ |
|
213
|
...defaultUseSidebarReturn, |
|
214
|
isSidebarShown: false, |
|
215
|
setMobileSidebarOpen: mockSetMobileSidebarOpen, |
|
216
|
}) |
|
217
|
const { container } = render(<MainNav />) |
|
218
|
const button = container.querySelector( |
|
219
|
"[data-testid='mobile-sidebar-button']" |
|
220
|
) |
|
221
|
expect(button).not.toBeInTheDocument() |
|
222
|
}) |
|
223
|
|
|
224
|
test("applies custom className", () => { |
|
225
|
const { container } = render(<MainNav className="custom-class" />) |
|
226
|
const nav = container.querySelector("[data-testid='main-nav']") |
|
227
|
expect(nav).toHaveClass("custom-class") |
|
228
|
}) |
|
229
|
|
|
230
|
test("applies custom itemsClassName", () => { |
|
231
|
const { container } = render( |
|
232
|
<MainNav itemsClassName="custom-items-class" /> |
|
233
|
) |
|
234
|
const navItems = container.querySelector("[data-testid='nav-items']") |
|
235
|
expect(navItems).toHaveClass("custom-items-class") |
|
236
|
}) |
|
237
|
|
|
238
|
test("always shows border-b on nav content regardless of collapse state", () => { |
|
239
|
const { container } = render(<MainNav />) |
|
240
|
const topBar = container.querySelector("[data-testid='main-nav-content']") |
|
241
|
expect(topBar).toBeInTheDocument() |
|
242
|
expect(topBar).toHaveClass("border-b") |
|
243
|
expect(topBar).toHaveClass("border-medusa-border-base") |
|
244
|
}) |
|
245
|
|
|
246
|
test("main nav content stacks above the collapsed nav items row", () => { |
|
247
|
const { container } = render(<MainNav />) |
|
248
|
const topBar = container.querySelector("[data-testid='main-nav-content']") |
|
249
|
expect(topBar).toBeInTheDocument() |
|
250
|
expect(topBar).toHaveClass("relative") |
|
251
|
expect(topBar).toHaveClass("z-50") |
|
252
|
}) |
|
253
|
|
|
254
|
test("adjusts layout when collapsed", () => { |
|
255
|
mockUseLayout.mockReturnValueOnce({ |
|
256
|
...defaultUseLayoutReturn, |
|
257
|
showCollapsedNavbar: true, |
|
258
|
}) |
|
259
|
const { container } = render(<MainNav />) |
|
260
|
const topBar = container.querySelector("[data-testid='main-nav-content']") |
|
261
|
expect(topBar).toBeInTheDocument() |
|
262
|
expect(topBar).toHaveClass("border-b border-medusa-border-base") |
|
263
|
expect(topBar).toHaveClass("relative") |
|
264
|
expect(topBar).toHaveClass("z-50") |
|
265
|
const actionsContainer = container.querySelector( |
|
266
|
"[data-testid='main-nav-actions']" |
|
267
|
) |
|
268
|
expect(actionsContainer).toBeInTheDocument() |
|
269
|
expect(actionsContainer).toHaveClass("flex-grow justify-between") |
|
270
|
const collapsedNavItems = container.querySelector( |
|
271
|
"[data-testid='collapsed-nav-items']" |
|
272
|
) |
|
273
|
expect(collapsedNavItems).toBeInTheDocument() |
|
274
|
expect(collapsedNavItems).toHaveClass("relative") |
|
275
|
expect(collapsedNavItems).toHaveClass("z-10") |
|
276
|
}) |
|
277
|
|
|
278
|
test("renders custom logo when provided", () => { |
|
279
|
mockUseMainNav.mockReturnValueOnce({ |
|
280
|
...defaultUseMainNavReturn, |
|
281
|
logo: <div data-testid="custom-logo">Custom Logo</div>, |
|
282
|
}) |
|
283
|
const { container } = render(<MainNav />) |
|
284
|
const customLogo = container.querySelector("[data-testid='custom-logo']") |
|
285
|
expect(customLogo).toBeInTheDocument() |
|
286
|
expect(customLogo).toHaveTextContent("Custom Logo") |
|
287
|
const defaultLogo = container.querySelector("[data-testid='logo-icon']") |
|
288
|
expect(defaultLogo).not.toBeInTheDocument() |
|
289
|
}) |
|
290
|
|
|
291
|
test("renders default logo when custom logo not provided", () => { |
|
292
|
const { container } = render(<MainNav />) |
|
293
|
const defaultLogo = container.querySelector("[data-testid='logo-icon']") |
|
294
|
expect(defaultLogo).toBeInTheDocument() |
|
295
|
}) |
|
296
|
|
|
297
|
test("uses custom logoUrl when provided", () => { |
|
298
|
mockUseMainNav.mockReturnValueOnce({ |
|
299
|
...defaultUseMainNavReturn, |
|
300
|
logoUrl: "https://custom-url.com", |
|
301
|
}) |
|
302
|
const { container } = render(<MainNav />) |
|
303
|
const logoLink = container.querySelector("[data-testid='logo-link']") |
|
304
|
expect(logoLink).toHaveAttribute("href", "https://custom-url.com") |
|
305
|
}) |
|
306
|
|
|
307
|
test("uses baseUrl as logoUrl when custom logoUrl not provided", () => { |
|
308
|
const { container } = render(<MainNav />) |
|
309
|
const logoLink = container.querySelector("[data-testid='logo-link']") |
|
310
|
expect(logoLink).toHaveAttribute("href", mockConfig.baseUrl) |
|
311
|
}) |
|
312
|
|
|
313
|
test("renders custom helpNavItem when provided", () => { |
|
314
|
const customHelpItem = { |
|
315
|
type: "dropdown" as const, |
|
316
|
title: "Custom Help", |
|
317
|
children: [ |
|
318
|
{ |
|
319
|
type: "link" as const, |
|
320
|
title: "Custom Support", |
|
321
|
link: "https://custom-support.com", |
|
322
|
}, |
|
323
|
], |
|
324
|
} |
|
325
|
mockUseMainNav.mockReturnValueOnce({ |
|
326
|
...defaultUseMainNavReturn, |
|
327
|
helpNavItem: customHelpItem, |
|
328
|
}) |
|
329
|
const { container } = render(<MainNav />) |
|
330
|
const helpDropdown = container.querySelector( |
|
331
|
"[data-testid='help-dropdown']" |
|
332
|
) |
|
333
|
expect(helpDropdown).toBeInTheDocument() |
|
334
|
expect(helpDropdown?.textContent).toContain("Custom Help") |
|
335
|
expect(helpDropdown?.textContent).toContain("Custom Support") |
|
336
|
}) |
|
337
|
|
|
338
|
test("renders default help dropdown when custom helpNavItem not provided", () => { |
|
339
|
const { container } = render(<MainNav />) |
|
340
|
const helpDropdown = container.querySelector( |
|
341
|
"[data-testid='help-dropdown']" |
|
342
|
) |
|
343
|
expect(helpDropdown).toBeInTheDocument() |
|
344
|
expect(helpDropdown?.textContent).toContain("Help") |
|
345
|
expect(helpDropdown?.textContent).toContain("Troubleshooting") |
|
346
|
}) |
|
347
|
}) |
|
348
|
|
|
349
|
describe("interaction", () => { |
|
350
|
test("opens mobile sidebar when button is clicked", () => { |
|
351
|
const { container } = render(<MainNav />) |
|
352
|
const button = container.querySelector( |
|
353
|
"[data-testid='mobile-sidebar-button']" |
|
354
|
) |
|
355
|
fireEvent.click(button!) |
|
356
|
expect(mockSetMobileSidebarOpen).toHaveBeenCalledWith(true) |
|
357
|
}) |
|
358
|
}) |