@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.
Files changed (40) hide show
  1. package/dist/AdminFieldRenderer.js +7 -2
  2. package/dist/AdminFieldRenderer.js.map +1 -1
  3. package/dist/AdminObjectPicker.js.map +1 -1
  4. package/dist/AdminPrimitiveArrayField.d.ts +26 -0
  5. package/dist/AdminPrimitiveArrayField.js +55 -0
  6. package/dist/AdminPrimitiveArrayField.js.map +1 -0
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.js +1 -0
  9. package/dist/index.js.map +1 -1
  10. package/dist/types.d.ts +7 -1
  11. package/dist/types.js.map +1 -1
  12. package/package.json +5 -4
  13. package/src/AdminFieldRenderer.test.tsx +445 -0
  14. package/src/AdminFieldRenderer.tsx +22 -2
  15. package/src/AdminModelForm.test.tsx +541 -0
  16. package/src/AdminModelList.test.tsx +81 -0
  17. package/src/AdminModelTable.test.tsx +267 -0
  18. package/src/AdminObjectPicker.tsx +0 -2
  19. package/src/AdminPrimitiveArrayField.test.tsx +186 -0
  20. package/src/AdminPrimitiveArrayField.tsx +186 -0
  21. package/src/AdminScriptList.test.tsx +49 -2
  22. package/src/AdminVersionConfig.test.tsx +196 -0
  23. package/src/CheckboxListEditor.test.tsx +80 -0
  24. package/src/ConfigurationScreen.test.tsx +140 -0
  25. package/src/ConsentFormEditor.test.tsx +512 -0
  26. package/src/ConsentFormList.test.tsx +115 -0
  27. package/src/ConsentResponseViewer.test.tsx +115 -0
  28. package/src/DocumentStorageBrowser.test.tsx +398 -0
  29. package/src/LocaleContentEditor.test.tsx +143 -0
  30. package/src/index.tsx +1 -0
  31. package/src/isolated/AdminFieldRenderer.isolated.tsx +289 -0
  32. package/src/isolated/AdminNestedArrayField.isolated.tsx +297 -0
  33. package/src/isolated/AdminObjectPicker.isolated.tsx +382 -0
  34. package/src/isolated/AdminRefField.isolated.tsx +29 -0
  35. package/src/isolated/AdminScriptRunModal.isolated.tsx +414 -0
  36. package/src/isolated/DocumentStorageBrowser.isolated.tsx +680 -0
  37. package/src/isolated/generateConsentPdf.isolated.ts +295 -0
  38. package/src/isolated/hooks.isolated.tsx +390 -0
  39. package/src/types.ts +7 -1
  40. package/src/AdminScriptRunModal.test.tsx +0 -69
