@terreno/admin-frontend 0.9.3 → 0.11.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.
- package/dist/AdminFieldRenderer.js +7 -2
- package/dist/AdminFieldRenderer.js.map +1 -1
- package/dist/AdminObjectPicker.d.ts +14 -0
- package/dist/AdminObjectPicker.js +128 -0
- package/dist/AdminObjectPicker.js.map +1 -0
- package/dist/AdminPrimitiveArrayField.d.ts +26 -0
- package/dist/AdminPrimitiveArrayField.js +55 -0
- package/dist/AdminPrimitiveArrayField.js.map +1 -0
- package/dist/AdminRefField.d.ts +0 -35
- package/dist/AdminRefField.js +2 -75
- package/dist/AdminRefField.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +8 -1
- package/dist/types.js.map +1 -1
- package/package.json +5 -4
- package/src/AdminFieldRenderer.test.tsx +445 -0
- package/src/AdminFieldRenderer.tsx +22 -2
- package/src/AdminModelForm.test.tsx +541 -0
- package/src/AdminModelList.test.tsx +81 -0
- package/src/AdminModelTable.test.tsx +267 -0
- package/src/AdminObjectPicker.tsx +253 -0
- package/src/AdminPrimitiveArrayField.test.tsx +186 -0
- package/src/AdminPrimitiveArrayField.tsx +186 -0
- package/src/AdminRefField.tsx +6 -81
- package/src/AdminScriptList.test.tsx +49 -2
- package/src/AdminVersionConfig.test.tsx +196 -0
- package/src/CheckboxListEditor.test.tsx +80 -0
- package/src/ConfigurationScreen.test.tsx +140 -0
- package/src/ConsentFormEditor.test.tsx +512 -0
- package/src/ConsentFormList.test.tsx +115 -0
- package/src/ConsentResponseViewer.test.tsx +115 -0
- package/src/DocumentStorageBrowser.test.tsx +398 -0
- package/src/LocaleContentEditor.test.tsx +143 -0
- package/src/index.tsx +2 -0
- package/src/isolated/AdminFieldRenderer.isolated.tsx +289 -0
- package/src/isolated/AdminNestedArrayField.isolated.tsx +297 -0
- package/src/isolated/AdminObjectPicker.isolated.tsx +382 -0
- package/src/isolated/AdminRefField.isolated.tsx +29 -0
- package/src/isolated/AdminScriptRunModal.isolated.tsx +414 -0
- package/src/isolated/DocumentStorageBrowser.isolated.tsx +680 -0
- package/src/isolated/generateConsentPdf.isolated.ts +295 -0
- package/src/isolated/hooks.isolated.tsx +390 -0
- package/src/types.ts +8 -1
- package/src/AdminScriptRunModal.test.tsx +0 -69
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type {Api} from "@reduxjs/toolkit/query/react";
|
|
2
|
+
import {
|
|
3
|
+
BooleanField,
|
|
4
|
+
Box,
|
|
5
|
+
Button,
|
|
6
|
+
Heading,
|
|
7
|
+
IconButton,
|
|
8
|
+
SelectField,
|
|
9
|
+
Text,
|
|
10
|
+
TextField,
|
|
11
|
+
} from "@terreno/ui";
|
|
12
|
+
import startCase from "lodash/startCase";
|
|
13
|
+
import React, {useCallback} from "react";
|
|
14
|
+
import {AdminRefField} from "./AdminRefField";
|
|
15
|
+
|
|
16
|
+
interface AdminPrimitiveArrayFieldProps {
|
|
17
|
+
title: string;
|
|
18
|
+
helperText?: string;
|
|
19
|
+
errorText?: string;
|
|
20
|
+
itemType: string;
|
|
21
|
+
itemEnum?: string[];
|
|
22
|
+
itemRef?: string;
|
|
23
|
+
value: PrimitiveItem[];
|
|
24
|
+
onChange: (value: PrimitiveItem[]) => void;
|
|
25
|
+
// biome-ignore lint/suspicious/noExplicitAny: RTK Query Api type is generic; admin code is type-erased here
|
|
26
|
+
api: Api<any, any, any, any>;
|
|
27
|
+
baseUrl: string;
|
|
28
|
+
modelConfigs?: Array<{name: string; routePath: string}>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type PrimitiveItem = string | number | boolean;
|
|
32
|
+
|
|
33
|
+
const defaultForType = (itemType: string): PrimitiveItem => {
|
|
34
|
+
if (itemType === "boolean") {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if (itemType === "number") {
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
return "";
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Renders an editable list of primitive values (string, number, boolean) or ObjectId refs.
|
|
45
|
+
* Used for Mongoose schemas like `tags: [String]`, `scores: [Number]`, `flags: [Boolean]`,
|
|
46
|
+
* or `memberIds: [{type: ObjectId, ref: "User"}]`.
|
|
47
|
+
*/
|
|
48
|
+
export const AdminPrimitiveArrayField: React.FC<AdminPrimitiveArrayFieldProps> = ({
|
|
49
|
+
title,
|
|
50
|
+
helperText,
|
|
51
|
+
errorText,
|
|
52
|
+
itemType,
|
|
53
|
+
itemEnum,
|
|
54
|
+
itemRef,
|
|
55
|
+
value,
|
|
56
|
+
onChange,
|
|
57
|
+
api,
|
|
58
|
+
baseUrl,
|
|
59
|
+
modelConfigs,
|
|
60
|
+
}) => {
|
|
61
|
+
const arrayValue = Array.isArray(value) ? value : [];
|
|
62
|
+
|
|
63
|
+
const handleAdd = useCallback(() => {
|
|
64
|
+
onChange([...arrayValue, defaultForType(itemType)]);
|
|
65
|
+
}, [arrayValue, itemType, onChange]);
|
|
66
|
+
|
|
67
|
+
const handleRemove = useCallback(
|
|
68
|
+
(index: number) => {
|
|
69
|
+
onChange(arrayValue.filter((_, i) => i !== index));
|
|
70
|
+
},
|
|
71
|
+
[arrayValue, onChange]
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const handleUpdate = useCallback(
|
|
75
|
+
(index: number, itemValue: PrimitiveItem) => {
|
|
76
|
+
onChange(arrayValue.map((item, i) => (i === index ? itemValue : item)));
|
|
77
|
+
},
|
|
78
|
+
[arrayValue, onChange]
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const refModel =
|
|
82
|
+
itemType === "objectid" && itemRef && modelConfigs
|
|
83
|
+
? modelConfigs.find((m) => m.name === itemRef)
|
|
84
|
+
: undefined;
|
|
85
|
+
|
|
86
|
+
const renderItemInput = (item: PrimitiveItem, index: number): React.ReactElement => {
|
|
87
|
+
if (itemType === "boolean") {
|
|
88
|
+
return (
|
|
89
|
+
<BooleanField
|
|
90
|
+
onChange={(val: boolean) => handleUpdate(index, val)}
|
|
91
|
+
title=""
|
|
92
|
+
value={Boolean(item)}
|
|
93
|
+
/>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (itemEnum && itemEnum.length > 0) {
|
|
97
|
+
return (
|
|
98
|
+
<SelectField
|
|
99
|
+
onChange={(val: string) => handleUpdate(index, val)}
|
|
100
|
+
options={itemEnum.map((v) => ({label: startCase(v), value: v}))}
|
|
101
|
+
title=""
|
|
102
|
+
value={item != null ? String(item) : ""}
|
|
103
|
+
/>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (itemType === "objectid" && refModel) {
|
|
107
|
+
return (
|
|
108
|
+
<AdminRefField
|
|
109
|
+
api={api}
|
|
110
|
+
baseUrl={baseUrl}
|
|
111
|
+
onChange={(val: string) => handleUpdate(index, val)}
|
|
112
|
+
refModelName={refModel.name}
|
|
113
|
+
routePath={refModel.routePath}
|
|
114
|
+
title=""
|
|
115
|
+
value={item != null ? String(item) : ""}
|
|
116
|
+
/>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (itemType === "number") {
|
|
120
|
+
return (
|
|
121
|
+
<TextField
|
|
122
|
+
onChange={(text: string) => {
|
|
123
|
+
const num = Number(text);
|
|
124
|
+
handleUpdate(index, Number.isNaN(num) ? text : num);
|
|
125
|
+
}}
|
|
126
|
+
testID={`admin-array-item-${index}`}
|
|
127
|
+
title=""
|
|
128
|
+
value={item != null ? String(item) : ""}
|
|
129
|
+
/>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
// Default: string
|
|
133
|
+
return (
|
|
134
|
+
<TextField
|
|
135
|
+
onChange={(val: string) => handleUpdate(index, val)}
|
|
136
|
+
testID={`admin-array-item-${index}`}
|
|
137
|
+
title=""
|
|
138
|
+
value={item != null ? String(item) : ""}
|
|
139
|
+
/>
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return (
|
|
144
|
+
<Box gap={2}>
|
|
145
|
+
<Box alignItems="center" direction="row" justifyContent="between">
|
|
146
|
+
<Heading size="sm">{title}</Heading>
|
|
147
|
+
<Button
|
|
148
|
+
iconName="plus"
|
|
149
|
+
onClick={handleAdd}
|
|
150
|
+
testID={`admin-array-add-${title}`}
|
|
151
|
+
text="Add"
|
|
152
|
+
variant="outline"
|
|
153
|
+
/>
|
|
154
|
+
</Box>
|
|
155
|
+
{helperText ? (
|
|
156
|
+
<Text color="secondaryDark" size="sm">
|
|
157
|
+
{helperText}
|
|
158
|
+
</Text>
|
|
159
|
+
) : null}
|
|
160
|
+
{errorText ? (
|
|
161
|
+
<Text color="error" size="sm">
|
|
162
|
+
{errorText}
|
|
163
|
+
</Text>
|
|
164
|
+
) : null}
|
|
165
|
+
|
|
166
|
+
{arrayValue.length === 0 ? (
|
|
167
|
+
<Box alignItems="center" padding={3}>
|
|
168
|
+
<Text color="secondaryDark">No items. Click "Add" to create one.</Text>
|
|
169
|
+
</Box>
|
|
170
|
+
) : (
|
|
171
|
+
arrayValue.map((item, index) => (
|
|
172
|
+
<Box alignItems="center" direction="row" gap={2} key={`item-${index}`}>
|
|
173
|
+
<Box flex="grow">{renderItemInput(item, index)}</Box>
|
|
174
|
+
<IconButton
|
|
175
|
+
accessibilityLabel="Remove item"
|
|
176
|
+
iconName="trash"
|
|
177
|
+
onClick={() => handleRemove(index)}
|
|
178
|
+
testID={`admin-array-remove-${index}`}
|
|
179
|
+
variant="destructive"
|
|
180
|
+
/>
|
|
181
|
+
</Box>
|
|
182
|
+
))
|
|
183
|
+
)}
|
|
184
|
+
</Box>
|
|
185
|
+
);
|
|
186
|
+
};
|
package/src/AdminRefField.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {Api} from "@reduxjs/toolkit/query/react";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import React from "react";
|
|
3
|
+
import {AdminObjectPicker} from "./AdminObjectPicker";
|
|
4
4
|
|
|
5
5
|
interface AdminRefFieldProps {
|
|
6
6
|
api: Api<any, any, any, any>;
|
|
@@ -14,51 +14,6 @@ interface AdminRefFieldProps {
|
|
|
14
14
|
helperText?: string;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
// Heuristic to find a display field from an item
|
|
18
|
-
const getDisplayValue = (item: any): string => {
|
|
19
|
-
for (const field of ["name", "title", "email", "label", "displayName"]) {
|
|
20
|
-
if (item[field]) {
|
|
21
|
-
return String(item[field]);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return item._id ?? String(item);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Select field for choosing a referenced model instance by ID.
|
|
29
|
-
*
|
|
30
|
-
* Fetches all items from the referenced model and displays them in a dropdown.
|
|
31
|
-
* Automatically determines a display label by checking common fields (name, title, email, etc.).
|
|
32
|
-
* Falls back to the item's _id if no suitable display field is found.
|
|
33
|
-
*
|
|
34
|
-
* @param props - Component props
|
|
35
|
-
* @param props.api - RTK Query API instance for fetching reference data
|
|
36
|
-
* @param props.baseUrl - Base URL for admin routes (not currently used, kept for consistency)
|
|
37
|
-
* @param props.routePath - Full route path to the referenced model's list endpoint
|
|
38
|
-
* @param props.refModelName - Name of the referenced model (e.g., "User")
|
|
39
|
-
* @param props.title - Label displayed above the select field
|
|
40
|
-
* @param props.value - Currently selected ID
|
|
41
|
-
* @param props.onChange - Callback when selection changes
|
|
42
|
-
* @param props.errorText - Optional validation error message
|
|
43
|
-
* @param props.helperText - Optional helper text shown below the field
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* <AdminRefField
|
|
48
|
-
* api={api}
|
|
49
|
-
* baseUrl="/admin"
|
|
50
|
-
* routePath="/admin/users"
|
|
51
|
-
* refModelName="User"
|
|
52
|
-
* title="Owner"
|
|
53
|
-
* value={ownerId}
|
|
54
|
-
* onChange={(id) => setOwnerId(id)}
|
|
55
|
-
* helperText="Select the user who owns this item"
|
|
56
|
-
* />
|
|
57
|
-
* ```
|
|
58
|
-
*
|
|
59
|
-
* @see AdminFieldRenderer for field type selection
|
|
60
|
-
* @see useAdminApi for the API hook pattern
|
|
61
|
-
*/
|
|
62
17
|
export const AdminRefField: React.FC<AdminRefFieldProps> = ({
|
|
63
18
|
api,
|
|
64
19
|
routePath,
|
|
@@ -69,44 +24,14 @@ export const AdminRefField: React.FC<AdminRefFieldProps> = ({
|
|
|
69
24
|
errorText,
|
|
70
25
|
helperText,
|
|
71
26
|
}) => {
|
|
72
|
-
const endpointKey = `adminRefList_${refModelName}`;
|
|
73
|
-
|
|
74
|
-
const enhancedApi = useMemo(() => {
|
|
75
|
-
return api.injectEndpoints({
|
|
76
|
-
endpoints: (build: any) => ({
|
|
77
|
-
[endpointKey]: build.query({
|
|
78
|
-
query: () => ({
|
|
79
|
-
method: "GET",
|
|
80
|
-
params: {limit: 100},
|
|
81
|
-
url: routePath,
|
|
82
|
-
}),
|
|
83
|
-
}),
|
|
84
|
-
}),
|
|
85
|
-
overrideExisting: true,
|
|
86
|
-
});
|
|
87
|
-
}, [api, routePath, endpointKey]);
|
|
88
|
-
|
|
89
|
-
const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
90
|
-
const useRefQuery = (enhancedApi as any)[`use${capitalize(endpointKey)}Query`];
|
|
91
|
-
const {data, isLoading} = useRefQuery();
|
|
92
|
-
|
|
93
|
-
const options = useMemo(() => {
|
|
94
|
-
if (!data?.data) {
|
|
95
|
-
return [];
|
|
96
|
-
}
|
|
97
|
-
return data.data.map((item: any) => ({
|
|
98
|
-
label: getDisplayValue(item),
|
|
99
|
-
value: item._id,
|
|
100
|
-
}));
|
|
101
|
-
}, [data]);
|
|
102
|
-
|
|
103
27
|
return (
|
|
104
|
-
<
|
|
105
|
-
|
|
28
|
+
<AdminObjectPicker
|
|
29
|
+
api={api}
|
|
106
30
|
errorText={errorText}
|
|
107
31
|
helperText={helperText}
|
|
108
32
|
onChange={onChange}
|
|
109
|
-
|
|
33
|
+
refModelName={refModelName}
|
|
34
|
+
routePath={routePath}
|
|
110
35
|
title={title}
|
|
111
36
|
value={value}
|
|
112
37
|
/>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
import {renderWithTheme} from "@terreno/ui/src/test-utils";
|
|
3
3
|
import React from "react";
|
|
4
|
+
import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
|
|
4
5
|
import {AdminScriptList} from "./AdminScriptList";
|
|
5
6
|
|
|
6
7
|
// Mock useAdminConfig to control returned data
|
|
@@ -14,9 +15,24 @@ mock.module("./useAdminConfig", () => ({
|
|
|
14
15
|
useAdminConfig: (...args: any[]) => mockUseAdminConfig(...args),
|
|
15
16
|
}));
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
const modalCallbacks: {
|
|
19
|
+
scriptName: string | null;
|
|
20
|
+
visible: boolean;
|
|
21
|
+
onDismiss: () => void;
|
|
22
|
+
} = {onDismiss: () => undefined, scriptName: null, visible: false};
|
|
23
|
+
|
|
24
|
+
// Mock AdminScriptRunModal so we can observe its props and invoke onDismiss
|
|
18
25
|
mock.module("./AdminScriptRunModal", () => ({
|
|
19
|
-
AdminScriptRunModal: (
|
|
26
|
+
AdminScriptRunModal: (props: {
|
|
27
|
+
scriptName: string | null;
|
|
28
|
+
visible: boolean;
|
|
29
|
+
onDismiss: () => void;
|
|
30
|
+
}) => {
|
|
31
|
+
modalCallbacks.scriptName = props.scriptName;
|
|
32
|
+
modalCallbacks.visible = props.visible;
|
|
33
|
+
modalCallbacks.onDismiss = props.onDismiss;
|
|
34
|
+
return null;
|
|
35
|
+
},
|
|
20
36
|
}));
|
|
21
37
|
|
|
22
38
|
const mockApi = {} as any;
|
|
@@ -103,6 +119,37 @@ describe("AdminScriptList", () => {
|
|
|
103
119
|
expect(getByTestId("admin-script-run-test-script")).toBeTruthy();
|
|
104
120
|
});
|
|
105
121
|
|
|
122
|
+
it("opens modal when Run button is pressed and dismisses it", async () => {
|
|
123
|
+
mockUseAdminConfig.mockReturnValue({
|
|
124
|
+
config: {
|
|
125
|
+
models: [],
|
|
126
|
+
scripts: [{description: "Test script", name: "test-script"}],
|
|
127
|
+
},
|
|
128
|
+
error: null,
|
|
129
|
+
isLoading: false,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const {getByTestId} = renderWithTheme(<AdminScriptList api={mockApi} baseUrl="/admin" />);
|
|
133
|
+
|
|
134
|
+
expect(modalCallbacks.visible).toBe(false);
|
|
135
|
+
expect(modalCallbacks.scriptName).toBeNull();
|
|
136
|
+
|
|
137
|
+
const runBtn = getByTestId("admin-script-run-test-script");
|
|
138
|
+
await act(async () => {
|
|
139
|
+
fireEvent.press(runBtn);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
expect(modalCallbacks.visible).toBe(true);
|
|
143
|
+
expect(modalCallbacks.scriptName).toBe("test-script");
|
|
144
|
+
|
|
145
|
+
await act(async () => {
|
|
146
|
+
modalCallbacks.onDismiss();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
expect(modalCallbacks.visible).toBe(false);
|
|
150
|
+
expect(modalCallbacks.scriptName).toBeNull();
|
|
151
|
+
});
|
|
152
|
+
|
|
106
153
|
it("disables Run button when isAdmin is false", () => {
|
|
107
154
|
mockUseAdminConfig.mockReturnValue({
|
|
108
155
|
config: {
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {renderWithTheme} from "@terreno/ui/src/test-utils";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
|
|
5
|
+
|
|
6
|
+
const routerBack = mock(() => {});
|
|
7
|
+
mock.module("expo-router", () => ({
|
|
8
|
+
router: {back: routerBack},
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
interface ApiState {
|
|
12
|
+
data: any;
|
|
13
|
+
error: unknown;
|
|
14
|
+
isLoading: boolean;
|
|
15
|
+
updateImpl: (body: any) => Promise<any>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const apiState: ApiState = {
|
|
19
|
+
data: null,
|
|
20
|
+
error: null,
|
|
21
|
+
isLoading: false,
|
|
22
|
+
updateImpl: async () => ({}),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const updateCalls: any[] = [];
|
|
26
|
+
const querySpecs: any[] = [];
|
|
27
|
+
const mutationSpecs: any[] = [];
|
|
28
|
+
const makeApi = () => ({
|
|
29
|
+
injectEndpoints: ({endpoints}: {endpoints: (b: any) => Record<string, any>}) => {
|
|
30
|
+
const build = {
|
|
31
|
+
mutation: (spec: any) => {
|
|
32
|
+
// Invoke the mutation's query lambda so the URL + body builder runs.
|
|
33
|
+
if (typeof spec?.query === "function") {
|
|
34
|
+
mutationSpecs.push(spec.query({demo: true}));
|
|
35
|
+
}
|
|
36
|
+
return spec;
|
|
37
|
+
},
|
|
38
|
+
query: (spec: any) => {
|
|
39
|
+
// Invoke the query lambda so the request descriptor builder runs.
|
|
40
|
+
if (typeof spec?.query === "function") {
|
|
41
|
+
querySpecs.push(spec.query());
|
|
42
|
+
}
|
|
43
|
+
return spec;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
endpoints(build);
|
|
47
|
+
return {
|
|
48
|
+
useAdminVersionConfigQuery: () => ({
|
|
49
|
+
data: apiState.data,
|
|
50
|
+
error: apiState.error,
|
|
51
|
+
isLoading: apiState.isLoading,
|
|
52
|
+
}),
|
|
53
|
+
useUpdateVersionConfigMutation: () => [
|
|
54
|
+
(body: any) => ({
|
|
55
|
+
unwrap: async () => {
|
|
56
|
+
updateCalls.push(body);
|
|
57
|
+
return apiState.updateImpl(body);
|
|
58
|
+
},
|
|
59
|
+
}),
|
|
60
|
+
{isLoading: false},
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
import {AdminVersionConfig} from "./AdminVersionConfig";
|
|
67
|
+
|
|
68
|
+
describe("AdminVersionConfig", () => {
|
|
69
|
+
beforeEach(() => {
|
|
70
|
+
routerBack.mockClear();
|
|
71
|
+
updateCalls.length = 0;
|
|
72
|
+
querySpecs.length = 0;
|
|
73
|
+
mutationSpecs.length = 0;
|
|
74
|
+
apiState.data = null;
|
|
75
|
+
apiState.error = null;
|
|
76
|
+
apiState.isLoading = false;
|
|
77
|
+
apiState.updateImpl = async () => ({});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("wires the injected query and mutation endpoints to the version-config URL", () => {
|
|
81
|
+
renderWithTheme(<AdminVersionConfig api={makeApi() as any} baseUrl="/admin" />);
|
|
82
|
+
expect(querySpecs.length).toBeGreaterThan(0);
|
|
83
|
+
expect(querySpecs[0]).toEqual({method: "GET", url: "/admin/version-config"});
|
|
84
|
+
expect(mutationSpecs.length).toBeGreaterThan(0);
|
|
85
|
+
expect(mutationSpecs[0]).toEqual({
|
|
86
|
+
body: {demo: true},
|
|
87
|
+
method: "PUT",
|
|
88
|
+
url: "/admin/version-config",
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("updates form state when a number field changes via handleFieldChange", async () => {
|
|
93
|
+
apiState.data = {
|
|
94
|
+
mobileRequiredVersion: 1,
|
|
95
|
+
mobileWarningVersion: 2,
|
|
96
|
+
requiredMessage: "R",
|
|
97
|
+
updateUrl: "https://x.com",
|
|
98
|
+
warningMessage: "W",
|
|
99
|
+
webRequiredVersion: 3,
|
|
100
|
+
webWarningVersion: 4,
|
|
101
|
+
};
|
|
102
|
+
const {UNSAFE_root, getByText} = renderWithTheme(
|
|
103
|
+
<AdminVersionConfig api={makeApi() as any} baseUrl="/admin" />
|
|
104
|
+
);
|
|
105
|
+
const numberNodes = UNSAFE_root.findAll((n: any) => typeof n.props?.onChange === "function");
|
|
106
|
+
// Fire onChange on every field with an onChange prop so every setState path runs.
|
|
107
|
+
numberNodes.forEach((n: any, index: number) => {
|
|
108
|
+
try {
|
|
109
|
+
n.props.onChange(String(100 + index));
|
|
110
|
+
} catch {
|
|
111
|
+
// Some nodes expect different argument shapes; ignore those.
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
await act(async () => {
|
|
115
|
+
fireEvent.press(getByText("Save"));
|
|
116
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
117
|
+
});
|
|
118
|
+
expect(updateCalls.length).toBe(1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("renders loading state", () => {
|
|
122
|
+
apiState.isLoading = true;
|
|
123
|
+
const {toJSON} = renderWithTheme(
|
|
124
|
+
<AdminVersionConfig api={makeApi() as any} baseUrl="/admin" />
|
|
125
|
+
);
|
|
126
|
+
expect(toJSON()).toBeDefined();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("renders error state and calls back on press", async () => {
|
|
130
|
+
apiState.error = new Error("boom");
|
|
131
|
+
const {getByText} = renderWithTheme(
|
|
132
|
+
<AdminVersionConfig api={makeApi() as any} baseUrl="/admin" />
|
|
133
|
+
);
|
|
134
|
+
await act(async () => {
|
|
135
|
+
fireEvent.press(getByText("Back"));
|
|
136
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
137
|
+
});
|
|
138
|
+
expect(routerBack).toHaveBeenCalled();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("populates the form with defaults when data is missing", () => {
|
|
142
|
+
apiState.data = null;
|
|
143
|
+
const {toJSON} = renderWithTheme(
|
|
144
|
+
<AdminVersionConfig api={makeApi() as any} baseUrl="/admin" />
|
|
145
|
+
);
|
|
146
|
+
expect(toJSON()).toBeDefined();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("populates form state from backend data and saves", async () => {
|
|
150
|
+
apiState.data = {
|
|
151
|
+
mobileRequiredVersion: 1,
|
|
152
|
+
mobileWarningVersion: 2,
|
|
153
|
+
requiredMessage: "R",
|
|
154
|
+
updateUrl: " https://x.com ",
|
|
155
|
+
warningMessage: "W",
|
|
156
|
+
webRequiredVersion: 3,
|
|
157
|
+
webWarningVersion: 4,
|
|
158
|
+
};
|
|
159
|
+
const {getByText} = renderWithTheme(
|
|
160
|
+
<AdminVersionConfig api={makeApi() as any} baseUrl="/admin" />
|
|
161
|
+
);
|
|
162
|
+
await act(async () => {
|
|
163
|
+
fireEvent.press(getByText("Save"));
|
|
164
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
165
|
+
});
|
|
166
|
+
expect(updateCalls.length).toBe(1);
|
|
167
|
+
expect(updateCalls[0].updateUrl).toBe("https://x.com");
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("handles save failures gracefully", async () => {
|
|
171
|
+
apiState.data = {};
|
|
172
|
+
apiState.updateImpl = async () => {
|
|
173
|
+
throw new Error("server down");
|
|
174
|
+
};
|
|
175
|
+
const {getByText} = renderWithTheme(
|
|
176
|
+
<AdminVersionConfig api={makeApi() as any} baseUrl="/admin" />
|
|
177
|
+
);
|
|
178
|
+
await act(async () => {
|
|
179
|
+
fireEvent.press(getByText("Save"));
|
|
180
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
181
|
+
});
|
|
182
|
+
expect(updateCalls.length).toBe(1);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("sets updateUrl to null when empty", async () => {
|
|
186
|
+
apiState.data = {updateUrl: null};
|
|
187
|
+
const {getByText} = renderWithTheme(
|
|
188
|
+
<AdminVersionConfig api={makeApi() as any} baseUrl="/admin" />
|
|
189
|
+
);
|
|
190
|
+
await act(async () => {
|
|
191
|
+
fireEvent.press(getByText("Save"));
|
|
192
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
193
|
+
});
|
|
194
|
+
expect(updateCalls[0].updateUrl).toBeNull();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {renderWithTheme} from "@terreno/ui/src/test-utils";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
|
|
5
|
+
import {CheckboxListEditor} from "./CheckboxListEditor";
|
|
6
|
+
|
|
7
|
+
const press = async (el: any): Promise<void> => {
|
|
8
|
+
await act(async () => {
|
|
9
|
+
fireEvent.press(el);
|
|
10
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
describe("CheckboxListEditor", () => {
|
|
15
|
+
it("renders title, helper, and checkbox items", () => {
|
|
16
|
+
const {toJSON, getByTestId} = renderWithTheme(
|
|
17
|
+
<CheckboxListEditor
|
|
18
|
+
errorText="err"
|
|
19
|
+
helperText="help"
|
|
20
|
+
onChange={() => {}}
|
|
21
|
+
title="Checkboxes"
|
|
22
|
+
value={[
|
|
23
|
+
{confirmationPrompt: "Confirm?", label: "First", required: true},
|
|
24
|
+
{label: "Second", required: false},
|
|
25
|
+
]}
|
|
26
|
+
/>
|
|
27
|
+
);
|
|
28
|
+
expect(toJSON()).toBeDefined();
|
|
29
|
+
expect(getByTestId("checkbox-add-button")).toBeDefined();
|
|
30
|
+
expect(getByTestId("checkbox-remove-0")).toBeDefined();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("handles non-array values gracefully", () => {
|
|
34
|
+
const {toJSON} = renderWithTheme(
|
|
35
|
+
<CheckboxListEditor onChange={() => {}} value={undefined as any} />
|
|
36
|
+
);
|
|
37
|
+
expect(toJSON()).toBeDefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("adds a new item when Add is clicked", async () => {
|
|
41
|
+
const onChange = mock((_: unknown) => undefined);
|
|
42
|
+
const {getByTestId} = renderWithTheme(<CheckboxListEditor onChange={onChange} value={[]} />);
|
|
43
|
+
await press(getByTestId("checkbox-add-button"));
|
|
44
|
+
expect(onChange).toHaveBeenCalled();
|
|
45
|
+
const next = (onChange.mock.calls[0] as any)[0];
|
|
46
|
+
expect(next).toEqual([{label: "", required: false}]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("removes an item when the remove icon is clicked", async () => {
|
|
50
|
+
const onChange = mock((_: unknown) => undefined);
|
|
51
|
+
const {getByTestId} = renderWithTheme(
|
|
52
|
+
<CheckboxListEditor
|
|
53
|
+
onChange={onChange}
|
|
54
|
+
value={[
|
|
55
|
+
{label: "a", required: false},
|
|
56
|
+
{label: "b", required: false},
|
|
57
|
+
]}
|
|
58
|
+
/>
|
|
59
|
+
);
|
|
60
|
+
await press(getByTestId("checkbox-remove-0"));
|
|
61
|
+
expect(onChange).toHaveBeenCalled();
|
|
62
|
+
const next = (onChange.mock.calls[0] as any)[0];
|
|
63
|
+
expect(next).toHaveLength(1);
|
|
64
|
+
expect(next[0].label).toBe("b");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("updates a field when label/prompt changes", () => {
|
|
68
|
+
const onChange = mock((_: unknown) => undefined);
|
|
69
|
+
const {getByTestId} = renderWithTheme(
|
|
70
|
+
<CheckboxListEditor onChange={onChange} value={[{label: "a", required: false}]} />
|
|
71
|
+
);
|
|
72
|
+
fireEvent.changeText(getByTestId("checkbox-label-0"), "new-label");
|
|
73
|
+
const first = (onChange.mock.calls[0] as any)[0];
|
|
74
|
+
expect(first[0].label).toBe("new-label");
|
|
75
|
+
|
|
76
|
+
fireEvent.changeText(getByTestId("checkbox-prompt-0"), "prompt?");
|
|
77
|
+
const second = (onChange.mock.calls[1] as any)[0];
|
|
78
|
+
expect(second[0].confirmationPrompt).toBe("prompt?");
|
|
79
|
+
});
|
|
80
|
+
});
|