@baukit/navigation 0.7.2 → 0.7.4

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