@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,833 @@
1
+ import { act, fireEvent, render, screen, within } from '@testing-library/react-native';
2
+ import { AccessibilityInfo, Platform, Text } from 'react-native';
3
+ import {
4
+ AppNavigation,
5
+ useNavigationBarHeight,
6
+ SectionPicker,
7
+ type NavigationIcon,
8
+ type NavigationTheme,
9
+ } from './native.js';
10
+ import type { NavigationItem } from './index.js';
11
+
12
+ // Resolve lazy native modules during setup, before Jest starts each test's timer.
13
+ const nativeModules = jest.requireActual<typeof import('react-native')>('react-native');
14
+ for (const name of [
15
+ 'AccessibilityInfo',
16
+ 'Image',
17
+ 'Modal',
18
+ 'Pressable',
19
+ 'ScrollView',
20
+ 'Text',
21
+ 'View',
22
+ 'useWindowDimensions',
23
+ ]) {
24
+ Reflect.get(nativeModules, name);
25
+ }
26
+
27
+ const labels = {
28
+ label: 'Primary',
29
+ collapseLabel: 'Collapse navigation',
30
+ expandLabel: 'Expand navigation',
31
+ closeLabel: 'Close',
32
+ };
33
+
34
+ const icon: NavigationIcon = ({ active }) => <Text>{active ? 'filled' : 'outline'}</Text>;
35
+ const section: NavigationItem<NavigationIcon> = {
36
+ id: 'progress',
37
+ label: 'Progress',
38
+ href: '/progress',
39
+ icon,
40
+ children: [
41
+ { id: 'overview', label: 'Overview', href: '/progress', icon },
42
+ { id: 'history', label: 'History', href: '/progress/history', icon },
43
+ ],
44
+ };
45
+ const items = [{ id: 'home', label: 'Home', href: '/', icon }, section];
46
+ const theme: NavigationTheme = {
47
+ background: '#fff',
48
+ text: '#111',
49
+ muted: '#595959',
50
+ activeBackground: '#dbe9f8',
51
+ activeText: '#005fcc',
52
+ ancestorText: '#004799',
53
+ border: '#767676',
54
+ focus: '#005fcc',
55
+ radius: 4,
56
+ spacing: 8,
57
+ };
58
+ const profile = { label: 'Account', initials: 'AB', href: '/profile' };
59
+ const event = () => ({ nativeEvent: {}, preventDefault: jest.fn() });
60
+
61
+ it('keeps keyboard focus visible on active navigation, picker and menu entries', async () => {
62
+ await render(
63
+ <>
64
+ <AppNavigation
65
+ {...labels}
66
+ items={items}
67
+ width={1024}
68
+ pathname="/progress/history"
69
+ theme={theme}
70
+ onNavigate={jest.fn()}
71
+ />
72
+ <SectionPicker
73
+ closeLabel={labels.closeLabel}
74
+ item={section}
75
+ pathname="/progress/history"
76
+ theme={theme}
77
+ onNavigate={jest.fn()}
78
+ />
79
+ </>,
80
+ );
81
+ const active = screen.getByRole('link', { name: 'History' });
82
+ await fireEvent(active, 'focus', event());
83
+ expect(active).toHaveStyle({
84
+ borderWidth: 3,
85
+ borderColor: theme.activeText,
86
+ backgroundColor: theme.activeBackground,
87
+ });
88
+ await fireEvent(active, 'blur', event());
89
+ expect(active).toHaveStyle({ borderColor: 'transparent' });
90
+ const picker = screen.getByRole('button', { name: 'Progress, History' });
91
+ await fireEvent(picker, 'focus', event());
92
+ expect(picker).toHaveStyle({ borderWidth: 3, borderColor: theme.focus });
93
+ await fireEvent(picker, 'blur', event());
94
+ expect(picker).toHaveStyle({ borderColor: theme.border });
95
+ await fireEvent.press(picker, event());
96
+ const entry = screen.getByRole('menuitem', { name: 'History' });
97
+ await fireEvent(entry, 'focus', event());
98
+ expect(entry).toHaveStyle({ borderWidth: 3, borderColor: theme.activeText });
99
+ await fireEvent(entry, 'blur', event());
100
+ expect(entry).toHaveStyle({ borderColor: 'transparent' });
101
+ });
102
+
103
+ it('shows an active disclosure with its selected child directly inside its section', async () => {
104
+ const navigate = jest.fn();
105
+ await render(
106
+ <AppNavigation
107
+ {...labels}
108
+ items={items}
109
+ profile={profile}
110
+ width={1024}
111
+ pathname="/progress/history"
112
+ theme={theme}
113
+ onNavigate={navigate}
114
+ />,
115
+ );
116
+ expect(screen.getByRole('button', { name: 'Progress' })).toHaveProp('accessibilityState', {
117
+ expanded: true,
118
+ selected: true,
119
+ });
120
+ expect(screen.getByRole('link', { name: 'History' })).toHaveProp('accessibilityState', {
121
+ selected: true,
122
+ });
123
+ expect(screen.getByRole('button', { name: 'Progress' })).toHaveStyle({
124
+ backgroundColor: theme.background,
125
+ });
126
+ expect(
127
+ within(screen.getByRole('button', { name: 'Progress' })).getByText('Progress'),
128
+ ).toHaveStyle({
129
+ color: theme.ancestorText,
130
+ fontWeight: '400',
131
+ });
132
+ expect(screen.getByRole('link', { name: 'History' })).toHaveStyle({
133
+ backgroundColor: theme.activeBackground,
134
+ });
135
+ expect(within(screen.getByRole('link', { name: 'History' })).getByText('History')).toHaveStyle({
136
+ color: theme.activeText,
137
+ fontWeight: '700',
138
+ });
139
+ for (const target of [
140
+ screen.getByRole('button', { name: 'Progress' }),
141
+ screen.getByRole('link', { name: 'History' }),
142
+ ]) {
143
+ expect(within(target).getByText('filled', { includeHiddenElements: true })).toBeOnTheScreen();
144
+ }
145
+ expect([
146
+ within(screen.getByTestId('navigation-section-progress')).getByRole('link', {
147
+ name: 'History',
148
+ }),
149
+ ]).toContain(screen.getByRole('link', { name: 'History' }));
150
+ await fireEvent.press(screen.getByRole('button', { name: 'Progress' }), event());
151
+ expect(screen.queryByRole('link', { name: 'History' })).toBeNull();
152
+ expect(navigate).not.toHaveBeenCalled();
153
+ await fireEvent.press(screen.getByRole('link', { name: 'Home' }), event());
154
+ expect(navigate).toHaveBeenCalledWith('/');
155
+ });
156
+ it('expands a collapsed rail, opens its group and notifies the caller', async () => {
157
+ const changed = jest.fn();
158
+ await render(
159
+ <AppNavigation
160
+ {...labels}
161
+ items={items}
162
+ width={1024}
163
+ pathname="/"
164
+ theme={theme}
165
+ onNavigate={jest.fn()}
166
+ defaultCollapsed
167
+ onCollapsedChange={changed}
168
+ />,
169
+ );
170
+ expect(screen.queryByText('Progress')).toBeNull();
171
+ await fireEvent.press(screen.getByRole('button', { name: 'Progress' }), event());
172
+ expect(changed).toHaveBeenCalledWith(false);
173
+ expect(screen.getByRole('button', { name: 'Progress' })).toHaveProp('accessibilityState', {
174
+ expanded: true,
175
+ selected: false,
176
+ });
177
+ expect(screen.getByRole('link', { name: 'History' })).toBeOnTheScreen();
178
+ await fireEvent.press(screen.getByRole('button', { name: 'Collapse navigation' }), event());
179
+ expect(changed).toHaveBeenLastCalledWith(true);
180
+ expect(screen.queryByText('Progress')).toBeNull();
181
+ });
182
+ it('fills the active parent only while the rail is collapsed', async () => {
183
+ await render(
184
+ <AppNavigation
185
+ {...labels}
186
+ items={items}
187
+ width={1024}
188
+ pathname="/progress/history"
189
+ theme={theme}
190
+ onNavigate={jest.fn()}
191
+ defaultCollapsed
192
+ />,
193
+ );
194
+ const parent = screen.getByRole('button', { name: 'Progress' });
195
+ expect(parent).toHaveStyle({ backgroundColor: theme.activeBackground });
196
+ expect(screen.queryByRole('link', { name: 'History' })).toBeNull();
197
+ await fireEvent.press(parent, event());
198
+ expect(parent).toHaveStyle({ backgroundColor: theme.background });
199
+ expect(screen.getByRole('link', { name: 'History' })).toHaveStyle({
200
+ backgroundColor: theme.activeBackground,
201
+ });
202
+ });
203
+ it('fills an active main item without children', async () => {
204
+ await render(
205
+ <AppNavigation
206
+ {...labels}
207
+ items={items}
208
+ width={1024}
209
+ pathname="/"
210
+ theme={theme}
211
+ onNavigate={jest.fn()}
212
+ />,
213
+ );
214
+ const home = screen.getByRole('link', { name: 'Home' });
215
+ expect(home).toHaveStyle({ backgroundColor: theme.activeBackground, borderColor: 'transparent' });
216
+ expect(within(home).getByText('Home')).toHaveStyle({
217
+ color: theme.activeText,
218
+ fontWeight: '700',
219
+ });
220
+ });
221
+ it('respects controlled collapse and route changes', async () => {
222
+ const changed = jest.fn();
223
+ const props = { items, width: 1024, theme, onNavigate: jest.fn(), onCollapsedChange: changed };
224
+ const view = await render(<AppNavigation {...labels} {...props} collapsed pathname="/" />);
225
+ await fireEvent.press(screen.getByRole('button', { name: 'Progress' }), event());
226
+ expect(changed).toHaveBeenCalledWith(false);
227
+ expect(screen.queryByText('Progress')).toBeNull();
228
+ await view.rerender(
229
+ <AppNavigation {...labels} {...props} collapsed={false} pathname="/progress/history" />,
230
+ );
231
+ expect(screen.getByRole('link', { name: 'History' })).toHaveProp('accessibilityState', {
232
+ selected: true,
233
+ });
234
+ });
235
+ it.each([320, 1023])(
236
+ 'rotates active bottom tabs and navigates inactive tabs to the root at %i',
237
+ async (width) => {
238
+ const navigate = jest.fn();
239
+ const view = await render(
240
+ <AppNavigation
241
+ {...labels}
242
+ items={items}
243
+ width={width}
244
+ pathname="/progress"
245
+ theme={theme}
246
+ onNavigate={navigate}
247
+ />,
248
+ );
249
+ expect(screen.getByRole('link', { name: 'Progress' })).toHaveStyle({
250
+ backgroundColor: theme.activeBackground,
251
+ });
252
+ await fireEvent.press(screen.getByRole('link', { name: 'Progress' }), event());
253
+ expect(navigate).toHaveBeenLastCalledWith('/progress/history');
254
+ await view.rerender(
255
+ <AppNavigation
256
+ {...labels}
257
+ items={items}
258
+ width={width}
259
+ pathname="/progress/history"
260
+ theme={theme}
261
+ onNavigate={navigate}
262
+ />,
263
+ );
264
+ await fireEvent.press(screen.getByRole('link', { name: 'Progress' }), event());
265
+ expect(navigate).toHaveBeenLastCalledWith('/progress');
266
+ await fireEvent.press(screen.getByRole('link', { name: 'Home' }), event());
267
+ expect(navigate).toHaveBeenLastCalledWith('/');
268
+ },
269
+ );
270
+ it.each([320, 1024])('keeps profile last at %i', async (width) => {
271
+ await render(
272
+ <AppNavigation
273
+ {...labels}
274
+ items={items}
275
+ profile={profile}
276
+ width={width}
277
+ pathname="/profile"
278
+ theme={theme}
279
+ onNavigate={jest.fn()}
280
+ />,
281
+ );
282
+ const links = screen.getAllByRole('link');
283
+ expect(links.at(-1)).toBe(screen.getByRole('link', { name: 'Account' }));
284
+ expect(links.at(-1)).toHaveProp('accessibilityState', { selected: true });
285
+ expect(links.at(-1)).toHaveStyle({ backgroundColor: theme.activeBackground });
286
+ });
287
+ it.each([
288
+ { metaKey: true },
289
+ { ctrlKey: true },
290
+ { shiftKey: true },
291
+ { altKey: true },
292
+ { button: 1 },
293
+ ])('preserves modified web presses: %j', async (modifier) => {
294
+ const navigate = jest.fn();
295
+ await render(
296
+ <AppNavigation
297
+ {...labels}
298
+ items={items}
299
+ width={320}
300
+ pathname="/"
301
+ theme={theme}
302
+ onNavigate={navigate}
303
+ />,
304
+ );
305
+ const press = { nativeEvent: modifier, preventDefault: jest.fn() };
306
+ await fireEvent.press(screen.getByRole('link', { name: 'Home' }), press);
307
+ expect(navigate).not.toHaveBeenCalled();
308
+ expect(press.preventDefault).not.toHaveBeenCalled();
309
+ });
310
+ it('uses roving tab stops for Arrow, Home and End', async () => {
311
+ await render(
312
+ <AppNavigation
313
+ {...labels}
314
+ items={items}
315
+ profile={profile}
316
+ width={1024}
317
+ pathname="/"
318
+ theme={theme}
319
+ onNavigate={jest.fn()}
320
+ />,
321
+ );
322
+ const key = (value: string) => ({ nativeEvent: { key: value }, preventDefault: jest.fn() });
323
+ expect(screen.getByRole('link', { name: 'Home' })).toHaveProp('tabIndex', 0);
324
+ await fireEvent(screen.getByRole('link', { name: 'Home' }), 'keyDown', key('ArrowDown'));
325
+ expect(screen.getByRole('button', { name: 'Progress' })).toHaveProp('tabIndex', 0);
326
+ await fireEvent(screen.getByRole('button', { name: 'Progress' }), 'keyDown', key('End'));
327
+ expect(screen.getByRole('link', { name: 'Account' })).toHaveProp('tabIndex', 0);
328
+ await fireEvent(screen.getByRole('link', { name: 'Account' }), 'keyDown', key('Home'));
329
+ expect(screen.getByRole('link', { name: 'Home' })).toHaveProp('tabIndex', 0);
330
+ });
331
+ it('opens a profile menu, runs an entry and closes', async () => {
332
+ const action = jest.fn();
333
+ await render(
334
+ <AppNavigation
335
+ {...labels}
336
+ items={items}
337
+ width={320}
338
+ pathname="/"
339
+ theme={theme}
340
+ onNavigate={jest.fn()}
341
+ profile={{
342
+ label: 'Account',
343
+ initials: 'AB',
344
+ menu: [{ id: 'signout', label: 'Sign out', onSelect: action }],
345
+ }}
346
+ />,
347
+ );
348
+ await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
349
+ expect(screen.getByRole('menuitem', { name: 'Sign out' })).toBeOnTheScreen();
350
+ await fireEvent.press(screen.getByRole('menuitem', { name: 'Sign out' }), event());
351
+ expect(action).toHaveBeenCalledTimes(1);
352
+ expect(screen.queryByRole('menuitem')).toBeNull();
353
+ });
354
+ it('shows a full-width section picker and navigates directly to a subpage', async () => {
355
+ const navigate = jest.fn();
356
+ await render(
357
+ <SectionPicker
358
+ closeLabel={labels.closeLabel}
359
+ item={section}
360
+ pathname="/progress"
361
+ theme={theme}
362
+ onNavigate={navigate}
363
+ />,
364
+ );
365
+ await fireEvent.press(screen.getByRole('button', { name: 'Progress, Overview' }), event());
366
+ await fireEvent.press(screen.getByRole('menuitem', { name: 'History' }), event());
367
+ expect(navigate).toHaveBeenCalledWith('/progress/history');
368
+ expect(screen.queryByRole('menuitem')).toBeNull();
369
+ });
370
+ it('skips disabled menu entries with the keyboard and dismisses with Escape', async () => {
371
+ const action = jest.fn();
372
+ await render(
373
+ <AppNavigation
374
+ {...labels}
375
+ items={items}
376
+ width={320}
377
+ pathname="/"
378
+ theme={theme}
379
+ onNavigate={jest.fn()}
380
+ profile={{
381
+ label: 'Account',
382
+ initials: 'AB',
383
+ menu: [
384
+ { id: 'settings', label: 'Settings', href: '/settings' },
385
+ { id: 'disabled', label: 'Unavailable', href: '/unavailable', disabled: true },
386
+ { id: 'signout', label: 'Sign out', onSelect: action },
387
+ ],
388
+ }}
389
+ />,
390
+ );
391
+ const key = (value: string) => ({
392
+ nativeEvent: { key: value },
393
+ preventDefault: jest.fn(),
394
+ stopPropagation: jest.fn(),
395
+ });
396
+ await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
397
+ expect(screen.getByRole('menuitem', { name: 'Unavailable' })).toBeDisabled();
398
+ await fireEvent(screen.getByRole('menuitem', { name: 'Settings' }), 'keyDown', key('ArrowDown'));
399
+ expect(screen.getByRole('menuitem', { name: 'Sign out' })).toHaveProp('tabIndex', 0);
400
+ await fireEvent(screen.getByTestId('navigation-menu'), 'keyDown', key('Escape'));
401
+ expect(screen.queryByTestId('navigation-menu')).toBeNull();
402
+ expect(action).not.toHaveBeenCalled();
403
+ expect(screen.getByRole('button', { name: 'Account' })).toHaveProp('accessibilityState', {
404
+ selected: false,
405
+ expanded: false,
406
+ });
407
+ });
408
+
409
+ it('marks the current section picker entry selected and highlights it', async () => {
410
+ await render(
411
+ <SectionPicker
412
+ closeLabel="Schließen"
413
+ item={section}
414
+ pathname="/progress/history?tab=recent"
415
+ theme={theme}
416
+ onNavigate={jest.fn()}
417
+ />,
418
+ );
419
+ await fireEvent.press(screen.getByRole('button', { name: 'Progress, History' }), event());
420
+ const current = screen.getByRole('menuitem', { name: 'History', selected: true });
421
+ expect(current).toHaveStyle({ backgroundColor: theme.activeBackground });
422
+ expect(within(current).getByText('History')).toHaveStyle({
423
+ color: theme.activeText,
424
+ fontWeight: '700',
425
+ });
426
+ expect(screen.getByRole('menuitem', { name: 'Overview' })).toHaveProp('accessibilityState', {
427
+ disabled: false,
428
+ selected: false,
429
+ });
430
+ const close = screen.getByRole('button', { name: 'Schließen' });
431
+ expect(within(close).getByText('Schließen')).toBeOnTheScreen();
432
+ await fireEvent.press(close, event());
433
+ expect(screen.queryByRole('menuitem')).toBeNull();
434
+ });
435
+ it('marks only the most-specific profile entry selected and honors custom matches', async () => {
436
+ const props = {
437
+ ...labels,
438
+ items,
439
+ width: 320,
440
+ theme,
441
+ onNavigate: jest.fn(),
442
+ profile: {
443
+ label: 'Account',
444
+ initials: 'AB',
445
+ menu: [
446
+ { id: 'settings', label: 'App settings', href: '/settings' },
447
+ {
448
+ id: 'profile-data',
449
+ label: 'Profile and data',
450
+ href: '/settings/profile?tab=data',
451
+ matches: (path: string) =>
452
+ path.startsWith('/alias') || path.startsWith('/settings/profile'),
453
+ },
454
+ ],
455
+ },
456
+ };
457
+ const { rerender } = await render(<AppNavigation {...props} pathname="/settings/profile" />);
458
+ await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
459
+ const current = screen.getByRole('menuitem', { name: 'Profile and data', selected: true });
460
+ expect(current).toHaveStyle({ backgroundColor: theme.activeBackground });
461
+ expect(within(current).getByText('Profile and data')).toHaveStyle({
462
+ color: theme.activeText,
463
+ fontWeight: '700',
464
+ });
465
+ expect(screen.getByRole('menuitem', { name: 'App settings' })).toHaveProp('accessibilityState', {
466
+ disabled: false,
467
+ selected: false,
468
+ });
469
+ await rerender(<AppNavigation {...props} pathname="/alias" />);
470
+ expect(
471
+ screen.getByRole('menuitem', { name: 'Profile and data', selected: true }),
472
+ ).toBeOnTheScreen();
473
+ });
474
+ it.each(['android', 'ios'] as const)(
475
+ 'sizes every %s target to its platform minimum',
476
+ async (platform) => {
477
+ const original = Platform.OS;
478
+ Platform.OS = platform;
479
+ try {
480
+ const minimum = platform === 'android' ? 48 : 44;
481
+ const props = {
482
+ ...labels,
483
+ items,
484
+ theme,
485
+ onNavigate: jest.fn(),
486
+ profile: {
487
+ label: 'Account',
488
+ initials: 'AB',
489
+ menu: [
490
+ { id: 'settings', label: 'Settings', href: '/settings' },
491
+ { id: 'action', label: 'Action', onSelect: jest.fn() },
492
+ ],
493
+ },
494
+ };
495
+ const { rerender } = await render(
496
+ <>
497
+ <AppNavigation {...props} width={1024} pathname="/progress/history" />
498
+ <SectionPicker
499
+ closeLabel={labels.closeLabel}
500
+ item={section}
501
+ pathname="/progress/history"
502
+ theme={theme}
503
+ onNavigate={jest.fn()}
504
+ />
505
+ </>,
506
+ );
507
+ function expectTargets() {
508
+ for (const role of ['button', 'link', 'menuitem'] as const) {
509
+ for (const target of screen.queryAllByRole(role))
510
+ expect(target).toHaveStyle({ minWidth: minimum, minHeight: minimum });
511
+ }
512
+ }
513
+ expectTargets();
514
+ await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
515
+ expect(screen.getAllByRole('menuitem')).toHaveLength(2);
516
+ expectTargets();
517
+ await fireEvent.press(screen.getByRole('button', { name: labels.closeLabel }), event());
518
+ await fireEvent.press(screen.getByRole('button', { name: 'Progress, History' }), event());
519
+ expect(screen.getAllByRole('menuitem')).toHaveLength(2);
520
+ expectTargets();
521
+ await rerender(<AppNavigation {...props} width={320} pathname="/" />);
522
+ expect(screen.getAllByRole('link')).toHaveLength(2);
523
+ expectTargets();
524
+ } finally {
525
+ Platform.OS = original;
526
+ }
527
+ },
528
+ );
529
+
530
+ it('waits for Modal presentation before moving accessibility focus', async () => {
531
+ const frames: FrameRequestCallback[] = [];
532
+ const frame = jest.spyOn(globalThis, 'requestAnimationFrame').mockImplementation((callback) => {
533
+ frames.push(callback);
534
+ return frames.length;
535
+ });
536
+ const focus = jest.spyOn(AccessibilityInfo, 'setAccessibilityFocus').mockReturnValue(undefined);
537
+ const handle = jest.spyOn(nativeModules, 'findNodeHandle').mockReturnValue(42);
538
+ try {
539
+ await render(
540
+ <SectionPicker
541
+ closeLabel="Close"
542
+ item={section}
543
+ pathname="/progress/history"
544
+ theme={theme}
545
+ onNavigate={jest.fn()}
546
+ />,
547
+ );
548
+ await fireEvent.press(screen.getByRole('button', { name: 'Progress, History' }), event());
549
+ await act(() => {
550
+ for (const callback of frames) callback(0);
551
+ });
552
+ expect(focus).not.toHaveBeenCalled();
553
+ const modal = screen.container.queryAll(
554
+ (node) => typeof node.props['onShow'] === 'function',
555
+ )[0];
556
+ if (modal === undefined) throw new Error('Modal presentation callback is missing');
557
+ await fireEvent(modal, 'show');
558
+ expect(focus).toHaveBeenCalledTimes(1);
559
+ expect(focus).toHaveBeenCalledWith(42);
560
+ await fireEvent.press(screen.getByRole('button', { name: 'Close' }), event());
561
+ expect(focus).toHaveBeenCalledTimes(2);
562
+ } finally {
563
+ frame.mockRestore();
564
+ focus.mockRestore();
565
+ handle.mockRestore();
566
+ }
567
+ });
568
+ it('hides navigation behind the profile menu from screen readers and restores it on close', async () => {
569
+ await render(
570
+ <AppNavigation
571
+ {...labels}
572
+ items={items}
573
+ width={320}
574
+ pathname="/"
575
+ theme={theme}
576
+ onNavigate={jest.fn()}
577
+ profile={{
578
+ label: 'Account',
579
+ initials: 'AB',
580
+ menu: [{ id: 'settings', label: 'Settings', href: '/settings' }],
581
+ }}
582
+ />,
583
+ );
584
+ await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
585
+ const background = screen.getByTestId('primary-navigation', { includeHiddenElements: true });
586
+ expect(background).toHaveProp('accessibilityElementsHidden', true);
587
+ expect(background).toHaveProp('importantForAccessibility', 'no-hide-descendants');
588
+ expect(screen.queryByRole('link', { name: 'Home' })).toBeNull();
589
+ expect(screen.getByRole('menuitem', { name: 'Settings' })).toBeOnTheScreen();
590
+ await fireEvent.press(screen.getByRole('button', { name: 'Close' }), event());
591
+ expect(background).not.toHaveProp('accessibilityElementsHidden', true);
592
+ expect(screen.getByRole('link', { name: 'Home' })).toBeOnTheScreen();
593
+ });
594
+ it('hides the section picker behind its modal while keeping the selected entry accessible', async () => {
595
+ await render(
596
+ <SectionPicker
597
+ closeLabel="Close"
598
+ item={section}
599
+ pathname="/progress/history"
600
+ theme={theme}
601
+ onNavigate={jest.fn()}
602
+ />,
603
+ );
604
+ await fireEvent.press(screen.getByRole('button', { name: 'Progress, History' }), event());
605
+ expect(screen.queryByRole('button', { name: 'Progress, History' })).toBeNull();
606
+ expect(
607
+ screen.getByRole('button', { name: 'Progress, History', includeHiddenElements: true }).parent,
608
+ ).toHaveProp('importantForAccessibility', 'no-hide-descendants');
609
+ expect(screen.getByRole('menuitem', { name: 'History', selected: true })).toHaveProp(
610
+ 'tabIndex',
611
+ 0,
612
+ );
613
+ await fireEvent.press(screen.getByRole('button', { name: 'Close' }), event());
614
+ expect(screen.getByRole('button', { name: 'Progress, History' })).toBeOnTheScreen();
615
+ });
616
+
617
+ it.each([false, true])(
618
+ 'keeps the profile subtitle accessible when collapsed is %s',
619
+ async (collapsed) => {
620
+ await render(
621
+ <AppNavigation
622
+ {...labels}
623
+ items={items}
624
+ width={1024}
625
+ pathname="/"
626
+ theme={theme}
627
+ collapsed={collapsed}
628
+ onNavigate={jest.fn()}
629
+ profile={{ label: 'Account', subtitle: 'Ada, syncing', initials: 'AD', href: '/profile' }}
630
+ />,
631
+ );
632
+ expect(screen.getByRole('link', { name: 'Account, Ada, syncing' })).toBeOnTheScreen();
633
+ if (collapsed) expect(screen.queryByText('Ada, syncing')).toBeNull();
634
+ else expect(screen.getByText('Ada, syncing')).toBeOnTheScreen();
635
+ },
636
+ );
637
+
638
+ it('renders the profile subtitle in its menu and applies product typography', async () => {
639
+ const themed: NavigationTheme = {
640
+ ...theme,
641
+ typography: {
642
+ fontFamily: 'ProductSans',
643
+ fontSize: 18,
644
+ subtitleFontSize: 13,
645
+ fontWeight: '500',
646
+ activeFontWeight: '800',
647
+ lineHeight: 24,
648
+ letterSpacing: 0.5,
649
+ },
650
+ };
651
+ await render(
652
+ <AppNavigation
653
+ {...labels}
654
+ items={items}
655
+ width={1024}
656
+ pathname="/settings"
657
+ theme={themed}
658
+ onNavigate={jest.fn()}
659
+ profile={{
660
+ label: 'Account',
661
+ subtitle: 'Ada, synced',
662
+ initials: 'AD',
663
+ menu: [{ id: 'settings', label: 'Settings', href: '/settings' }],
664
+ }}
665
+ />,
666
+ );
667
+ expect(screen.getByText('Home')).toHaveStyle({
668
+ fontFamily: 'ProductSans',
669
+ fontSize: 18,
670
+ fontWeight: '500',
671
+ lineHeight: 24,
672
+ letterSpacing: 0.5,
673
+ });
674
+ expect(screen.getByText('Account')).toHaveStyle({ fontFamily: 'ProductSans', fontWeight: '800' });
675
+ expect(screen.getByText('Ada, synced')).toHaveStyle({ fontFamily: 'ProductSans', fontSize: 13 });
676
+ await fireEvent.press(screen.getByRole('button', { name: 'Account, Ada, synced' }), event());
677
+ expect(screen.getByTestId('navigation-menu')).toHaveProp(
678
+ 'accessibilityLabel',
679
+ 'Account, Ada, synced',
680
+ );
681
+ expect(screen.getAllByText('Ada, synced', { includeHiddenElements: true })).toHaveLength(2);
682
+ expect(screen.getByText('Settings')).toHaveStyle({
683
+ fontFamily: 'ProductSans',
684
+ fontSize: 18,
685
+ fontWeight: '800',
686
+ });
687
+ expect(screen.getByText('Close')).toHaveStyle({ fontFamily: 'ProductSans', fontWeight: '500' });
688
+ });
689
+
690
+ it('uses product typography for compact labels and section pickers', async () => {
691
+ const themed: NavigationTheme = {
692
+ ...theme,
693
+ typography: { fontFamily: 'ProductSans', barFontSize: 12, fontSize: 17 },
694
+ };
695
+ await render(
696
+ <AppNavigation
697
+ {...labels}
698
+ items={items}
699
+ width={320}
700
+ pathname="/"
701
+ theme={themed}
702
+ onNavigate={jest.fn()}
703
+ />,
704
+ );
705
+ expect(screen.getByText('Home')).toHaveStyle({ fontFamily: 'ProductSans', fontSize: 12 });
706
+ await render(
707
+ <SectionPicker
708
+ closeLabel="Close"
709
+ item={section}
710
+ pathname="/progress"
711
+ theme={themed}
712
+ onNavigate={jest.fn()}
713
+ />,
714
+ );
715
+ expect(screen.getByText('Overview')).toHaveStyle({ fontFamily: 'ProductSans', fontSize: 17 });
716
+ });
717
+
718
+ it.each([
719
+ { width: 320, collapsed: false },
720
+ { width: 1024, collapsed: false },
721
+ { width: 1024, collapsed: true },
722
+ ])(
723
+ 'renders a custom profile avatar at $width px, collapsed=$collapsed',
724
+ async ({ width, collapsed }) => {
725
+ const renderAvatar = jest.fn(({ active, size }: { active: boolean; size: number }) => (
726
+ <Text accessibilityRole="image" accessibilityLabel="Product glyph" testID="profile-glyph">
727
+ {active ? 'Selected glyph' : 'Glyph'} at {size}
728
+ </Text>
729
+ ));
730
+ const { rerender } = await render(
731
+ <AppNavigation
732
+ {...labels}
733
+ items={items}
734
+ width={width}
735
+ pathname="/settings"
736
+ collapsed={collapsed}
737
+ theme={theme}
738
+ onNavigate={jest.fn()}
739
+ profile={{
740
+ label: 'Account',
741
+ subtitle: 'Ada, synced',
742
+ initials: 'AD',
743
+ imageUrl: '/avatar.png',
744
+ renderAvatar,
745
+ menu: [{ id: 'settings', label: 'Settings', href: '/settings' }],
746
+ }}
747
+ />,
748
+ );
749
+ const button = screen.getByRole('button', { name: 'Account, Ada, synced' });
750
+ expect(
751
+ within(button).getByTestId('profile-glyph', { includeHiddenElements: true }),
752
+ ).toHaveTextContent('Selected glyph at 28');
753
+ expect(screen.queryByRole('image', { name: 'Product glyph' })).toBeNull();
754
+ expect(screen.queryByText('AD', { includeHiddenElements: true })).toBeNull();
755
+ expect(renderAvatar).toHaveBeenCalledWith({ active: true, size: 28 });
756
+ await fireEvent(button, 'focus', event());
757
+ expect(button).toHaveStyle({ borderColor: theme.activeText });
758
+ await fireEvent.press(button, event());
759
+ expect(screen.getByTestId('navigation-menu')).toHaveProp(
760
+ 'accessibilityLabel',
761
+ 'Account, Ada, synced',
762
+ );
763
+ expect(
764
+ within(screen.getByTestId('navigation-menu')).getByText('Ada, synced', {
765
+ includeHiddenElements: true,
766
+ }),
767
+ ).toHaveTextContent('Ada, synced');
768
+ await fireEvent.press(screen.getByRole('button', { name: 'Close' }), event());
769
+ expect(screen.getByRole('button', { name: 'Account, Ada, synced' })).toBeOnTheScreen();
770
+ await rerender(
771
+ <AppNavigation
772
+ {...labels}
773
+ items={items}
774
+ width={width}
775
+ pathname="/"
776
+ collapsed={collapsed}
777
+ theme={theme}
778
+ onNavigate={jest.fn()}
779
+ profile={{ label: 'Account', initials: 'AD', renderAvatar, href: '/profile' }}
780
+ />,
781
+ );
782
+ expect(screen.getByRole('link', { name: 'Account' })).toBeOnTheScreen();
783
+ expect(renderAvatar).toHaveBeenLastCalledWith({ active: false, size: 28 });
784
+ },
785
+ );
786
+
787
+ function ReservedHeight() {
788
+ return <Text testID="reserved-height">{useNavigationBarHeight(24)}</Text>;
789
+ }
790
+
791
+ it.each([1, 2])('reserves compact navigation space at font scale %s', async (fontScale) => {
792
+ jest
793
+ .spyOn(nativeModules, 'useWindowDimensions')
794
+ .mockReturnValue({ width: 600, height: 800, scale: 1, fontScale });
795
+ await render(
796
+ <>
797
+ <ReservedHeight />
798
+ <AppNavigation
799
+ {...labels}
800
+ items={items}
801
+ pathname="/"
802
+ theme={theme}
803
+ onNavigate={jest.fn()}
804
+ insets={{ bottom: 24 }}
805
+ />
806
+ </>,
807
+ );
808
+ expect(screen.getByTestId('primary-navigation')).toHaveStyle({ minHeight: 64 * fontScale + 24 });
809
+ expect(screen.getByTestId('reserved-height').props['children']).toBe(64 * fontScale + 24);
810
+ jest.restoreAllMocks();
811
+ });
812
+
813
+ it.each([600, 1024])(
814
+ 'enters a visible parent when its active child is hidden at width %s',
815
+ async (width) => {
816
+ await render(
817
+ <AppNavigation
818
+ {...labels}
819
+ items={items}
820
+ width={width}
821
+ defaultCollapsed
822
+ pathname="/progress/history?range=4w"
823
+ theme={theme}
824
+ onNavigate={jest.fn()}
825
+ />,
826
+ );
827
+ expect(screen.getByRole(width === 1024 ? 'button' : 'link', { name: 'Progress' })).toHaveProp(
828
+ 'tabIndex',
829
+ 0,
830
+ );
831
+ expect(screen.getByRole('link', { name: 'Home' })).toHaveProp('tabIndex', -1);
832
+ },
833
+ );