-
-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathDashboardLayout.test.tsx
More file actions
471 lines (395 loc) · 14 KB
/
DashboardLayout.test.tsx
File metadata and controls
471 lines (395 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/**
* @vitest-environment jsdom
*/
import * as React from 'react';
import { describe, test, expect, vi } from 'vitest';
import { render, within, screen } from '@testing-library/react';
import DashboardIcon from '@mui/icons-material/Dashboard';
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
import BarChartIcon from '@mui/icons-material/BarChart';
import DescriptionIcon from '@mui/icons-material/Description';
import LayersIcon from '@mui/icons-material/Layers';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom/vitest';
import { AppProvider, Navigation } from '../AppProvider';
import { DashboardLayout } from './DashboardLayout';
describe('DashboardLayout', () => {
test('renders content correctly', async () => {
render(<DashboardLayout>Hello world</DashboardLayout>);
expect(screen.getByText('Hello world')).toBeTruthy();
});
test('renders branding correctly in header', async () => {
const BRANDING = {
title: 'My Company',
logo: <img src="https://placehold.co/600x400" alt="Placeholder Logo" />,
};
render(
<AppProvider branding={BRANDING}>
<DashboardLayout>Hello world</DashboardLayout>
</AppProvider>,
);
const header = screen.getByRole('banner');
expect(within(header).getByText('My Company')).toBeTruthy();
expect(within(header).getByAltText('Placeholder Logo')).toBeTruthy();
});
test('can switch theme', async () => {
const user = userEvent.setup();
render(
<AppProvider>
<DashboardLayout>Hello world</DashboardLayout>
</AppProvider>,
);
const getBackgroundColorCSSVariable = () =>
getComputedStyle(document.documentElement).getPropertyValue(
'--mui-palette-common-background',
);
const header = screen.getByRole('banner');
const themeSwitcherButton = within(header).getByLabelText('Switch to dark mode');
expect(getBackgroundColorCSSVariable()).toBe('#fff');
await user.click(themeSwitcherButton);
expect(getBackgroundColorCSSVariable()).toBe('#000');
await user.click(themeSwitcherButton);
expect(getBackgroundColorCSSVariable()).toBe('#fff');
});
test('navigation works correctly', async () => {
const NAVIGATION: Navigation = [
{
kind: 'header',
title: 'Main items',
},
{
title: 'Dashboard',
segment: 'dashboard',
icon: <DashboardIcon />,
},
{
title: 'Orders',
segment: 'orders',
icon: <ShoppingCartIcon />,
},
{
kind: 'divider',
},
{
kind: 'header',
title: 'Analytics',
},
{
segment: 'reports',
title: 'Reports',
icon: <BarChartIcon />,
children: [
{
segment: 'sales',
title: 'Sales',
icon: <DescriptionIcon />,
},
{
segment: 'traffic',
title: 'Traffic',
icon: <DescriptionIcon />,
},
],
},
{
segment: 'integrations',
title: 'Integrations',
icon: <LayersIcon />,
},
];
const user = userEvent.setup();
render(
<AppProvider navigation={NAVIGATION}>
<DashboardLayout>Hello world</DashboardLayout>
</AppProvider>,
);
const desktopNavigation = screen.getByRole('navigation', { name: 'Desktop' });
// List subheaders are present
expect(within(desktopNavigation).getByText('Main items')).toBeTruthy();
expect(within(desktopNavigation).getByText('Analytics')).toBeTruthy();
// List items and their links are present
const dashboardLink = within(desktopNavigation).getByRole('link', { name: 'Dashboard' });
const ordersLink = within(desktopNavigation).getByRole('link', { name: 'Orders' });
expect(dashboardLink.getAttribute('href')).toBe('/dashboard');
expect(ordersLink.getAttribute('href')).toBe('/orders');
const reportsItem = within(desktopNavigation).getByText('Reports');
expect(reportsItem).toBeTruthy();
expect(within(desktopNavigation).getByText('Integrations')).toBeTruthy();
// Nested list items show when parent item is clicked
expect(within(desktopNavigation).queryByText('Sales')).toBeNull();
expect(within(desktopNavigation).queryByText('Traffic')).toBeNull();
await user.click(reportsItem);
expect(within(desktopNavigation).getByText('Sales')).toBeTruthy();
expect(within(desktopNavigation).getByText('Traffic')).toBeTruthy();
});
test('starts with parent items expanded if any of their children is the current page', () => {
const NAVIGATION: Navigation = [
{
segment: 'reports',
title: 'Reports',
icon: <BarChartIcon />,
children: [
{
segment: 'sales',
title: 'Sales',
icon: <DescriptionIcon />,
},
{
segment: 'traffic',
title: 'Traffic',
icon: <DescriptionIcon />,
},
],
},
];
const mockRouter = {
pathname: '/reports/sales',
searchParams: new URLSearchParams(),
navigate: vi.fn(),
};
render(
<AppProvider navigation={NAVIGATION} router={mockRouter}>
<DashboardLayout>Hello world</DashboardLayout>
</AppProvider>,
);
const desktopNavigation = screen.getByRole('navigation', { name: 'Desktop' });
expect(within(desktopNavigation).getByText('Sales')).toBeTruthy();
expect(within(desktopNavigation).getByText('Traffic')).toBeTruthy();
});
test('shows correct selected page item', () => {
const NAVIGATION: Navigation = [
{
title: 'Dashboard',
segment: 'dashboard',
icon: <DashboardIcon />,
},
{
title: 'Orders',
segment: 'orders',
icon: <ShoppingCartIcon />,
},
{
segment: 'dynamic',
title: 'Dynamic',
icon: <BarChartIcon />,
pattern: 'dynamic/:dynamicId',
},
{
segment: 'optional',
title: 'Optional',
pattern: 'optional{/:optionalId}?',
},
{
segment: 'oneOrMore',
title: 'One or more',
pattern: 'oneormore{/:oneormoreId}+',
},
{
segment: 'zeroOrMore',
title: 'Zero or more',
pattern: 'zeroormore{/:zeroormoreId}*',
},
];
function AppWithPathname({ pathname }: { pathname: string }) {
const mockRouter = {
pathname,
searchParams: new URLSearchParams(),
navigate: vi.fn(),
};
return (
<AppProvider navigation={NAVIGATION} router={mockRouter}>
<DashboardLayout>Hello world</DashboardLayout>
</AppProvider>
);
}
const { rerender } = render(<AppWithPathname pathname="/dashboard" />);
const desktopNavigation = screen.getByRole('navigation', { name: 'Desktop' });
expect(within(desktopNavigation).getByRole('link', { name: 'Dashboard' })).toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/orders" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Dashboard' })).not.toHaveClass(
'Mui-selected',
);
expect(within(desktopNavigation).getByRole('link', { name: 'Orders' })).toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/dynamic" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic' })).not.toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/dynamic/123" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic' })).toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/dynamic/123/456" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic' })).not.toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/optional" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Optional' })).toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/optional/123" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Optional' })).toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/optional/123/456" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Optional' })).not.toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/oneormore" />);
expect(within(desktopNavigation).getByRole('link', { name: 'One or more' })).not.toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/oneormore/123" />);
expect(within(desktopNavigation).getByRole('link', { name: 'One or more' })).toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/oneormore/123/456" />);
expect(within(desktopNavigation).getByRole('link', { name: 'One or more' })).toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/zeroormore" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Zero or more' })).toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/zeroormore/123" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Zero or more' })).toHaveClass(
'Mui-selected',
);
rerender(<AppWithPathname pathname="/zeroormore/123/456" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Zero or more' })).toHaveClass(
'Mui-selected',
);
});
test('renders navigation actions', async () => {
const NAVIGATION: Navigation = [
{
title: 'Item 1',
segment: 'item1',
icon: <DescriptionIcon />,
action: <div>Action 1</div>,
},
{
title: 'Item',
segment: 'item2',
icon: <DescriptionIcon />,
action: <div>Action 2</div>,
},
];
render(
<AppProvider navigation={NAVIGATION}>
<DashboardLayout>Hello world</DashboardLayout>
</AppProvider>,
);
const desktopNavigation = screen.getByRole('navigation', { name: 'Desktop' });
expect(within(desktopNavigation).getByText('Action 1')).toBeTruthy();
expect(within(desktopNavigation).getByText('Action 2')).toBeTruthy();
});
test('renders sidebar footer slot content', async () => {
function SidebarFooter() {
return <div>I am footer</div>;
}
render(
<AppProvider>
<DashboardLayout slots={{ sidebarFooter: SidebarFooter }}>Hello world</DashboardLayout>
</AppProvider>,
);
const desktopNavigation = screen.getByRole('navigation', { name: 'Desktop' });
expect(within(desktopNavigation).getByText('I am footer')).toBeTruthy();
});
test('renders without the navigation and toggle button', async () => {
const NAVIGATION: Navigation = [
{
title: 'Dashboard',
segment: 'dashboard',
icon: <DashboardIcon />,
},
{
title: 'Orders',
segment: 'orders',
icon: <ShoppingCartIcon />,
},
];
render(
<AppProvider navigation={NAVIGATION}>
<DashboardLayout hideNavigation>Hello world</DashboardLayout>
</AppProvider>,
);
const desktopNavigation = screen.queryByRole('navigation', { name: 'Desktop' });
const navigationToggle = screen.queryByLabelText('Collapse menu');
// Expect that navigation and menu button are not rendered
expect(desktopNavigation).toBeNull();
expect(navigationToggle).toBeNull();
// Ensure that main content is still rendered
expect(screen.getByText('Hello world')).toBeTruthy();
});
test('renders without default collapsed navigation on desktop', async () => {
const NAVIGATION: Navigation = [
{
title: 'Dashboard',
segment: 'dashboard',
icon: <DashboardIcon />,
},
];
render(
<AppProvider navigation={NAVIGATION}>
<DashboardLayout defaultSidebarCollapsed>Hello world</DashboardLayout>
</AppProvider>,
);
// Expect that menu button has expand action
expect(screen.getAllByLabelText('Expand menu')).toBeTruthy();
expect(screen.queryByLabelText('Collapse menu')).toBeNull();
// Ensure that main content is still rendered
expect(screen.getByText('Hello world')).toBeTruthy();
});
test('renders the sidebar in collapsed state when navigationMenuOpen is false', () => {
render(
<DashboardLayout navigationMenuOpen={false}>
<div>Test Content</div>
</DashboardLayout>,
);
// Expect that menu button has expand action
expect(screen.getAllByLabelText('Expand menu')).toBeTruthy();
expect(screen.queryByLabelText('Collapse menu')).toBeNull();
});
test('renders the sidebar in expanded state when navigationMenuOpen is true', () => {
render(
<DashboardLayout navigationMenuOpen>
<div>Test Content</div>
</DashboardLayout>,
);
expect(screen.getAllByLabelText('Collapse menu')).toBeTruthy();
});
test('calls onNavigationMenuOpen callback when navigationMenuOpen state changes to open', () => {
const mockToggleSidebar = vi.fn();
const { rerender } = render(
<DashboardLayout navigationMenuOpen={false} onNavigationMenuClose={mockToggleSidebar}>
<div>Test Content</div>
</DashboardLayout>,
);
// Trigger sidebar open action
rerender(
<DashboardLayout navigationMenuOpen onNavigationMenuClose={mockToggleSidebar}>
<div>Test Content</div>
</DashboardLayout>,
);
expect(mockToggleSidebar).toHaveBeenCalledOnce();
});
test('calls onNavigationMenuClose callback when navigationMenuOpen state changes to close', () => {
const mockToggleSidebar = vi.fn();
const { rerender } = render(
<DashboardLayout navigationMenuOpen onNavigationMenuClose={mockToggleSidebar}>
<div>Test Content</div>
</DashboardLayout>,
);
// Trigger sidebar close action
rerender(
<DashboardLayout navigationMenuOpen={false} onNavigationMenuClose={mockToggleSidebar}>
<div>Test Content</div>
</DashboardLayout>,
);
expect(mockToggleSidebar).toHaveBeenCalledOnce();
});
});