@terreno/ui 0.30.0 → 0.31.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.
@@ -0,0 +1,377 @@
1
+ import {afterEach, beforeEach, describe, expect, it, mock, spyOn} from "bun:test";
2
+ import {act, render} from "@testing-library/react-native";
3
+ import React from "react";
4
+ import {Platform, Text as RNText, View} from "react-native";
5
+ import {Gesture} from "react-native-gesture-handler";
6
+ import * as Reanimated from "react-native-reanimated";
7
+
8
+ import {DraggableList, DragItem} from "./DraggableList";
9
+
10
+ interface PanEvent {
11
+ translationY: number;
12
+ }
13
+
14
+ interface CapturedGesture {
15
+ onBegin?: () => void;
16
+ onChange?: (event: PanEvent) => void;
17
+ onFinalize?: () => void;
18
+ }
19
+
20
+ let capturedGestures: CapturedGesture[] = [];
21
+
22
+ /** Replaces Gesture.Pan with a chainable stub that records the registered handlers. */
23
+ const capturePanGestures = () => {
24
+ return spyOn(Gesture, "Pan").mockImplementation(() => {
25
+ const captured: CapturedGesture = {};
26
+ capturedGestures.push(captured);
27
+ const chainable = {
28
+ onBegin: (handler: () => void) => {
29
+ captured.onBegin = handler;
30
+ return chainable;
31
+ },
32
+ onChange: (handler: (event: PanEvent) => void) => {
33
+ captured.onChange = handler;
34
+ return chainable;
35
+ },
36
+ onFinalize: (handler: () => void) => {
37
+ captured.onFinalize = handler;
38
+ return chainable;
39
+ },
40
+ };
41
+ return chainable as unknown as ReturnType<typeof Gesture.Pan>;
42
+ });
43
+ };
44
+
45
+ const renderItem = ({item}: {item: string}): React.ReactElement => <RNText>{item}</RNText>;
46
+
47
+ describe("DraggableList", () => {
48
+ let panSpy: ReturnType<typeof capturePanGestures>;
49
+
50
+ beforeEach(() => {
51
+ capturedGestures = [];
52
+ panSpy = capturePanGestures();
53
+ });
54
+
55
+ afterEach(() => {
56
+ panSpy.mockRestore();
57
+ });
58
+
59
+ it("renders one draggable item per id", () => {
60
+ const {getByText} = render(
61
+ <DraggableList
62
+ callbackNewDataIds={() => {}}
63
+ dataIDs={["a", "b", "c"]}
64
+ renderItem={renderItem}
65
+ />
66
+ );
67
+
68
+ expect(getByText("a")).toBeTruthy();
69
+ expect(getByText("b")).toBeTruthy();
70
+ expect(getByText("c")).toBeTruthy();
71
+ });
72
+
73
+ it("falls back to the deprecated data prop", () => {
74
+ const {getByText} = render(
75
+ <DraggableList callbackNewDataIds={() => {}} data={["one", "two"]} renderItem={renderItem} />
76
+ );
77
+
78
+ expect(getByText("one")).toBeTruthy();
79
+ expect(getByText("two")).toBeTruthy();
80
+ });
81
+
82
+ it("renders an empty list when no ids are provided", () => {
83
+ const {toJSON} = render(
84
+ <DraggableList callbackNewDataIds={() => {}} renderItem={renderItem} />
85
+ );
86
+
87
+ expect(toJSON()).toBeTruthy();
88
+ expect(capturedGestures).toHaveLength(0);
89
+ });
90
+
91
+ it("renders a custom grip element", () => {
92
+ const {getByText} = render(
93
+ <DraggableList
94
+ callbackNewDataIds={() => {}}
95
+ dataIDs={["a"]}
96
+ renderGrip={<RNText>grip</RNText>}
97
+ renderItem={renderItem}
98
+ />
99
+ );
100
+
101
+ expect(getByText("grip")).toBeTruthy();
102
+ });
103
+
104
+ it("renders a grip render function", () => {
105
+ const {getByText} = render(
106
+ <DraggableList
107
+ callbackNewDataIds={() => {}}
108
+ dataIDs={["a"]}
109
+ renderGrip={() => <RNText>grip-fn</RNText>}
110
+ renderItem={renderItem}
111
+ />
112
+ );
113
+
114
+ expect(getByText("grip-fn")).toBeTruthy();
115
+ });
116
+
117
+ it("throws when the data prop is not an array", () => {
118
+ expect(() =>
119
+ render(
120
+ <DraggableList
121
+ callbackNewDataIds={() => {}}
122
+ dataIDs={"nope" as unknown as string[]}
123
+ renderItem={renderItem}
124
+ />
125
+ )
126
+ ).toThrow(/should be \[\]/);
127
+ });
128
+
129
+ it("throws when renderItem is missing", () => {
130
+ expect(() =>
131
+ render(
132
+ <DraggableList
133
+ callbackNewDataIds={() => {}}
134
+ dataIDs={["a"]}
135
+ renderItem={undefined as unknown as typeof renderItem}
136
+ />
137
+ )
138
+ ).toThrow(/"renderItem" prop is missing/);
139
+ });
140
+
141
+ it("throws when callbackNewDataIds is missing", () => {
142
+ expect(() =>
143
+ render(
144
+ <DraggableList
145
+ callbackNewDataIds={undefined as unknown as (ids: string[]) => void}
146
+ dataIDs={["a"]}
147
+ renderItem={renderItem}
148
+ />
149
+ )
150
+ ).toThrow(/"callbackNewDataIds" prop is missing/);
151
+ });
152
+
153
+ it("throws when callbackNewDataIds is not a function", () => {
154
+ expect(() =>
155
+ render(
156
+ <DraggableList
157
+ callbackNewDataIds={"nope" as unknown as (ids: string[]) => void}
158
+ dataIDs={["a"]}
159
+ renderItem={renderItem}
160
+ />
161
+ )
162
+ ).toThrow(/should be function type/);
163
+ });
164
+
165
+ it("updates positions when the ids change", () => {
166
+ const {getByText, rerender} = render(
167
+ <DraggableList callbackNewDataIds={() => {}} dataIDs={["a", "b"]} renderItem={renderItem} />
168
+ );
169
+
170
+ rerender(
171
+ <DraggableList callbackNewDataIds={() => {}} dataIDs={["b", "a"]} renderItem={renderItem} />
172
+ );
173
+
174
+ expect(getByText("a")).toBeTruthy();
175
+ expect(getByText("b")).toBeTruthy();
176
+ });
177
+
178
+ describe("drag gestures", () => {
179
+ it("vibrates when the drag begins", () => {
180
+ const passVibration = mock(() => {});
181
+ render(
182
+ <DraggableList
183
+ callbackNewDataIds={() => {}}
184
+ dataIDs={["a", "b"]}
185
+ passVibration={passVibration}
186
+ renderItem={renderItem}
187
+ />
188
+ );
189
+
190
+ act(() => {
191
+ capturedGestures[0]?.onBegin?.();
192
+ });
193
+
194
+ expect(passVibration).toHaveBeenCalled();
195
+ });
196
+
197
+ it("begins the drag without a vibration callback", () => {
198
+ render(
199
+ <DraggableList callbackNewDataIds={() => {}} dataIDs={["a", "b"]} renderItem={renderItem} />
200
+ );
201
+
202
+ expect(() =>
203
+ act(() => {
204
+ capturedGestures[0]?.onBegin?.();
205
+ })
206
+ ).not.toThrow();
207
+ });
208
+
209
+ it("reorders items and vibrates when dragged past another item", () => {
210
+ const passVibration = mock(() => {});
211
+ render(
212
+ <DraggableList
213
+ callbackNewDataIds={() => {}}
214
+ dataIDs={["a", "b", "c"]}
215
+ itemHeight={50}
216
+ itemsGap={5}
217
+ passVibration={passVibration}
218
+ renderItem={renderItem}
219
+ />
220
+ );
221
+
222
+ act(() => {
223
+ capturedGestures[0]?.onBegin?.();
224
+ capturedGestures[0]?.onChange?.({translationY: 120});
225
+ });
226
+
227
+ // Once for the drag start, once for crossing into a new position.
228
+ expect(passVibration.mock.calls.length).toBeGreaterThan(1);
229
+ });
230
+
231
+ it("does not reorder when the item stays in place", () => {
232
+ const passVibration = mock(() => {});
233
+ render(
234
+ <DraggableList
235
+ callbackNewDataIds={() => {}}
236
+ dataIDs={["a", "b", "c"]}
237
+ passVibration={passVibration}
238
+ renderItem={renderItem}
239
+ />
240
+ );
241
+
242
+ act(() => {
243
+ capturedGestures[0]?.onChange?.({translationY: 1});
244
+ });
245
+
246
+ expect(passVibration).not.toHaveBeenCalled();
247
+ });
248
+
249
+ it("clamps the new position to the end of the list", () => {
250
+ const callbackNewDataIds = mock(() => {});
251
+ render(
252
+ <DraggableList
253
+ callbackNewDataIds={callbackNewDataIds}
254
+ dataIDs={["a", "b", "c"]}
255
+ renderItem={renderItem}
256
+ />
257
+ );
258
+
259
+ act(() => {
260
+ capturedGestures[0]?.onChange?.({translationY: 10000});
261
+ capturedGestures[0]?.onFinalize?.();
262
+ });
263
+
264
+ expect(callbackNewDataIds).toHaveBeenCalledWith(["c", "b", "a"]);
265
+ });
266
+
267
+ it("reports the new order once per unique arrangement", () => {
268
+ const callbackNewDataIds = mock(() => {});
269
+ render(
270
+ <DraggableList
271
+ callbackNewDataIds={callbackNewDataIds}
272
+ dataIDs={["a", "b"]}
273
+ renderItem={renderItem}
274
+ />
275
+ );
276
+
277
+ act(() => {
278
+ capturedGestures[0]?.onFinalize?.();
279
+ capturedGestures[0]?.onFinalize?.();
280
+ });
281
+
282
+ expect(callbackNewDataIds).toHaveBeenCalledTimes(1);
283
+ expect(callbackNewDataIds).toHaveBeenCalledWith(["a", "b"]);
284
+ });
285
+ });
286
+
287
+ describe("animated styles", () => {
288
+ const originalOS = Platform.OS;
289
+
290
+ afterEach(() => {
291
+ Platform.OS = originalOS;
292
+ });
293
+
294
+ it("omits shadows on Android", () => {
295
+ Platform.OS = "android";
296
+ const {getByText} = render(
297
+ <DraggableList callbackNewDataIds={() => {}} dataIDs={["a"]} renderItem={renderItem} />
298
+ );
299
+
300
+ expect(getByText("a")).toBeTruthy();
301
+ });
302
+
303
+ it("adds shadow styles on iOS", () => {
304
+ Platform.OS = "ios";
305
+ const {getByText} = render(
306
+ <DraggableList callbackNewDataIds={() => {}} dataIDs={["a"]} renderItem={renderItem} />
307
+ );
308
+
309
+ expect(getByText("a")).toBeTruthy();
310
+ });
311
+ });
312
+
313
+ it("mirrors the shared positions into state", () => {
314
+ let invocations = 0;
315
+ const reactionSpy = spyOn(Reanimated, "useAnimatedReaction").mockImplementation(((
316
+ prepare: () => unknown,
317
+ react: (current: unknown, previous: unknown) => void
318
+ ) => {
319
+ invocations += 1;
320
+ // Only run the first registered reaction (the list-level one) so the
321
+ // render-phase state update settles instead of looping.
322
+ if (invocations === 1) {
323
+ react(prepare(), undefined);
324
+ }
325
+ }) as unknown as typeof Reanimated.useAnimatedReaction);
326
+
327
+ const {getByText} = render(
328
+ <DraggableList callbackNewDataIds={() => {}} dataIDs={["a", "b"]} renderItem={renderItem} />
329
+ );
330
+
331
+ expect(getByText("a")).toBeTruthy();
332
+ reactionSpy.mockRestore();
333
+ });
334
+
335
+ describe("DragItem", () => {
336
+ const baseProps = {
337
+ index: 0,
338
+ item: "a",
339
+ itemBorderRadius: 8,
340
+ itemHeight: 50,
341
+ itemsCount: 1,
342
+ itemsGap: 5,
343
+ plainPosition: 0,
344
+ positions: {value: {a: 0}},
345
+ renderItem,
346
+ scrollY: {value: 0},
347
+ };
348
+
349
+ it("syncs its offset when the shared positions change", () => {
350
+ const reactionSpy = spyOn(Reanimated, "useAnimatedReaction").mockImplementation(((
351
+ prepare: () => unknown,
352
+ react: (current: unknown, previous: unknown) => void
353
+ ) => {
354
+ react(prepare(), undefined);
355
+ }) as unknown as typeof Reanimated.useAnimatedReaction);
356
+
357
+ const {getByText} = render(
358
+ <View>
359
+ <DragItem {...baseProps} item="b" itemsCount={2} positions={{value: {a: 0, b: 1}}} />
360
+ </View>
361
+ );
362
+
363
+ expect(getByText("b")).toBeTruthy();
364
+ reactionSpy.mockRestore();
365
+ });
366
+
367
+ it("falls back to a zero offset when the computed offset is not a number", () => {
368
+ const {getByText} = render(
369
+ <View>
370
+ <DragItem {...baseProps} itemHeight={Number.NaN} />
371
+ </View>
372
+ );
373
+
374
+ expect(getByText("a")).toBeTruthy();
375
+ });
376
+ });
377
+ });
@@ -0,0 +1,69 @@
1
+ import {describe, expect, it} from "bun:test";
2
+ import type {ReactTestRendererJSON} from "react-test-renderer";
3
+
4
+ import {EditableCard} from "./EditableCard";
5
+ import {Text} from "./Text";
6
+ import {renderWithTheme} from "./test-utils";
7
+
8
+ describe("EditableCard", () => {
9
+ it("renders correctly with all props", () => {
10
+ const {toJSON} = renderWithTheme(
11
+ <EditableCard
12
+ badge={{iconName: "check", secondary: true, status: "success", value: "Verified"}}
13
+ description="123 Main Street"
14
+ helperText="Last updated 2 days ago"
15
+ iconName="location-dot"
16
+ onEdit={() => {}}
17
+ title="Home Address"
18
+ />
19
+ );
20
+ expect(toJSON()).toMatchSnapshot();
21
+ });
22
+
23
+ it("renders title, description and helper text", () => {
24
+ const {getByText} = renderWithTheme(
25
+ <EditableCard
26
+ description="123 Main Street"
27
+ helperText="Last updated 2 days ago"
28
+ title="Home Address"
29
+ />
30
+ );
31
+ expect(getByText("Home Address")).toBeTruthy();
32
+ expect(getByText("123 Main Street")).toBeTruthy();
33
+ expect(getByText("Last updated 2 days ago")).toBeTruthy();
34
+ });
35
+
36
+ it("renders the badge", () => {
37
+ const {getByText} = renderWithTheme(
38
+ <EditableCard
39
+ badge={{secondary: true, status: "success", value: "Verified"}}
40
+ title="Home Address"
41
+ />
42
+ );
43
+ expect(getByText("Verified")).toBeTruthy();
44
+ });
45
+
46
+ it("uses the attention surface when attention is set", () => {
47
+ const {toJSON} = renderWithTheme(
48
+ <EditableCard attention description="123 Main Street" title="Home Address" />
49
+ );
50
+ const rendered = toJSON() as ReactTestRendererJSON;
51
+ expect(rendered.props.style).toMatchObject({backgroundColor: "#F2F9FA"});
52
+ });
53
+
54
+ it("renders children below the text content", () => {
55
+ const {getByText} = renderWithTheme(
56
+ <EditableCard title="Home Address">
57
+ <Text>Extra content</Text>
58
+ </EditableCard>
59
+ );
60
+ expect(getByText("Extra content")).toBeTruthy();
61
+ });
62
+
63
+ it("renders with testID", () => {
64
+ const {getByTestId} = renderWithTheme(
65
+ <EditableCard testID="editable-card" title="Home Address" />
66
+ );
67
+ expect(getByTestId("editable-card")).toBeTruthy();
68
+ });
69
+ });
@@ -0,0 +1,73 @@
1
+ import type React from "react";
2
+
3
+ import {Badge} from "./Badge";
4
+ import {Box} from "./Box";
5
+ import {Card} from "./Card";
6
+ import type {EditableCardProps} from "./Common";
7
+ import {Icon} from "./Icon";
8
+ import {IconButton} from "./IconButton";
9
+ import {Text} from "./Text";
10
+
11
+ export const EditableCard = ({
12
+ children,
13
+ attention = false,
14
+ badge,
15
+ color,
16
+ description,
17
+ editAccessibilityLabel = "Edit",
18
+ helperText,
19
+ iconName,
20
+ onEdit,
21
+ padding = 3,
22
+ title,
23
+ ...rest
24
+ }: EditableCardProps): React.ReactElement => {
25
+ return (
26
+ <Card
27
+ borderLeft="default"
28
+ borderRight="default"
29
+ color={color ?? (attention ? "secondaryExtraLight" : "base")}
30
+ direction="row"
31
+ gap={2}
32
+ padding={padding}
33
+ rounding="md"
34
+ width="100%"
35
+ {...rest}
36
+ >
37
+ {Boolean(iconName) && (
38
+ <Box paddingY={1}>
39
+ <Icon iconName={iconName!} size="md" />
40
+ </Box>
41
+ )}
42
+ <Box direction="column" flex="grow" gap={1} minWidth={0}>
43
+ {(Boolean(title) || Boolean(badge)) && (
44
+ <Box alignItems="center" direction="row" gap={2}>
45
+ {Boolean(title) && (
46
+ <Box flex="shrink" minWidth={0}>
47
+ <Text bold size="md" truncate>
48
+ {title}
49
+ </Text>
50
+ </Box>
51
+ )}
52
+ {Boolean(badge) && <Badge {...badge!} />}
53
+ </Box>
54
+ )}
55
+ {Boolean(description) && <Text size="md">{description}</Text>}
56
+ {Boolean(helperText) && (
57
+ <Text color="secondaryLight" size="sm">
58
+ {helperText}
59
+ </Text>
60
+ )}
61
+ {children}
62
+ </Box>
63
+ {Boolean(onEdit) && (
64
+ <IconButton
65
+ accessibilityLabel={editAccessibilityLabel}
66
+ iconName="pencil"
67
+ onClick={onEdit!}
68
+ variant="ghost"
69
+ />
70
+ )}
71
+ </Card>
72
+ );
73
+ };
@@ -1,5 +1,3 @@
1
- // noExplicitAny: test mock typing
2
- // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
3
1
  import {describe, expect, it, mock} from "bun:test";
