@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.
Files changed (91) hide show
  1. package/LICENSE +21 -203
  2. package/dist/Badge.d.ts +1 -1
  3. package/dist/Badge.js +37 -1
  4. package/dist/Badge.js.map +1 -1
  5. package/dist/Common.d.ts +149 -3
  6. package/dist/Common.js.map +1 -1
  7. package/dist/DateTimeField.js +5 -21
  8. package/dist/DateTimeField.js.map +1 -1
  9. package/dist/EditableCard.d.ts +3 -0
  10. package/dist/EditableCard.js +23 -0
  11. package/dist/EditableCard.js.map +1 -0
  12. package/dist/Filter.d.ts +13 -0
  13. package/dist/Filter.js +120 -0
  14. package/dist/Filter.js.map +1 -0
  15. package/dist/FilterAccordion.d.ts +8 -0
  16. package/dist/FilterAccordion.js +59 -0
  17. package/dist/FilterAccordion.js.map +1 -0
  18. package/dist/FilterBoolean.d.ts +8 -0
  19. package/dist/FilterBoolean.js +74 -0
  20. package/dist/FilterBoolean.js.map +1 -0
  21. package/dist/FilterChangesBadge.d.ts +8 -0
  22. package/dist/FilterChangesBadge.js +11 -0
  23. package/dist/FilterChangesBadge.js.map +1 -0
  24. package/dist/FilterSelectMenu.d.ts +8 -0
  25. package/dist/FilterSelectMenu.js +37 -0
  26. package/dist/FilterSelectMenu.js.map +1 -0
  27. package/dist/IconRegistry.js +11 -6
  28. package/dist/IconRegistry.js.map +1 -1
  29. package/dist/Page.d.ts +2 -1
  30. package/dist/Page.js +0 -2
  31. package/dist/Page.js.map +1 -1
  32. package/dist/PickerSelect.d.ts +8 -1
  33. package/dist/PickerSelect.js +2 -2
  34. package/dist/PickerSelect.js.map +1 -1
  35. package/dist/SelectField.js +2 -2
  36. package/dist/SelectField.js.map +1 -1
  37. package/dist/Theme.js +3 -0
  38. package/dist/Theme.js.map +1 -1
  39. package/dist/WebDropdownMenu.d.ts +5 -1
  40. package/dist/WebDropdownMenu.js +24 -10
  41. package/dist/WebDropdownMenu.js.map +1 -1
  42. package/dist/icons/BarsFilterIcon.d.ts +9 -0
  43. package/dist/icons/BarsFilterIcon.js +12 -0
  44. package/dist/icons/BarsFilterIcon.js.map +1 -0
  45. package/dist/index.d.ts +7 -0
  46. package/dist/index.js +7 -0
  47. package/dist/index.js.map +1 -1
  48. package/package.json +26 -25
  49. package/src/ActionSheet.test.tsx +202 -172
  50. package/src/Badge.test.tsx +22 -1
  51. package/src/Badge.tsx +43 -0
  52. package/src/Box.test.tsx +31 -23
  53. package/src/Common.ts +167 -5
  54. package/src/ConsentFormScreen.test.tsx +22 -0
  55. package/src/ConsentNavigator.test.tsx +6 -8
  56. package/src/DateTimeField.test.tsx +66 -18
  57. package/src/DateTimeField.tsx +5 -19
  58. package/src/DraggableList.test.tsx +377 -0
  59. package/src/EditableCard.test.tsx +69 -0
  60. package/src/EditableCard.tsx +73 -0
  61. package/src/Filter.test.tsx +94 -0
  62. package/src/Filter.tsx +257 -0
  63. package/src/FilterAccordion.test.tsx +58 -0
  64. package/src/FilterAccordion.tsx +113 -0
  65. package/src/FilterBoolean.test.tsx +65 -0
  66. package/src/FilterBoolean.tsx +130 -0
  67. package/src/FilterChangesBadge.test.tsx +16 -0
  68. package/src/FilterChangesBadge.tsx +13 -0
  69. package/src/FilterSelectMenu.test.tsx +56 -0
  70. package/src/FilterSelectMenu.tsx +76 -0
  71. package/src/IconRegistry.test.tsx +6 -4
  72. package/src/IconRegistry.tsx +13 -8
  73. package/src/Page.tsx +2 -3
  74. package/src/PickerSelect.test.tsx +43 -12
  75. package/src/PickerSelect.tsx +10 -0
  76. package/src/SelectField.tsx +2 -0
  77. package/src/SidebarNavigation.test.tsx +339 -0
  78. package/src/Theme.tsx +3 -0
  79. package/src/WebDropdownMenu.test.tsx +214 -1
  80. package/src/WebDropdownMenu.tsx +43 -21
  81. package/src/__snapshots__/Card.test.tsx.snap +1 -0
  82. package/src/__snapshots__/EditableCard.test.tsx.snap +242 -0
  83. package/src/__snapshots__/Filter.test.tsx.snap +161 -0
  84. package/src/__snapshots__/FilterAccordion.test.tsx.snap +83 -0
  85. package/src/__snapshots__/FilterBoolean.test.tsx.snap +109 -0
  86. package/src/__snapshots__/FilterChangesBadge.test.tsx.snap +18 -0
  87. package/src/__snapshots__/FilterSelectMenu.test.tsx.snap +359 -0
  88. package/src/bunSetup.ts +6 -2
  89. package/src/conflictFieldDiff.test.ts +43 -0
  90. package/src/icons/BarsFilterIcon.tsx +18 -0
  91. package/src/index.tsx +7 -0
