@terreno/admin-frontend 0.10.0 → 0.11.1

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
@@ -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
- // Mock AdminScriptRunModal since it depends on useAdminScripts
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: () => null,
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
+ });
@@ -0,0 +1,140 @@
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 QueryState<T> {
7
+ data: T | undefined;
8
+ isLoading: boolean;
9
+ }
10
+ const metaState: QueryState<any> = {data: undefined, isLoading: false};
11
+ const valuesState: QueryState<any> = {data: undefined, isLoading: false};
12
+ let updateImpl: (body: any) => Promise<any> = async () => ({});
13
+ const updateCalls: any[] = [];
14
+
15
+ mock.module("./useConfigurationApi", () => ({
16
+ useConfigurationApi: () => ({
17
+ useMetaQuery: () => ({data: metaState.data, isLoading: metaState.isLoading}),
18
+ useUpdateMutation: () => [
19
+ (body: any) => ({
20
+ unwrap: async () => {
21
+ updateCalls.push(body);
22
+ return updateImpl(body);
23
+ },
24
+ }),
25
+ {isLoading: false},
26
+ ],
27
+ useValuesQuery: () => ({
28
+ data: valuesState.data,
29
+ isLoading: valuesState.isLoading,
30
+ }),
31
+ }),
32
+ }));
33
+
34
+ import {ConfigurationScreen} from "./ConfigurationScreen";
35
+
36
+ const baseMeta = {
37
+ sections: [
38
+ {
39
+ description: "root-desc",
40
+ displayName: "Root",
41
+ fields: {
42
+ enabled: {required: false, type: "boolean"},
43
+ name: {required: false, type: "string"},
44
+ },
45
+ name: "__root__",
46
+ },
47
+ {
48
+ description: "smtp-desc",
49
+ displayName: "SMTP",
50
+ fields: {
51
+ apiKey: {required: false, secret: true, type: "string"},
52
+ host: {required: false, type: "string"},
53
+ notes: {required: false, type: "string", widget: "markdown"},
54
+ port: {default: 587, required: false, type: "number"},
55
+ },
56
+ name: "smtp",
57
+ },
58
+ ],
59
+ };
60
+
61
+ describe("ConfigurationScreen", () => {
62
+ beforeEach(() => {
63
+ metaState.data = undefined;
64
+ metaState.isLoading = false;
65
+ valuesState.data = undefined;
66
+ valuesState.isLoading = false;
67
+ updateImpl = async () => ({});
68
+ updateCalls.length = 0;
69
+ });
70
+
71
+ it("renders loading state on initial load", () => {
72
+ metaState.isLoading = true;
73
+ valuesState.isLoading = true;
74
+ const {toJSON} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
75
+ expect(toJSON()).toBeDefined();
76
+ });
77
+
78
+ it("renders an empty-state when metadata is missing", () => {
79
+ const {toJSON, getByText} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
80
+ expect(toJSON()).toBeDefined();
81
+ expect(getByText(/No configuration metadata available/)).toBeDefined();
82
+ });
83
+
84
+ it("renders no sections when metadata has empty sections", () => {
85
+ metaState.data = {sections: []};
86
+ valuesState.data = {};
87
+ const {getByText} = renderWithTheme(
88
+ <ConfigurationScreen api={{} as any} basePath="/config" title="My Config" />
89
+ );
90
+ expect(getByText(/No configuration sections defined/)).toBeDefined();
91
+ });
92
+
93
+ it("renders all field types and saves edits", async () => {
94
+ metaState.data = baseMeta;
95
+ valuesState.data = {
96
+ enabled: true,
97
+ name: "Initial",
98
+ smtp: {apiKey: "secret", host: "h", notes: "notes", port: 25},
99
+ };
100
+ const {getByTestId} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
101
+ await act(async () => {
102
+ fireEvent.changeText(getByTestId("config-__root__-name"), "Updated");
103
+ await new Promise((r) => setTimeout(r, 50));
104
+ });
105
+ await act(async () => {
106
+ fireEvent.press(getByTestId("config-save-button"));
107
+ await new Promise((r) => setTimeout(r, 200));
108
+ });
109
+ expect(updateCalls.length).toBe(1);
110
+ expect(updateCalls[0].name).toBe("Updated");
111
+ });
112
+
113
+ it("handles save errors without throwing", async () => {
114
+ metaState.data = baseMeta;
115
+ valuesState.data = {};
116
+ updateImpl = async () => {
117
+ throw new Error("nope");
118
+ };
119
+ const {getByTestId} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
120
+ await act(async () => {
121
+ fireEvent.changeText(getByTestId("config-__root__-name"), "Boom");
122
+ await new Promise((r) => setTimeout(r, 50));
123
+ });
124
+ await act(async () => {
125
+ fireEvent.press(getByTestId("config-save-button"));
126
+ await new Promise((r) => setTimeout(r, 200));
127
+ });
128
+ expect(updateCalls.length).toBe(1);
129
+ });
130
+
131
+ it("accepts non-numeric number input and preserves the raw text", async () => {
132
+ metaState.data = baseMeta;
133
+ valuesState.data = {};
134
+ const {getByTestId} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
135
+ await act(async () => {
136
+ fireEvent.changeText(getByTestId("config-smtp-port"), "not-a-number");
137
+ await new Promise((r) => setTimeout(r, 50));
138
+ });
139
+ });
140
+ });