@@ -0,0 +1,115 @@
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
+ interface ReadState {
7
+ data: any;
8
+ isLoading: boolean;
9
+ }
10
+ const readState: ReadState = {data: undefined, isLoading: false};
11
+
12
+ mock.module("./useAdminApi", () => ({
13
+ useAdminApi: () => ({
14
+ useReadQuery: (_id: string, opts: {skip?: boolean}) => {
15
+ if (opts?.skip) {
16
+ return {data: undefined, isLoading: false};
17
+ }
18
+ return {data: readState.data, isLoading: readState.isLoading};
19
+ },
20
+ }),
21
+ }));
22
+
23
+ const pdfCalls: any[] = [];
24
+ let pdfImpl: (r: any) => Promise<void> = async () => {};
25
+ mock.module("./generateConsentPdf", () => ({
26
+ generateConsentPdf: async (r: any) => {
27
+ pdfCalls.push(r);
28
+ return pdfImpl(r);
29
+ },
30
+ }));
31
+
32
+ import {ConsentResponseViewer} from "./ConsentResponseViewer";
33
+
34
+ describe("ConsentResponseViewer", () => {
35
+ beforeEach(() => {
36
+ readState.data = undefined;
37
+ readState.isLoading = false;
38
+ pdfCalls.length = 0;
39
+ pdfImpl = async () => {};
40
+ });
41
+
42
+ it("renders loading state", () => {
43
+ readState.isLoading = true;
44
+ const {toJSON} = renderWithTheme(
45
+ <ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r1" />
46
+ );
47
+ expect(toJSON()).toBeDefined();
48
+ });
49
+
50
+ it("shows not-found state when there is no id / data", () => {
51
+ const {getByText} = renderWithTheme(
52
+ <ConsentResponseViewer api={{} as any} baseUrl="/admin" id="" />
53
+ );
54
+ expect(getByText(/Response not found\./)).toBeDefined();
55
+ });
56
+
57
+ it("renders a full response and triggers PDF download", async () => {
58
+ readState.data = {
59
+ agreed: true,
60
+ agreedAt: "2024-01-02T03:04:05.000Z",
61
+ checkboxValues: {0: true, 1: false},
62
+ consentFormId: {title: "My Form"},
63
+ contentSnapshot: "# Hello",
64
+ formVersionSnapshot: {title: "My Form", version: 1},
65
+ ipAddress: "127.0.0.1",
66
+ locale: "en",
67
+ signature: "data:image/png;base64,abc",
68
+ userAgent: "jest",
69
+ userId: {_id: "u1"},
70
+ };
71
+ const {getByText} = renderWithTheme(
72
+ <ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r1" />
73
+ );
74
+ await act(async () => {
75
+ fireEvent.press(getByText("Download PDF"));
76
+ await new Promise((r) => setTimeout(r, 100));
77
+ });
78
+ expect(pdfCalls.length).toBe(1);
79
+ });
80
+
81
+ it("renders minimal declined response without optional sections", () => {
82
+ readState.data = {
83
+ agreed: false,
84
+ consentFormId: "raw-id",
85
+ userId: "u2",
86
+ };
87
+ const {toJSON} = renderWithTheme(
88
+ <ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r2" />
89
+ );
90
+ expect(toJSON()).toBeDefined();
91
+ });
92
+
93
+ it("handles PDF generation errors", async () => {
94
+ readState.data = {agreed: true};
95
+ pdfImpl = async () => {
96
+ throw new Error("broken");
97
+ };
98
+ const {getByText} = renderWithTheme(
99
+ <ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r1" />
100
+ );
101
+ await act(async () => {
102
+ fireEvent.press(getByText("Download PDF"));
103
+ await new Promise((r) => setTimeout(r, 100));
104
+ });
105
+ expect(pdfCalls.length).toBe(1);
106
+ });
107
+
108
+ it("formats invalid dates gracefully", () => {
109
+ readState.data = {agreed: true, agreedAt: "not-a-date"};
110
+ const {toJSON} = renderWithTheme(
111
+ <ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r1" />
112
+ );
113
+ expect(toJSON()).toBeDefined();
114
+ });
115
+ });
@@ -0,0 +1,398 @@
1
+ import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
2
+ import {renderWithTheme} from "@terreno/ui/src/test-utils";
3
+ import React from "react";
4
+ import {Platform} from "react-native";
5
+ import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
6
+
7
+ mock.module("react-native-webview", () => ({
8
+ WebView: (props: any) => React.createElement("WebView", {...props, testID: "mock-webview"}),
9
+ }));
10
+
11
+ interface ListState {
12
+ data: any;
13
+ isLoading: boolean;
14
+ isError: boolean;
15
+ error: any;
16
+ }
17
+ const listState: ListState = {data: undefined, error: null, isError: false, isLoading: false};
18
+ const refetchMock = mock(() => {});
19
+ const uploadCalls: any[] = [];
20
+ const deleteCalls: string[] = [];
21
+ const deleteFolderCalls: string[] = [];
22
+ const createFolderCalls: any[] = [];
23
+ const downloadCalls: string[] = [];
24
+ let uploadImpl: (body: any) => Promise<any> = async () => ({});
25
+ let deleteImpl: (p: string) => Promise<any> = async () => ({});
26
+ let deleteFolderImpl: (p: string) => Promise<any> = async () => ({});
27
+ let createFolderImpl: (body: any) => Promise<any> = async () => ({});
28
+ let downloadImpl: (p: string) => Promise<any> = async () => new Blob(["hi"]);
29
+
30
+ mock.module("./useDocumentStorageApi", () => ({
31
+ useDocumentStorageApi: () => ({
32
+ useCreateFolderMutation: () => [
33
+ (body: any) => ({
34
+ unwrap: async () => {
35
+ createFolderCalls.push(body);
36
+ return createFolderImpl(body);
37
+ },
38
+ }),
39
+ ],
40
+ useDeleteFolderMutation: () => [
41
+ (p: string) => ({
42
+ unwrap: async () => {
43
+ deleteFolderCalls.push(p);
44
+ return deleteFolderImpl(p);
45
+ },
46
+ }),
47
+ ],
48
+ useDeleteMutation: () => [
49
+ (p: string) => ({
50
+ unwrap: async () => {
51
+ deleteCalls.push(p);
52
+ return deleteImpl(p);
53
+ },
54
+ }),
55
+ ],
56
+ useLazyDownloadQuery: () => [
57
+ (p: string) => ({
58
+ unwrap: async () => {
59
+ downloadCalls.push(p);
60
+ return downloadImpl(p);
61
+ },
62
+ }),
63
+ ],
64
+ useListQuery: (_prefix: string | undefined, opts: {skip?: boolean}) => {
65
+ if (opts?.skip) {
66
+ return {
67
+ data: undefined,
68
+ error: null,
69
+ isError: false,
70
+ isLoading: false,
71
+ refetch: refetchMock,
72
+ };
73
+ }
74
+ return {...listState, refetch: refetchMock};
75
+ },
76
+ useUploadMutation: () => [
77
+ (body: any) => ({
78
+ unwrap: async () => {
79
+ uploadCalls.push(body);
80
+ return uploadImpl(body);
81
+ },
82
+ }),
83
+ {isLoading: false},
84
+ ],
85
+ }),
86
+ }));
87
+
88
+ import {DocumentStorageBrowser} from "./DocumentStorageBrowser";
89
+
90
+ const press = async (el: any): Promise<void> => {
91
+ await act(async () => {
92
+ fireEvent.press(el);
93
+ await new Promise((r) => setTimeout(r, 150));
94
+ });
95
+ };
96
+
97
+ describe("DocumentStorageBrowser", () => {
98
+ const originalPlatformOS = Platform.OS;
99
+
100
+ beforeEach(() => {
101
+ listState.data = undefined;
102
+ listState.isLoading = false;
103
+ listState.isError = false;
104
+ listState.error = null;
105
+ refetchMock.mockClear();
106
+ uploadCalls.length = 0;
107
+ deleteCalls.length = 0;
108
+ deleteFolderCalls.length = 0;
109
+ createFolderCalls.length = 0;
110
+ downloadCalls.length = 0;
111
+ uploadImpl = async () => ({});
112
+ deleteImpl = async () => ({});
113
+ deleteFolderImpl = async () => ({});
114
+ createFolderImpl = async () => ({});
115
+ downloadImpl = async () => new Blob(["hi"]);
116
+ });
117
+
118
+ afterEach(() => {
119
+ Object.defineProperty(Platform, "OS", {configurable: true, value: originalPlatformOS});
120
+ });
121
+
122
+ it("renders loading state", () => {
123
+ listState.isLoading = true;
124
+ const {toJSON} = renderWithTheme(
125
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
126
+ );
127
+ expect(toJSON()).toBeDefined();
128
+ });
129
+
130
+ it("renders error state", () => {
131
+ listState.isError = true;
132
+ listState.error = {status: 500};
133
+ const {getByText} = renderWithTheme(
134
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
135
+ );
136
+ expect(getByText(/Failed to load files/)).toBeDefined();
137
+ });
138
+
139
+ it("renders not-configured state on 503", () => {
140
+ listState.isError = true;
141
+ listState.error = {status: 503};
142
+ const onSettingsPress = mock(() => undefined);
143
+ const {getByText} = renderWithTheme(
144
+ <DocumentStorageBrowser
145
+ api={{} as any}
146
+ basePath="/documents"
147
+ onSettingsPress={onSettingsPress}
148
+ />
149
+ );
150
+ expect(getByText(/Storage is not configured/)).toBeDefined();
151
+ });
152
+
153
+ it("renders empty state when no files or folders", () => {
154
+ listState.data = {files: [], folders: []};
155
+ const {getByText} = renderWithTheme(
156
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
157
+ );
158
+ expect(getByText(/No files found/)).toBeDefined();
159
+ });
160
+
161
+ it("renders a data table with files and folders", () => {
162
+ listState.data = {
163
+ files: [
164
+ {
165
+ contentType: "image/png",
166
+ fullPath: "a.png",
167
+ name: "a.png",
168
+ size: 2048,
169
+ updated: "2024-01-01T00:00:00Z",
170
+ },
171
+ {
172
+ contentType: null,
173
+ fullPath: "b.bin",
174
+ name: "b.bin",
175
+ size: 0,
176
+ updated: "",
177
+ },
178
+ ],
179
+ folders: ["sub/"],
180
+ };
181
+ const {toJSON} = renderWithTheme(
182
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
183
+ );
184
+ expect(toJSON()).toBeDefined();
185
+ });
186
+
187
+ it("refreshes the list when refresh is pressed", async () => {
188
+ listState.data = {files: [], folders: []};
189
+ const {getByTestId} = renderWithTheme(
190
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
191
+ );
192
+ await press(getByTestId("document-refresh-button"));
193
+ expect(refetchMock).toHaveBeenCalled();
194
+ });
195
+
196
+ it("opens the new-folder modal when pressed", async () => {
197
+ listState.data = {files: [], folders: []};
198
+ const {getByTestId, toJSON} = renderWithTheme(
199
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
200
+ );
201
+ await press(getByTestId("document-new-folder-button"));
202
+ expect(toJSON()).toBeDefined();
203
+ });
204
+
205
+ it("does not show upload/new folder when allowUpload is false", () => {
206
+ listState.data = {files: [], folders: []};
207
+ const {queryByTestId} = renderWithTheme(
208
+ <DocumentStorageBrowser allowUpload={false} api={{} as any} basePath="/documents" />
209
+ );
210
+ expect(queryByTestId("document-upload-button")).toBeNull();
211
+ expect(queryByTestId("document-new-folder-button")).toBeNull();
212
+ });
213
+
214
+ it("uses onFileSelect when provided", () => {
215
+ listState.data = {
216
+ files: [{contentType: "text/plain", fullPath: "a.txt", name: "a.txt", size: 1, updated: ""}],
217
+ folders: [],
218
+ };
219
+ const onFileSelect = mock(() => undefined);
220
+ const {toJSON} = renderWithTheme(
221
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" onFileSelect={onFileSelect} />
222
+ );
223
+ expect(toJSON()).toBeDefined();
224
+ });
225
+
226
+ it("renders settings button when onSettingsPress is provided", () => {
227
+ listState.data = {files: [], folders: []};
228
+ const onSettingsPress = mock(() => undefined);
229
+ const {toJSON} = renderWithTheme(
230
+ <DocumentStorageBrowser
231
+ api={{} as any}
232
+ basePath="/documents"
233
+ onSettingsPress={onSettingsPress}
234
+ />
235
+ );
236
+ expect(toJSON()).toBeDefined();
237
+ });
238
+
239
+ it("does not render when allowDelete is false (folder actions return null)", () => {
240
+ listState.data = {files: [], folders: ["sub/"]};
241
+ const {toJSON} = renderWithTheme(
242
+ <DocumentStorageBrowser allowDelete={false} api={{} as any} basePath="/documents" />
243
+ );
244
+ expect(toJSON()).toBeDefined();
245
+ });
246
+
247
+ it("uses custom title when provided", () => {
248
+ listState.data = {files: [], folders: []};
249
+ const {getByText} = renderWithTheme(
250
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" title="Custom Storage" />
251
+ );
252
+ expect(getByText("Custom Storage")).toBeDefined();
253
+ });
254
+
255
+ it("reconfigures and refetches when refresh is pressed after 503", async () => {
256
+ listState.isError = true;
257
+ listState.error = {status: 503};
258
+ const {getByTestId, rerender} = renderWithTheme(
259
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
260
+ );
261
+ await act(async () => {
262
+ await new Promise((r) => setTimeout(r, 50));
263
+ });
264
+ await press(getByTestId("document-refresh-button"));
265
+ listState.isError = false;
266
+ listState.error = null;
267
+ listState.data = {files: [], folders: []};
268
+ rerender(<DocumentStorageBrowser api={{} as any} basePath="/documents" />);
269
+ expect(true).toBe(true);
270
+ });
271
+
272
+ it("supports originalStatus 503 error shape", () => {
273
+ listState.isError = true;
274
+ listState.error = {originalStatus: 503};
275
+ const {getByText} = renderWithTheme(
276
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
277
+ );
278
+ expect(getByText(/Storage is not configured/)).toBeDefined();
279
+ });
280
+
281
+ it("renders breadcrumbs that can be clicked", () => {
282
+ listState.data = {files: [], folders: []};
283
+ const {toJSON} = renderWithTheme(
284
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
285
+ );
286
+ // No current prefix, only root crumb
287
+ expect(toJSON()).toBeDefined();
288
+ });
289
+
290
+ it("renders new folder modal markup when opened", async () => {
291
+ listState.data = {files: [], folders: []};
292
+ const {getByTestId, toJSON} = renderWithTheme(
293
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
294
+ );
295
+ await press(getByTestId("document-new-folder-button"));
296
+ expect(toJSON()).toBeDefined();
297
+ });
298
+
299
+ it("uploads a file when the hidden input fires change", async () => {
300
+ Object.defineProperty(Platform, "OS", {configurable: true, value: "web"});
301
+ listState.data = {files: [], folders: []};
302
+ const {UNSAFE_getAllByType} = renderWithTheme(
303
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
304
+ );
305
+ const hostInputs = UNSAFE_getAllByType("input" as any);
306
+ const fileInput = hostInputs.find((node: any) => node.props?.type === "file") as any;
307
+ expect(fileInput).toBeDefined();
308
+ const fakeFile = new File(["x"], "x.txt", {type: "text/plain"});
309
+ await act(async () => {
310
+ fileInput.props.onChange({target: {files: [fakeFile], value: ""}});
311
+ await new Promise((r) => setTimeout(r, 30));
312
+ });
313
+ expect(uploadCalls.length).toBe(1);
314
+ expect(uploadCalls[0].prefix).toBeUndefined();
315
+ });
316
+
317
+ it("ignores upload change when no file selected", async () => {
318
+ Object.defineProperty(Platform, "OS", {configurable: true, value: "web"});
319
+ listState.data = {files: [], folders: []};
320
+ const {UNSAFE_getAllByType} = renderWithTheme(
321
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
322
+ );
323
+ const hostInputs = UNSAFE_getAllByType("input" as any);
324
+ const fileInput = hostInputs.find((node: any) => node.props?.type === "file") as any;
325
+ await act(async () => {
326
+ fileInput.props.onChange({target: {files: []}});
327
+ await new Promise((r) => setTimeout(r, 10));
328
+ });
329
+ expect(uploadCalls.length).toBe(0);
330
+ });
331
+
332
+ it("handles upload errors without throwing", async () => {
333
+ Object.defineProperty(Platform, "OS", {configurable: true, value: "web"});
334
+ listState.data = {files: [], folders: []};
335
+ uploadImpl = async () => {
336
+ throw new Error("upload failed");
337
+ };
338
+ const {UNSAFE_getAllByType} = renderWithTheme(
339
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
340
+ );
341
+ const hostInputs = UNSAFE_getAllByType("input" as any);
342
+ const fileInput = hostInputs.find((node: any) => node.props?.type === "file") as any;
343
+ const fakeFile = new File(["x"], "y.txt", {type: "text/plain"});
344
+ await act(async () => {
345
+ fileInput.props.onChange({target: {files: [fakeFile], value: ""}});
346
+ await new Promise((r) => setTimeout(r, 30));
347
+ });
348
+ expect(uploadCalls.length).toBe(1);
349
+ });
350
+
351
+ it("renders folder rows with folder delete action available", () => {
352
+ listState.data = {
353
+ files: [
354
+ {
355
+ contentType: "application/pdf",
356
+ fullPath: "doc.pdf",
357
+ name: "doc.pdf",
358
+ size: 1024,
359
+ updated: "2024-01-01T00:00:00Z",
360
+ },
361
+ ],
362
+ folders: ["sub-folder/"],
363
+ };
364
+ const {toJSON} = renderWithTheme(
365
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
366
+ );
367
+ expect(toJSON()).toBeDefined();
368
+ });
369
+
370
+ it("formats file sizes across all unit ranges", () => {
371
+ listState.data = {
372
+ files: [
373
+ {contentType: "b/b", fullPath: "0", name: "0", size: 0, updated: ""},
374
+ {contentType: "b/b", fullPath: "b", name: "b", size: 512, updated: ""},
375
+ {contentType: "b/b", fullPath: "kb", name: "kb", size: 2048, updated: ""},
376
+ {
377
+ contentType: "b/b",
378
+ fullPath: "mb",
379
+ name: "mb",
380
+ size: 5 * 1024 * 1024,
381
+ updated: "",
382
+ },
383
+ {
384
+ contentType: "b/b",
385
+ fullPath: "gb",
386
+ name: "gb",
387
+ size: 3 * 1024 * 1024 * 1024,
388
+ updated: "",
389
+ },
390
+ ],
391
+ folders: [],
392
+ };
393
+ const {toJSON} = renderWithTheme(
394
+ <DocumentStorageBrowser api={{} as any} basePath="/documents" />
395
+ );
396
+ expect(toJSON()).toBeDefined();
397
+ });
398
+ });
@@ -0,0 +1,143 @@
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 {LocaleContentEditor} from "./LocaleContentEditor";
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("LocaleContentEditor", () => {
15
+ it("renders without crashing and shows 'no content' when empty", () => {
16
+ const {toJSON} = renderWithTheme(
17
+ <LocaleContentEditor
18
+ errorText="boom"
19
+ helperText="help"
20
+ onChange={() => {}}
21
+ title="Content"
22
+ value={{}}
23
+ />
24
+ );
25
+ expect(toJSON()).toBeDefined();
26
+ });
27
+
28
+ it("renders locale tabs when value has locales", () => {
29
+ const {toJSON, getByText} = renderWithTheme(
30
+ <LocaleContentEditor onChange={() => {}} title="Content" value={{en: "Hello", es: "Hola"}} />
31
+ );
32
+ expect(toJSON()).toBeDefined();
33
+ expect(getByText(/Editing:/).props.children).toBeDefined();
34
+ });
35
+
36
+ it("switches active locale when clicking a locale tab", async () => {
37
+ const {getByText} = renderWithTheme(
38
+ <LocaleContentEditor onChange={() => {}} value={{en: "Hello", fr: "Salut"}} />
39
+ );
40
+
41
+ await press(getByText("French"));
42
+ expect(getByText("Editing: fr")).toBeDefined();
43
+ });
44
+
45
+ it("removes the active locale when remove is pressed", async () => {
46
+ const onChange = mock((_: Record<string, string>) => undefined);
47
+ const {getByText} = renderWithTheme(
48
+ <LocaleContentEditor onChange={onChange} value={{en: "Hello", es: "Hola"}} />
49
+ );
50
+
51
+ await press(getByText(/Remove /));
52
+ expect(onChange).toHaveBeenCalled();
53
+ expect(Object.keys((onChange.mock.calls[0] as any)[0])).toEqual(["es"]);
54
+ });
55
+
56
+ it("falls back to empty object when value is not an object", () => {
57
+ const {toJSON} = renderWithTheme(
58
+ <LocaleContentEditor onChange={() => {}} value={null as any} />
59
+ );
60
+ expect(toJSON()).toBeDefined();
61
+ });
62
+
63
+ it("adds a new locale via the SelectField and Add Locale button", async () => {
64
+ const onChange = mock((_: Record<string, string>) => undefined);
65
+ const {UNSAFE_root, toJSON} = renderWithTheme(
66
+ <LocaleContentEditor onChange={onChange} value={{}} />
67
+ );
68
+ // Find the SelectField and invoke its onChange prop directly.
69
+ const selects = UNSAFE_root.findAll(
70
+ (n: any) => typeof n.props?.onChange === "function" && Array.isArray(n.props?.options)
71
+ );
72
+ expect(selects.length).toBeGreaterThan(0);
73
+ await act(async () => {
74
+ (selects[0] as any).props.onChange("en");
75
+ });
76
+ // Find the Add Locale button (may be nested)
77
+ const addBtns = UNSAFE_root.findAll((n: any) => n.props?.text === "Add Locale");
78
+ expect(addBtns.length).toBeGreaterThan(0);
79
+ await act(async () => {
80
+ (addBtns[0] as any).props.onClick?.();
81
+ });
82
+ expect(onChange).toHaveBeenCalled();
83
+ expect(Object.keys((onChange.mock.calls[0] as any)[0])).toContain("en");
84
+ expect(toJSON()).toBeDefined();
85
+ });
86
+
87
+ it("does not add a locale that already exists", async () => {
88
+ const onChange = mock((_: Record<string, string>) => undefined);
89
+ const {UNSAFE_root} = renderWithTheme(
90
+ <LocaleContentEditor onChange={onChange} value={{en: "Hi"}} />
91
+ );
92
+ const selects = UNSAFE_root.findAll(
93
+ (n: any) => typeof n.props?.onChange === "function" && Array.isArray(n.props?.options)
94
+ );
95
+ await act(async () => {
96
+ (selects[0] as any).props.onChange("en");
97
+ });
98
+ const addBtns = UNSAFE_root.findAll((n: any) => n.props?.text === "Add Locale");
99
+ await act(async () => {
100
+ (addBtns[0] as any).props.onClick?.();
101
+ });
102
+ // Should NOT have called onChange with an add because "en" already exists.
103
+ expect(onChange).not.toHaveBeenCalled();
104
+ });
105
+
106
+ it("does not add when no new locale selected", async () => {
107
+ const onChange = mock((_: Record<string, string>) => undefined);
108
+ const {UNSAFE_root} = renderWithTheme(<LocaleContentEditor onChange={onChange} value={{}} />);
109
+ const addBtns = UNSAFE_root.findAll((n: any) => n.props?.text === "Add Locale");
110
+ await act(async () => {
111
+ (addBtns[0] as any).props.onClick?.();
112
+ });
113
+ expect(onChange).not.toHaveBeenCalled();
114
+ });
115
+
116
+ it("edits content for the active locale via MarkdownEditorField onChange", async () => {
117
+ const onChange = mock((_: Record<string, string>) => undefined);
118
+ const {UNSAFE_root} = renderWithTheme(
119
+ <LocaleContentEditor onChange={onChange} value={{en: "Hi", es: "Hola"}} />
120
+ );
121
+ const editors = UNSAFE_root.findAll(
122
+ (n: any) =>
123
+ typeof n.props?.testID === "string" && n.props.testID.startsWith("locale-content-")
124
+ );
125
+ expect(editors.length).toBeGreaterThan(0);
126
+ await act(async () => {
127
+ (editors[0] as any).props.onChange("new content");
128
+ });
129
+ expect(onChange).toHaveBeenCalled();
130
+ const arg = (onChange.mock.calls[0] as any)[0];
131
+ expect(arg.en).toBe("new content");
132
+ expect(arg.es).toBe("Hola");
133
+ });
134
+
135
+ it("removing the active locale selects the next available locale", async () => {
136
+ const onChange = mock((_: Record<string, string>) => undefined);
137
+ const {getByText} = renderWithTheme(
138
+ <LocaleContentEditor onChange={onChange} value={{en: "Hi", es: "Hola"}} />
139
+ );
140
+ await press(getByText("Remove en"));
141
+ expect(onChange).toHaveBeenCalled();
142
+ });
143
+ });
package/src/index.tsx CHANGED
@@ -4,6 +4,7 @@ export {AdminModelList} from "./AdminModelList";
4
4
  export {AdminModelTable} from "./AdminModelTable";
5
5
  export {AdminNestedArrayField} from "./AdminNestedArrayField";
6
6
  export {AdminObjectPicker} from "./AdminObjectPicker";
7
+ export {AdminPrimitiveArrayField} from "./AdminPrimitiveArrayField";
7
8
  export {AdminRefField} from "./AdminRefField";
8
9
  export {AdminScriptList} from "./AdminScriptList";
9
10
  export {AdminScriptRunModal} from "./AdminScriptRunModal";