4
2
  import {act, fireEvent} from "@testing-library/react-native";
5
3
  import type {ReactTestInstance} from "react-test-renderer";
@@ -7,6 +5,17 @@ import type {ReactTestInstance} from "react-test-renderer";
7
5
  import {RNPickerSelect} from "./PickerSelect";
8
6
  import {renderWithTheme} from "./test-utils";
9
7
 
8
+ /** Minimal element stub used to fake `document.activeElement` on web. */
9
+ interface MockElement {
10
+ blur: () => void;
11
+ }
12
+
13
+ /** Globals the web rendering tests stub out. */
14
+ const globalScope = globalThis as unknown as {
15
+ document?: unknown;
16
+ HTMLElement?: new () => MockElement;
17
+ };
18
+
10
19
  // Note: @react-native-picker/picker is mocked globally in bunSetup.ts
11
20
 
12
21
  describe("PickerSelect", () => {
@@ -166,22 +175,24 @@ describe("PickerSelect", () => {
166
175
 
167
176
  describe("web rendering (Platform.OS === 'web')", () => {
168
177
  const PlatformModule = require("react-native").Platform;
169
- let savedOS: any;
178
+ let savedOS: unknown;
170
179
 
171
180
  let hadDocument = false;
172
- let savedDocument: any;
181
+ let savedDocument: unknown;
173
182
 
174
183
  const searchEnabledWebProps = {...defaultProps, disableSearch: false};
175
184
 
176
185
  const ensureDocument = () => {
177
186
  hadDocument = "document" in globalThis;
178
- savedDocument = (globalThis as any).document;
179
- if (typeof (globalThis as any).HTMLElement === "undefined") {
180
- (globalThis as any).HTMLElement = class HTMLElement {};
187
+ savedDocument = globalScope.document;
188
+ if (globalScope.HTMLElement === undefined) {
189
+ globalScope.HTMLElement = class HTMLElement {
190
+ blur = (): void => {};
191
+ };
181
192
  }
182
- const el = new (globalThis as any).HTMLElement();
193
+ const el = new globalScope.HTMLElement();
183
194
  el.blur = () => {};
184
- (globalThis as any).document = {
195
+ globalScope.document = {
185
196
  activeElement: el,
186
197
  addEventListener: () => {},
187
198
  body: {
@@ -194,9 +205,9 @@ describe("PickerSelect", () => {
194
205
 
195
206
  const restoreDocument = () => {
196
207
  if (hadDocument) {
197
- (globalThis as any).document = savedDocument;
208
+ globalScope.document = savedDocument;
198
209
  } else {
199
- delete (globalThis as any).document;
210
+ delete globalScope.document;
200
211
  }
201
212
  };
202
213
 
@@ -517,7 +528,7 @@ describe("PickerSelect", () => {
517
528
 
518
529
  describe("android rendering", () => {
519
530
  const PlatformModule = require("react-native").Platform;
520
- let savedOS: any;
531
+ let savedOS: unknown;
521
532
 
522
533
  it("renders android headless when useNativeAndroidPickerStyle is false", () => {
523
534
  savedOS = PlatformModule.OS;