@terreno/ui 56.0.0-beta.0 → 56.0.0-beta.2

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,339 @@
1
+ import {afterAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
+ import {fireEvent} from "@testing-library/react-native";
3
+ import React, {type ReactNode} from "react";
4
+ import {View} from "react-native";
5
+
6
+ import type {SidebarNavigationItem} from "./Common";
7
+ import {renderWithTheme} from "./test-utils";
8
+
9
+ interface MockChildrenProps {
10
+ children?: ReactNode;
11
+ }
12
+
13
+ interface NavigatorContext {
14
+ state: {index: number; routes: {key: string; name: string}[]};
15
+ navigation: {navigate: (route: string) => void};
16
+ descriptors: Record<string, {options: Record<string, unknown>}>;
17
+ }
18
+
19
+ const navigate = mock((_route: string) => {});
20
+
21
+ let navigatorContext: NavigatorContext = {
22
+ descriptors: {},
23
+ navigation: {navigate},
24
+ state: {index: 0, routes: [{key: "home-key", name: "index"}]},
25
+ };
26
+
27
+ // bunSetup.ts mocks expo-router without Navigator.useContext, which
28
+ // SidebarNavigatorContent depends on. Restored in afterAll to avoid leaking
29
+ // into other test files.
30
+ mock.module("expo-router", () => ({
31
+ Link: ({children, ...props}: MockChildrenProps) => React.createElement("Link", props, children),
32
+ Navigator: Object.assign(
33
+ ({children, ...props}: MockChildrenProps) => React.createElement("Navigator", props, children),
34
+ {useContext: () => navigatorContext}
35
+ ),
36
+ router: {
37
+ back: mock(() => {}),
38
+ canGoBack: mock(() => true),
39
+ navigate: mock(() => {}),
40
+ push: mock(() => {}),
41
+ replace: mock(() => {}),
42
+ },
43
+ Slot: ({children, ...props}: MockChildrenProps) => React.createElement("Slot", props, children),
44
+ Stack: ({children, ...props}: MockChildrenProps) => React.createElement("Stack", props, children),
45
+ Tabs: ({children, ...props}: MockChildrenProps) => React.createElement("Tabs", props, children),
46
+ useLocalSearchParams: mock(() => ({})),
47
+ useRouter: mock(() => ({
48
+ back: mock(() => {}),
49
+ canGoBack: mock(() => true),
50
+ navigate: mock(() => {}),
51
+ push: mock(() => {}),
52
+ replace: mock(() => {}),
53
+ })),
54
+ useSegments: mock(() => []),
55
+ }));
56
+
57
+ import {SidebarNavigation, SidebarNavigationPanel} from "./SidebarNavigation";
58
+
59
+ afterAll(() => {
60
+ mock.module("expo-router", () => ({
61
+ Link: ({children, ...props}: MockChildrenProps) => React.createElement("Link", props, children),
62
+ Navigator: ({children, ...props}: MockChildrenProps) =>
63
+ React.createElement("Navigator", props, children),
64
+ router: {
65
+ back: mock(() => {}),
66
+ canGoBack: mock(() => true),
67
+ navigate: mock(() => {}),
68
+ push: mock(() => {}),
69
+ replace: mock(() => {}),
70
+ },
71
+ Slot: ({children, ...props}: MockChildrenProps) => React.createElement("Slot", props, children),
72
+ Stack: ({children, ...props}: MockChildrenProps) =>
73
+ React.createElement("Stack", props, children),
74
+ Tabs: ({children, ...props}: MockChildrenProps) => React.createElement("Tabs", props, children),
75
+ useLocalSearchParams: mock(() => ({})),
76
+ useRouter: mock(() => ({
77
+ back: mock(() => {}),
78
+ canGoBack: mock(() => true),
79
+ navigate: mock(() => {}),
80
+ push: mock(() => {}),
81
+ replace: mock(() => {}),
82
+ })),
83
+ useSegments: mock(() => []),
84
+ }));
85
+ });
86
+
87
+ const topItems: SidebarNavigationItem[] = [
88
+ {iconName: "house", label: "Home", route: "index"},
89
+ {badge: 5, iconName: "bell", label: "Alerts", route: "alerts"},
90
+ {badge: true, badgeStatus: "info", iconName: "envelope", label: "Inbox", route: "inbox"},
91
+ ];
92
+
93
+ const bottomItems: SidebarNavigationItem[] = [
94
+ {iconName: "gear", label: "Settings", route: "settings"},
95
+ ];
96
+
97
+ describe("SidebarNavigationPanel", () => {
98
+ it("renders every top and bottom item", () => {
99
+ const onNavigate = mock((_route: string) => {});
100
+ const {getByLabelText} = renderWithTheme(
101
+ <SidebarNavigationPanel
102
+ activeRoute="index"
103
+ bottomItems={bottomItems}
104
+ onNavigate={onNavigate}
105
+ topItems={topItems}
106
+ />
107
+ );
108
+
109
+ for (const item of [...topItems, ...bottomItems]) {
110
+ expect(getByLabelText(item.label)).toBeTruthy();
111
+ }
112
+ });
113
+
114
+ it("renders children next to the rail", () => {
115
+ const {getByTestId} = renderWithTheme(
116
+ <SidebarNavigationPanel
117
+ activeRoute="index"
118
+ bottomItems={bottomItems}
119
+ onNavigate={mock((_route: string) => {})}
120
+ topItems={topItems}
121
+ >
122
+ <View testID="panel-child" />
123
+ </SidebarNavigationPanel>
124
+ );
125
+
126
+ expect(getByTestId("panel-child")).toBeTruthy();
127
+ });
128
+
129
+ it("calls onNavigate with the pressed item route", () => {
130
+ const onNavigate = mock((_route: string) => {});
131
+ const {getByLabelText} = renderWithTheme(
132
+ <SidebarNavigationPanel
133
+ activeRoute="index"
134
+ bottomItems={bottomItems}
135
+ onNavigate={onNavigate}
136
+ topItems={topItems}
137
+ />
138
+ );
139
+
140
+ fireEvent.press(getByLabelText("Settings"));
141
+ expect(onNavigate).toHaveBeenCalledWith("settings");
142
+ });
143
+
144
+ it("hides labels while collapsed and shows them once hovered", () => {
145
+ const {UNSAFE_getAllByType, queryByText, getByText} = renderWithTheme(
146
+ <SidebarNavigationPanel
147
+ activeRoute="index"
148
+ bottomItems={bottomItems}
149
+ onNavigate={mock((_route: string) => {})}
150
+ topItems={topItems}
151
+ />
152
+ );
153
+
154
+ expect(queryByText("Home")).toBeNull();
155
+
156
+ const rail = UNSAFE_getAllByType(View).find(
157
+ (node) => node.props.onMouseEnter !== undefined && node.props.onMouseLeave !== undefined
158
+ );
159
+ if (!rail) {
160
+ throw new Error("expected the sidebar rail to expose hover handlers");
161
+ }
162
+
163
+ fireEvent(rail, "mouseEnter");
164
+ expect(getByText("Home")).toBeTruthy();
165
+ expect(getByText("Settings")).toBeTruthy();
166
+
167
+ fireEvent(rail, "mouseLeave");
168
+ expect(queryByText("Home")).toBeNull();
169
+ });
170
+
171
+ it("bolds the active label and leaves inactive labels unbolded", () => {
172
+ const {UNSAFE_getAllByType, getByText} = renderWithTheme(
173
+ <SidebarNavigationPanel
174
+ activeRoute="alerts"
175
+ bottomItems={bottomItems}
176
+ onNavigate={mock((_route: string) => {})}
177
+ topItems={topItems}
178
+ />
179
+ );
180
+
181
+ const rail = UNSAFE_getAllByType(View).find((node) => node.props.onMouseEnter !== undefined);
182
+ if (!rail) {
183
+ throw new Error("expected the sidebar rail to expose hover handlers");
184
+ }
185
+ fireEvent(rail, "mouseEnter");
186
+
187
+ const fontFamilyOf = (label: string): unknown =>
188
+ Object.assign(
189
+ {},
190
+ ...(Array.isArray(getByText(label).props.style)
191
+ ? getByText(label).props.style.flat(Number.POSITIVE_INFINITY)
192
+ : [getByText(label).props.style])
193
+ ).fontFamily;
194
+
195
+ expect(fontFamilyOf("Alerts")).not.toBe(fontFamilyOf("Home"));
196
+ expect(String(fontFamilyOf("Alerts"))).toContain("bold");
197
+ });
198
+
199
+ it("applies hover styling to individual items", () => {
200
+ const {getByLabelText} = renderWithTheme(
201
+ <SidebarNavigationPanel
202
+ activeRoute="index"
203
+ bottomItems={bottomItems}
204
+ onNavigate={mock((_route: string) => {})}
205
+ topItems={topItems}
206
+ />
207
+ );
208
+
209
+ const item = getByLabelText("Alerts");
210
+ const flatten = (style: unknown): Record<string, unknown> =>
211
+ Object.assign({}, ...(Array.isArray(style) ? style : [style]));
212
+
213
+ const initialBackground = flatten(item.props.style).backgroundColor;
214
+ fireEvent(item, "hoverIn");
215
+ expect(flatten(getByLabelText("Alerts").props.style).backgroundColor).not.toBe(
216
+ initialBackground
217
+ );
218
+
219
+ fireEvent(item, "hoverOut");
220
+ expect(flatten(getByLabelText("Alerts").props.style).backgroundColor).toBe(initialBackground);
221
+ });
222
+
223
+ it("applies custom panel and item styles", () => {
224
+ const {getByLabelText, UNSAFE_getAllByType} = renderWithTheme(
225
+ <SidebarNavigationPanel
226
+ activeRoute="index"
227
+ bottomItems={bottomItems}
228
+ itemStyle={{opacity: 0.5}}
229
+ onNavigate={mock((_route: string) => {})}
230
+ panelStyle={{borderRightWidth: 4}}
231
+ topItems={topItems}
232
+ />
233
+ );
234
+
235
+ const rail = UNSAFE_getAllByType(View).find((node) => node.props.onMouseEnter !== undefined);
236
+ expect(rail?.props.style).toContainEqual({borderRightWidth: 4});
237
+ expect(getByLabelText("Home").props.style).toContainEqual({opacity: 0.5});
238
+ });
239
+ });
240
+
241
+ describe("SidebarNavigation", () => {
242
+ beforeEach(() => {
243
+ navigate.mockClear();
244
+ navigatorContext = {
245
+ descriptors: {},
246
+ navigation: {navigate},
247
+ state: {index: 0, routes: [{key: "home-key", name: "index"}]},
248
+ };
249
+ });
250
+
251
+ it("exposes Screen for per-screen options", () => {
252
+ expect(SidebarNavigation.Screen).toBeDefined();
253
+ });
254
+
255
+ it("renders the sidebar for the active navigator route", () => {
256
+ const {getByLabelText} = renderWithTheme(
257
+ <SidebarNavigation bottomItems={bottomItems} topItems={topItems} />
258
+ );
259
+
260
+ expect(getByLabelText("Home")).toBeTruthy();
261
+ expect(getByLabelText("Settings")).toBeTruthy();
262
+ });
263
+
264
+ it("navigates through the navigator and forwards the route to onNavigate", () => {
265
+ const onNavigate = mock((_route: string) => {});
266
+ const {getByLabelText} = renderWithTheme(
267
+ <SidebarNavigation bottomItems={bottomItems} onNavigate={onNavigate} topItems={topItems} />
268
+ );
269
+
270
+ fireEvent.press(getByLabelText("Alerts"));
271
+ expect(navigate).toHaveBeenCalledWith("alerts");
272
+ expect(onNavigate).toHaveBeenCalledWith("alerts");
273
+ });
274
+
275
+ it("navigates without an onNavigate callback", () => {
276
+ const {getByLabelText} = renderWithTheme(
277
+ <SidebarNavigation bottomItems={bottomItems} topItems={topItems} />
278
+ );
279
+
280
+ fireEvent.press(getByLabelText("Inbox"));
281
+ expect(navigate).toHaveBeenCalledWith("inbox");
282
+ });
283
+
284
+ it("renders the header with the screen title and header slots", () => {
285
+ navigatorContext = {
286
+ descriptors: {
287
+ "home-key": {
288
+ options: {
289
+ headerLeft: () => <View testID="header-left" />,
290
+ headerRight: () => <View testID="header-right" />,
291
+ title: "Dashboard",
292
+ },
293
+ },
294
+ },
295
+ navigation: {navigate},
296
+ state: {index: 0, routes: [{key: "home-key", name: "index"}]},
297
+ };
298
+
299
+ const {getByTestId, getByText} = renderWithTheme(
300
+ <SidebarNavigation bottomItems={bottomItems} topItems={topItems} />
301
+ );
302
+
303
+ expect(getByText("Dashboard")).toBeTruthy();
304
+ expect(getByTestId("header-left")).toBeTruthy();
305
+ expect(getByTestId("header-right")).toBeTruthy();
306
+ });
307
+
308
+ it("omits the header when the active screen has no title or header slots", () => {
309
+ const {queryByText} = renderWithTheme(
310
+ <SidebarNavigation bottomItems={bottomItems} topItems={topItems} />
311
+ );
312
+
313
+ expect(queryByText("Dashboard")).toBeNull();
314
+ });
315
+
316
+ it("renders Screen children passed to the navigator", () => {
317
+ const {getByTestId} = renderWithTheme(
318
+ <SidebarNavigation bottomItems={bottomItems} topItems={topItems}>
319
+ <View testID="screen-child" />
320
+ </SidebarNavigation>
321
+ );
322
+
323
+ expect(getByTestId("screen-child")).toBeTruthy();
324
+ });
325
+
326
+ it("handles an empty navigator route list", () => {
327
+ navigatorContext = {
328
+ descriptors: {},
329
+ navigation: {navigate},
330
+ state: {index: 0, routes: []},
331
+ };
332
+
333
+ const {getByLabelText} = renderWithTheme(
334
+ <SidebarNavigation bottomItems={bottomItems} topItems={topItems} />
335
+ );
336
+
337
+ expect(getByLabelText("Home")).toBeTruthy();
338
+ });
339
+ });
package/src/Theme.tsx CHANGED
@@ -137,6 +137,7 @@ const defaultTheme: TerrenoThemeConfig = {
137
137
  primary: "primary400",
138
138
  secondaryDark: "secondary500",
139
139
  secondaryExtraDark: "secondary800",
140
+ secondaryExtraLight: "secondary000",
140
141
  secondaryLight: "secondary100",
141
142
  success: "success200",
142
143
  successLight: "success000",
@@ -1,5 +1,6 @@
1
1
  import {describe, expect, it, mock} from "bun:test";
2
2
  import {act, fireEvent, renderHook} from "@testing-library/react-native";
3
+ import {assert} from "chai";
3
4
  import type {ComponentProps} from "react";
4
5
  import {Dimensions} from "react-native";
5
6
 
@@ -714,6 +715,19 @@ describe("scheduleAfterPaint", () => {
714
715
  });
715
716
 
716
717
  describe("WebDropdownMenu layout callbacks", () => {
718
+ // scheduleAfterPaint defers to requestAnimationFrame; run callbacks synchronously so
719
+ // scrolling happens inside the act() block that fires the layout events.
720
+ const runAnimationFramesSynchronously = (): (() => void) => {
721
+ const globals = globalThis as {requestAnimationFrame?: typeof requestAnimationFrame};
722
+ const originalRaf = globals.requestAnimationFrame;
723
+ globals.requestAnimationFrame = ((callback: FrameRequestCallback) => {
724
+ callback(0);
725
+ return 1;
726
+ }) as unknown as typeof requestAnimationFrame;
727
+ return () => {
728
+ globals.requestAnimationFrame = originalRaf;
729
+ };
730
+ };
717
731
  const anchor = {height: 40, width: 200, x: 16, y: 32};
718
732
  const options = [
719
733
  {label: "Option A", value: "a"},
@@ -767,6 +781,120 @@ describe("WebDropdownMenu layout callbacks", () => {
767
781
  expect(getByTestId("web_dropdown_option_b")).toBeTruthy();
768
782
  });
769
783
 
784
+ it("centers the selected option once its layout and the viewport height are known", () => {
785
+ const restoreRaf = runAnimationFramesSynchronously();
786
+
787
+ try {
788
+ const {getByTestId, root} = renderWithTheme(
789
+ <WebDropdownMenu
790
+ anchor={anchor}
791
+ disableSearch
792
+ onClose={() => {}}
793
+ onSelect={() => {}}
794
+ options={options}
795
+ selectedIndex={2}
796
+ visible
797
+ />
798
+ );
799
+
800
+ const scrollView = root.findAll((node) => node.type === "ScrollView")[0];
801
+ const scrollCalls: Array<{animated?: boolean; y?: number}> = [];
802
+ const scrollRef = scrollView.props.ref as {current: unknown};
803
+ scrollRef.current = {
804
+ scrollTo: (options: {animated?: boolean; y?: number}) => {
805
+ scrollCalls.push(options);
806
+ },
807
+ };
808
+
809
+ act(() => {
810
+ fireEvent(getByTestId("web_dropdown_option_c"), "layout", {
811
+ nativeEvent: {layout: {height: 40, width: 200, x: 0, y: 80}},
812
+ });
813
+ fireEvent(scrollView, "layout", {nativeEvent: {layout: {height: 60, width: 200}}});
814
+ });
815
+
816
+ // offset 80 - viewport 60 / 2 + height 40 / 2 = 70
817
+ assert.deepEqual(scrollCalls, [{animated: false, y: 70}]);
818
+ } finally {
819
+ restoreRaf();
820
+ }
821
+ });
822
+
823
+ it("clamps the centered scroll offset at the top of the list", () => {
824
+ const restoreRaf = runAnimationFramesSynchronously();
825
+
826
+ try {
827
+ const {getByTestId, root} = renderWithTheme(
828
+ <WebDropdownMenu
829
+ anchor={anchor}
830
+ disableSearch
831
+ onClose={() => {}}
832
+ onSelect={() => {}}
833
+ options={options}
834
+ selectedIndex={0}
835
+ visible
836
+ />
837
+ );
838
+
839
+ const scrollView = root.findAll((node) => node.type === "ScrollView")[0];
840
+ const scrollCalls: Array<{animated?: boolean; y?: number}> = [];
841
+ const scrollRef = scrollView.props.ref as {current: unknown};
842
+ scrollRef.current = {
843
+ scrollTo: (options: {animated?: boolean; y?: number}) => {
844
+ scrollCalls.push(options);
845
+ },
846
+ };
847
+
848
+ act(() => {
849
+ fireEvent(scrollView, "layout", {nativeEvent: {layout: {height: 200, width: 200}}});
850
+ fireEvent(getByTestId("web_dropdown_option_a"), "layout", {
851
+ nativeEvent: {layout: {height: 40, width: 200, x: 0, y: 0}},
852
+ });
853
+ });
854
+
855
+ assert.deepEqual(scrollCalls, [{animated: false, y: 0}]);
856
+ } finally {
857
+ restoreRaf();
858
+ }
859
+ });
860
+
861
+ it("does not scroll when the list viewport height has not been measured yet", () => {
862
+ const restoreRaf = runAnimationFramesSynchronously();
863
+
864
+ try {
865
+ const {getByTestId, root} = renderWithTheme(
866
+ <WebDropdownMenu
867
+ anchor={anchor}
868
+ disableSearch
869
+ onClose={() => {}}
870
+ onSelect={() => {}}
871
+ options={options}
872
+ selectedIndex={1}
873
+ visible
874
+ />
875
+ );
876
+
877
+ const scrollView = root.findAll((node) => node.type === "ScrollView")[0];
878
+ const scrollCalls: Array<{animated?: boolean; y?: number}> = [];
879
+ const scrollRef = scrollView.props.ref as {current: unknown};
880
+ scrollRef.current = {
881
+ scrollTo: (options: {animated?: boolean; y?: number}) => {
882
+ scrollCalls.push(options);
883
+ },
884
+ };
885
+
886
+ act(() => {
887
+ fireEvent(getByTestId("web_dropdown_option_b"), "layout", {
888
+ nativeEvent: {layout: {height: 40, width: 200, x: 0, y: 40}},
889
+ });
890
+ });
891
+
892
+ assert.lengthOf(scrollCalls, 0);
893
+ } finally {
894
+ restoreRaf();
895
+ }
896
+ });
897
+
770
898
  it("does not schedule a scroll for option layout when nothing is selected", () => {
771
899
  const {getByTestId} = renderWithTheme(
772
900
  <WebDropdownMenu
@@ -444,3 +444,4 @@ exports[`Card display variant renders image when imageUri is provided 1`] = `
444
444
  "type": "View",
445
445
  }
446
446
  `;
447
+