@baukit/navigation 0.7.3 → 0.8.0

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.
@@ -1,5 +1,5 @@
1
1
  import { act, fireEvent, render, screen, within } from '@testing-library/react-native';
2
- import { AccessibilityInfo, Platform, Text } from 'react-native';
2
+ import { AccessibilityInfo, Platform, ScrollView, Text } from 'react-native';
3
3
  import {
4
4
  AppNavigation,
5
5
  useNavigationBarHeight,
@@ -7,7 +7,7 @@ import {
7
7
  type NavigationIcon,
8
8
  type NavigationTheme,
9
9
  } from './native.js';
10
- import type { NavigationItem } from './index.js';
10
+ import { getNavigationBarHeight, type NavigationItem } from './index.js';
11
11
 
12
12
  // Resolve lazy native modules during setup, before Jest starts each test's timer.
13
13
  const nativeModules = jest.requireActual<typeof import('react-native')>('react-native');
@@ -24,6 +24,10 @@ for (const name of [
24
24
  Reflect.get(nativeModules, name);
25
25
  }
26
26
 
27
+ afterEach(() => {
28
+ jest.restoreAllMocks();
29
+ });
30
+
27
31
  const labels = {
28
32
  label: 'Primary',
29
33
  collapseLabel: 'Collapse navigation',
@@ -47,6 +51,7 @@ const theme: NavigationTheme = {
47
51
  background: '#fff',
48
52
  text: '#111',
49
53
  muted: '#595959',
54
+ danger: '#b3261e',
50
55
  activeBackground: '#dbe9f8',
51
56
  activeText: '#005fcc',
52
57
  ancestorText: '#004799',
@@ -58,6 +63,70 @@ const theme: NavigationTheme = {
58
63
  const profile = { label: 'Account', initials: 'AB', href: '/profile' };
59
64
  const event = () => ({ nativeEvent: {}, preventDefault: jest.fn() });
60
65
 
66
+ it('fills the compact viewport, permits overflow and reveals selected German items at 200%', async () => {
67
+ jest.spyOn(nativeModules, 'useWindowDimensions').mockReturnValue({
68
+ width: 360,
69
+ height: 800,
70
+ scale: 1,
71
+ fontScale: 2,
72
+ });
73
+ const scrollTo = jest.spyOn(ScrollView.prototype, 'scrollTo');
74
+ const germanItems = [
75
+ { id: 'today', label: 'Mein heutiger Tagesablauf', href: '/', icon },
76
+ { id: 'journal', label: 'Persönliches Tagebuch', href: '/journal', icon },
77
+ { id: 'exercises', label: 'Entspannungsübungen', href: '/exercises', icon },
78
+ { id: 'community', label: 'Gemeinschaft und Austausch', href: '/community', icon },
79
+ ];
80
+ const props = {
81
+ ...labels,
82
+ items: germanItems,
83
+ theme,
84
+ onNavigate: jest.fn(),
85
+ profile: { ...profile, label: 'Profil und Einstellungen' },
86
+ };
87
+ await render(<AppNavigation {...props} pathname="/" />);
88
+ const scroll = screen.getByTestId('navigation-items');
89
+ expect(scroll).toHaveProp('scrollEnabled', true);
90
+ expect(scroll).toHaveProp(
91
+ 'contentContainerStyle',
92
+ expect.objectContaining({
93
+ minWidth: '100%',
94
+ flexGrow: 1,
95
+ }),
96
+ );
97
+ expect(scroll.props['contentContainerStyle']).not.toHaveProperty('width');
98
+ const layout = (x: number, width: number) => ({
99
+ nativeEvent: { layout: { x, y: 0, width, height: 130 } },
100
+ });
101
+ await fireEvent(scroll, 'layout', layout(0, 360));
102
+ for (const [index, id] of [
103
+ ...germanItems.map((item) => `navigation-section-${item.id}`),
104
+ 'navigation-profile',
105
+ ].entries()) {
106
+ await fireEvent(screen.getByTestId(id), 'layout', layout(index * 72, 72));
107
+ }
108
+ await fireEvent(scroll, 'contentSizeChange', 360, 130);
109
+ expect(scrollTo).not.toHaveBeenCalled();
110
+ for (const [index, id] of [
111
+ ...germanItems.map((item) => `navigation-section-${item.id}`),
112
+ 'navigation-profile',
113
+ ].entries()) {
114
+ await fireEvent(screen.getByTestId(id), 'layout', layout(index * 180, 180));
115
+ }
116
+ await fireEvent(scroll, 'contentSizeChange', 900, 130);
117
+ await screen.rerender(<AppNavigation {...props} pathname="/community" />);
118
+ expect(scrollTo).toHaveBeenLastCalledWith({ x: 450, animated: false });
119
+ await screen.rerender(<AppNavigation {...props} pathname="/profile" />);
120
+ expect(scrollTo).toHaveBeenLastCalledWith({ x: 540, animated: false });
121
+ expect(screen.getByRole('link', { name: 'Profil und Einstellungen' })).toHaveProp('tabIndex', 0);
122
+ scrollTo.mockClear();
123
+ await fireEvent.scroll(scroll, { nativeEvent: { contentOffset: { x: 0, y: 0 } } });
124
+ expect(scrollTo).not.toHaveBeenCalled();
125
+ await screen.rerender(<AppNavigation {...props} pathname="/" />);
126
+ expect(scrollTo).not.toHaveBeenCalled();
127
+ jest.restoreAllMocks();
128
+ });
129
+
61
130
  it('keeps keyboard focus visible on active navigation, picker and menu entries', async () => {
62
131
  await render(
63
132
  <>
@@ -329,6 +398,7 @@ it('uses roving tab stops for Arrow, Home and End', async () => {
329
398
  expect(screen.getByRole('link', { name: 'Home' })).toHaveProp('tabIndex', 0);
330
399
  });
331
400
  it('opens a profile menu, runs an entry and closes', async () => {
401
+ jest.replaceProperty(Platform, 'OS', 'android');
332
402
  const action = jest.fn();
333
403
  await render(
334
404
  <AppNavigation
@@ -348,10 +418,18 @@ it('opens a profile menu, runs an entry and closes', async () => {
348
418
  await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
349
419
  expect(screen.getByRole('menuitem', { name: 'Sign out' })).toBeOnTheScreen();
350
420
  await fireEvent.press(screen.getByRole('menuitem', { name: 'Sign out' }), event());
421
+ await act(async () => {
422
+ await new Promise<void>((resolve) =>
423
+ requestAnimationFrame(() => {
424
+ resolve();
425
+ }),
426
+ );
427
+ });
351
428
  expect(action).toHaveBeenCalledTimes(1);
352
429
  expect(screen.queryByRole('menuitem')).toBeNull();
353
430
  });
354
431
  it('shows a full-width section picker and navigates directly to a subpage', async () => {
432
+ jest.replaceProperty(Platform, 'OS', 'android');
355
433
  const navigate = jest.fn();
356
434
  await render(
357
435
  <SectionPicker
@@ -364,6 +442,13 @@ it('shows a full-width section picker and navigates directly to a subpage', asyn
364
442
  );
365
443
  await fireEvent.press(screen.getByRole('button', { name: 'Progress, Overview' }), event());
366
444
  await fireEvent.press(screen.getByRole('menuitem', { name: 'History' }), event());
445
+ await act(async () => {
446
+ await new Promise<void>((resolve) =>
447
+ requestAnimationFrame(() => {
448
+ resolve();
449
+ }),
450
+ );
451
+ });
367
452
  expect(navigate).toHaveBeenCalledWith('/progress/history');
368
453
  expect(screen.queryByRole('menuitem')).toBeNull();
369
454
  });
@@ -784,11 +869,17 @@ it.each([
784
869
  },
785
870
  );
786
871
 
787
- function ReservedHeight() {
788
- return <Text testID="reserved-height">{useNavigationBarHeight(24)}</Text>;
872
+ function ReservedHeight({
873
+ theme,
874
+ slotHeight = 0,
875
+ }: {
876
+ readonly theme?: NavigationTheme;
877
+ readonly slotHeight?: number;
878
+ }) {
879
+ return <Text testID="reserved-height">{useNavigationBarHeight(24, theme, slotHeight)}</Text>;
789
880
  }
790
881
 
791
- it.each([1, 2])('reserves compact navigation space at font scale %s', async (fontScale) => {
882
+ it.each([1, 1.3, 2])('reserves compact navigation space at font scale %s', async (fontScale) => {
792
883
  jest
793
884
  .spyOn(nativeModules, 'useWindowDimensions')
794
885
  .mockReturnValue({ width: 600, height: 800, scale: 1, fontScale });
@@ -802,11 +893,26 @@ it.each([1, 2])('reserves compact navigation space at font scale %s', async (fon
802
893
  theme={theme}
803
894
  onNavigate={jest.fn()}
804
895
  insets={{ bottom: 24 }}
896
+ profile={{ label: 'Account', initials: 'AB', href: '/profile' }}
805
897
  />
806
898
  </>,
807
899
  );
808
- expect(screen.getByTestId('primary-navigation')).toHaveStyle({ minHeight: 64 * fontScale + 24 });
809
- expect(screen.getByTestId('reserved-height').props['children']).toBe(64 * fontScale + 24);
900
+ const height = getNavigationBarHeight(fontScale, 24);
901
+ expect(screen.getByTestId('primary-navigation')).toHaveStyle({ minHeight: height });
902
+ expect(screen.getByTestId('reserved-height').props['children']).toBe(height);
903
+ expect(screen.getByText('Home')).toHaveStyle({ lineHeight: 16 });
904
+ expect(screen.getByText('AB', { includeHiddenElements: true })).toHaveProp(
905
+ 'allowFontScaling',
906
+ false,
907
+ );
908
+ expect(screen.getByText('filled', { includeHiddenElements: true }).parent).toHaveStyle({
909
+ height: Math.max(28, 24 * fontScale),
910
+ justifyContent: 'center',
911
+ });
912
+ expect(screen.getByText('AB', { includeHiddenElements: true }).parent?.parent).toHaveStyle({
913
+ height: Math.max(28, 24 * fontScale),
914
+ justifyContent: 'center',
915
+ });
810
916
  jest.restoreAllMocks();
811
917
  });
812
918
 
@@ -831,3 +937,297 @@ it.each([600, 1024])(
831
937
  expect(screen.getByRole('link', { name: 'Home' })).toHaveProp('tabIndex', -1);
832
938
  },
833
939
  );
940
+
941
+ it('uses the same custom typography and spacing in the bar and height hook', async () => {
942
+ jest.spyOn(nativeModules, 'useWindowDimensions').mockReturnValue({
943
+ width: 600,
944
+ height: 800,
945
+ scale: 1,
946
+ fontScale: 1.3,
947
+ });
948
+ const customTheme = { ...theme, spacing: 12, typography: { barFontSize: 18, lineHeight: 30 } };
949
+ await render(
950
+ <>
951
+ <ReservedHeight theme={customTheme} />
952
+ <AppNavigation
953
+ {...labels}
954
+ items={items}
955
+ profile={profile}
956
+ pathname="/"
957
+ theme={customTheme}
958
+ onNavigate={jest.fn()}
959
+ insets={{ bottom: 24 }}
960
+ />
961
+ </>,
962
+ );
963
+ expect(screen.getByTestId('primary-navigation')).toHaveStyle({
964
+ minHeight: 129.2,
965
+ borderTopWidth: 1,
966
+ });
967
+ expect(screen.getByTestId('reserved-height').props['children']).toBe(129.2);
968
+ expect(screen.getByRole('link', { name: 'Account' })).toHaveStyle({
969
+ padding: 8,
970
+ borderWidth: 3,
971
+ gap: 12,
972
+ });
973
+ expect(screen.getByText('Account')).toHaveStyle({ fontSize: 18, lineHeight: 30 });
974
+ jest.restoreAllMocks();
975
+ });
976
+
977
+ it.each(['android', 'ios'] as const)(
978
+ 'runs profile navigation and actions after %s dismissal',
979
+ async (platform) => {
980
+ jest.replaceProperty(Platform, 'OS', platform);
981
+ let dismiss: (() => void) | undefined;
982
+ const OriginalModal = nativeModules.Modal;
983
+ class ObservedModal extends OriginalModal {
984
+ override render() {
985
+ dismiss = this.props.onDismiss;
986
+ return super.render();
987
+ }
988
+ }
989
+ jest.spyOn(nativeModules, 'Modal', 'get').mockReturnValue(ObservedModal);
990
+ const frames: FrameRequestCallback[] = [];
991
+ jest.spyOn(globalThis, 'requestAnimationFrame').mockImplementation((callback) => {
992
+ frames.push(callback);
993
+ return frames.length;
994
+ });
995
+ jest.spyOn(globalThis, 'cancelAnimationFrame').mockImplementation(() => undefined);
996
+ const navigate = jest.fn(() => {
997
+ expect(screen.queryByRole('menu')).toBeNull();
998
+ });
999
+ const action = jest.fn(() => {
1000
+ expect(screen.queryByRole('menu')).toBeNull();
1001
+ });
1002
+ await render(
1003
+ <AppNavigation
1004
+ {...labels}
1005
+ items={items}
1006
+ pathname="/"
1007
+ theme={theme}
1008
+ onNavigate={navigate}
1009
+ profile={{
1010
+ label: 'Account',
1011
+ initials: 'AB',
1012
+ menu: [
1013
+ { id: 'settings', label: 'Settings', href: '/settings' },
1014
+ { id: 'signout', label: 'Sign out', onSelect: action },
1015
+ ],
1016
+ }}
1017
+ />,
1018
+ );
1019
+ for (const name of ['Settings', 'Sign out']) {
1020
+ await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
1021
+ await fireEvent.press(screen.getByRole('menuitem', { name }), event());
1022
+ expect(navigate).toHaveBeenCalledTimes(name === 'Settings' ? 0 : 1);
1023
+ expect(action).not.toHaveBeenCalled();
1024
+ expect(screen.queryByRole('menu')).toBeNull();
1025
+ if (platform === 'ios') {
1026
+ expect(dismiss).toBeDefined();
1027
+ await act(() => {
1028
+ dismiss?.();
1029
+ });
1030
+ await act(() => {
1031
+ dismiss?.();
1032
+ });
1033
+ } else {
1034
+ expect(frames).toHaveLength(name === 'Settings' ? 1 : 2);
1035
+ await act(() => {
1036
+ frames.at(-1)?.(0);
1037
+ });
1038
+ await act(() => {
1039
+ frames.at(-1)?.(0);
1040
+ });
1041
+ }
1042
+ }
1043
+ expect(navigate).toHaveBeenCalledTimes(1);
1044
+ expect(navigate).toHaveBeenCalledWith('/settings');
1045
+ expect(action).toHaveBeenCalledTimes(1);
1046
+ jest.restoreAllMocks();
1047
+ },
1048
+ );
1049
+
1050
+ it('cancels a queued selection when navigation unmounts', async () => {
1051
+ jest.replaceProperty(Platform, 'OS', 'android');
1052
+ let frame: FrameRequestCallback | undefined;
1053
+ jest.spyOn(globalThis, 'requestAnimationFrame').mockImplementation((callback) => {
1054
+ frame = callback;
1055
+ return 42;
1056
+ });
1057
+ const cancel = jest.spyOn(globalThis, 'cancelAnimationFrame').mockImplementation(() => undefined);
1058
+ const navigate = jest.fn();
1059
+ const view = await render(
1060
+ <SectionPicker
1061
+ closeLabel="Close"
1062
+ item={section}
1063
+ pathname="/progress"
1064
+ theme={theme}
1065
+ onNavigate={navigate}
1066
+ />,
1067
+ );
1068
+ await fireEvent.press(screen.getByRole('button', { name: 'Progress, Overview' }), event());
1069
+ await fireEvent.press(screen.getByRole('menuitem', { name: 'History' }), event());
1070
+ expect(navigate).not.toHaveBeenCalled();
1071
+ await view.unmount();
1072
+ expect(cancel).toHaveBeenCalledWith(42);
1073
+ await act(() => {
1074
+ frame?.(0);
1075
+ });
1076
+ expect(navigate).not.toHaveBeenCalled();
1077
+ jest.restoreAllMocks();
1078
+ });
1079
+
1080
+ it.each([false, true])(
1081
+ 'renders danger menu text on the background, selected=%s',
1082
+ async (selected) => {
1083
+ await render(
1084
+ <AppNavigation
1085
+ {...labels}
1086
+ items={items}
1087
+ theme={theme}
1088
+ pathname={selected ? '/delete' : '/'}
1089
+ onNavigate={jest.fn()}
1090
+ profile={{
1091
+ label: 'Account',
1092
+ initials: 'AB',
1093
+ menu: [{ id: 'delete', label: 'Delete account', tone: 'danger', href: '/delete' }],
1094
+ }}
1095
+ />,
1096
+ );
1097
+ await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
1098
+ const entry = screen.getByRole('menuitem', { name: 'Delete account' });
1099
+ expect(entry).toHaveStyle({ backgroundColor: theme.background });
1100
+ expect(within(entry).getByText('Delete account')).toHaveStyle({ color: theme.danger });
1101
+ expect(entry).toHaveProp('accessibilityState', { disabled: false, selected });
1102
+ },
1103
+ );
1104
+
1105
+ it.each(
1106
+ [1, 2].flatMap((fontScale) => [
1107
+ { width: 320, collapsed: false, fontScale },
1108
+ { width: 1024, collapsed: false, fontScale },
1109
+ { width: 1024, collapsed: true, fontScale },
1110
+ ]),
1111
+ )(
1112
+ 'renders brand and accessory at $width, collapsed=$collapsed, font scale $fontScale',
1113
+ async ({ width, collapsed, fontScale }) => {
1114
+ jest
1115
+ .spyOn(nativeModules, 'useWindowDimensions')
1116
+ .mockReturnValue({ width, height: 900, scale: 1, fontScale });
1117
+ const renderBrand = jest.fn(({ collapsed: small }: { collapsed: boolean }) => (
1118
+ <Text>{small ? 'B' : 'Baukit'}</Text>
1119
+ ));
1120
+ const renderAccessory = jest.fn(() => <Text accessibilityRole="text">Workout active</Text>);
1121
+ const changed = jest.fn();
1122
+ await render(
1123
+ <AppNavigation
1124
+ {...labels}
1125
+ width={width}
1126
+ collapsed={collapsed}
1127
+ items={items}
1128
+ pathname="/"
1129
+ theme={theme}
1130
+ onNavigate={jest.fn()}
1131
+ renderBrand={renderBrand}
1132
+ renderAccessory={renderAccessory}
1133
+ onBarHeightChange={changed}
1134
+ />,
1135
+ );
1136
+ const layout = width === 320 ? 'bar' : 'rail';
1137
+ expect(renderBrand).toHaveBeenCalledWith({ layout, collapsed });
1138
+ expect(renderAccessory).toHaveBeenCalledWith({ layout, collapsed });
1139
+ expect(screen.getByText(collapsed ? 'B' : 'Baukit')).toBeOnTheScreen();
1140
+ expect(screen.getByText('Workout active')).toBeOnTheScreen();
1141
+ if (layout === 'bar') {
1142
+ expect(screen.getByTestId('navigation-items')).toHaveStyle({
1143
+ height: getNavigationBarHeight(fontScale) - 1,
1144
+ });
1145
+ await fireEvent(screen.getByTestId('navigation-slots'), 'layout', {
1146
+ nativeEvent: { layout: { x: 0, y: 0, width: 320, height: 52 } },
1147
+ });
1148
+ expect(screen.getByTestId('primary-navigation')).toHaveStyle({
1149
+ minHeight: getNavigationBarHeight(fontScale, 0, { slotHeight: 52 }),
1150
+ });
1151
+ expect(changed).toHaveBeenLastCalledWith(
1152
+ getNavigationBarHeight(fontScale, 0, { slotHeight: 52 }),
1153
+ );
1154
+ } else expect(changed).not.toHaveBeenCalled();
1155
+ },
1156
+ );
1157
+
1158
+ it('drops a queued action if the menu reopens before the dismissal frame', async () => {
1159
+ jest.replaceProperty(Platform, 'OS', 'android');
1160
+ const frames: FrameRequestCallback[] = [];
1161
+ jest.spyOn(globalThis, 'requestAnimationFrame').mockImplementation((callback) => {
1162
+ frames.push(callback);
1163
+ return frames.length;
1164
+ });
1165
+ const cancel = jest.spyOn(globalThis, 'cancelAnimationFrame').mockImplementation(() => undefined);
1166
+ const action = jest.fn();
1167
+ await render(
1168
+ <AppNavigation
1169
+ {...labels}
1170
+ items={items}
1171
+ theme={theme}
1172
+ pathname="/"
1173
+ onNavigate={jest.fn()}
1174
+ profile={{
1175
+ label: 'Account',
1176
+ initials: 'AB',
1177
+ menu: [{ id: 'signout', label: 'Sign out', onSelect: action }],
1178
+ }}
1179
+ />,
1180
+ );
1181
+ await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
1182
+ await fireEvent.press(screen.getByRole('menuitem', { name: 'Sign out' }), event());
1183
+ await fireEvent.press(screen.getByRole('button', { name: 'Account' }), event());
1184
+ expect(cancel).toHaveBeenCalledWith(1);
1185
+ await act(() => {
1186
+ frames[0]?.(0);
1187
+ });
1188
+ expect(action).not.toHaveBeenCalled();
1189
+ await fireEvent.press(screen.getByRole('menuitem', { name: 'Sign out' }), event());
1190
+ await act(() => {
1191
+ frames[1]?.(0);
1192
+ });
1193
+ expect(action).toHaveBeenCalledTimes(1);
1194
+ });
1195
+
1196
+ it('reserves measured slot height through the native height hook at 200%', async () => {
1197
+ jest
1198
+ .spyOn(nativeModules, 'useWindowDimensions')
1199
+ .mockReturnValue({ width: 320, height: 900, scale: 1, fontScale: 2 });
1200
+ await render(<ReservedHeight slotHeight={64} />);
1201
+ expect(screen.getByTestId('reserved-height').props['children']).toBe(
1202
+ getNavigationBarHeight(2, 24, { slotHeight: 64 }),
1203
+ );
1204
+ });
1205
+
1206
+ it('removes slot height when conditional content disappears', async () => {
1207
+ jest
1208
+ .spyOn(nativeModules, 'useWindowDimensions')
1209
+ .mockReturnValue({ width: 320, height: 900, scale: 1, fontScale: 1 });
1210
+ const props = {
1211
+ ...labels,
1212
+ items,
1213
+ theme,
1214
+ pathname: '/',
1215
+ onNavigate: jest.fn(),
1216
+ onBarHeightChange: jest.fn(),
1217
+ };
1218
+ await render(<AppNavigation {...props} renderBrand={() => <Text>Baukit</Text>} />);
1219
+ await fireEvent(screen.getByTestId('navigation-slots'), 'layout', {
1220
+ nativeEvent: { layout: { x: 0, y: 0, width: 320, height: 52 } },
1221
+ });
1222
+ expect(props.onBarHeightChange).toHaveBeenLastCalledWith(
1223
+ getNavigationBarHeight(1, 0, { slotHeight: 52 }),
1224
+ );
1225
+ await screen.rerender(
1226
+ <AppNavigation {...props} renderBrand={() => null} renderAccessory={() => false} />,
1227
+ );
1228
+ expect(screen.queryByTestId('navigation-slots')).toBeNull();
1229
+ expect(screen.getByTestId('primary-navigation')).toHaveStyle({
1230
+ minHeight: getNavigationBarHeight(),
1231
+ });
1232
+ expect(props.onBarHeightChange).toHaveBeenLastCalledWith(getNavigationBarHeight());
1233
+ });