@baukit/navigation 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,483 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+ import { cleanup, fireEvent, render, screen, within } from '@testing-library/react';
3
+ import { AppNavigation, SectionPicker, type NavigationIcon } from './web.js';
4
+ import type { NavigationItem, NavigationProfile } from './index.js';
5
+
6
+ const labels = {
7
+ label: 'Primary',
8
+ collapseLabel: 'Collapse navigation',
9
+ expandLabel: 'Expand navigation',
10
+ closeLabel: 'Close',
11
+ };
12
+
13
+ const icon: NavigationIcon = ({ active }) => <span>{active ? 'filled' : 'outline'}</span>;
14
+ const section: NavigationItem<NavigationIcon> = {
15
+ id: 'progress',
16
+ label: 'Progress',
17
+ href: '/progress',
18
+ icon,
19
+ children: [
20
+ { id: 'overview', label: 'Overview', href: '/progress', icon },
21
+ { id: 'history', label: 'History', href: '/progress/history', icon },
22
+ ],
23
+ };
24
+ const items = [{ id: 'home', label: 'Home', href: '/', icon }, section];
25
+ const profile: NavigationProfile = { label: 'Account', initials: 'AB', href: '/profile' };
26
+ afterEach(cleanup);
27
+
28
+ describe('rail', () => {
29
+ it('renders links and an active disclosure with children immediately below it', () => {
30
+ const navigate = vi.fn();
31
+ render(
32
+ <AppNavigation
33
+ {...labels}
34
+ items={items}
35
+ profile={profile}
36
+ width={1024}
37
+ pathname="/progress/history"
38
+ onNavigate={navigate}
39
+ />,
40
+ );
41
+ expect(screen.getByRole('link', { name: 'Home' }).getAttribute('href')).toBe('/');
42
+ const parent = screen.getByRole('button', { name: 'Progress' });
43
+ expect(parent.getAttribute('aria-expanded')).toBe('true');
44
+ expect(parent.getAttribute('data-active')).toBe('ancestor');
45
+ const activeChild = screen.getByRole('link', { name: 'History' });
46
+ expect(activeChild.getAttribute('data-active')).toBe('page');
47
+ expect(within(parent).getByText('filled')).toBeTruthy();
48
+ expect(within(activeChild).getByText('filled')).toBeTruthy();
49
+ expect(screen.getByRole('navigation').querySelectorAll('[data-active="page"]')).toHaveLength(1);
50
+ expect(parent.nextElementSibling).toBe(screen.getByRole('group', { name: 'Progress' }));
51
+ expect(parent.getAttribute('aria-controls')).toBe(parent.nextElementSibling?.id);
52
+ expect(screen.getByRole('link', { name: 'History' }).getAttribute('aria-current')).toBe('page');
53
+ fireEvent.click(parent);
54
+ expect(parent.getAttribute('aria-expanded')).toBe('false');
55
+ expect(screen.queryByRole('link', { name: 'History' })).toBeNull();
56
+ expect(navigate).not.toHaveBeenCalled();
57
+ fireEvent.click(screen.getByRole('link', { name: 'Home' }));
58
+ expect(navigate).toHaveBeenCalledWith('/', expect.objectContaining({ defaultPrevented: true }));
59
+ });
60
+ it('fills the active parent in a collapsed rail and its child after expanding', () => {
61
+ render(
62
+ <AppNavigation
63
+ {...labels}
64
+ items={items}
65
+ width={1024}
66
+ pathname="/progress/history"
67
+ defaultCollapsed
68
+ />,
69
+ );
70
+ const parent = screen.getByRole('button', { name: 'Progress' });
71
+ expect(parent.getAttribute('data-active')).toBe('page');
72
+ expect(screen.queryByRole('link', { name: 'History' })).toBeNull();
73
+ fireEvent.click(parent);
74
+ expect(parent.getAttribute('data-active')).toBe('ancestor');
75
+ expect(screen.getByRole('link', { name: 'History' }).getAttribute('data-active')).toBe('page');
76
+ });
77
+ it('fills an active main item without children', () => {
78
+ render(<AppNavigation {...labels} items={items} width={1024} pathname="/" />);
79
+ expect(screen.getByRole('link', { name: 'Home' }).getAttribute('data-active')).toBe('page');
80
+ expect(screen.getByRole('button', { name: 'Progress' }).hasAttribute('data-active')).toBe(
81
+ false,
82
+ );
83
+ });
84
+ it('expands a collapsed rail and opens the clicked group', () => {
85
+ const changed = vi.fn();
86
+ render(
87
+ <AppNavigation
88
+ {...labels}
89
+ items={items}
90
+ width={1024}
91
+ pathname="/"
92
+ defaultCollapsed
93
+ onCollapsedChange={changed}
94
+ />,
95
+ );
96
+ expect(screen.getByRole('button', { name: 'Progress' }).title).toBe('Progress');
97
+ fireEvent.click(screen.getByRole('button', { name: 'Progress' }));
98
+ expect(changed).toHaveBeenCalledWith(false);
99
+ expect(screen.getByRole('navigation').getAttribute('data-collapsed')).toBe('false');
100
+ expect(screen.getByRole('button', { name: 'Progress' }).getAttribute('aria-expanded')).toBe(
101
+ 'true',
102
+ );
103
+ fireEvent.click(screen.getByRole('button', { name: 'Collapse navigation' }));
104
+ expect(screen.getByRole('navigation').getAttribute('data-collapsed')).toBe('true');
105
+ expect(screen.queryByRole('link', { name: 'History' })).toBeNull();
106
+ });
107
+ it('connects disclosures when product ids contain spaces', () => {
108
+ render(
109
+ <AppNavigation
110
+ {...labels}
111
+ items={[{ ...section, id: 'training progress' }]}
112
+ width={1024}
113
+ pathname="/progress"
114
+ />,
115
+ );
116
+ const parent = screen.getByRole('button', { name: 'Progress' });
117
+ const controls = parent.getAttribute('aria-controls');
118
+ expect(controls).not.toMatch(/\s/);
119
+ expect(document.getElementById(controls ?? '')).toBe(
120
+ screen.getByRole('group', { name: 'Progress' }),
121
+ );
122
+ });
123
+ it('respects controlled collapse and opens sections when the route changes', () => {
124
+ const changed = vi.fn();
125
+ const props = { items, width: 1024, onCollapsedChange: changed };
126
+ const { rerender } = render(<AppNavigation {...labels} {...props} pathname="/" collapsed />);
127
+ fireEvent.click(screen.getByRole('button', { name: 'Progress' }));
128
+ expect(changed).toHaveBeenCalledWith(false);
129
+ expect(screen.getByRole('navigation').getAttribute('data-collapsed')).toBe('true');
130
+ rerender(
131
+ <AppNavigation {...labels} {...props} pathname="/progress/history" collapsed={false} />,
132
+ );
133
+ expect(screen.getByRole('button', { name: 'Progress' }).getAttribute('aria-expanded')).toBe(
134
+ 'true',
135
+ );
136
+ });
137
+ it('moves focus with arrows, Home and End through visible children and profile', () => {
138
+ render(
139
+ <AppNavigation
140
+ {...labels}
141
+ items={items}
142
+ profile={profile}
143
+ width={1024}
144
+ pathname="/progress"
145
+ />,
146
+ );
147
+ const home = screen.getByRole('link', { name: 'Home' });
148
+ home.focus();
149
+ fireEvent.keyDown(home, { key: 'ArrowDown' });
150
+ expect(document.activeElement).toBe(screen.getByRole('button', { name: 'Progress' }));
151
+ fireEvent.keyDown(document.activeElement ?? home, { key: 'ArrowDown' });
152
+ expect(document.activeElement).toBe(screen.getByRole('link', { name: 'Overview' }));
153
+ fireEvent.keyDown(document.activeElement ?? home, { key: 'End' });
154
+ expect(document.activeElement).toBe(screen.getByRole('link', { name: 'Account' }));
155
+ fireEvent.keyDown(document.activeElement ?? home, { key: 'Home' });
156
+ expect(document.activeElement).toBe(home);
157
+ });
158
+ });
159
+ it.each([
160
+ { metaKey: true },
161
+ { ctrlKey: true },
162
+ { shiftKey: true },
163
+ { altKey: true },
164
+ { button: 1 },
165
+ ])('leaves modified clicks to the browser: %j', (modifier) => {
166
+ const navigate = vi.fn();
167
+ render(
168
+ <AppNavigation {...labels} items={items} width={320} pathname="/" onNavigate={navigate} />,
169
+ );
170
+ expect(fireEvent.click(screen.getByRole('link', { name: 'Home' }), modifier)).toBe(true);
171
+ expect(navigate).not.toHaveBeenCalled();
172
+ });
173
+ it('renders a real anchor through the optional link renderer', () => {
174
+ render(
175
+ <AppNavigation
176
+ {...labels}
177
+ items={items}
178
+ width={320}
179
+ pathname="/"
180
+ renderLink={(props) => <a {...props} data-router="custom" />}
181
+ />,
182
+ );
183
+ expect(screen.getByRole('link', { name: 'Home' }).getAttribute('data-router')).toBe('custom');
184
+ });
185
+ it('rotates active compact tabs and sends inactive tabs to their root', () => {
186
+ const navigate = vi.fn();
187
+ const { rerender } = render(
188
+ <AppNavigation
189
+ {...labels}
190
+ items={items}
191
+ width={1023}
192
+ pathname="/progress"
193
+ onNavigate={navigate}
194
+ />,
195
+ );
196
+ fireEvent.click(screen.getByRole('link', { name: 'Progress' }));
197
+ expect(screen.getByRole('link', { name: 'Progress' }).getAttribute('data-active')).toBe('page');
198
+ expect(navigate).toHaveBeenLastCalledWith('/progress/history', expect.anything());
199
+ rerender(
200
+ <AppNavigation
201
+ {...labels}
202
+ items={items}
203
+ width={320}
204
+ pathname="/progress/history"
205
+ onNavigate={navigate}
206
+ />,
207
+ );
208
+ fireEvent.click(screen.getByRole('link', { name: 'Progress' }));
209
+ expect(navigate).toHaveBeenLastCalledWith('/progress', expect.anything());
210
+ fireEvent.click(screen.getByRole('link', { name: 'Home' }));
211
+ expect(navigate).toHaveBeenLastCalledWith('/', expect.anything());
212
+ });
213
+ it.each([320, 1024])('keeps profile last at width %i', (width) => {
214
+ render(
215
+ <AppNavigation {...labels} items={items} profile={profile} width={width} pathname="/profile" />,
216
+ );
217
+ const links = within(screen.getByRole('navigation')).getAllByRole('link');
218
+ expect(links.at(-1)).toBe(screen.getByRole('link', { name: 'Account' }));
219
+ expect(links.at(-1)?.getAttribute('data-active')).toBe('page');
220
+ expect(links.at(-1)?.getAttribute('aria-current')).toBe('page');
221
+ });
222
+ it('handles profile menu focus, disabled items, actions, Escape and outside clicks', () => {
223
+ const action = vi.fn();
224
+ render(
225
+ <AppNavigation
226
+ {...labels}
227
+ items={items}
228
+ width={320}
229
+ pathname="/"
230
+ profile={{
231
+ label: 'Account',
232
+ initials: 'AB',
233
+ menu: [
234
+ { id: 'disabled', label: 'Unavailable', href: '/disabled', disabled: true },
235
+ { id: 'settings', label: 'Settings', href: '/settings' },
236
+ { id: 'signout', label: 'Sign out', onSelect: action },
237
+ ],
238
+ }}
239
+ />,
240
+ );
241
+ const trigger = screen.getByRole('button', { name: 'Account' });
242
+ fireEvent.keyDown(trigger, { key: 'ArrowDown' });
243
+ expect(document.activeElement).toBe(screen.getByRole('menuitem', { name: 'Settings' }));
244
+ fireEvent.keyDown(document.activeElement ?? trigger, { key: 'End' });
245
+ expect(document.activeElement).toBe(screen.getByRole('menuitem', { name: 'Sign out' }));
246
+ fireEvent.keyDown(document.activeElement ?? trigger, { key: 'Escape' });
247
+ expect(screen.queryByRole('menu')).toBeNull();
248
+ expect(document.activeElement).toBe(trigger);
249
+ fireEvent.click(trigger);
250
+ fireEvent.click(screen.getByRole('menuitem', { name: 'Sign out' }));
251
+ expect(action).toHaveBeenCalledOnce();
252
+ expect(screen.queryByRole('menu')).toBeNull();
253
+ fireEvent.click(trigger);
254
+ fireEvent.pointerDown(document.body);
255
+ expect(screen.queryByRole('menu')).toBeNull();
256
+ fireEvent.click(trigger);
257
+ fireEvent.keyDown(screen.getByRole('menuitem', { name: 'Settings' }), { key: 'Tab' });
258
+ expect(screen.queryByRole('menu')).toBeNull();
259
+ });
260
+ it('lets a picker select a section page directly', () => {
261
+ const navigate = vi.fn();
262
+ render(<SectionPicker item={section} pathname="/progress" onNavigate={navigate} />);
263
+ fireEvent.click(screen.getByRole('button', { name: 'Progress, Overview' }));
264
+ fireEvent.click(screen.getByRole('menuitem', { name: 'History' }));
265
+ expect(navigate).toHaveBeenCalledWith('/progress/history', expect.anything());
266
+ expect(screen.queryByRole('menu')).toBeNull();
267
+ });
268
+ it('keeps an all-disabled profile menu dismissible with the keyboard', () => {
269
+ render(
270
+ <AppNavigation
271
+ {...labels}
272
+ items={items}
273
+ width={320}
274
+ pathname="/"
275
+ profile={{
276
+ label: 'Account',
277
+ initials: 'AB',
278
+ menu: [{ id: 'unavailable', label: 'Unavailable', href: '/settings', disabled: true }],
279
+ }}
280
+ />,
281
+ );
282
+ const trigger = screen.getByRole('button', { name: 'Account' });
283
+ fireEvent.keyDown(trigger, { key: 'ArrowDown' });
284
+ const menu = screen.getByRole('menu');
285
+ expect(document.activeElement).toBe(menu);
286
+ fireEvent.keyDown(menu, { key: 'Escape' });
287
+ expect(screen.queryByRole('menu')).toBeNull();
288
+ expect(document.activeElement).toBe(trigger);
289
+ });
290
+
291
+ it('marks only the most-specific profile route current with query and hash hrefs', () => {
292
+ const props = {
293
+ ...labels,
294
+ items,
295
+ profile: {
296
+ label: 'Account',
297
+ initials: 'AB',
298
+ menu: [
299
+ { id: 'settings', label: 'App settings', href: '/settings' },
300
+ { id: 'profile-data', label: 'Profile and data', href: '/settings/profile?tab=data#top' },
301
+ { id: 'account-alias', label: 'Alias', href: '/account', matches: () => false },
302
+ ],
303
+ },
304
+ };
305
+ const { rerender } = render(
306
+ <AppNavigation {...props} pathname="/settings/profile?tab=data#top" />,
307
+ );
308
+ fireEvent.click(screen.getByRole('button', { name: 'Account' }));
309
+ const menu = screen.getByRole('menu');
310
+ expect(menu.querySelectorAll('[data-active="page"]')).toHaveLength(1);
311
+ expect(menu.querySelectorAll('[aria-current="page"]')).toHaveLength(1);
312
+ expect(
313
+ screen.getByRole('menuitem', { name: 'Profile and data' }).getAttribute('aria-current'),
314
+ ).toBe('page');
315
+ expect(screen.getByRole('menuitem', { name: 'App settings' }).hasAttribute('data-active')).toBe(
316
+ false,
317
+ );
318
+ rerender(<AppNavigation {...props} pathname="/settings/profile" />);
319
+ expect(menu.querySelectorAll('[data-active="page"]')).toHaveLength(1);
320
+ expect(menu.querySelectorAll('[aria-current="page"]')).toHaveLength(1);
321
+ expect(
322
+ screen.getByRole('menuitem', { name: 'Profile and data' }).getAttribute('aria-current'),
323
+ ).toBe('page');
324
+ });
325
+ it('uses a profile entry matcher instead of its href', () => {
326
+ render(
327
+ <AppNavigation
328
+ {...labels}
329
+ items={items}
330
+ pathname="/alias?tab=profile"
331
+ profile={{
332
+ label: 'Account',
333
+ initials: 'AB',
334
+ menu: [
335
+ {
336
+ id: 'profile-data',
337
+ label: 'Profile and data',
338
+ href: '/settings/profile',
339
+ matches: (path) => path === '/alias?tab=profile',
340
+ },
341
+ ],
342
+ }}
343
+ />,
344
+ );
345
+ fireEvent.click(screen.getByRole('button', { name: 'Account' }));
346
+ expect(
347
+ screen.getByRole('menuitem', { name: 'Profile and data' }).getAttribute('aria-current'),
348
+ ).toBe('page');
349
+ });
350
+ it('uses product labels for the navigation and collapse controls', () => {
351
+ render(
352
+ <AppNavigation
353
+ items={items}
354
+ pathname="/"
355
+ width={1024}
356
+ label="Hauptnavigation"
357
+ collapseLabel="Einklappen"
358
+ expandLabel="Ausklappen"
359
+ />,
360
+ );
361
+ expect(screen.getByRole('navigation', { name: 'Hauptnavigation' })).toBeTruthy();
362
+ fireEvent.click(screen.getByRole('button', { name: 'Einklappen' }));
363
+ expect(screen.getByRole('button', { name: 'Ausklappen' })).toBeTruthy();
364
+ });
365
+
366
+ it.each([false, true])(
367
+ 'includes the profile subtitle in its label when collapsed is %s',
368
+ (collapsed) => {
369
+ render(
370
+ <AppNavigation
371
+ {...labels}
372
+ items={items}
373
+ width={1024}
374
+ pathname="/"
375
+ collapsed={collapsed}
376
+ profile={{ label: 'Account', subtitle: 'Ada, syncing', initials: 'AD', href: '/profile' }}
377
+ />,
378
+ );
379
+ expect(screen.getByRole('link', { name: 'Account, Ada, syncing' }).getAttribute('href')).toBe(
380
+ '/profile',
381
+ );
382
+ },
383
+ );
384
+
385
+ it('renders the profile subtitle in the rail and in its menu', () => {
386
+ render(
387
+ <AppNavigation
388
+ {...labels}
389
+ items={items}
390
+ width={1024}
391
+ pathname="/"
392
+ profile={{
393
+ label: 'Account',
394
+ subtitle: 'Ada, synced',
395
+ initials: 'AD',
396
+ menu: [{ id: 'settings', label: 'Settings', href: '/settings' }],
397
+ }}
398
+ />,
399
+ );
400
+ const button = screen.getByRole('button', { name: 'Account, Ada, synced' });
401
+ expect(within(button).getByText('Ada, synced')).not.toBeNull();
402
+ fireEvent.click(button);
403
+ const menu = screen.getByRole('menu', { name: 'Account, Ada, synced' });
404
+ expect(within(menu).getByText('Account')).not.toBeNull();
405
+ expect(within(menu).getByText('Ada, synced')).not.toBeNull();
406
+ expect(within(menu).getByRole('menuitem', { name: 'Settings' })).not.toBeNull();
407
+ });
408
+
409
+ it.each([
410
+ { width: 320, collapsed: false },
411
+ { width: 1024, collapsed: false },
412
+ { width: 1024, collapsed: true },
413
+ ])('renders a custom profile avatar at $width px, collapsed=$collapsed', ({ width, collapsed }) => {
414
+ const renderAvatar = vi.fn(({ active, size }: { active: boolean; size: number }) => (
415
+ <span role="img" aria-label="Product glyph" data-testid="profile-glyph">
416
+ {active ? 'Selected glyph' : 'Glyph'} at {size}
417
+ </span>
418
+ ));
419
+ const { rerender } = render(
420
+ <AppNavigation
421
+ {...labels}
422
+ items={items}
423
+ width={width}
424
+ pathname="/settings"
425
+ collapsed={collapsed}
426
+ profile={{
427
+ label: 'Account',
428
+ subtitle: 'Ada, synced',
429
+ initials: 'AD',
430
+ imageUrl: '/avatar.png',
431
+ renderAvatar,
432
+ menu: [{ id: 'settings', label: 'Settings', href: '/settings' }],
433
+ }}
434
+ />,
435
+ );
436
+ const button = screen.getByRole('button', { name: 'Account, Ada, synced' });
437
+ expect(within(button).getByTestId('profile-glyph').textContent).toBe('Selected glyph at 28');
438
+ expect(screen.queryByRole('img', { name: 'Product glyph' })).toBeNull();
439
+ expect(button.querySelector('img')).toBeNull();
440
+ expect(renderAvatar).toHaveBeenCalledWith({ active: true, size: 28 });
441
+ button.focus();
442
+ fireEvent.click(button);
443
+ const menu = screen.getByRole('menu', { name: 'Account, Ada, synced' });
444
+ expect(within(menu).getByText('Ada, synced')).not.toBeNull();
445
+ expect(document.activeElement).toBe(within(menu).getByRole('menuitem', { name: 'Settings' }));
446
+ fireEvent.keyDown(menu, { key: 'Escape' });
447
+ expect(document.activeElement).toBe(button);
448
+ rerender(
449
+ <AppNavigation
450
+ {...labels}
451
+ items={items}
452
+ width={width}
453
+ pathname="/"
454
+ collapsed={collapsed}
455
+ profile={{ label: 'Account', initials: 'AD', renderAvatar, href: '/profile' }}
456
+ />,
457
+ );
458
+ expect(screen.getByRole('link', { name: 'Account' }).textContent).toContain('Glyph at 28');
459
+ expect(renderAvatar).toHaveBeenLastCalledWith({ active: false, size: 28 });
460
+ });
461
+
462
+ it.each([600, 1024])('enters the visible parent of a hidden active child at width %s', (width) => {
463
+ const insights = {
464
+ ...section,
465
+ id: 'insights',
466
+ label: 'Insights',
467
+ href: '/insights',
468
+ children: [{ id: 'charts', label: 'Charts', href: '/insights/charts' }],
469
+ };
470
+ render(
471
+ <AppNavigation
472
+ {...labels}
473
+ items={[{ id: 'home', label: 'Home', href: '/', icon }, insights]}
474
+ width={width}
475
+ pathname="/insights/charts?range=4w"
476
+ defaultCollapsed
477
+ />,
478
+ );
479
+ const parent = screen.getByRole(width === 1024 ? 'button' : 'link', { name: 'Insights' });
480
+ expect(parent.tabIndex).toBe(0);
481
+ expect(screen.getByRole('link', { name: 'Home' }).tabIndex).toBe(-1);
482
+ expect(screen.queryByRole('link', { name: 'Charts' })).toBeNull();
483
+ });