@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,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
|
+
});
|
|
@@ -0,0 +1,512 @@
|
|
|
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 State {
|
|
7
|
+
formData: any;
|
|
8
|
+
isFormLoading: boolean;
|
|
9
|
+
}
|
|
10
|
+
const state: State = {formData: null, isFormLoading: false};
|
|
11
|
+
const createCalls: any[] = [];
|
|
12
|
+
const updateCalls: any[] = [];
|
|
13
|
+
const publishCalls: string[] = [];
|
|
14
|
+
const generateCalls: any[] = [];
|
|
15
|
+
const translateCalls: any[] = [];
|
|
16
|
+
let createImpl: (body: any) => Promise<any> = async (b) => ({_id: "new-id", ...b});
|
|
17
|
+
let updateImpl: (args: any) => Promise<any> = async (a) => ({_id: a.id});
|
|
18
|
+
let publishImpl: (id: string) => Promise<any> = async () => ({});
|
|
19
|
+
let generateImpl: (body: any) => Promise<any> = async () => ({
|
|
20
|
+
data: {content: "generated"},
|
|
21
|
+
});
|
|
22
|
+
let translateImpl: (body: any) => Promise<any> = async () => ({
|
|
23
|
+
data: {content: "translated"},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
mock.module("./useAdminApi", () => ({
|
|
27
|
+
useAdminApi: () => ({
|
|
28
|
+
useCreateMutation: () => [
|
|
29
|
+
(body: any) => ({
|
|
30
|
+
unwrap: async () => {
|
|
31
|
+
createCalls.push(body);
|
|
32
|
+
return createImpl(body);
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
{isLoading: false},
|
|
36
|
+
],
|
|
37
|
+
useReadQuery: (_id: string, opts: {skip?: boolean}) => {
|
|
38
|
+
if (opts?.skip) {
|
|
39
|
+
return {data: undefined, isLoading: false};
|
|
40
|
+
}
|
|
41
|
+
return {data: state.formData, isLoading: state.isFormLoading};
|
|
42
|
+
},
|
|
43
|
+
useUpdateMutation: () => [
|
|
44
|
+
(args: any) => ({
|
|
45
|
+
unwrap: async () => {
|
|
46
|
+
updateCalls.push(args);
|
|
47
|
+
return updateImpl(args);
|
|
48
|
+
},
|
|
49
|
+
}),
|
|
50
|
+
{isLoading: false},
|
|
51
|
+
],
|
|
52
|
+
}),
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
const mutationSpecs: any[] = [];
|
|
56
|
+
const makeApi = () => ({
|
|
57
|
+
injectEndpoints: ({endpoints}: {endpoints: (b: any) => Record<string, any>}) => {
|
|
58
|
+
endpoints({
|
|
59
|
+
mutation: (spec: any) => {
|
|
60
|
+
// Invoke the mutation query lambda with both an object body and a string
|
|
61
|
+
// id so we exercise the URL + body builders for every mutation shape
|
|
62
|
+
// (generate/translate take a body object, publish takes an id string).
|
|
63
|
+
if (typeof spec?.query === "function") {
|
|
64
|
+
mutationSpecs.push(
|
|
65
|
+
spec.query({
|
|
66
|
+
content: "c",
|
|
67
|
+
description: "d",
|
|
68
|
+
fromLocale: "en",
|
|
69
|
+
locale: "en",
|
|
70
|
+
toLocale: "es",
|
|
71
|
+
type: "agreement",
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
mutationSpecs.push(spec.query("form-id"));
|
|
75
|
+
}
|
|
76
|
+
return spec;
|
|
77
|
+
},
|
|
78
|
+
query: (spec: any) => spec,
|
|
79
|
+
});
|
|
80
|
+
return {
|
|
81
|
+
useGenerateConsentContentMutation: () => [
|
|
82
|
+
(body: any) => ({
|
|
83
|
+
unwrap: async () => {
|
|
84
|
+
generateCalls.push(body);
|
|
85
|
+
return generateImpl(body);
|
|
86
|
+
},
|
|
87
|
+
}),
|
|
88
|
+
{isLoading: false},
|
|
89
|
+
],
|
|
90
|
+
usePublishConsentFormMutation: () => [
|
|
91
|
+
(id: string) => ({
|
|
92
|
+
unwrap: async () => {
|
|
93
|
+
publishCalls.push(id);
|
|
94
|
+
return publishImpl(id);
|
|
95
|
+
},
|
|
96
|
+
}),
|
|
97
|
+
{isLoading: false},
|
|
98
|
+
],
|
|
99
|
+
useTranslateConsentContentMutation: () => [
|
|
100
|
+
(body: any) => ({
|
|
101
|
+
unwrap: async () => {
|
|
102
|
+
translateCalls.push(body);
|
|
103
|
+
return translateImpl(body);
|
|
104
|
+
},
|
|
105
|
+
}),
|
|
106
|
+
{isLoading: false},
|
|
107
|
+
],
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
import {ConsentFormEditor} from "./ConsentFormEditor";
|
|
113
|
+
|
|
114
|
+
const press = async (el: any): Promise<void> => {
|
|
115
|
+
await act(async () => {
|
|
116
|
+
fireEvent.press(el);
|
|
117
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
describe("ConsentFormEditor", () => {
|
|
122
|
+
beforeEach(() => {
|
|
123
|
+
state.formData = null;
|
|
124
|
+
state.isFormLoading = false;
|
|
125
|
+
createCalls.length = 0;
|
|
126
|
+
updateCalls.length = 0;
|
|
127
|
+
publishCalls.length = 0;
|
|
128
|
+
generateCalls.length = 0;
|
|
129
|
+
translateCalls.length = 0;
|
|
130
|
+
mutationSpecs.length = 0;
|
|
131
|
+
createImpl = async (b) => ({_id: "new-id", ...b});
|
|
132
|
+
updateImpl = async (a) => ({_id: a.id});
|
|
133
|
+
publishImpl = async () => ({});
|
|
134
|
+
generateImpl = async () => ({data: {content: "generated"}});
|
|
135
|
+
translateImpl = async () => ({data: {content: "translated"}});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("renders loading state in edit mode", () => {
|
|
139
|
+
state.isFormLoading = true;
|
|
140
|
+
const {toJSON} = renderWithTheme(
|
|
141
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" id="f1" />
|
|
142
|
+
);
|
|
143
|
+
expect(toJSON()).toBeDefined();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("renders create mode with default values", () => {
|
|
147
|
+
const {getByTestId} = renderWithTheme(
|
|
148
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
|
|
149
|
+
);
|
|
150
|
+
expect(getByTestId("consent-form-title-input")).toBeDefined();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("fails validation without title or slug", async () => {
|
|
154
|
+
const {getByTestId} = renderWithTheme(
|
|
155
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
|
|
156
|
+
);
|
|
157
|
+
await press(getByTestId("consent-form-save-button"));
|
|
158
|
+
expect(createCalls.length).toBe(0);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("auto-slugs titles and creates a form on save", async () => {
|
|
162
|
+
const onSave = mock((_: any) => undefined);
|
|
163
|
+
const {getByTestId} = renderWithTheme(
|
|
164
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" onSave={onSave} />
|
|
165
|
+
);
|
|
166
|
+
await act(async () => {
|
|
167
|
+
fireEvent.changeText(getByTestId("consent-form-title-input"), "Hello World");
|
|
168
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
169
|
+
});
|
|
170
|
+
await press(getByTestId("consent-form-save-button"));
|
|
171
|
+
expect(createCalls.length).toBe(1);
|
|
172
|
+
expect(createCalls[0].slug).toBe("hello-world");
|
|
173
|
+
expect(onSave).toHaveBeenCalled();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("respects manually-set slugs and handles create errors", async () => {
|
|
177
|
+
createImpl = async () => {
|
|
178
|
+
throw new Error("conflict");
|
|
179
|
+
};
|
|
180
|
+
const {getByTestId} = renderWithTheme(
|
|
181
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
|
|
182
|
+
);
|
|
183
|
+
await act(async () => {
|
|
184
|
+
fireEvent.changeText(getByTestId("consent-form-title-input"), "Title");
|
|
185
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
186
|
+
});
|
|
187
|
+
await act(async () => {
|
|
188
|
+
fireEvent.changeText(getByTestId("consent-form-slug-input"), "custom-slug");
|
|
189
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
190
|
+
});
|
|
191
|
+
await press(getByTestId("consent-form-save-button"));
|
|
192
|
+
expect(createCalls.length).toBe(1);
|
|
193
|
+
expect(createCalls[0].slug).toBe("custom-slug");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("loads form data in edit mode and saves an update", async () => {
|
|
197
|
+
state.formData = {
|
|
198
|
+
active: true,
|
|
199
|
+
agreeButtonText: "Yes",
|
|
200
|
+
allowDecline: true,
|
|
201
|
+
captureSignature: true,
|
|
202
|
+
checkboxes: [{label: "Agree", required: true}],
|
|
203
|
+
content: {en: "Hi", es: "Hola"},
|
|
204
|
+
declineButtonText: "No",
|
|
205
|
+
defaultLocale: "en",
|
|
206
|
+
order: 2,
|
|
207
|
+
required: true,
|
|
208
|
+
requireScrollToBottom: true,
|
|
209
|
+
slug: "privacy",
|
|
210
|
+
title: "Privacy",
|
|
211
|
+
type: "privacy",
|
|
212
|
+
};
|
|
213
|
+
const {getByTestId} = renderWithTheme(
|
|
214
|
+
<ConsentFormEditor
|
|
215
|
+
api={makeApi() as any}
|
|
216
|
+
baseUrl="/admin"
|
|
217
|
+
hasAiSupport
|
|
218
|
+
id="f1"
|
|
219
|
+
supportedLocales={["en", "es"]}
|
|
220
|
+
/>
|
|
221
|
+
);
|
|
222
|
+
await press(getByTestId("consent-form-save-button"));
|
|
223
|
+
expect(updateCalls.length).toBe(1);
|
|
224
|
+
expect(updateCalls[0].id).toBe("f1");
|
|
225
|
+
expect(updateCalls[0].body.title).toBe("Privacy");
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("publishes a form on publish press", async () => {
|
|
229
|
+
state.formData = {slug: "s", title: "T"};
|
|
230
|
+
const {getByTestId} = renderWithTheme(
|
|
231
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" id="f1" />
|
|
232
|
+
);
|
|
233
|
+
await press(getByTestId("consent-form-publish-button"));
|
|
234
|
+
expect(publishCalls).toEqual(["f1"]);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("handles publish errors", async () => {
|
|
238
|
+
state.formData = {slug: "s", title: "T"};
|
|
239
|
+
publishImpl = async () => {
|
|
240
|
+
throw new Error("fail");
|
|
241
|
+
};
|
|
242
|
+
const {getByTestId} = renderWithTheme(
|
|
243
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" id="f1" />
|
|
244
|
+
);
|
|
245
|
+
await press(getByTestId("consent-form-publish-button"));
|
|
246
|
+
expect(publishCalls.length).toBe(1);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("opens the AI generate modal in create mode", async () => {
|
|
250
|
+
const {getByTestId, toJSON} = renderWithTheme(
|
|
251
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
|
|
252
|
+
);
|
|
253
|
+
await press(getByTestId("consent-form-generate-button"));
|
|
254
|
+
expect(toJSON()).toBeDefined();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("adds, edits, and removes checkboxes", async () => {
|
|
258
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
259
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
|
|
260
|
+
);
|
|
261
|
+
await press(getByTestId("consent-form-add-checkbox-button"));
|
|
262
|
+
expect(getByTestId("consent-form-checkbox-0")).toBeDefined();
|
|
263
|
+
await act(async () => {
|
|
264
|
+
fireEvent.changeText(getByTestId("consent-form-checkbox-0-label-input"), "I agree");
|
|
265
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
266
|
+
});
|
|
267
|
+
await act(async () => {
|
|
268
|
+
fireEvent.changeText(getByTestId("consent-form-checkbox-0-prompt-input"), "Confirm?");
|
|
269
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
270
|
+
});
|
|
271
|
+
await press(getByTestId("consent-form-checkbox-0-remove-button"));
|
|
272
|
+
expect(queryByTestId("consent-form-checkbox-0")).toBeNull();
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("toggles allowDecline and shows decline text field", async () => {
|
|
276
|
+
state.formData = {
|
|
277
|
+
allowDecline: true,
|
|
278
|
+
declineButtonText: "Nope",
|
|
279
|
+
slug: "s",
|
|
280
|
+
title: "T",
|
|
281
|
+
};
|
|
282
|
+
const {getByTestId} = renderWithTheme(
|
|
283
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" id="f1" />
|
|
284
|
+
);
|
|
285
|
+
expect(getByTestId("consent-form-decline-button-text-input")).toBeDefined();
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("renders and switches segmented control in multi-locale mode", async () => {
|
|
289
|
+
const {toJSON} = renderWithTheme(
|
|
290
|
+
<ConsentFormEditor
|
|
291
|
+
api={makeApi() as any}
|
|
292
|
+
baseUrl="/admin"
|
|
293
|
+
hasAiSupport
|
|
294
|
+
supportedLocales={["en", "es"]}
|
|
295
|
+
/>
|
|
296
|
+
);
|
|
297
|
+
expect(toJSON()).toBeDefined();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("calls onCancel when cancel is pressed", async () => {
|
|
301
|
+
const onCancel = mock(() => undefined);
|
|
302
|
+
const {getByTestId} = renderWithTheme(
|
|
303
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" onCancel={onCancel} />
|
|
304
|
+
);
|
|
305
|
+
await press(getByTestId("consent-form-cancel-button"));
|
|
306
|
+
expect(onCancel).toHaveBeenCalled();
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("handlePublish early-returns when no id", async () => {
|
|
310
|
+
// Create mode has no id → publish button shouldn't exist, but exercising
|
|
311
|
+
// the handler via the save flow at least touches the non-id path.
|
|
312
|
+
const {queryByTestId} = renderWithTheme(
|
|
313
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
|
|
314
|
+
);
|
|
315
|
+
expect(queryByTestId("consent-form-publish-button")).toBeNull();
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("wires all three mutations (generate/publish/translate) to the right URLs", () => {
|
|
319
|
+
renderWithTheme(<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />);
|
|
320
|
+
// The mock invokes each mutation's query lambda, so mutationSpecs contains
|
|
321
|
+
// the resolved request descriptors for generate, publish, and translate.
|
|
322
|
+
const urls = mutationSpecs.map((s: any) => s.url).sort();
|
|
323
|
+
expect(urls).toContain("/consent-forms/generate");
|
|
324
|
+
expect(urls).toContain("/consent-forms/form-id/publish");
|
|
325
|
+
expect(urls).toContain("/consent-forms/translate");
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("refuses to save without a slug even when title is present", async () => {
|
|
329
|
+
const {getByTestId} = renderWithTheme(
|
|
330
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
|
|
331
|
+
);
|
|
332
|
+
// Set a title so slug would auto-generate.
|
|
333
|
+
await act(async () => {
|
|
334
|
+
fireEvent.changeText(getByTestId("consent-form-title-input"), "Draft");
|
|
335
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
336
|
+
});
|
|
337
|
+
// Now clear the slug explicitly — this hits the "Slug is required" branch.
|
|
338
|
+
await act(async () => {
|
|
339
|
+
fireEvent.changeText(getByTestId("consent-form-slug-input"), "");
|
|
340
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
341
|
+
});
|
|
342
|
+
await press(getByTestId("consent-form-save-button"));
|
|
343
|
+
expect(createCalls.length).toBe(0);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("generates AI content into the active locale and closes the modal", async () => {
|
|
347
|
+
const {getByTestId, UNSAFE_root} = renderWithTheme(
|
|
348
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
|
|
349
|
+
);
|
|
350
|
+
await press(getByTestId("consent-form-generate-button"));
|
|
351
|
+
// Find the Modal's primary "Generate" button and invoke it directly. The
|
|
352
|
+
// description input lives inside the Modal's portal and may not be
|
|
353
|
+
// mounted; invoking primaryButtonOnClick still exercises handleGenerate.
|
|
354
|
+
const modals = UNSAFE_root.findAll(
|
|
355
|
+
(n: any) => typeof n.props?.primaryButtonOnClick === "function"
|
|
356
|
+
);
|
|
357
|
+
expect(modals.length).toBeGreaterThan(0);
|
|
358
|
+
await act(async () => {
|
|
359
|
+
(modals[0] as any).props.primaryButtonOnClick();
|
|
360
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
361
|
+
});
|
|
362
|
+
expect(generateCalls.length).toBe(1);
|
|
363
|
+
expect(generateCalls[0].locale).toBe("en");
|
|
364
|
+
expect(generateCalls[0].type).toBe("agreement");
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("surfaces generation errors via toast.catch", async () => {
|
|
368
|
+
generateImpl = async () => {
|
|
369
|
+
throw new Error("boom");
|
|
370
|
+
};
|
|
371
|
+
const {getByTestId, UNSAFE_root} = renderWithTheme(
|
|
372
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
|
|
373
|
+
);
|
|
374
|
+
await press(getByTestId("consent-form-generate-button"));
|
|
375
|
+
const modals = UNSAFE_root.findAll(
|
|
376
|
+
(n: any) => typeof n.props?.primaryButtonOnClick === "function"
|
|
377
|
+
);
|
|
378
|
+
await act(async () => {
|
|
379
|
+
(modals[0] as any).props.primaryButtonOnClick();
|
|
380
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
381
|
+
});
|
|
382
|
+
expect(generateCalls.length).toBe(1);
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it("translates from the default locale into the active locale", async () => {
|
|
386
|
+
state.formData = {
|
|
387
|
+
content: {en: "Hello world"},
|
|
388
|
+
defaultLocale: "en",
|
|
389
|
+
slug: "s",
|
|
390
|
+
title: "T",
|
|
391
|
+
};
|
|
392
|
+
const {UNSAFE_root} = renderWithTheme(
|
|
393
|
+
<ConsentFormEditor
|
|
394
|
+
api={makeApi() as any}
|
|
395
|
+
baseUrl="/admin"
|
|
396
|
+
hasAiSupport
|
|
397
|
+
id="f1"
|
|
398
|
+
supportedLocales={["en", "es"]}
|
|
399
|
+
/>
|
|
400
|
+
);
|
|
401
|
+
// Switch to non-default locale (es) so the Translate button appears.
|
|
402
|
+
const segmented = UNSAFE_root.findAll(
|
|
403
|
+
(n: any) => typeof n.props?.onChange === "function" && Array.isArray(n.props?.items)
|
|
404
|
+
);
|
|
405
|
+
expect(segmented.length).toBeGreaterThan(0);
|
|
406
|
+
await act(async () => {
|
|
407
|
+
(segmented[0] as any).props.onChange(1);
|
|
408
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
409
|
+
});
|
|
410
|
+
// Now find the translate button and press it.
|
|
411
|
+
const translateButton = UNSAFE_root.findAll(
|
|
412
|
+
(n: any) => n.props?.testID === "consent-form-translate-es-button"
|
|
413
|
+
);
|
|
414
|
+
expect(translateButton.length).toBeGreaterThan(0);
|
|
415
|
+
await act(async () => {
|
|
416
|
+
(translateButton[0] as any).props.onClick();
|
|
417
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
418
|
+
});
|
|
419
|
+
expect(translateCalls.length).toBe(1);
|
|
420
|
+
expect(translateCalls[0].toLocale).toBe("es");
|
|
421
|
+
expect(translateCalls[0].content).toBe("Hello world");
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("short-circuits translation when source locale has empty content", async () => {
|
|
425
|
+
state.formData = {
|
|
426
|
+
content: {en: " "},
|
|
427
|
+
defaultLocale: "en",
|
|
428
|
+
slug: "s",
|
|
429
|
+
title: "T",
|
|
430
|
+
};
|
|
431
|
+
const {UNSAFE_root} = renderWithTheme(
|
|
432
|
+
<ConsentFormEditor
|
|
433
|
+
api={makeApi() as any}
|
|
434
|
+
baseUrl="/admin"
|
|
435
|
+
hasAiSupport
|
|
436
|
+
id="f1"
|
|
437
|
+
supportedLocales={["en", "es"]}
|
|
438
|
+
/>
|
|
439
|
+
);
|
|
440
|
+
const segmented = UNSAFE_root.findAll(
|
|
441
|
+
(n: any) => typeof n.props?.onChange === "function" && Array.isArray(n.props?.items)
|
|
442
|
+
);
|
|
443
|
+
await act(async () => {
|
|
444
|
+
(segmented[0] as any).props.onChange(1);
|
|
445
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
446
|
+
});
|
|
447
|
+
const translateButton = UNSAFE_root.findAll(
|
|
448
|
+
(n: any) => n.props?.testID === "consent-form-translate-es-button"
|
|
449
|
+
);
|
|
450
|
+
await act(async () => {
|
|
451
|
+
(translateButton[0] as any).props.onClick();
|
|
452
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
453
|
+
});
|
|
454
|
+
expect(translateCalls.length).toBe(0);
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
it("surfaces translation errors via toast.catch", async () => {
|
|
458
|
+
translateImpl = async () => {
|
|
459
|
+
throw new Error("nope");
|
|
460
|
+
};
|
|
461
|
+
state.formData = {
|
|
462
|
+
content: {en: "Hello"},
|
|
463
|
+
defaultLocale: "en",
|
|
464
|
+
slug: "s",
|
|
465
|
+
title: "T",
|
|
466
|
+
};
|
|
467
|
+
const {UNSAFE_root} = renderWithTheme(
|
|
468
|
+
<ConsentFormEditor
|
|
469
|
+
api={makeApi() as any}
|
|
470
|
+
baseUrl="/admin"
|
|
471
|
+
hasAiSupport
|
|
472
|
+
id="f1"
|
|
473
|
+
supportedLocales={["en", "es"]}
|
|
474
|
+
/>
|
|
475
|
+
);
|
|
476
|
+
const segmented = UNSAFE_root.findAll(
|
|
477
|
+
(n: any) => typeof n.props?.onChange === "function" && Array.isArray(n.props?.items)
|
|
478
|
+
);
|
|
479
|
+
await act(async () => {
|
|
480
|
+
(segmented[0] as any).props.onChange(1);
|
|
481
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
482
|
+
});
|
|
483
|
+
const translateButton = UNSAFE_root.findAll(
|
|
484
|
+
(n: any) => n.props?.testID === "consent-form-translate-es-button"
|
|
485
|
+
);
|
|
486
|
+
await act(async () => {
|
|
487
|
+
(translateButton[0] as any).props.onClick();
|
|
488
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
489
|
+
});
|
|
490
|
+
expect(translateCalls.length).toBe(1);
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
it("dismisses the generate modal via secondary action", async () => {
|
|
494
|
+
const {getByTestId, UNSAFE_root} = renderWithTheme(
|
|
495
|
+
<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
|
|
496
|
+
);
|
|
497
|
+
await press(getByTestId("consent-form-generate-button"));
|
|
498
|
+
const modals = UNSAFE_root.findAll(
|
|
499
|
+
(n: any) => typeof n.props?.secondaryButtonOnClick === "function"
|
|
500
|
+
);
|
|
501
|
+
expect(modals.length).toBeGreaterThan(0);
|
|
502
|
+
await act(async () => {
|
|
503
|
+
(modals[0] as any).props.secondaryButtonOnClick();
|
|
504
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
505
|
+
});
|
|
506
|
+
await act(async () => {
|
|
507
|
+
(modals[0] as any).props.onDismiss?.();
|
|
508
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
509
|
+
});
|
|
510
|
+
expect(generateCalls.length).toBe(0);
|
|
511
|
+
});
|
|
512
|
+
});
|