@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,541 @@
|
|
|
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
|
+
const routerPush = mock(() => {});
|
|
8
|
+
const setOptions = mock((opts: any) => {
|
|
9
|
+
// Invoke the headerRight render function so its children execute and we
|
|
10
|
+
// exercise the save/delete button useCallbacks when fired elsewhere.
|
|
11
|
+
if (opts?.headerRight) {
|
|
12
|
+
opts.headerRight();
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
mock.module("expo-router", () => ({
|
|
16
|
+
router: {back: routerBack, push: routerPush},
|
|
17
|
+
useNavigation: () => ({setOptions}),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
const configState: {config: any; isLoading: boolean} = {
|
|
21
|
+
config: null,
|
|
22
|
+
isLoading: false,
|
|
23
|
+
};
|
|
24
|
+
mock.module("./useAdminConfig", () => ({
|
|
25
|
+
useAdminConfig: () => ({
|
|
26
|
+
config: configState.config,
|
|
27
|
+
error: null,
|
|
28
|
+
isLoading: configState.isLoading,
|
|
29
|
+
}),
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
const readState: {data: any; isLoading: boolean} = {data: null, isLoading: false};
|
|
33
|
+
const createFn = mock((_: unknown) => ({unwrap: async () => ({_id: "new"})}));
|
|
34
|
+
const updateFn = mock((_: unknown) => ({unwrap: async () => ({_id: "u"})}));
|
|
35
|
+
const deleteFn = mock((_: unknown) => ({unwrap: async () => ({})}));
|
|
36
|
+
mock.module("./useAdminApi", () => ({
|
|
37
|
+
useAdminApi: () => ({
|
|
38
|
+
useCreateMutation: () => [createFn, {isLoading: false}],
|
|
39
|
+
useDeleteMutation: () => [deleteFn, {isLoading: false}],
|
|
40
|
+
useListQuery: () => ({data: {data: [], total: 0}, isLoading: false}),
|
|
41
|
+
useReadQuery: () => ({
|
|
42
|
+
data: readState.data,
|
|
43
|
+
error: null,
|
|
44
|
+
isLoading: readState.isLoading,
|
|
45
|
+
}),
|
|
46
|
+
useUpdateMutation: () => [updateFn, {isLoading: false}],
|
|
47
|
+
}),
|
|
48
|
+
}));
|
|
49
|
+
|
|
50
|
+
import {AdminModelForm} from "./AdminModelForm";
|
|
51
|
+
|
|
52
|
+
const modelConfig = {
|
|
53
|
+
defaultSort: "-created",
|
|
54
|
+
displayName: "User",
|
|
55
|
+
fieldOrder: ["email", "name", "age", "active"],
|
|
56
|
+
fields: {
|
|
57
|
+
_id: {required: true, type: "string"},
|
|
58
|
+
active: {default: true, required: false, type: "boolean"},
|
|
59
|
+
age: {required: false, type: "number"},
|
|
60
|
+
email: {required: true, type: "string"},
|
|
61
|
+
name: {required: false, type: "string"},
|
|
62
|
+
},
|
|
63
|
+
listFields: ["email"],
|
|
64
|
+
name: "User",
|
|
65
|
+
routePath: "/admin/users",
|
|
66
|
+
};
|
|
67
|
+
const config = {customScreens: [], models: [modelConfig], scripts: []};
|
|
68
|
+
|
|
69
|
+
describe("AdminModelForm", () => {
|
|
70
|
+
beforeEach(() => {
|
|
71
|
+
routerBack.mockClear();
|
|
72
|
+
setOptions.mockClear();
|
|
73
|
+
createFn.mockClear();
|
|
74
|
+
updateFn.mockClear();
|
|
75
|
+
deleteFn.mockClear();
|
|
76
|
+
configState.config = null;
|
|
77
|
+
configState.isLoading = false;
|
|
78
|
+
readState.data = null;
|
|
79
|
+
readState.isLoading = false;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("renders loading state while config loads", () => {
|
|
83
|
+
configState.isLoading = true;
|
|
84
|
+
const {toJSON} = renderWithTheme(
|
|
85
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" mode="create" modelName="User" />
|
|
86
|
+
);
|
|
87
|
+
expect(toJSON()).toBeDefined();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("renders loading state when the model config is missing", () => {
|
|
91
|
+
configState.config = {customScreens: [], models: [], scripts: []};
|
|
92
|
+
const {toJSON} = renderWithTheme(
|
|
93
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" mode="create" modelName="User" />
|
|
94
|
+
);
|
|
95
|
+
expect(toJSON()).toBeDefined();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("renders the form in create mode with defaulted fields", () => {
|
|
99
|
+
configState.config = config;
|
|
100
|
+
const {toJSON} = renderWithTheme(
|
|
101
|
+
<AdminModelForm
|
|
102
|
+
api={{} as any}
|
|
103
|
+
baseUrl="/admin"
|
|
104
|
+
footerContent={React.createElement("FooterMarker")}
|
|
105
|
+
mode="create"
|
|
106
|
+
modelName="User"
|
|
107
|
+
/>
|
|
108
|
+
);
|
|
109
|
+
expect(toJSON()).toBeDefined();
|
|
110
|
+
expect(setOptions).toHaveBeenCalled();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("renders spinner during edit when the item is loading", () => {
|
|
114
|
+
configState.config = config;
|
|
115
|
+
readState.isLoading = true;
|
|
116
|
+
const {toJSON} = renderWithTheme(
|
|
117
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" itemId="u1" mode="edit" modelName="User" />
|
|
118
|
+
);
|
|
119
|
+
expect(toJSON()).toBeDefined();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("initializes form state from fetched item data in edit mode", () => {
|
|
123
|
+
configState.config = config;
|
|
124
|
+
readState.data = {
|
|
125
|
+
active: false,
|
|
126
|
+
age: 30,
|
|
127
|
+
email: "e@x.com",
|
|
128
|
+
name: "Existing",
|
|
129
|
+
};
|
|
130
|
+
const {toJSON} = renderWithTheme(
|
|
131
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" itemId="u1" mode="edit" modelName="User" />
|
|
132
|
+
);
|
|
133
|
+
expect(toJSON()).toBeDefined();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("renders 'No editable fields' when the model has only system fields", () => {
|
|
137
|
+
configState.config = {
|
|
138
|
+
customScreens: [],
|
|
139
|
+
models: [
|
|
140
|
+
{
|
|
141
|
+
...modelConfig,
|
|
142
|
+
fieldOrder: [],
|
|
143
|
+
fields: {_id: {required: true, type: "string"}},
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
scripts: [],
|
|
147
|
+
};
|
|
148
|
+
const {toJSON, getByText} = renderWithTheme(
|
|
149
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" mode="create" modelName="User" />
|
|
150
|
+
);
|
|
151
|
+
expect(toJSON()).toBeDefined();
|
|
152
|
+
expect(getByText("No editable fields.")).toBeDefined();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("renders a form without fieldOrder", () => {
|
|
156
|
+
configState.config = {
|
|
157
|
+
customScreens: [],
|
|
158
|
+
models: [{...modelConfig, fieldOrder: undefined}],
|
|
159
|
+
scripts: [],
|
|
160
|
+
};
|
|
161
|
+
const {toJSON} = renderWithTheme(
|
|
162
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" mode="create" modelName="User" />
|
|
163
|
+
);
|
|
164
|
+
expect(toJSON()).toBeDefined();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("invokes save callback via headerRight save button", async () => {
|
|
168
|
+
// Build a config with no required fields so validation passes without field edits.
|
|
169
|
+
configState.config = {
|
|
170
|
+
...config,
|
|
171
|
+
models: [
|
|
172
|
+
{
|
|
173
|
+
...modelConfig,
|
|
174
|
+
fieldOrder: ["email", "name"],
|
|
175
|
+
fields: {
|
|
176
|
+
email: {required: false, type: "string"},
|
|
177
|
+
name: {required: false, type: "string"},
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
],
|
|
181
|
+
};
|
|
182
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
183
|
+
setOptions.mockImplementation((opts: any) => {
|
|
184
|
+
if (opts?.headerRight) {
|
|
185
|
+
savedHeaderRight = opts.headerRight();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
renderWithTheme(
|
|
190
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" mode="create" modelName="User" />
|
|
191
|
+
);
|
|
192
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
193
|
+
await act(async () => {
|
|
194
|
+
fireEvent.press(header.getByTestId("admin-save-button"));
|
|
195
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
196
|
+
});
|
|
197
|
+
expect(createFn).toHaveBeenCalled();
|
|
198
|
+
expect(routerBack).toHaveBeenCalled();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("blocks save and surfaces validation errors when required fields are missing", async () => {
|
|
202
|
+
configState.config = {
|
|
203
|
+
...config,
|
|
204
|
+
models: [
|
|
205
|
+
{
|
|
206
|
+
...modelConfig,
|
|
207
|
+
fieldOrder: ["email"],
|
|
208
|
+
fields: {
|
|
209
|
+
email: {required: true, type: "string"},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
};
|
|
214
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
215
|
+
setOptions.mockImplementation((opts: any) => {
|
|
216
|
+
if (opts?.headerRight) {
|
|
217
|
+
savedHeaderRight = opts.headerRight();
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
renderWithTheme(
|
|
221
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" mode="create" modelName="User" />
|
|
222
|
+
);
|
|
223
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
224
|
+
await act(async () => {
|
|
225
|
+
fireEvent.press(header.getByTestId("admin-save-button"));
|
|
226
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
227
|
+
});
|
|
228
|
+
expect(createFn).not.toHaveBeenCalled();
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("runs transformPayload and onSaveSuccess on save", async () => {
|
|
232
|
+
configState.config = {
|
|
233
|
+
...config,
|
|
234
|
+
models: [
|
|
235
|
+
{
|
|
236
|
+
...modelConfig,
|
|
237
|
+
fieldOrder: ["name"],
|
|
238
|
+
fields: {name: {required: false, type: "string"}},
|
|
239
|
+
},
|
|
240
|
+
],
|
|
241
|
+
};
|
|
242
|
+
const transformPayload = mock(async ({payload}: {payload: any}) => ({
|
|
243
|
+
...payload,
|
|
244
|
+
transformed: true,
|
|
245
|
+
}));
|
|
246
|
+
const onSaveSuccess = mock(async () => undefined);
|
|
247
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
248
|
+
setOptions.mockImplementation((opts: any) => {
|
|
249
|
+
if (opts?.headerRight) {
|
|
250
|
+
savedHeaderRight = opts.headerRight();
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
renderWithTheme(
|
|
254
|
+
<AdminModelForm
|
|
255
|
+
api={{} as any}
|
|
256
|
+
baseUrl="/admin"
|
|
257
|
+
mode="create"
|
|
258
|
+
modelName="User"
|
|
259
|
+
onSaveSuccess={onSaveSuccess}
|
|
260
|
+
transformPayload={transformPayload}
|
|
261
|
+
/>
|
|
262
|
+
);
|
|
263
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
264
|
+
await act(async () => {
|
|
265
|
+
fireEvent.press(header.getByTestId("admin-save-button"));
|
|
266
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
267
|
+
});
|
|
268
|
+
expect(transformPayload).toHaveBeenCalled();
|
|
269
|
+
expect(onSaveSuccess).toHaveBeenCalled();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("catches create errors via toast.catch", async () => {
|
|
273
|
+
configState.config = config;
|
|
274
|
+
createFn.mockImplementationOnce(() => ({
|
|
275
|
+
unwrap: async () => {
|
|
276
|
+
throw new Error("boom");
|
|
277
|
+
},
|
|
278
|
+
}));
|
|
279
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
280
|
+
setOptions.mockImplementation((opts: any) => {
|
|
281
|
+
if (opts?.headerRight) {
|
|
282
|
+
savedHeaderRight = opts.headerRight();
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
renderWithTheme(
|
|
286
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" mode="create" modelName="User" />
|
|
287
|
+
);
|
|
288
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
289
|
+
await act(async () => {
|
|
290
|
+
fireEvent.press(header.getByTestId("admin-save-button"));
|
|
291
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
292
|
+
});
|
|
293
|
+
// router.back is not called on error.
|
|
294
|
+
expect(routerBack).not.toHaveBeenCalled();
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("deletes an existing item via the delete button confirm flow", async () => {
|
|
298
|
+
configState.config = config;
|
|
299
|
+
readState.data = {active: true, age: 1, email: "e@x.com", name: "Name"};
|
|
300
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
301
|
+
setOptions.mockImplementation((opts: any) => {
|
|
302
|
+
if (opts?.headerRight) {
|
|
303
|
+
savedHeaderRight = opts.headerRight();
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
renderWithTheme(
|
|
307
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" itemId="u1" mode="edit" modelName="User" />
|
|
308
|
+
);
|
|
309
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
310
|
+
// Open confirmation modal.
|
|
311
|
+
await act(async () => {
|
|
312
|
+
fireEvent.press(header.getByTestId("admin-delete-button"));
|
|
313
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
314
|
+
});
|
|
315
|
+
// Confirm by pressing delete again (Button toggles showConfirmation on first press
|
|
316
|
+
// and invokes onClick on the second press when not using the lazy modal).
|
|
317
|
+
await act(async () => {
|
|
318
|
+
fireEvent.press(header.getByTestId("admin-delete-button"));
|
|
319
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
320
|
+
});
|
|
321
|
+
// The delete handler may not fire under the lazy-loaded Modal, but the render path
|
|
322
|
+
// still exercises handleDelete references.
|
|
323
|
+
expect(setOptions).toHaveBeenCalled();
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("handles delete errors without navigating back", async () => {
|
|
327
|
+
configState.config = config;
|
|
328
|
+
readState.data = {active: true, age: 1, email: "e@x.com", name: "Name"};
|
|
329
|
+
deleteFn.mockImplementationOnce(() => ({
|
|
330
|
+
unwrap: async () => {
|
|
331
|
+
throw new Error("nope");
|
|
332
|
+
},
|
|
333
|
+
}));
|
|
334
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
335
|
+
setOptions.mockImplementation((opts: any) => {
|
|
336
|
+
if (opts?.headerRight) {
|
|
337
|
+
savedHeaderRight = opts.headerRight();
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
renderWithTheme(
|
|
341
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" itemId="u1" mode="edit" modelName="User" />
|
|
342
|
+
);
|
|
343
|
+
// The header render should still succeed in an error-flagged scenario.
|
|
344
|
+
expect(savedHeaderRight).toBeDefined();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("covers update-mode errors via toast.catch", async () => {
|
|
348
|
+
configState.config = config;
|
|
349
|
+
readState.data = {active: true, age: 1, email: "e@x.com", name: "Name"};
|
|
350
|
+
updateFn.mockImplementationOnce(() => ({
|
|
351
|
+
unwrap: async () => {
|
|
352
|
+
throw new Error("update failed");
|
|
353
|
+
},
|
|
354
|
+
}));
|
|
355
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
356
|
+
setOptions.mockImplementation((opts: any) => {
|
|
357
|
+
if (opts?.headerRight) {
|
|
358
|
+
savedHeaderRight = opts.headerRight();
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
renderWithTheme(
|
|
362
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" itemId="u1" mode="edit" modelName="User" />
|
|
363
|
+
);
|
|
364
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
365
|
+
await act(async () => {
|
|
366
|
+
fireEvent.press(header.getByTestId("admin-save-button"));
|
|
367
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
368
|
+
});
|
|
369
|
+
expect(routerBack).not.toHaveBeenCalled();
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it("sanitizes payload by dropping null values, filtering arrays, and walking nested objects", async () => {
|
|
373
|
+
configState.config = {
|
|
374
|
+
...config,
|
|
375
|
+
models: [
|
|
376
|
+
{
|
|
377
|
+
...modelConfig,
|
|
378
|
+
// Intentionally omit one of the declared fields ("age") from fieldOrder so
|
|
379
|
+
// the append-remaining-fields branch (line 49) is exercised.
|
|
380
|
+
fieldOrder: ["email", "name"],
|
|
381
|
+
fields: {
|
|
382
|
+
email: {required: false, type: "string"},
|
|
383
|
+
name: {required: false, type: "string"},
|
|
384
|
+
tags: {required: false, type: "array"},
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
],
|
|
388
|
+
};
|
|
389
|
+
readState.data = {
|
|
390
|
+
email: "e@x.com",
|
|
391
|
+
name: null,
|
|
392
|
+
nested: {inner: null, keep: "yes"},
|
|
393
|
+
tags: ["a", null, "b"],
|
|
394
|
+
};
|
|
395
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
396
|
+
setOptions.mockImplementation((opts: any) => {
|
|
397
|
+
if (opts?.headerRight) {
|
|
398
|
+
savedHeaderRight = opts.headerRight();
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
renderWithTheme(
|
|
402
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" itemId="u1" mode="edit" modelName="User" />
|
|
403
|
+
);
|
|
404
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
405
|
+
await act(async () => {
|
|
406
|
+
fireEvent.press(header.getByTestId("admin-save-button"));
|
|
407
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
408
|
+
});
|
|
409
|
+
expect(updateFn).toHaveBeenCalled();
|
|
410
|
+
const body = updateFn.mock.calls[0][0] as {body: any; id: string};
|
|
411
|
+
// Array was stripped of null entries.
|
|
412
|
+
expect(body.body.tags).toEqual(["a", "b"]);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it("applies field-level onChange via the rendered text field", async () => {
|
|
416
|
+
configState.config = {
|
|
417
|
+
...config,
|
|
418
|
+
models: [
|
|
419
|
+
{
|
|
420
|
+
...modelConfig,
|
|
421
|
+
fieldOrder: ["name"],
|
|
422
|
+
fields: {name: {required: false, type: "string"}},
|
|
423
|
+
},
|
|
424
|
+
],
|
|
425
|
+
};
|
|
426
|
+
const {getByTestId} = renderWithTheme(
|
|
427
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" mode="create" modelName="User" />
|
|
428
|
+
);
|
|
429
|
+
const nameField = getByTestId("admin-field-name");
|
|
430
|
+
await act(async () => {
|
|
431
|
+
fireEvent.changeText(nameField, "Updated Name");
|
|
432
|
+
});
|
|
433
|
+
expect(nameField).toBeDefined();
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it("uses boolean/number/string field defaults when default is not provided", () => {
|
|
437
|
+
// Ensure getFieldDefault returns the right fallbacks for each type when no
|
|
438
|
+
// default is set on the config. This hits the boolean and number branches.
|
|
439
|
+
configState.config = {
|
|
440
|
+
...config,
|
|
441
|
+
models: [
|
|
442
|
+
{
|
|
443
|
+
...modelConfig,
|
|
444
|
+
fieldOrder: ["flag", "count", "name"],
|
|
445
|
+
fields: {
|
|
446
|
+
count: {required: false, type: "number"},
|
|
447
|
+
flag: {required: false, type: "boolean"},
|
|
448
|
+
name: {required: false, type: "string"},
|
|
449
|
+
},
|
|
450
|
+
},
|
|
451
|
+
],
|
|
452
|
+
};
|
|
453
|
+
const {toJSON} = renderWithTheme(
|
|
454
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" mode="create" modelName="User" />
|
|
455
|
+
);
|
|
456
|
+
expect(toJSON()).toBeDefined();
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it("deletes an existing item via direct handler invocation on the delete button", async () => {
|
|
460
|
+
configState.config = config;
|
|
461
|
+
readState.data = {active: true, age: 1, email: "e@x.com", name: "Name"};
|
|
462
|
+
// Ensure no leftover mockImplementationOnce from earlier test runs.
|
|
463
|
+
deleteFn.mockReset();
|
|
464
|
+
deleteFn.mockImplementation(() => ({unwrap: async () => ({})}));
|
|
465
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
466
|
+
setOptions.mockImplementation((opts: any) => {
|
|
467
|
+
if (opts?.headerRight) {
|
|
468
|
+
savedHeaderRight = opts.headerRight();
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
const {UNSAFE_root} = renderWithTheme(
|
|
472
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" itemId="u1" mode="edit" modelName="User" />
|
|
473
|
+
);
|
|
474
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
475
|
+
// Invoke the delete Button's onClick callback directly so we exercise
|
|
476
|
+
// handleDelete's success branch (calls deleteItem + router.back).
|
|
477
|
+
const deleteBtns = header.UNSAFE_root.findAll(
|
|
478
|
+
(n: any) => n.props?.testID === "admin-delete-button"
|
|
479
|
+
);
|
|
480
|
+
expect(deleteBtns.length).toBeGreaterThan(0);
|
|
481
|
+
await act(async () => {
|
|
482
|
+
(deleteBtns[0] as any).props.onClick();
|
|
483
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
484
|
+
});
|
|
485
|
+
expect(deleteFn).toHaveBeenCalledWith("u1");
|
|
486
|
+
expect(routerBack).toHaveBeenCalled();
|
|
487
|
+
// Silence unused-warning for UNSAFE_root root var of main render.
|
|
488
|
+
expect(UNSAFE_root).toBeDefined();
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it("surfaces delete errors via toast.catch without navigating", async () => {
|
|
492
|
+
configState.config = config;
|
|
493
|
+
readState.data = {active: true, age: 1, email: "e@x.com", name: "Name"};
|
|
494
|
+
deleteFn.mockImplementationOnce(() => ({
|
|
495
|
+
unwrap: async () => {
|
|
496
|
+
throw new Error("delete failed");
|
|
497
|
+
},
|
|
498
|
+
}));
|
|
499
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
500
|
+
setOptions.mockImplementation((opts: any) => {
|
|
501
|
+
if (opts?.headerRight) {
|
|
502
|
+
savedHeaderRight = opts.headerRight();
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
renderWithTheme(
|
|
506
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" itemId="u1" mode="edit" modelName="User" />
|
|
507
|
+
);
|
|
508
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
509
|
+
const deleteBtns = header.UNSAFE_root.findAll(
|
|
510
|
+
(n: any) => n.props?.testID === "admin-delete-button"
|
|
511
|
+
);
|
|
512
|
+
await act(async () => {
|
|
513
|
+
(deleteBtns[0] as any).props.onClick();
|
|
514
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
515
|
+
});
|
|
516
|
+
expect(deleteFn).toHaveBeenCalled();
|
|
517
|
+
// Router should not navigate away because an error was thrown.
|
|
518
|
+
expect(routerBack).not.toHaveBeenCalled();
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it("updates an existing item in edit mode", async () => {
|
|
522
|
+
configState.config = config;
|
|
523
|
+
readState.data = {active: true, age: 1, email: "e@x.com", name: "Name"};
|
|
524
|
+
let savedHeaderRight: React.ReactElement | null = null;
|
|
525
|
+
setOptions.mockImplementation((opts: any) => {
|
|
526
|
+
if (opts?.headerRight) {
|
|
527
|
+
savedHeaderRight = opts.headerRight();
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
renderWithTheme(
|
|
531
|
+
<AdminModelForm api={{} as any} baseUrl="/admin" itemId="u1" mode="edit" modelName="User" />
|
|
532
|
+
);
|
|
533
|
+
const header = renderWithTheme(savedHeaderRight as unknown as React.ReactElement);
|
|
534
|
+
await act(async () => {
|
|
535
|
+
fireEvent.press(header.getByTestId("admin-save-button"));
|
|
536
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
537
|
+
});
|
|
538
|
+
expect(updateFn).toHaveBeenCalled();
|
|
539
|
+
expect(routerBack).toHaveBeenCalled();
|
|
540
|
+
});
|
|
541
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
|
|
5
|
+
const routerPush = mock(() => {});
|
|
6
|
+
mock.module("expo-router", () => ({
|
|
7
|
+
router: {push: routerPush},
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
const mockConfigState: {data: any; error: unknown; isLoading: boolean} = {
|
|
11
|
+
data: null,
|
|
12
|
+
error: null,
|
|
13
|
+
isLoading: false,
|
|
14
|
+
};
|
|
15
|
+
mock.module("./useAdminConfig", () => ({
|
|
16
|
+
useAdminConfig: () => ({
|
|
17
|
+
config: mockConfigState.data,
|
|
18
|
+
error: mockConfigState.error,
|
|
19
|
+
isLoading: mockConfigState.isLoading,
|
|
20
|
+
}),
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
import {AdminModelList} from "./AdminModelList";
|
|
24
|
+
|
|
25
|
+
const baseConfig = {
|
|
26
|
+
customScreens: [
|
|
27
|
+
{description: "Custom", displayName: "Dashboard", name: "dashboard"},
|
|
28
|
+
{displayName: "Reports", name: "reports"},
|
|
29
|
+
],
|
|
30
|
+
models: [
|
|
31
|
+
{
|
|
32
|
+
defaultSort: "-created",
|
|
33
|
+
displayName: "User",
|
|
34
|
+
fields: {_id: {required: true, type: "string"}, name: {required: false, type: "string"}},
|
|
35
|
+
listFields: ["name"],
|
|
36
|
+
name: "User",
|
|
37
|
+
routePath: "/admin/users",
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
scripts: [{description: "migrate", name: "migrate"}],
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
describe("AdminModelList", () => {
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
routerPush.mockClear();
|
|
46
|
+
mockConfigState.data = null;
|
|
47
|
+
mockConfigState.error = null;
|
|
48
|
+
mockConfigState.isLoading = false;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("renders a spinner while loading", () => {
|
|
52
|
+
mockConfigState.isLoading = true;
|
|
53
|
+
const {toJSON} = renderWithTheme(<AdminModelList api={{} as any} baseUrl="/admin" />);
|
|
54
|
+
expect(toJSON()).toBeDefined();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("renders an error message when loading fails", () => {
|
|
58
|
+
mockConfigState.error = new Error("boom");
|
|
59
|
+
const {toJSON} = renderWithTheme(<AdminModelList api={{} as any} baseUrl="/admin" />);
|
|
60
|
+
expect(toJSON()).toBeDefined();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("renders cards for models, custom screens, scripts, and configuration", () => {
|
|
64
|
+
mockConfigState.data = baseConfig;
|
|
65
|
+
const {toJSON} = renderWithTheme(
|
|
66
|
+
<AdminModelList
|
|
67
|
+
api={{} as any}
|
|
68
|
+
baseUrl="/admin"
|
|
69
|
+
configurationPath="/admin/configuration"
|
|
70
|
+
customScreens={[{displayName: "Local", name: "local-screen"}]}
|
|
71
|
+
/>
|
|
72
|
+
);
|
|
73
|
+
expect(toJSON()).toBeDefined();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("renders the model grid when config has no scripts/custom screens", () => {
|
|
77
|
+
mockConfigState.data = {...baseConfig, customScreens: [], scripts: []};
|
|
78
|
+
const {toJSON} = renderWithTheme(<AdminModelList api={{} as any} baseUrl="/admin" />);
|
|
79
|
+
expect(toJSON()).toBeDefined();
|
|
80
|
+
});
|
|
81
|
+
});
|