@terreno/ui 0.29.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.
Files changed (43) hide show
  1. package/LICENSE +21 -203
  2. package/dist/AiSuggestionBox.js +7 -12
  3. package/dist/AiSuggestionBox.js.map +1 -1
  4. package/dist/Common.d.ts +65 -0
  5. package/dist/Common.js.map +1 -1
  6. package/dist/EditableCard.d.ts +3 -0
  7. package/dist/EditableCard.js +23 -0
  8. package/dist/EditableCard.js.map +1 -0
  9. package/dist/Theme.js +1 -0
  10. package/dist/Theme.js.map +1 -1
  11. package/dist/ThumbsUpDownFeedback.d.ts +8 -0
  12. package/dist/ThumbsUpDownFeedback.js +60 -0
  13. package/dist/ThumbsUpDownFeedback.js.map +1 -0
  14. package/dist/index.d.ts +2 -0
  15. package/dist/index.js +2 -0
  16. package/dist/index.js.map +1 -1
  17. package/package.json +3 -2
  18. package/src/ActionSheet.test.tsx +80 -37
  19. package/src/AiSuggestionBox.test.tsx +2 -2
  20. package/src/AiSuggestionBox.tsx +17 -48
  21. package/src/Common.ts +75 -0
  22. package/src/ConsentHistory.test.tsx +224 -0
  23. package/src/ConsentNavigator.test.tsx +6 -8
  24. package/src/DataTable.test.tsx +15 -21
  25. package/src/DateTimeField.test.tsx +36 -0
  26. package/src/DraggableList.test.tsx +377 -0
  27. package/src/EditableCard.test.tsx +69 -0
  28. package/src/EditableCard.tsx +73 -0
  29. package/src/PickerSelect.test.tsx +23 -12
  30. package/src/SidebarNavigation.test.tsx +339 -0
  31. package/src/SignatureCaptureField.test.tsx +30 -0
  32. package/src/TapToEdit.test.tsx +8 -14
  33. package/src/Theme.tsx +1 -0
  34. package/src/ThumbsUpDownFeedback.test.tsx +100 -0
  35. package/src/ThumbsUpDownFeedback.tsx +118 -0
  36. package/src/Tooltip.test.tsx +1 -0
  37. package/src/__snapshots__/AiSuggestionBox.test.tsx.snap +108 -30
  38. package/src/__snapshots__/Card.test.tsx.snap +1 -0
  39. package/src/__snapshots__/EditableCard.test.tsx.snap +242 -0
  40. package/src/__snapshots__/SignatureCaptureField.test.tsx.snap +45 -0
  41. package/src/bunSetup.ts +11 -2
  42. package/src/generateConsentHistoryPdf.test.ts +253 -0
  43. package/src/index.tsx +2 -0
@@ -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;
@@ -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
+ });
@@ -91,6 +91,36 @@ describe("SignatureCaptureField", () => {
91
91
  expect(toJSON()).toMatchSnapshot();
92
92
  });
93
93
 
