@terreno/admin-frontend 0.10.0 → 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.js.map +1 -1
- package/dist/AdminPrimitiveArrayField.d.ts +26 -0
- package/dist/AdminPrimitiveArrayField.js +55 -0
- package/dist/AdminPrimitiveArrayField.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +7 -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 +0 -2
- package/src/AdminPrimitiveArrayField.test.tsx +186 -0
- package/src/AdminPrimitiveArrayField.tsx +186 -0
- 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 +1 -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 +7 -1
- package/src/AdminScriptRunModal.test.tsx +0 -69
|
@@ -0,0 +1,289 @@
|
|
|
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 {fireEvent} from "../../../ui/node_modules/@testing-library/react-native";
|
|
5
|
+
|
|
6
|
+
mock.module("../AdminNestedArrayField", () => ({
|
|
7
|
+
AdminNestedArrayField: ({title}: {title: string}) =>
|
|
8
|
+
React.createElement("AdminNestedArrayField", {title}),
|
|
9
|
+
}));
|
|
10
|
+
mock.module("../AdminRefField", () => ({
|
|
11
|
+
AdminRefField: ({title}: {title: string}) => React.createElement("AdminRefField", {title}),
|
|
12
|
+
}));
|
|
13
|
+
mock.module("../CheckboxListEditor", () => ({
|
|
14
|
+
CheckboxListEditor: ({title}: {title: string}) =>
|
|
15
|
+
React.createElement("CheckboxListEditor", {title}),
|
|
16
|
+
}));
|
|
17
|
+
mock.module("../LocaleContentEditor", () => ({
|
|
18
|
+
LocaleContentEditor: ({title}: {title: string}) =>
|
|
19
|
+
React.createElement("LocaleContentEditor", {title}),
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
import {AdminFieldRenderer} from "../AdminFieldRenderer";
|
|
23
|
+
|
|
24
|
+
const api = {} as any;
|
|
25
|
+
const base = {api, baseUrl: "/admin", onChange: () => {}};
|
|
26
|
+
|
|
27
|
+
describe("AdminFieldRenderer", () => {
|
|
28
|
+
it.each([
|
|
29
|
+
["boolean default", {required: false, type: "boolean"}, true],
|
|
30
|
+
["number default", {required: false, type: "number"}, 42],
|
|
31
|
+
["string default", {required: false, type: "string"}, "hello"],
|
|
32
|
+
["date (by type)", {required: false, type: "date"}, "2024-01-01"],
|
|
33
|
+
["datetime", {required: false, type: "datetime"}, "2024-01-01T00:00:00Z"],
|
|
34
|
+
["string field with 'date' in name", {required: false, type: "string"}, "2024-01-01"],
|
|
35
|
+
["object (JSON)", {required: false, type: "object"}, {foo: "bar"}],
|
|
36
|
+
["mixed (JSON)", {required: false, type: "mixed"}, [1, 2]],
|
|
37
|
+
[
|
|
38
|
+
"array with items",
|
|
39
|
+
{items: {name: {required: false, type: "string"}}, required: false, type: "array"},
|
|
40
|
+
[],
|
|
41
|
+
],
|
|
42
|
+
["array without items", {required: false, type: "array"}, [1, 2, 3]],
|
|
43
|
+
["array string fallback", {required: false, type: "array"}, "raw-string"],
|
|
44
|
+
[
|
|
45
|
+
"enum",
|
|
46
|
+
{
|
|
47
|
+
enum: ["active", "inactive"],
|
|
48
|
+
required: false,
|
|
49
|
+
type: "string",
|
|
50
|
+
},
|
|
51
|
+
"active",
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
"enum with null value",
|
|
55
|
+
{
|
|
56
|
+
enum: [null, "a"],
|
|
57
|
+
required: false,
|
|
58
|
+
type: "string",
|
|
59
|
+
},
|
|
60
|
+
undefined,
|
|
61
|
+
],
|
|
62
|
+
["widget markdown", {required: false, type: "string", widget: "markdown"}, "# Hi"],
|
|
63
|
+
["widget textarea", {required: false, type: "string", widget: "textarea"}, "some text"],
|
|
64
|
+
["widget checkbox-list", {required: false, type: "array", widget: "checkbox-list"}, []],
|
|
65
|
+
[
|
|
66
|
+
"widget checkbox-list non-array",
|
|
67
|
+
{required: false, type: "string", widget: "checkbox-list"},
|
|
68
|
+
["x"],
|
|
69
|
+
],
|
|
70
|
+
[
|
|
71
|
+
"widget locale-content",
|
|
72
|
+
{required: false, type: "object", widget: "locale-content"},
|
|
73
|
+
{en: "hi"},
|
|
74
|
+
],
|
|
75
|
+
])("renders %s type", (_label, fieldConfig, value) => {
|
|
76
|
+
const fieldKey = _label === "string field with 'date' in name" ? "birthDate" : "field";
|
|
77
|
+
const {toJSON} = renderWithTheme(
|
|
78
|
+
<AdminFieldRenderer
|
|
79
|
+
{...base}
|
|
80
|
+
fieldConfig={fieldConfig as any}
|
|
81
|
+
fieldKey={fieldKey}
|
|
82
|
+
value={value}
|
|
83
|
+
/>
|
|
84
|
+
);
|
|
85
|
+
expect(toJSON()).toBeDefined();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("renders reference field when ref+modelConfigs match", () => {
|
|
89
|
+
const {toJSON} = renderWithTheme(
|
|
90
|
+
<AdminFieldRenderer
|
|
91
|
+
{...base}
|
|
92
|
+
fieldConfig={{ref: "User", required: false, type: "string"}}
|
|
93
|
+
fieldKey="owner"
|
|
94
|
+
modelConfigs={[{name: "User", routePath: "/admin/users"}]}
|
|
95
|
+
value=""
|
|
96
|
+
/>
|
|
97
|
+
);
|
|
98
|
+
expect(toJSON()).toBeDefined();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("falls back to default when ref model is missing", () => {
|
|
102
|
+
const {toJSON} = renderWithTheme(
|
|
103
|
+
<AdminFieldRenderer
|
|
104
|
+
{...base}
|
|
105
|
+
fieldConfig={{ref: "Unknown", required: false, type: "string"}}
|
|
106
|
+
fieldKey="owner"
|
|
107
|
+
modelConfigs={[{name: "User", routePath: "/admin/users"}]}
|
|
108
|
+
value=""
|
|
109
|
+
/>
|
|
110
|
+
);
|
|
111
|
+
expect(toJSON()).toBeDefined();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("renders dynamic enum from a sibling plural array (e.g. variant→variants)", () => {
|
|
115
|
+
const {toJSON} = renderWithTheme(
|
|
116
|
+
<AdminFieldRenderer
|
|
117
|
+
{...base}
|
|
118
|
+
fieldConfig={{required: false, type: "string"}}
|
|
119
|
+
fieldKey="variant"
|
|
120
|
+
parentFormState={{variants: [{key: "a"}, {key: "b"}]}}
|
|
121
|
+
value="a"
|
|
122
|
+
/>
|
|
123
|
+
);
|
|
124
|
+
expect(toJSON()).toBeDefined();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("renders locale-default widget using sibling content map", () => {
|
|
128
|
+
const {toJSON} = renderWithTheme(
|
|
129
|
+
<AdminFieldRenderer
|
|
130
|
+
{...base}
|
|
131
|
+
fieldConfig={{required: false, type: "string", widget: "locale-default"}}
|
|
132
|
+
fieldKey="defaultLocale"
|
|
133
|
+
parentFormState={{content: {en: "hi", es: "hola"}}}
|
|
134
|
+
value="en"
|
|
135
|
+
/>
|
|
136
|
+
);
|
|
137
|
+
expect(toJSON()).toBeDefined();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("renders locale-default as disabled when no locales exist", () => {
|
|
141
|
+
const {toJSON} = renderWithTheme(
|
|
142
|
+
<AdminFieldRenderer
|
|
143
|
+
{...base}
|
|
144
|
+
fieldConfig={{required: false, type: "string", widget: "locale-default"}}
|
|
145
|
+
fieldKey="defaultLocale"
|
|
146
|
+
parentFormState={{}}
|
|
147
|
+
value=""
|
|
148
|
+
/>
|
|
149
|
+
);
|
|
150
|
+
expect(toJSON()).toBeDefined();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("number field onChange converts numeric text to Number, falls back to raw text for NaN", () => {
|
|
154
|
+
const changes: unknown[] = [];
|
|
155
|
+
const {getByTestId} = renderWithTheme(
|
|
156
|
+
<AdminFieldRenderer
|
|
157
|
+
{...base}
|
|
158
|
+
fieldConfig={{required: false, type: "number"}}
|
|
159
|
+
fieldKey="qty"
|
|
160
|
+
onChange={(v) => changes.push(v)}
|
|
161
|
+
value={1}
|
|
162
|
+
/>
|
|
163
|
+
);
|
|
164
|
+
const input = getByTestId("admin-field-qty");
|
|
165
|
+
fireEvent.changeText(input, "42");
|
|
166
|
+
fireEvent.changeText(input, "abc");
|
|
167
|
+
expect(changes).toEqual([42, "abc"]);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("enum onChange maps empty string to undefined", () => {
|
|
171
|
+
const changes: unknown[] = [];
|
|
172
|
+
const {UNSAFE_root} = renderWithTheme(
|
|
173
|
+
<AdminFieldRenderer
|
|
174
|
+
{...base}
|
|
175
|
+
fieldConfig={{enum: [null, "a", "b"], required: false, type: "string"}}
|
|
176
|
+
fieldKey="status"
|
|
177
|
+
onChange={(v) => changes.push(v)}
|
|
178
|
+
value=""
|
|
179
|
+
/>
|
|
180
|
+
);
|
|
181
|
+
// Pull the first element that has an `options` prop (SelectField) and
|
|
182
|
+
// invoke its onChange directly to exercise the empty-string → undefined
|
|
183
|
+
// wrapper.
|
|
184
|
+
const candidates = UNSAFE_root.findAll(
|
|
185
|
+
(n) => n.props && Array.isArray((n.props as {options?: unknown}).options)
|
|
186
|
+
);
|
|
187
|
+
const select = candidates[0];
|
|
188
|
+
(select.props as {onChange?: (v: string) => void}).onChange?.("");
|
|
189
|
+
(select.props as {onChange?: (v: string) => void}).onChange?.("a");
|
|
190
|
+
expect(changes).toEqual([undefined, "a"]);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("object field onChange parses JSON text and falls back to raw string", () => {
|
|
194
|
+
const changes: unknown[] = [];
|
|
195
|
+
const {getByTestId} = renderWithTheme(
|
|
196
|
+
<AdminFieldRenderer
|
|
197
|
+
{...base}
|
|
198
|
+
fieldConfig={{required: false, type: "object"}}
|
|
199
|
+
fieldKey="meta"
|
|
200
|
+
onChange={(v) => changes.push(v)}
|
|
201
|
+
value={{a: 1}}
|
|
202
|
+
/>
|
|
203
|
+
);
|
|
204
|
+
const input = getByTestId("admin-field-meta");
|
|
205
|
+
fireEvent.changeText(input, '{"a":2}');
|
|
206
|
+
fireEvent.changeText(input, "not-json");
|
|
207
|
+
fireEvent.changeText(input, "");
|
|
208
|
+
expect(changes).toEqual([{a: 2}, "not-json", undefined]);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("array without items onChange parses JSON array then falls back to raw text", () => {
|
|
212
|
+
const changes: unknown[] = [];
|
|
213
|
+
const {getByTestId} = renderWithTheme(
|
|
214
|
+
<AdminFieldRenderer
|
|
215
|
+
{...base}
|
|
216
|
+
fieldConfig={{required: false, type: "array"}}
|
|
217
|
+
fieldKey="tags"
|
|
218
|
+
onChange={(v) => changes.push(v)}
|
|
219
|
+
value={[1, 2, 3]}
|
|
220
|
+
/>
|
|
221
|
+
);
|
|
222
|
+
const input = getByTestId("admin-field-tags");
|
|
223
|
+
fireEvent.changeText(input, "[4,5]");
|
|
224
|
+
fireEvent.changeText(input, "oops");
|
|
225
|
+
expect(changes).toEqual([[4, 5], "oops"]);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("renders mixed type with null value (serializes to empty string)", () => {
|
|
229
|
+
const {toJSON} = renderWithTheme(
|
|
230
|
+
<AdminFieldRenderer
|
|
231
|
+
{...base}
|
|
232
|
+
fieldConfig={{required: false, type: "mixed"}}
|
|
233
|
+
fieldKey="anything"
|
|
234
|
+
value={null}
|
|
235
|
+
/>
|
|
236
|
+
);
|
|
237
|
+
expect(toJSON()).toBeDefined();
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("renders mixed type with string value (serializes as-is)", () => {
|
|
241
|
+
const {toJSON} = renderWithTheme(
|
|
242
|
+
<AdminFieldRenderer
|
|
243
|
+
{...base}
|
|
244
|
+
fieldConfig={{required: false, type: "mixed"}}
|
|
245
|
+
fieldKey="anything"
|
|
246
|
+
value="plain string"
|
|
247
|
+
/>
|
|
248
|
+
);
|
|
249
|
+
expect(toJSON()).toBeDefined();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("array without items uses raw string when value is already a string", () => {
|
|
253
|
+
const {toJSON} = renderWithTheme(
|
|
254
|
+
<AdminFieldRenderer
|
|
255
|
+
{...base}
|
|
256
|
+
fieldConfig={{required: false, type: "array"}}
|
|
257
|
+
fieldKey="raw"
|
|
258
|
+
value="[already,string]"
|
|
259
|
+
/>
|
|
260
|
+
);
|
|
261
|
+
expect(toJSON()).toBeDefined();
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("dynamic enum skips rendering when sibling plural exists but has no .key values", () => {
|
|
265
|
+
const {toJSON} = renderWithTheme(
|
|
266
|
+
<AdminFieldRenderer
|
|
267
|
+
{...base}
|
|
268
|
+
fieldConfig={{required: false, type: "string"}}
|
|
269
|
+
fieldKey="variant"
|
|
270
|
+
parentFormState={{variants: [{key: ""}, {key: null}]}}
|
|
271
|
+
value=""
|
|
272
|
+
/>
|
|
273
|
+
);
|
|
274
|
+
expect(toJSON()).toBeDefined();
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("dynamic enum ignores sibling when first item has no .key", () => {
|
|
278
|
+
const {toJSON} = renderWithTheme(
|
|
279
|
+
<AdminFieldRenderer
|
|
280
|
+
{...base}
|
|
281
|
+
fieldConfig={{required: false, type: "string"}}
|
|
282
|
+
fieldKey="variant"
|
|
283
|
+
parentFormState={{variants: [{notKey: "x"}]}}
|
|
284
|
+
value=""
|
|
285
|
+
/>
|
|
286
|
+
);
|
|
287
|
+
expect(toJSON()).toBeDefined();
|
|
288
|
+
});
|
|
289
|
+
});
|
|
@@ -0,0 +1,297 @@
|
|
|
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 {fireEvent} from "../../../ui/node_modules/@testing-library/react-native";
|
|
5
|
+
|
|
6
|
+
mock.module("../AdminFieldRenderer", () => ({
|
|
7
|
+
AdminFieldRenderer: ({fieldKey, onChange}: {fieldKey: string; onChange: (v: any) => void}) =>
|
|
8
|
+
React.createElement("AdminFieldRenderer", {
|
|
9
|
+
fieldKey,
|
|
10
|
+
onChange,
|
|
11
|
+
testID: `admin-field-renderer-${fieldKey}`,
|
|
12
|
+
}),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
mock.module("@terreno/ui", () => {
|
|
16
|
+
const RN = require("react-native");
|
|
17
|
+
const ReactMod = require("react");
|
|
18
|
+
const DraggableList = ({
|
|
19
|
+
dataIDs,
|
|
20
|
+
renderItem,
|
|
21
|
+
callbackNewDataIds,
|
|
22
|
+
}: {
|
|
23
|
+
dataIDs: string[];
|
|
24
|
+
renderItem: (arg: {item: string}) => React.ReactElement;
|
|
25
|
+
callbackNewDataIds: (ids: string[]) => void;
|
|
26
|
+
}) => {
|
|
27
|
+
return ReactMod.createElement(RN.View, {testID: "mock-draggable-list"}, [
|
|
28
|
+
...dataIDs.map((id) => ReactMod.cloneElement(renderItem({item: id}), {key: id})),
|
|
29
|
+
ReactMod.createElement(RN.Pressable, {
|
|
30
|
+
key: "__reorder",
|
|
31
|
+
onPress: () => callbackNewDataIds([...dataIDs].reverse()),
|
|
32
|
+
testID: "mock-reorder",
|
|
33
|
+
}),
|
|
34
|
+
]);
|
|
35
|
+
};
|
|
36
|
+
const Box = ({children, ...rest}: any) => ReactMod.createElement(RN.View, rest, children);
|
|
37
|
+
const Button = ({text, onClick, iconName}: any) =>
|
|
38
|
+
ReactMod.createElement(
|
|
39
|
+
RN.Pressable,
|
|
40
|
+
{onPress: onClick, testID: `btn-${iconName ?? text}`},
|
|
41
|
+
ReactMod.createElement(RN.Text, {}, text)
|
|
42
|
+
);
|
|
43
|
+
const Card = ({children}: any) => ReactMod.createElement(RN.View, {testID: "card"}, children);
|
|
44
|
+
const Heading = ({children}: any) => ReactMod.createElement(RN.Text, {}, children);
|
|
45
|
+
const Text = ({children, ...rest}: any) => ReactMod.createElement(RN.Text, rest, children);
|
|
46
|
+
const IconButton = ({onClick, accessibilityLabel}: any) =>
|
|
47
|
+
ReactMod.createElement(RN.Pressable, {
|
|
48
|
+
onPress: onClick,
|
|
49
|
+
testID: `icon-${accessibilityLabel}`,
|
|
50
|
+
});
|
|
51
|
+
return {Box, Button, Card, DraggableList, Heading, IconButton, Text};
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
import {AdminNestedArrayField} from "../AdminNestedArrayField";
|
|
55
|
+
|
|
56
|
+
const baseItems = {
|
|
57
|
+
active: {default: true, required: false, type: "boolean"},
|
|
58
|
+
count: {required: false, type: "number"},
|
|
59
|
+
name: {required: false, type: "string"},
|
|
60
|
+
tags: {required: false, type: "array"},
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
describe("AdminNestedArrayField", () => {
|
|
64
|
+
it("renders empty state when no items", () => {
|
|
65
|
+
const {toJSON, getByText} = renderWithTheme(
|
|
66
|
+
<AdminNestedArrayField
|
|
67
|
+
api={{} as any}
|
|
68
|
+
baseUrl="/admin"
|
|
69
|
+
helperText="help"
|
|
70
|
+
items={baseItems as any}
|
|
71
|
+
onChange={() => {}}
|
|
72
|
+
title="Items"
|
|
73
|
+
value={[]}
|
|
74
|
+
/>
|
|
75
|
+
);
|
|
76
|
+
expect(toJSON()).toBeDefined();
|
|
77
|
+
expect(getByText(/No items/)).toBeDefined();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("renders items with error text", () => {
|
|
81
|
+
const {toJSON} = renderWithTheme(
|
|
82
|
+
<AdminNestedArrayField
|
|
83
|
+
api={{} as any}
|
|
84
|
+
baseUrl="/admin"
|
|
85
|
+
errorText="oops"
|
|
86
|
+
items={baseItems as any}
|
|
87
|
+
onChange={() => {}}
|
|
88
|
+
title="Items"
|
|
89
|
+
value={[
|
|
90
|
+
{active: true, count: 1, name: "a", tags: []},
|
|
91
|
+
{active: false, count: 2, name: "b", tags: []},
|
|
92
|
+
]}
|
|
93
|
+
/>
|
|
94
|
+
);
|
|
95
|
+
expect(toJSON()).toBeDefined();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("handles non-array incoming value gracefully", () => {
|
|
99
|
+
const {toJSON} = renderWithTheme(
|
|
100
|
+
<AdminNestedArrayField
|
|
101
|
+
api={{} as any}
|
|
102
|
+
baseUrl="/admin"
|
|
103
|
+
items={baseItems as any}
|
|
104
|
+
onChange={() => {}}
|
|
105
|
+
title="Items"
|
|
106
|
+
value={null as any}
|
|
107
|
+
/>
|
|
108
|
+
);
|
|
109
|
+
expect(toJSON()).toBeDefined();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("adds a new item using buildDefaultItem for all supported types", () => {
|
|
113
|
+
const onChange = mock(() => {});
|
|
114
|
+
const {getByTestId} = renderWithTheme(
|
|
115
|
+
<AdminNestedArrayField
|
|
116
|
+
api={{} as any}
|
|
117
|
+
baseUrl="/admin"
|
|
118
|
+
items={baseItems as any}
|
|
119
|
+
onChange={onChange}
|
|
120
|
+
title="Items"
|
|
121
|
+
value={[]}
|
|
122
|
+
/>
|
|
123
|
+
);
|
|
124
|
+
fireEvent.press(getByTestId("btn-plus"));
|
|
125
|
+
expect(onChange).toHaveBeenCalled();
|
|
126
|
+
const added = onChange.mock.calls[0][0];
|
|
127
|
+
expect(added.length).toBe(1);
|
|
128
|
+
expect(added[0].active).toBe(true);
|
|
129
|
+
expect(added[0].count).toBe(0);
|
|
130
|
+
expect(added[0].name).toBe("");
|
|
131
|
+
expect(Array.isArray(added[0].tags)).toBe(true);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("builds default item for string type without default value", () => {
|
|
135
|
+
const onChange = mock(() => {});
|
|
136
|
+
const {getByTestId} = renderWithTheme(
|
|
137
|
+
<AdminNestedArrayField
|
|
138
|
+
api={{} as any}
|
|
139
|
+
baseUrl="/admin"
|
|
140
|
+
items={{description: {required: false, type: "string"}} as any}
|
|
141
|
+
onChange={onChange}
|
|
142
|
+
title="Items"
|
|
143
|
+
value={[]}
|
|
144
|
+
/>
|
|
145
|
+
);
|
|
146
|
+
fireEvent.press(getByTestId("btn-plus"));
|
|
147
|
+
expect(onChange.mock.calls[0][0][0].description).toBe("");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("builds default item for boolean/number/array types without default values", () => {
|
|
151
|
+
const onChange = mock(() => {});
|
|
152
|
+
const items = {
|
|
153
|
+
enabled: {required: false, type: "boolean"},
|
|
154
|
+
qty: {required: false, type: "number"},
|
|
155
|
+
tags: {required: false, type: "array"},
|
|
156
|
+
};
|
|
157
|
+
const {getByTestId} = renderWithTheme(
|
|
158
|
+
<AdminNestedArrayField
|
|
159
|
+
api={{} as any}
|
|
160
|
+
baseUrl="/admin"
|
|
161
|
+
items={items as any}
|
|
162
|
+
onChange={onChange}
|
|
163
|
+
title="Items"
|
|
164
|
+
value={[]}
|
|
165
|
+
/>
|
|
166
|
+
);
|
|
167
|
+
fireEvent.press(getByTestId("btn-plus"));
|
|
168
|
+
const added = onChange.mock.calls[0][0][0];
|
|
169
|
+
expect(added.enabled).toBe(false);
|
|
170
|
+
expect(added.qty).toBe(0);
|
|
171
|
+
expect(Array.isArray(added.tags)).toBe(true);
|
|
172
|
+
expect(added.tags.length).toBe(0);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("removes an item via IconButton", () => {
|
|
176
|
+
const onChange = mock(() => {});
|
|
177
|
+
const {getAllByTestId} = renderWithTheme(
|
|
178
|
+
<AdminNestedArrayField
|
|
179
|
+
api={{} as any}
|
|
180
|
+
baseUrl="/admin"
|
|
181
|
+
items={baseItems as any}
|
|
182
|
+
onChange={onChange}
|
|
183
|
+
title="Items"
|
|
184
|
+
value={[
|
|
185
|
+
{active: true, count: 1, name: "a", tags: []},
|
|
186
|
+
{active: false, count: 2, name: "b", tags: []},
|
|
187
|
+
]}
|
|
188
|
+
/>
|
|
189
|
+
);
|
|
190
|
+
const removeButtons = getAllByTestId("icon-Remove item");
|
|
191
|
+
fireEvent.press(removeButtons[0]);
|
|
192
|
+
expect(onChange).toHaveBeenCalled();
|
|
193
|
+
const next = onChange.mock.calls[0][0];
|
|
194
|
+
expect(next.length).toBe(1);
|
|
195
|
+
expect(next[0].name).toBe("b");
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("updates a sub-field value via AdminFieldRenderer onChange", () => {
|
|
199
|
+
const onChange = mock(() => {});
|
|
200
|
+
const {getAllByTestId} = renderWithTheme(
|
|
201
|
+
<AdminNestedArrayField
|
|
202
|
+
api={{} as any}
|
|
203
|
+
baseUrl="/admin"
|
|
204
|
+
items={baseItems as any}
|
|
205
|
+
onChange={onChange}
|
|
206
|
+
title="Items"
|
|
207
|
+
value={[
|
|
208
|
+
{active: true, count: 1, name: "a", tags: []},
|
|
209
|
+
{active: false, count: 2, name: "b", tags: []},
|
|
210
|
+
]}
|
|
211
|
+
/>
|
|
212
|
+
);
|
|
213
|
+
const nameFields = getAllByTestId("admin-field-renderer-name");
|
|
214
|
+
(nameFields[1] as any).props.onChange("new name");
|
|
215
|
+
expect(onChange).toHaveBeenCalled();
|
|
216
|
+
const next = onChange.mock.calls[0][0];
|
|
217
|
+
expect(next[1].name).toBe("new name");
|
|
218
|
+
expect(next[0].name).toBe("a");
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("reorders items via DraggableList callback", () => {
|
|
222
|
+
const onChange = mock(() => {});
|
|
223
|
+
const {getByTestId} = renderWithTheme(
|
|
224
|
+
<AdminNestedArrayField
|
|
225
|
+
api={{} as any}
|
|
226
|
+
baseUrl="/admin"
|
|
227
|
+
items={baseItems as any}
|
|
228
|
+
onChange={onChange}
|
|
229
|
+
title="Items"
|
|
230
|
+
value={[
|
|
231
|
+
{active: true, count: 1, name: "a", tags: []},
|
|
232
|
+
{active: false, count: 2, name: "b", tags: []},
|
|
233
|
+
{active: true, count: 3, name: "c", tags: []},
|
|
234
|
+
]}
|
|
235
|
+
/>
|
|
236
|
+
);
|
|
237
|
+
fireEvent.press(getByTestId("mock-reorder"));
|
|
238
|
+
expect(onChange).toHaveBeenCalled();
|
|
239
|
+
const next = onChange.mock.calls[0][0];
|
|
240
|
+
expect(next.map((i: any) => i.name)).toEqual(["c", "b", "a"]);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("supports re-render with longer value array (grows itemIds)", () => {
|
|
244
|
+
const onChange = mock(() => {});
|
|
245
|
+
const {rerender, toJSON} = renderWithTheme(
|
|
246
|
+
<AdminNestedArrayField
|
|
247
|
+
api={{} as any}
|
|
248
|
+
baseUrl="/admin"
|
|
249
|
+
items={baseItems as any}
|
|
250
|
+
onChange={onChange}
|
|
251
|
+
title="Items"
|
|
252
|
+
value={[{active: true, count: 1, name: "a", tags: []}]}
|
|
253
|
+
/>
|
|
254
|
+
);
|
|
255
|
+
rerender(
|
|
256
|
+
<AdminNestedArrayField
|
|
257
|
+
api={{} as any}
|
|
258
|
+
baseUrl="/admin"
|
|
259
|
+
items={baseItems as any}
|
|
260
|
+
onChange={onChange}
|
|
261
|
+
title="Items"
|
|
262
|
+
value={[
|
|
263
|
+
{active: true, count: 1, name: "a", tags: []},
|
|
264
|
+
{active: false, count: 2, name: "b", tags: []},
|
|
265
|
+
]}
|
|
266
|
+
/>
|
|
267
|
+
);
|
|
268
|
+
expect(toJSON()).toBeDefined();
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("supports re-render with shorter value array (shrinks itemIds)", () => {
|
|
272
|
+
const {rerender, toJSON} = renderWithTheme(
|
|
273
|
+
<AdminNestedArrayField
|
|
274
|
+
api={{} as any}
|
|
275
|
+
baseUrl="/admin"
|
|
276
|
+
items={baseItems as any}
|
|
277
|
+
onChange={() => {}}
|
|
278
|
+
title="Items"
|
|
279
|
+
value={[
|
|
280
|
+
{active: true, count: 1, name: "a", tags: []},
|
|
281
|
+
{active: false, count: 2, name: "b", tags: []},
|
|
282
|
+
]}
|
|
283
|
+
/>
|
|
284
|
+
);
|
|
285
|
+
rerender(
|
|
286
|
+
<AdminNestedArrayField
|
|
287
|
+
api={{} as any}
|
|
288
|
+
baseUrl="/admin"
|
|
289
|
+
items={baseItems as any}
|
|
290
|
+
onChange={() => {}}
|
|
291
|
+
title="Items"
|
|
292
|
+
value={[{active: true, count: 1, name: "a", tags: []}]}
|
|
293
|
+
/>
|
|
294
|
+
);
|
|
295
|
+
expect(toJSON()).toBeDefined();
|
|
296
|
+
});
|
|
297
|
+
});
|