@terreno/ui 56.0.0-beta.0 → 57.0.0-beta.1
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.
- package/LICENSE +21 -203
- package/dist/Badge.d.ts +1 -1
- package/dist/Badge.js +37 -1
- package/dist/Badge.js.map +1 -1
- package/dist/Common.d.ts +149 -3
- package/dist/Common.js.map +1 -1
- package/dist/DateTimeField.js +5 -21
- package/dist/DateTimeField.js.map +1 -1
- package/dist/EditableCard.d.ts +3 -0
- package/dist/EditableCard.js +23 -0
- package/dist/EditableCard.js.map +1 -0
- package/dist/Filter.d.ts +13 -0
- package/dist/Filter.js +120 -0
- package/dist/Filter.js.map +1 -0
- package/dist/FilterAccordion.d.ts +8 -0
- package/dist/FilterAccordion.js +59 -0
- package/dist/FilterAccordion.js.map +1 -0
- package/dist/FilterBoolean.d.ts +8 -0
- package/dist/FilterBoolean.js +74 -0
- package/dist/FilterBoolean.js.map +1 -0
- package/dist/FilterChangesBadge.d.ts +8 -0
- package/dist/FilterChangesBadge.js +11 -0
- package/dist/FilterChangesBadge.js.map +1 -0
- package/dist/FilterSelectMenu.d.ts +8 -0
- package/dist/FilterSelectMenu.js +37 -0
- package/dist/FilterSelectMenu.js.map +1 -0
- package/dist/IconRegistry.js +11 -6
- package/dist/IconRegistry.js.map +1 -1
- package/dist/Page.d.ts +2 -1
- package/dist/Page.js +0 -2
- package/dist/Page.js.map +1 -1
- package/dist/PickerSelect.d.ts +8 -1
- package/dist/PickerSelect.js +2 -2
- package/dist/PickerSelect.js.map +1 -1
- package/dist/SelectField.js +2 -2
- package/dist/SelectField.js.map +1 -1
- package/dist/Theme.js +3 -0
- package/dist/Theme.js.map +1 -1
- package/dist/WebDropdownMenu.d.ts +5 -1
- package/dist/WebDropdownMenu.js +24 -10
- package/dist/WebDropdownMenu.js.map +1 -1
- package/dist/icons/BarsFilterIcon.d.ts +9 -0
- package/dist/icons/BarsFilterIcon.js +12 -0
- package/dist/icons/BarsFilterIcon.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/package.json +26 -25
- package/src/ActionSheet.test.tsx +202 -172
- package/src/Badge.test.tsx +22 -1
- package/src/Badge.tsx +43 -0
- package/src/Box.test.tsx +31 -23
- package/src/Common.ts +167 -5
- package/src/ConsentFormScreen.test.tsx +22 -0
- package/src/ConsentNavigator.test.tsx +6 -8
- package/src/DateTimeField.test.tsx +66 -18
- package/src/DateTimeField.tsx +5 -19
- package/src/DraggableList.test.tsx +377 -0
- package/src/EditableCard.test.tsx +69 -0
- package/src/EditableCard.tsx +73 -0
- package/src/Filter.test.tsx +94 -0
- package/src/Filter.tsx +257 -0
- package/src/FilterAccordion.test.tsx +58 -0
- package/src/FilterAccordion.tsx +113 -0
- package/src/FilterBoolean.test.tsx +65 -0
- package/src/FilterBoolean.tsx +130 -0
- package/src/FilterChangesBadge.test.tsx +16 -0
- package/src/FilterChangesBadge.tsx +13 -0
- package/src/FilterSelectMenu.test.tsx +56 -0
- package/src/FilterSelectMenu.tsx +76 -0
- package/src/IconRegistry.test.tsx +6 -4
- package/src/IconRegistry.tsx +13 -8
- package/src/Page.tsx +2 -3
- package/src/PickerSelect.test.tsx +43 -12
- package/src/PickerSelect.tsx +10 -0
- package/src/SelectField.tsx +2 -0
- package/src/SidebarNavigation.test.tsx +339 -0
- package/src/Theme.tsx +3 -0
- package/src/WebDropdownMenu.test.tsx +214 -1
- package/src/WebDropdownMenu.tsx +43 -21
- package/src/__snapshots__/Card.test.tsx.snap +1 -0
- package/src/__snapshots__/EditableCard.test.tsx.snap +242 -0
- package/src/__snapshots__/Filter.test.tsx.snap +161 -0
- package/src/__snapshots__/FilterAccordion.test.tsx.snap +83 -0
- package/src/__snapshots__/FilterBoolean.test.tsx.snap +109 -0
- package/src/__snapshots__/FilterChangesBadge.test.tsx.snap +18 -0
- package/src/__snapshots__/FilterSelectMenu.test.tsx.snap +359 -0
- package/src/bunSetup.ts +6 -2
- package/src/conflictFieldDiff.test.ts +43 -0
- package/src/icons/BarsFilterIcon.tsx +18 -0
- package/src/index.tsx +7 -0
|
@@ -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
|
@@ -128,6 +128,8 @@ const defaultTheme: TerrenoThemeConfig = {
|
|
|
128
128
|
},
|
|
129
129
|
surface: {
|
|
130
130
|
base: "neutral000",
|
|
131
|
+
baseAlternate: "neutral050",
|
|
132
|
+
baseHover: "secondary000",
|
|
131
133
|
disabled: "neutral500",
|
|
132
134
|
error: "error200",
|
|
133
135
|
errorLight: "error000",
|
|
@@ -137,6 +139,7 @@ const defaultTheme: TerrenoThemeConfig = {
|
|
|
137
139
|
primary: "primary400",
|
|
138
140
|
secondaryDark: "secondary500",
|
|
139
141
|
secondaryExtraDark: "secondary800",
|
|
142
|
+
secondaryExtraLight: "secondary000",
|
|
140
143
|
secondaryLight: "secondary100",
|
|
141
144
|
success: "success200",
|
|
142
145
|
successLight: "success000",
|
|
@@ -1,10 +1,16 @@
|
|
|
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
|
|
|
6
7
|
import {renderWithTheme} from "./test-utils";
|
|
7
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
resolveDocumentBodyPortalTarget,
|
|
10
|
+
scheduleAfterPaint,
|
|
11
|
+
useWebDropdownAnchor,
|
|
12
|
+
WebDropdownMenu,
|
|
13
|
+
} from "./WebDropdownMenu";
|
|
8
14
|
|
|
9
15
|
describe("WebDropdownMenu", () => {
|
|
10
16
|
const anchor = {height: 40, width: 200, x: 16, y: 32};
|
|
@@ -505,6 +511,20 @@ describe("WebDropdownMenu keepTriggerFocus portal overlay", () => {
|
|
|
505
511
|
);
|
|
506
512
|
};
|
|
507
513
|
|
|
514
|
+
it("resolves document.body as the portal target when it is an HTMLElement", () => {
|
|
515
|
+
ensureWebDocument();
|
|
516
|
+
const body = new (globalThis as {HTMLElement: typeof HTMLElement}).HTMLElement();
|
|
517
|
+
Object.defineProperty((globalThis as {document: Document}).document, "body", {
|
|
518
|
+
configurable: true,
|
|
519
|
+
value: body,
|
|
520
|
+
});
|
|
521
|
+
try {
|
|
522
|
+
assert.strictEqual(resolveDocumentBodyPortalTarget(), body);
|
|
523
|
+
} finally {
|
|
524
|
+
restoreWebDocument();
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
|
|
508
528
|
it("renders fixed-position overlay instead of Modal when keepTriggerFocus is true on web", () => {
|
|
509
529
|
ensureWebDocument();
|
|
510
530
|
savedOS = PlatformModule.OS;
|
|
@@ -530,6 +550,25 @@ describe("WebDropdownMenu keepTriggerFocus portal overlay", () => {
|
|
|
530
550
|
}
|
|
531
551
|
});
|
|
532
552
|
|
|
553
|
+
it("renders in the body portal without requiring trigger focus retention", () => {
|
|
554
|
+
ensureWebDocument();
|
|
555
|
+
savedOS = PlatformModule.OS;
|
|
556
|
+
try {
|
|
557
|
+
PlatformModule.OS = "web";
|
|
558
|
+
const {getByTestId, queryByTestId, root} = renderKeepFocusMenu({
|
|
559
|
+
keepTriggerFocus: false,
|
|
560
|
+
renderInBodyPortal: true,
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
assert.isOk(getByTestId("web_dropdown_menu"));
|
|
564
|
+
assert.isNull(queryByTestId("web_dropdown_modal"));
|
|
565
|
+
assert.equal(root.findAll((node) => node.type === "Modal").length, 0);
|
|
566
|
+
} finally {
|
|
567
|
+
PlatformModule.OS = savedOS;
|
|
568
|
+
restoreWebDocument();
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
|
|
533
572
|
it("renders a placeholder view when keepTriggerFocus menu is closed on web", () => {
|
|
534
573
|
ensureWebDocument();
|
|
535
574
|
savedOS = PlatformModule.OS;
|
|
@@ -643,6 +682,53 @@ describe("useWebDropdownAnchor", () => {
|
|
|
643
682
|
expect(result.current.anchor).toEqual({height: 40, width: 100, x: 10, y: 20});
|
|
644
683
|
});
|
|
645
684
|
|
|
685
|
+
it("ignores an older measurement that completes after a newer request", () => {
|
|
686
|
+
const {result} = renderHook(() => useWebDropdownAnchor());
|
|
687
|
+
const callbacks: Array<(x: number, y: number, width: number, height: number) => void> = [];
|
|
688
|
+
const measureInWindow = mock(
|
|
689
|
+
(callback: (x: number, y: number, width: number, height: number) => void): void => {
|
|
690
|
+
callbacks.push(callback);
|
|
691
|
+
}
|
|
692
|
+
);
|
|
693
|
+
(result.current.triggerRef as {current: unknown}).current = {measureInWindow};
|
|
694
|
+
const onFirstMeasured = mock(() => {});
|
|
695
|
+
const onSecondMeasured = mock(() => {});
|
|
696
|
+
|
|
697
|
+
act(() => {
|
|
698
|
+
result.current.measure(onFirstMeasured);
|
|
699
|
+
result.current.measure(onSecondMeasured);
|
|
700
|
+
});
|
|
701
|
+
act(() => {
|
|
702
|
+
callbacks[1](20, 30, 120, 50);
|
|
703
|
+
callbacks[0](10, 15, 100, 40);
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
assert.deepEqual(result.current.anchor, {height: 50, width: 120, x: 20, y: 30});
|
|
707
|
+
assert.equal(onFirstMeasured.mock.calls.length, 0);
|
|
708
|
+
assert.equal(onSecondMeasured.mock.calls.length, 1);
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
it("ignores a pending measurement after cancellation", () => {
|
|
712
|
+
const {result} = renderHook(() => useWebDropdownAnchor());
|
|
713
|
+
let completeMeasurement = (_x: number, _y: number, _width: number, _height: number): void => {};
|
|
714
|
+
const measureInWindow = mock(
|
|
715
|
+
(callback: (x: number, y: number, width: number, height: number) => void): void => {
|
|
716
|
+
completeMeasurement = callback;
|
|
717
|
+
}
|
|
718
|
+
);
|
|
719
|
+
(result.current.triggerRef as {current: unknown}).current = {measureInWindow};
|
|
720
|
+
const onMeasured = mock(() => {});
|
|
721
|
+
|
|
722
|
+
act(() => {
|
|
723
|
+
result.current.measure(onMeasured);
|
|
724
|
+
result.current.cancelPendingMeasurement();
|
|
725
|
+
completeMeasurement(10, 20, 100, 40);
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
assert.deepEqual(result.current.anchor, {height: 0, width: 0, x: 0, y: 0});
|
|
729
|
+
assert.equal(onMeasured.mock.calls.length, 0);
|
|
730
|
+
});
|
|
731
|
+
|
|
646
732
|
it("exercises the Pressable style callback for hover/pressed states", () => {
|
|
647
733
|
const {root} = renderWithTheme(
|
|
648
734
|
<WebDropdownMenu
|
|
@@ -714,6 +800,19 @@ describe("scheduleAfterPaint", () => {
|
|
|
714
800
|
});
|
|
715
801
|
|
|
716
802
|
describe("WebDropdownMenu layout callbacks", () => {
|
|
803
|
+
// scheduleAfterPaint defers to requestAnimationFrame; run callbacks synchronously so
|
|
804
|
+
// scrolling happens inside the act() block that fires the layout events.
|
|
805
|
+
const runAnimationFramesSynchronously = (): (() => void) => {
|
|
806
|
+
const globals = globalThis as {requestAnimationFrame?: typeof requestAnimationFrame};
|
|
807
|
+
const originalRaf = globals.requestAnimationFrame;
|
|
808
|
+
globals.requestAnimationFrame = ((callback: FrameRequestCallback) => {
|
|
809
|
+
callback(0);
|
|
810
|
+
return 1;
|
|
811
|
+
}) as unknown as typeof requestAnimationFrame;
|
|
812
|
+
return () => {
|
|
813
|
+
globals.requestAnimationFrame = originalRaf;
|
|
814
|
+
};
|
|
815
|
+
};
|
|
717
816
|
const anchor = {height: 40, width: 200, x: 16, y: 32};
|
|
718
817
|
const options = [
|
|
719
818
|
{label: "Option A", value: "a"},
|
|
@@ -767,6 +866,120 @@ describe("WebDropdownMenu layout callbacks", () => {
|
|
|
767
866
|
expect(getByTestId("web_dropdown_option_b")).toBeTruthy();
|
|
768
867
|
});
|
|
769
868
|
|
|
869
|
+
it("centers the selected option once its layout and the viewport height are known", () => {
|
|
870
|
+
const restoreRaf = runAnimationFramesSynchronously();
|
|
871
|
+
|
|
872
|
+
try {
|
|
873
|
+
const {getByTestId, root} = renderWithTheme(
|
|
874
|
+
<WebDropdownMenu
|
|
875
|
+
anchor={anchor}
|
|
876
|
+
disableSearch
|
|
877
|
+
onClose={() => {}}
|
|
878
|
+
onSelect={() => {}}
|
|
879
|
+
options={options}
|
|
880
|
+
selectedIndex={2}
|
|
881
|
+
visible
|
|
882
|
+
/>
|
|
883
|
+
);
|
|
884
|
+
|
|
885
|
+
const scrollView = root.findAll((node) => node.type === "ScrollView")[0];
|
|
886
|
+
const scrollCalls: Array<{animated?: boolean; y?: number}> = [];
|
|
887
|
+
const scrollRef = scrollView.props.ref as {current: unknown};
|
|
888
|
+
scrollRef.current = {
|
|
889
|
+
scrollTo: (options: {animated?: boolean; y?: number}) => {
|
|
890
|
+
scrollCalls.push(options);
|
|
891
|
+
},
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
act(() => {
|
|
895
|
+
fireEvent(getByTestId("web_dropdown_option_c"), "layout", {
|
|
896
|
+
nativeEvent: {layout: {height: 40, width: 200, x: 0, y: 80}},
|
|
897
|
+
});
|
|
898
|
+
fireEvent(scrollView, "layout", {nativeEvent: {layout: {height: 60, width: 200}}});
|
|
899
|
+
});
|
|
900
|
+
|
|
901
|
+
// offset 80 - viewport 60 / 2 + height 40 / 2 = 70
|
|
902
|
+
assert.deepEqual(scrollCalls, [{animated: false, y: 70}]);
|
|
903
|
+
} finally {
|
|
904
|
+
restoreRaf();
|
|
905
|
+
}
|
|
906
|
+
});
|
|
907
|
+
|
|
908
|
+
it("clamps the centered scroll offset at the top of the list", () => {
|
|
909
|
+
const restoreRaf = runAnimationFramesSynchronously();
|
|
910
|
+
|
|
911
|
+
try {
|
|
912
|
+
const {getByTestId, root} = renderWithTheme(
|
|
913
|
+
<WebDropdownMenu
|
|
914
|
+
anchor={anchor}
|
|
915
|
+
disableSearch
|
|
916
|
+
onClose={() => {}}
|
|
917
|
+
onSelect={() => {}}
|
|
918
|
+
options={options}
|
|
919
|
+
selectedIndex={0}
|
|
920
|
+
visible
|
|
921
|
+
/>
|
|
922
|
+
);
|
|
923
|
+
|
|
924
|
+
const scrollView = root.findAll((node) => node.type === "ScrollView")[0];
|
|
925
|
+
const scrollCalls: Array<{animated?: boolean; y?: number}> = [];
|
|
926
|
+
const scrollRef = scrollView.props.ref as {current: unknown};
|
|
927
|
+
scrollRef.current = {
|
|
928
|
+
scrollTo: (options: {animated?: boolean; y?: number}) => {
|
|
929
|
+
scrollCalls.push(options);
|
|
930
|
+
},
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
act(() => {
|
|
934
|
+
fireEvent(scrollView, "layout", {nativeEvent: {layout: {height: 200, width: 200}}});
|
|
935
|
+
fireEvent(getByTestId("web_dropdown_option_a"), "layout", {
|
|
936
|
+
nativeEvent: {layout: {height: 40, width: 200, x: 0, y: 0}},
|
|
937
|
+
});
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
assert.deepEqual(scrollCalls, [{animated: false, y: 0}]);
|
|
941
|
+
} finally {
|
|
942
|
+
restoreRaf();
|
|
943
|
+
}
|
|
944
|
+
});
|
|
945
|
+
|
|
946
|
+
it("does not scroll when the list viewport height has not been measured yet", () => {
|
|
947
|
+
const restoreRaf = runAnimationFramesSynchronously();
|
|
948
|
+
|
|
949
|
+
try {
|
|
950
|
+
const {getByTestId, root} = renderWithTheme(
|
|
951
|
+
<WebDropdownMenu
|
|
952
|
+
anchor={anchor}
|
|
953
|
+
disableSearch
|
|
954
|
+
onClose={() => {}}
|
|
955
|
+
onSelect={() => {}}
|
|
956
|
+
options={options}
|
|
957
|
+
selectedIndex={1}
|
|
958
|
+
visible
|
|
959
|
+
/>
|
|
960
|
+
);
|
|
961
|
+
|
|
962
|
+
const scrollView = root.findAll((node) => node.type === "ScrollView")[0];
|
|
963
|
+
const scrollCalls: Array<{animated?: boolean; y?: number}> = [];
|
|
964
|
+
const scrollRef = scrollView.props.ref as {current: unknown};
|
|
965
|
+
scrollRef.current = {
|
|
966
|
+
scrollTo: (options: {animated?: boolean; y?: number}) => {
|
|
967
|
+
scrollCalls.push(options);
|
|
968
|
+
},
|
|
969
|
+
};
|
|
970
|
+
|
|
971
|
+
act(() => {
|
|
972
|
+
fireEvent(getByTestId("web_dropdown_option_b"), "layout", {
|
|
973
|
+
nativeEvent: {layout: {height: 40, width: 200, x: 0, y: 40}},
|
|
974
|
+
});
|
|
975
|
+
});
|
|
976
|
+
|
|
977
|
+
assert.lengthOf(scrollCalls, 0);
|
|
978
|
+
} finally {
|
|
979
|
+
restoreRaf();
|
|
980
|
+
}
|
|
981
|
+
});
|
|
982
|
+
|
|
770
983
|
it("does not schedule a scroll for option layout when nothing is selected", () => {
|
|
771
984
|
const {getByTestId} = renderWithTheme(
|
|
772
985
|
<WebDropdownMenu
|