94
+ it("renders a placeholder box when disabled with a drawn value that has no image", () => {
95
+ const {toJSON} = renderWithTheme(
96
+ <SignatureCaptureField disabled onChange={() => {}} value={{image: "", mode: "draw"}} />
97
+ );
98
+ expect(toJSON()).toMatchSnapshot();
99
+ });
100
+
101
+ it("renders a read-only typed signature when disabled with a typed value", () => {
102
+ const {getByDisplayValue, queryByText} = renderWithTheme(
103
+ <SignatureCaptureField
104
+ disabled
105
+ helperText="Signed on Jan 1"
106
+ onChange={() => {}}
107
+ title="Signature"
108
+ value={{fontKey: "dancing-script", mode: "type", typedName: "Jane Doe"}}
109
+ />
110
+ );
111
+ expect(getByDisplayValue("Jane Doe")).toBeTruthy();
112
+ expect(queryByText("Draw")).toBeNull();
113
+ expect(queryByText("Type")).toBeNull();
114
+ });
115
+
116
+ it("renders the disabled typed field when disabled without a value", () => {
117
+ const {getByText, queryByText} = renderWithTheme(
118
+ <SignatureCaptureField disabled helperText="No signature captured" onChange={() => {}} />
119
+ );
120
+ expect(getByText("No signature captured")).toBeTruthy();
121
+ expect(queryByText("Draw")).toBeNull();
122
+ });
123
+
94
124
  it("forwards the resolved input test id to the typed name input in type mode", () => {
95
125
  const {UNSAFE_getAllByProps} = renderWithTheme(
96
126
  <SignatureCaptureField {...defaultProps} testID="sig" />
@@ -1,6 +1,4 @@
1
- // noExplicitAny: test mock typing
2
- // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
3
- import {describe, expect, it, mock} from "bun:test";
1
+ import {describe, expect, it, mock, spyOn} from "bun:test";
4
2
  import {act, fireEvent} from "@testing-library/react-native";
5
3
  import {Linking} from "react-native";
6
4
 
@@ -141,9 +139,7 @@ describe("TapToEdit", () => {
141
139
  });
142
140
 
143
141
  it("invokes Linking.openURL for url type when clicked", async () => {
144
- const originalOpen = Linking.openURL;
145
- const openMock = mock(() => Promise.resolve(true));
146
- (Linking as any).openURL = openMock;
142
+ const openURLSpy = spyOn(Linking, "openURL").mockImplementation(() => Promise.resolve(true));
147
143
 
148
144
  const {getByLabelText} = renderWithTheme(
149
145
  <TapToEdit editable={false} title="Site" type="url" value="https://example.com" />
@@ -152,15 +148,13 @@ describe("TapToEdit", () => {
152
148
  await act(async () => {
153
149
  fireEvent.press(getByLabelText("Link"));
154
150
  });
155
- expect(openMock).toHaveBeenCalled();
151
+ expect(openURLSpy).toHaveBeenCalled();
156
152
 
157
- (Linking as any).openURL = originalOpen;
153
+ openURLSpy.mockRestore();
158
154
  });
159
155
 
160
156
  it("invokes Linking.openURL with google maps for address type when clicked", async () => {
161
- const originalOpen = Linking.openURL;
162
- const openMock = mock(() => Promise.resolve(true));
163
- (Linking as any).openURL = openMock;
157
+ const openURLSpy = spyOn(Linking, "openURL").mockImplementation(() => Promise.resolve(true));
164
158
 
165
159
  const {getByLabelText} = renderWithTheme(
166
160
  <TapToEdit
@@ -174,11 +168,11 @@ describe("TapToEdit", () => {
174
168
  await act(async () => {
175
169
  fireEvent.press(getByLabelText("Link"));
176
170
  });
177
- expect(openMock).toHaveBeenCalled();
178
- const arg = openMock.mock.calls[0][0];
171
+ expect(openURLSpy).toHaveBeenCalled();
172
+ const arg = openURLSpy.mock.calls[0][0];
179
173
  expect(arg).toContain("google.com/maps");
180
174
 
181
- (Linking as any).openURL = originalOpen;
175
+ openURLSpy.mockRestore();
182
176
  });
183
177
 
184
178
  it("throws when editable is true and setValue is not provided", () => {
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",
@@ -0,0 +1,100 @@
1
+ import {describe, expect, it, jest} from "bun:test";
2
+ import {fireEvent} from "@testing-library/react-native";
3
+
4
+ import {ThumbsUpDownFeedback} from "./ThumbsUpDownFeedback";
5
+ import {renderWithTheme} from "./test-utils";
6
+
7
+ describe("ThumbsUpDownFeedback", () => {
8
+ it("renders both options", () => {
9
+ const {getByTestId} = renderWithTheme(
10
+ <ThumbsUpDownFeedback onChange={jest.fn()} testID="feedback" />
11
+ );
12
+ expect(getByTestId("feedback-positive")).toBeTruthy();
13
+ expect(getByTestId("feedback-negative")).toBeTruthy();
14
+ });
15
+
16
+ it("calls onChange with the pressed value", () => {
17
+ const onChange = jest.fn();
18
+ const {getByTestId} = renderWithTheme(
19
+ <ThumbsUpDownFeedback onChange={onChange} testID="feedback" />
20
+ );
21
+ fireEvent.press(getByTestId("feedback-negative"));
22
+ expect(onChange).toHaveBeenCalledWith("negative");
23
+ });
24
+
25
+ it("clears the selection when pressing the selected option", () => {
26
+ const onChange = jest.fn();
27
+ const {getByTestId} = renderWithTheme(
28
+ <ThumbsUpDownFeedback onChange={onChange} testID="feedback" value="positive" />
29
+ );
30
+ fireEvent.press(getByTestId("feedback-positive"));
31
+ expect(onChange).toHaveBeenCalledWith(undefined);
32
+ });
33
+
34
+ it("exposes the selection and custom labels to assistive technology", () => {
35
+ const {getByTestId} = renderWithTheme(
36
+ <ThumbsUpDownFeedback
37
+ negativeAccessibilityLabel="Not helpful"
38
+ onChange={jest.fn()}
39
+ positiveAccessibilityLabel="Helpful"
40
+ testID="feedback"
41
+ value="negative"
42
+ />
43
+ );
44
+ const positive = getByTestId("feedback-positive");
45
+ const negative = getByTestId("feedback-negative");
46
+ expect(positive.props.accessibilityLabel).toBe("Helpful");
47
+ expect(negative.props.accessibilityLabel).toBe("Not helpful");
48
+ expect(positive.props.accessibilityState.selected).toBe(false);
49
+ expect(negative.props.accessibilityState.selected).toBe(true);
50
+ // react-native-web ignores accessibilityState, so the flat aria prop is what reaches the DOM.
51
+ expect(positive.props["aria-checked"]).toBe(false);
52
+ expect(negative.props["aria-checked"]).toBe(true);
53
+ });
54
+
55
+ it("activates on the spacebar, which react-native-web only handles for buttons", async () => {
56
+ const onChange = jest.fn();
57
+ const preventDefault = jest.fn();
58
+ const {getByTestId} = renderWithTheme(
59
+ <ThumbsUpDownFeedback onChange={onChange} testID="feedback" />
60
+ );
61
+ await getByTestId("feedback-positive").props.onKeyDown({key: " ", preventDefault});
62
+ expect(preventDefault).toHaveBeenCalled();
63
+ expect(onChange).toHaveBeenCalledWith("positive");
64
+ });
65
+
66
+ it("ignores auto-repeat from a held spacebar", async () => {
67
+ const onChange = jest.fn();
68
+ const preventDefault = jest.fn();
69
+ const {getByTestId} = renderWithTheme(
70
+ <ThumbsUpDownFeedback onChange={onChange} testID="feedback" />
71
+ );
72
+ await getByTestId("feedback-positive").props.onKeyDown({
73
+ key: " ",
74
+ preventDefault,
75
+ repeat: true,
76
+ });
77
+ expect(preventDefault).toHaveBeenCalled();
78
+ expect(onChange).not.toHaveBeenCalled();
79
+ });
80
+
81
+ it("ignores other keys so react-native-web keeps handling enter", async () => {
82
+ const onChange = jest.fn();
83
+ const preventDefault = jest.fn();
84
+ const {getByTestId} = renderWithTheme(
85
+ <ThumbsUpDownFeedback onChange={onChange} testID="feedback" />
86
+ );
87
+ await getByTestId("feedback-positive").props.onKeyDown({key: "Enter", preventDefault});
88
+ expect(preventDefault).not.toHaveBeenCalled();
89
+ expect(onChange).not.toHaveBeenCalled();
90
+ });
91
+
92
+ it("does not call onChange when disabled", () => {
93
+ const onChange = jest.fn();
94
+ const {getByTestId} = renderWithTheme(
95
+ <ThumbsUpDownFeedback disabled onChange={onChange} testID="feedback" />
96
+ );
97
+ fireEvent.press(getByTestId("feedback-positive"));
98
+ expect(onChange).not.toHaveBeenCalled();
99
+ });
100
+ });