@@ -1,5 +1,3 @@
1
- // noExplicitAny: test mock typing
2
- // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
3
1
  import {
4
2
  afterAll,
5
3
  afterEach,
@@ -11,7 +9,9 @@ import {
11
9
  mock,
12
10
  } from "bun:test";
13
11
  import {act, userEvent} from "@testing-library/react-native";
12
+ import {assert} from "chai";
14
13
  import {DateTime} from "luxon";
14
+ import type {ReactTestInstance} from "react-test-renderer";
15
15
 
16
16
  import {DateTimeField} from "./DateTimeField";
17
17
  import {renderWithTheme, setupComponentTest, teardownComponentTest} from "./test-utils";
@@ -279,8 +279,8 @@ describe("DateTimeField", () => {
279
279
  });
280
280
 
281
281
  // Month should be capped and always return a valid date
282
- const validCalls = mockOnChange.mock.calls.filter((args: any[]) => {
283
- const date = DateTime.fromISO(args[0]);
282
+ const validCalls = mockOnChange.mock.calls.filter((args: unknown[]) => {
283
+ const date = DateTime.fromISO(String(args[0]));
284
284
  return date.isValid && date.month <= 12;
285
285
  });
286
286
 
@@ -307,7 +307,7 @@ describe("DateTimeField", () => {
307
307
  // and must never emit an invalid minute (60).
308
308
  expect(mockOnChange).toHaveBeenCalled();
309
309
  expect(mockOnChange).toHaveBeenCalledTimes(4);
310
- const calls = mockOnChange.mock.calls.map(([iso]: any) => iso);
310
+ const calls = mockOnChange.mock.calls.map((call: unknown[]) => String(call[0]));
311
311
  // No call should use an invalid "60" minute.
312
312
  expect(calls.some((iso: string) => iso.includes(":60:00.000Z"))).toBe(false);
313
313
  // It should include a reset to "00" and then a valid "06".
@@ -762,7 +762,7 @@ describe("DateTimeField", () => {
762
762
  );
763
763
 
764
764
  const actionSheet = UNSAFE_root.findAll(
765
- (n: any) => n.props?.onChange && n.props?.visible !== undefined
765
+ (n: ReactTestInstance) => n.props?.onChange && n.props?.visible !== undefined
766
766
  );
767
767
  expect(actionSheet.length).toBeGreaterThan(0);
768
768
  await act(async () => {
@@ -783,7 +783,7 @@ describe("DateTimeField", () => {
783
783
  );
784
784
 
785
785
  const actionSheet = UNSAFE_root.findAll(
786
- (n: any) => n.props?.onChange && n.props?.visible !== undefined
786
+ (n: ReactTestInstance) => n.props?.onChange && n.props?.visible !== undefined
787
787
  );
788
788
  expect(actionSheet.length).toBeGreaterThan(0);
789
789
  await act(async () => {
@@ -804,7 +804,7 @@ describe("DateTimeField", () => {
804
804
  );
805
805
 
806
806
  const actionSheet = UNSAFE_root.findAll(
807
- (n: any) => n.props?.onChange && n.props?.visible !== undefined
807
+ (n: ReactTestInstance) => n.props?.onChange && n.props?.visible !== undefined
808
808
  );
809
809
  expect(actionSheet.length).toBeGreaterThan(0);
810
810
  await act(async () => {
@@ -825,7 +825,7 @@ describe("DateTimeField", () => {
825
825
  );
826
826
 
827
827
  const actionSheet = UNSAFE_root.findAll(
828
- (n: any) => n.props?.onChange && n.props?.visible !== undefined
828
+ (n: ReactTestInstance) => n.props?.onChange && n.props?.visible !== undefined
829
829
  );
830
830
  expect(actionSheet.length).toBeGreaterThan(0);
831
831
  await act(async () => {
@@ -848,7 +848,7 @@ describe("DateTimeField", () => {
848
848
  />
849
849
  );
850
850
 
851
- const amPmSelects = UNSAFE_root.findAll((n: any) => n.props?.onAmPmChange);
851
+ const amPmSelects = UNSAFE_root.findAll((n: ReactTestInstance) => n.props?.onAmPmChange);
852
852
  expect(amPmSelects.length).toBeGreaterThan(0);
853
853
  await act(async () => {
854
854
  amPmSelects[0].props.onAmPmChange("pm");
@@ -871,7 +871,7 @@ describe("DateTimeField", () => {
871
871
  />
872
872
  );
873
873
 
874
- const tzPickers = UNSAFE_root.findAll((n: any) => n.props?.onTimezoneChange);
874
+ const tzPickers = UNSAFE_root.findAll((n: ReactTestInstance) => n.props?.onTimezoneChange);
875
875
  expect(tzPickers.length).toBeGreaterThan(0);
876
876
  await act(async () => {
877
877
  tzPickers[0].props.onTimezoneChange("America/Chicago");
@@ -890,7 +890,7 @@ describe("DateTimeField", () => {
890
890
  />
891
891
  );
892
892
 
893
- const tzPickers = UNSAFE_root.findAll((n: any) => n.props?.onTimezoneChange);
893
+ const tzPickers = UNSAFE_root.findAll((n: ReactTestInstance) => n.props?.onTimezoneChange);
894
894
  expect(tzPickers.length).toBeGreaterThan(0);
895
895
  await act(async () => {
896
896
  tzPickers[0].props.onTimezoneChange("America/Chicago");
@@ -995,7 +995,7 @@ describe("DateTimeField", () => {
995
995
  );
996
996
 
997
997
  const actionSheet = UNSAFE_root.findAll(
998
- (n: any) => n.props?.onDismiss && n.props?.visible !== undefined
998
+ (n: ReactTestInstance) => n.props?.onDismiss && n.props?.visible !== undefined
999
999
  );
1000
1000
  expect(actionSheet.length).toBeGreaterThan(0);
1001
1001
  await act(async () => {
@@ -1015,7 +1015,7 @@ describe("DateTimeField", () => {
1015
1015
  />
1016
1016
  );
1017
1017
 
1018
- const pressables = UNSAFE_root.findAll((n: any) => n.props?.onLayout);
1018
+ const pressables = UNSAFE_root.findAll((n: ReactTestInstance) => n.props?.onLayout);
1019
1019
  expect(pressables.length).toBeGreaterThan(0);
1020
1020
  await act(async () => {
1021
1021
  pressables[0].props.onLayout({nativeEvent: {layout: {width: 500}}});
@@ -1035,7 +1035,7 @@ describe("DateTimeField", () => {
1035
1035
  value="2023-05-15T15:30:00.000Z"
1036
1036
  />
1037
1037
  );
1038
- const selects = UNSAFE_root.findAll((n: any) => {
1038
+ const selects = UNSAFE_root.findAll((n: ReactTestInstance) => {
1039
1039
  const opts = n.props?.options;
1040
1040
  return (
1041
1041
  Array.isArray(opts) &&
@@ -1180,7 +1180,7 @@ describe("DateTimeField", () => {
1180
1180
 
1181
1181
  mockOnChange.mockClear();
1182
1182
  const actionSheet = UNSAFE_root.findAll(
1183
- (n: any) => n.props?.onChange && n.props?.visible !== undefined
1183
+ (n: ReactTestInstance) => n.props?.onChange && n.props?.visible !== undefined
1184
1184
  );
1185
1185
  expect(actionSheet.length).toBeGreaterThan(0);
1186
1186
  await act(async () => {
@@ -1282,7 +1282,7 @@ describe("DateTimeField", () => {
1282
1282
  />
1283
1283
  );
1284
1284
 
1285
- const tzPickers = UNSAFE_root.findAll((n: any) => n.props?.onTimezoneChange);
1285
+ const tzPickers = UNSAFE_root.findAll((n: ReactTestInstance) => n.props?.onTimezoneChange);
1286
1286
  expect(tzPickers.length).toBeGreaterThan(0);
1287
1287
  await act(async () => {
1288
1288
  tzPickers[0].props.onTimezoneChange("America/Chicago");
@@ -1301,7 +1301,7 @@ describe("DateTimeField", () => {
1301
1301
  />
1302
1302
  );
1303
1303
 
1304
- const tzPickers = UNSAFE_root.findAll((n: any) => n.props?.onTimezoneChange);
1304
+ const tzPickers = UNSAFE_root.findAll((n: ReactTestInstance) => n.props?.onTimezoneChange);
1305
1305
  expect(tzPickers.length).toBeGreaterThan(0);
1306
1306
  await act(async () => {
1307
1307
  tzPickers[0].props.onTimezoneChange("America/Chicago");
@@ -1310,6 +1310,40 @@ describe("DateTimeField", () => {
1310
1310
  });
1311
1311
  });
1312
1312
 
1313
+ describe("TimezonePicker onChange", () => {
1314
+ it("forwards the picked timezone to onTimezoneChange and emits a new value", async () => {
1315
+ setDesktop();
1316
+ const mockTzChange = mock(() => {});
1317
+ const {UNSAFE_root} = renderWithTheme(
1318
+ <DateTimeField
1319
+ onChange={mockOnChange}
1320
+ onTimezoneChange={mockTzChange}
1321
+ timezone="America/New_York"
1322
+ type="time"
1323
+ value="2023-05-15T15:30:00.000Z"
1324
+ />
1325
+ );
1326
+
1327
+ // Select the rendered TimezonePicker itself rather than any ancestor that happens to
1328
+ // have an onTimezoneChange prop, so the component's own handler runs.
1329
+ const pickers = UNSAFE_root.findAll((node: ReactTestInstance) =>
1330
+ Boolean((node.props as {shortTimezone?: boolean}).shortTimezone)
1331
+ );
1332
+ assert.isAbove(pickers.length, 0);
1333
+ await act(async () => {
1334
+ pickers[0].props.onChange("America/Chicago");
1335
+ });
1336
+
1337
+ assert.isTrue(mockTzChange.mock.calls.some((call) => call[0] === "America/Chicago"));
1338
+ // The picked timezone is applied to the current segments and emitted.
1339
+ assert.isAbove(mockOnChange.mock.calls.length, 0);
1340
+ assert.equal(
1341
+ mockOnChange.mock.calls[mockOnChange.mock.calls.length - 1][0],
1342
+ "2023-05-15T16:30:00.000Z"
1343
+ );
1344
+ });
1345
+ });
1346
+
1313
1347
  describe("minute validation in validateField", () => {
1314
1348
  it("should validate minute field for datetime type via hour change triggering revalidation", async () => {
1315
1349
  setDesktop();
@@ -1332,4 +1366,18 @@ describe("DateTimeField", () => {
1332
1366
  expect(hourInput).toBeTruthy();
1333
1367
  });
1334
1368
  });
1369
+
1370
+ describe("timezone abbreviation", () => {
1371
+ it("falls back to the current time when there is no value and no complete segments", () => {
1372
+ setMobile();
1373
+ const {getByText} = renderWithTheme(
1374
+ <DateTimeField onChange={mockOnChange} timezone="America/New_York" type="time" value="" />
1375
+ );
1376
+
1377
+ const expectedAbbr = DateTime.now().setZone("America/New_York").offsetNameShort ?? "";
1378
+ assert.isNotEmpty(expectedAbbr);
1379
+ // With no value the placeholder is built from the current time in the given timezone.
1380
+ assert.isTrue(Boolean(getByText(`12:00 PM ${expectedAbbr}`)));
1381
+ });
1382
+ });
1335
1383
  });
@@ -3,6 +3,7 @@ import {DateTime} from "luxon";
3
3
  import React, {type FC, useCallback, useEffect, useMemo, useRef, useState} from "react";
4
4
  import {Pressable, TextInput, View} from "react-native";
5
5
 
6
+ import type {ActionSheet} from "./ActionSheet";
6
7
  import {Box} from "./Box";
7
8
  import type {DateTimeFieldProps} from "./Common";
8
9
  import {DateTimeActionSheet} from "./DateTimeActionSheet";
@@ -424,9 +425,7 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
424
425
  }): React.ReactElement => {
425
426
  const {theme} = useTheme();
426
427
  const fieldTestIDs = resolveFieldTestIDsFromProps({testID, testIDs});
427
- // noExplicitAny: ActionSheet class is defined in ActionSheet.tsx which imports from Common.ts indirectly; using its type here would create a circular dependency
428
- // biome-ignore lint/suspicious/noExplicitAny: ActionSheet class is defined in ActionSheet.tsx which imports from Common.ts indirectly; using its type here would create a circular dependency
429
- const dateActionSheetRef: React.RefObject<any> = React.createRef();
428
+ const dateActionSheetRef: React.RefObject<ActionSheet | null> = React.createRef();
430
429
  const [amPm, setAmPm] = useState<"am" | "pm">("am");
431
430
  const [showDate, setShowDate] = useState(false);
432
431
  const [month, setMonth] = useState("");
@@ -532,6 +531,7 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
532
531
  }
533
532
  }
534
533
 
534
+ // Minutes are not validated here: handleFieldChange validates them before this is reached.
535
535
  if (type === "time" || type === "datetime") {
536
536
  if (fieldIndex === (type === "time" ? 0 : 3)) {
537
537
  // Hour
@@ -539,12 +539,6 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
539
539
  if (Number.isNaN(hourNum) || hourNum < 1 || hourNum > 12) {
540
540
  return "Hour must be between 1 and 12";
541
541
  }
542
- } else if (fieldIndex === (type === "time" ? 1 : 4)) {
543
- // Minute
544
- const minuteNum = parseInt(fieldValue, 10);
545
- if (Number.isNaN(minuteNum) || minuteNum < 0 || minuteNum > 59) {
546
- return "Minute must be between 0 and 59";
547
- }
548
542
  }
549
543
  }
550
544
 
@@ -691,11 +685,7 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
691
685
  setFieldErrors((prev) => ({...prev, [index]: "Minute must be between 0 and 59"}));
692
686
  }
693
687
 
694
- // Auto-advance to next field if current field is full
695
- const configs = getFieldConfigs();
696
- if (finalValue.length === config.maxLength && index < configs.length - 1) {
697
- inputRefs.current[index + 1]?.focus();
698
- }
688
+ // The minute segment is always last, so there is nothing to advance focus to.
699
689
  return;
700
690
  }
701
691
 
@@ -811,11 +801,7 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
811
801
  .set({millisecond: 0, second: 0})
812
802
  .toISO()
813
803
  : parsedDate.set({millisecond: 0, second: 0}).toUTC().toISO();
814
- if (!normalized) {
815
- console.warn("Invalid date passed to DateTimeField", parsedDate);
816
- return;
817
- }
818
- onChange(normalized);
804
+ onChange(normalized ?? "");
819
805
  setShowDate(false);
820
806
  },
821
807
  [onChange, type]
@@ -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
+ });