@terreno/admin-frontend 0.14.0 → 0.14.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 (47) hide show
  1. package/dist/AdminObjectPicker.js +1 -0
  2. package/dist/AdminObjectPicker.js.map +1 -1
  3. package/dist/AdminVersionConfig.js +1 -0
  4. package/dist/AdminVersionConfig.js.map +1 -1
  5. package/dist/ConsentFormEditor.js +42 -28
  6. package/dist/ConsentFormEditor.js.map +1 -1
  7. package/dist/useAdminApi.js +1 -2
  8. package/dist/useAdminApi.js.map +1 -1
  9. package/dist/useAdminConfig.js +1 -0
  10. package/dist/useAdminConfig.js.map +1 -1
  11. package/dist/useAdminScripts.js +1 -0
  12. package/dist/useAdminScripts.js.map +1 -1
  13. package/dist/useConfigurationApi.js +1 -0
  14. package/dist/useConfigurationApi.js.map +1 -1
  15. package/dist/useDocumentStorageApi.js +1 -0
  16. package/dist/useDocumentStorageApi.js.map +1 -1
  17. package/package.json +2 -2
  18. package/src/AdminFieldRenderer.test.tsx +47 -22
  19. package/src/AdminModelForm.test.tsx +145 -40
  20. package/src/AdminModelList.test.tsx +13 -5
  21. package/src/AdminModelTable.test.tsx +57 -31
  22. package/src/AdminObjectPicker.tsx +1 -0
  23. package/src/AdminPrimitiveArrayField.test.tsx +13 -10
  24. package/src/AdminScriptList.test.tsx +6 -4
  25. package/src/AdminScriptRunModal.test.tsx +4 -2
  26. package/src/AdminVersionConfig.test.tsx +28 -23
  27. package/src/AdminVersionConfig.tsx +1 -0
  28. package/src/CheckboxListEditor.test.tsx +8 -6
  29. package/src/ConfigurationScreen.test.tsx +15 -11
  30. package/src/ConsentFormEditor.test.tsx +94 -75
  31. package/src/ConsentFormEditor.tsx +44 -33
  32. package/src/ConsentFormList.test.tsx +27 -10
  33. package/src/ConsentResponseViewer.test.tsx +14 -12
  34. package/src/DocumentStorageBrowser.test.tsx +68 -42
  35. package/src/LocaleContentEditor.test.tsx +27 -18
  36. package/src/isolated/AdminFieldRenderer.isolated.tsx +4 -2
  37. package/src/isolated/AdminNestedArrayField.isolated.tsx +48 -36
  38. package/src/isolated/AdminObjectPicker.isolated.tsx +30 -26
  39. package/src/isolated/AdminRefField.isolated.tsx +3 -1
  40. package/src/isolated/AdminScriptRunModal.isolated.tsx +23 -17
  41. package/src/isolated/DocumentStorageBrowser.isolated.tsx +79 -63
  42. package/src/isolated/hooks.isolated.tsx +39 -33
  43. package/src/useAdminApi.ts +1 -2
  44. package/src/useAdminConfig.ts +1 -0
  45. package/src/useAdminScripts.ts +1 -0
  46. package/src/useConfigurationApi.ts +1 -0
  47. package/src/useDocumentStorageApi.ts +1 -0
@@ -1,23 +1,25 @@
1
+ // noExplicitAny: test mocks use type-erased RTK Query API doubles and dynamic mock returns
1
2
  // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
2
3
  import {beforeEach, describe, expect, it, mock} from "bun:test";
3
4
  import {renderWithTheme} from "@terreno/ui/src/test-utils";
4
5
  import React from "react";
5
6
  import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
7
+ import type {AdminApi} from "./types";
6
8
 
7
9
  interface QueryState<T> {
8
10
  data: T | undefined;
9
11
  isLoading: boolean;
10
12
  }
11
- const metaState: QueryState<any> = {data: undefined, isLoading: false};
12
- const valuesState: QueryState<any> = {data: undefined, isLoading: false};
13
- let updateImpl: (body: any) => Promise<any> = async () => ({});
14
- const updateCalls: any[] = [];
13
+ const metaState: QueryState<unknown> = {data: undefined, isLoading: false};
14
+ const valuesState: QueryState<unknown> = {data: undefined, isLoading: false};
15
+ let updateImpl: (body: unknown) => Promise<unknown> = async () => ({});
16
+ const updateCalls: unknown[] = [];
15
17
 
16
18
  mock.module("./useConfigurationApi", () => ({
17
19
  useConfigurationApi: () => ({
18
20
  useMetaQuery: () => ({data: metaState.data, isLoading: metaState.isLoading}),
19
21
  useUpdateMutation: () => [
20
- (body: any) => ({
22
+ (body: unknown) => ({
21
23
  unwrap: async () => {
22
24
  updateCalls.push(body);
23
25
  return updateImpl(body);
@@ -72,12 +74,14 @@ describe("ConfigurationScreen", () => {
72
74
  it("renders loading state on initial load", () => {
73
75
  metaState.isLoading = true;
74
76
  valuesState.isLoading = true;
75
- const {toJSON} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
77
+ const {toJSON} = renderWithTheme(<ConfigurationScreen api={{} as unknown as AdminApi} />);
76
78
  expect(toJSON()).toBeDefined();
77
79
  });
78
80
 
79
81
  it("renders an empty-state when metadata is missing", () => {
80
- const {toJSON, getByText} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
82
+ const {toJSON, getByText} = renderWithTheme(
83
+ <ConfigurationScreen api={{} as unknown as AdminApi} />
84
+ );
81
85
  expect(toJSON()).toBeDefined();
82
86
  expect(getByText(/No configuration metadata available/)).toBeDefined();
83
87
  });
@@ -86,7 +90,7 @@ describe("ConfigurationScreen", () => {
86
90
  metaState.data = {sections: []};
87
91
  valuesState.data = {};
88
92
  const {getByText} = renderWithTheme(
89
- <ConfigurationScreen api={{} as any} basePath="/config" title="My Config" />
93
+ <ConfigurationScreen api={{} as unknown as AdminApi} basePath="/config" title="My Config" />
90
94
  );
91
95
  expect(getByText(/No configuration sections defined/)).toBeDefined();
92
96
  });
@@ -98,7 +102,7 @@ describe("ConfigurationScreen", () => {
98
102
  name: "Initial",
99
103
  smtp: {apiKey: "secret", host: "h", notes: "notes", port: 25},
100
104
  };
101
- const {getByTestId} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
105
+ const {getByTestId} = renderWithTheme(<ConfigurationScreen api={{} as unknown as AdminApi} />);
102
106
  await act(async () => {
103
107
  fireEvent.changeText(getByTestId("config-__root__-name"), "Updated");
104
108
  await new Promise((r) => setTimeout(r, 50));
@@ -117,7 +121,7 @@ describe("ConfigurationScreen", () => {
117
121
  updateImpl = async () => {
118
122
  throw new Error("nope");
119
123
  };
120
- const {getByTestId} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
124
+ const {getByTestId} = renderWithTheme(<ConfigurationScreen api={{} as unknown as AdminApi} />);
121
125
  await act(async () => {
122
126
  fireEvent.changeText(getByTestId("config-__root__-name"), "Boom");
123
127
  await new Promise((r) => setTimeout(r, 50));
@@ -132,7 +136,7 @@ describe("ConfigurationScreen", () => {
132
136
  it("accepts non-numeric number input and preserves the raw text", async () => {
133
137
  metaState.data = baseMeta;
134
138
  valuesState.data = {};
135
- const {getByTestId} = renderWithTheme(<ConfigurationScreen api={{} as any} />);
139
+ const {getByTestId} = renderWithTheme(<ConfigurationScreen api={{} as unknown as AdminApi} />);
136
140
  await act(async () => {
137
141
  fireEvent.changeText(getByTestId("config-smtp-port"), "not-a-number");
138
142
  await new Promise((r) => setTimeout(r, 50));
@@ -1,33 +1,41 @@
1
+ // noExplicitAny: test mocks use type-erased RTK Query API doubles and UNSAFE_root traversal
1
2
  // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
2
3
  import {beforeEach, describe, expect, it, mock} from "bun:test";
3
4
  import {renderWithTheme} from "@terreno/ui/src/test-utils";
4
5
  import React from "react";
6
+ import type {ReactTestInstance} from "react-test-renderer";
5
7
  import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
8
+ import type {AdminApi} from "./types";
6
9
 
7
10
  interface State {
8
- formData: any;
11
+ formData: Record<string, unknown> | null;
9
12
  isFormLoading: boolean;
10
13
  }
11
14
  const state: State = {formData: null, isFormLoading: false};
12
- const createCalls: any[] = [];
13
- const updateCalls: any[] = [];
15
+ const createCalls: unknown[] = [];
16
+ const updateCalls: unknown[] = [];
14
17
  const publishCalls: string[] = [];
15
- const generateCalls: any[] = [];
16
- const translateCalls: any[] = [];
17
- let createImpl: (body: any) => Promise<any> = async (b) => ({_id: "new-id", ...b});
18
- let updateImpl: (args: any) => Promise<any> = async (a) => ({_id: a.id});
19
- let publishImpl: (id: string) => Promise<any> = async () => ({});
20
- let generateImpl: (body: any) => Promise<any> = async () => ({
18
+ const generateCalls: unknown[] = [];
19
+ const translateCalls: unknown[] = [];
20
+ let createImpl: (body: unknown) => Promise<unknown> = async (b) => ({
21
+ _id: "new-id",
22
+ ...(b as Record<string, unknown>),
23
+ });
24
+ let updateImpl: (args: unknown) => Promise<unknown> = async (a) => ({
25
+ _id: (a as Record<string, unknown>).id,
26
+ });
27
+ let publishImpl: (id: string) => Promise<unknown> = async () => ({});
28
+ let generateImpl: (body: unknown) => Promise<unknown> = async () => ({
21
29
  data: {content: "generated"},
22
30
  });
23
- let translateImpl: (body: any) => Promise<any> = async () => ({
31
+ let translateImpl: (body: unknown) => Promise<unknown> = async () => ({
24
32
  data: {content: "translated"},
25
33
  });
26
34
 
27
35
  mock.module("./useAdminApi", () => ({
28
36
  useAdminApi: () => ({
29
37
  useCreateMutation: () => [
30
- (body: any) => ({
38
+ (body: unknown) => ({
31
39
  unwrap: async () => {
32
40
  createCalls.push(body);
33
41
  return createImpl(body);
@@ -42,7 +50,7 @@ mock.module("./useAdminApi", () => ({
42
50
  return {data: state.formData, isLoading: state.isFormLoading};
43
51
  },
44
52
  useUpdateMutation: () => [
45
- (args: any) => ({
53
+ (args: unknown) => ({
46
54
  unwrap: async () => {
47
55
  updateCalls.push(args);
48
56
  return updateImpl(args);
@@ -53,11 +61,11 @@ mock.module("./useAdminApi", () => ({
53
61
  }),
54
62
  }));
55
63
 
56
- const mutationSpecs: any[] = [];
64
+ const mutationSpecs: unknown[] = [];
57
65
  const makeApi = () => ({
58
- injectEndpoints: ({endpoints}: {endpoints: (b: any) => Record<string, any>}) => {
66
+ injectEndpoints: ({endpoints}: {endpoints: (b: unknown) => Record<string, unknown>}) => {
59
67
  endpoints({
60
- mutation: (spec: any) => {
68
+ mutation: (spec: Record<string, unknown>) => {
61
69
  // Invoke the mutation query lambda with both an object body and a string
62
70
  // id so we exercise the URL + body builders for every mutation shape
63
71
  // (generate/translate take a body object, publish takes an id string).
@@ -76,11 +84,11 @@ const makeApi = () => ({
76
84
  }
77
85
  return spec;
78
86
  },
79
- query: (spec: any) => spec,
87
+ query: (spec: unknown) => spec,
80
88
  });
81
89
  return {
82
90
  useGenerateConsentContentMutation: () => [
83
- (body: any) => ({
91
+ (body: unknown) => ({
84
92
  unwrap: async () => {
85
93
  generateCalls.push(body);
86
94
  return generateImpl(body);
@@ -98,7 +106,7 @@ const makeApi = () => ({
98
106
  {isLoading: false},
99
107
  ],
100
108
  useTranslateConsentContentMutation: () => [
101
- (body: any) => ({
109
+ (body: unknown) => ({
102
110
  unwrap: async () => {
103
111
  translateCalls.push(body);
104
112
  return translateImpl(body);
@@ -112,7 +120,7 @@ const makeApi = () => ({
112
120
 
113
121
  import {ConsentFormEditor} from "./ConsentFormEditor";
114
122
 
115
- const press = async (el: any): Promise<void> => {
123
+ const press = async (el: ReactTestInstance): Promise<void> => {
116
124
  await act(async () => {
117
125
  fireEvent.press(el);
118
126
  await new Promise((r) => setTimeout(r, 150));
@@ -129,8 +137,8 @@ describe("ConsentFormEditor", () => {
129
137
  generateCalls.length = 0;
130
138
  translateCalls.length = 0;
131
139
  mutationSpecs.length = 0;
132
- createImpl = async (b) => ({_id: "new-id", ...b});
133
- updateImpl = async (a) => ({_id: a.id});
140
+ createImpl = async (b) => ({_id: "new-id", ...(b as Record<string, unknown>)});
141
+ updateImpl = async (a) => ({_id: (a as Record<string, unknown>).id});
134
142
  publishImpl = async () => ({});
135
143
  generateImpl = async () => ({data: {content: "generated"}});
136
144
  translateImpl = async () => ({data: {content: "translated"}});
@@ -139,7 +147,7 @@ describe("ConsentFormEditor", () => {
139
147
  it("renders loading state in edit mode", () => {
140
148
  state.isFormLoading = true;
141
149
  const {toJSON} = renderWithTheme(
142
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" id="f1" />
150
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" id="f1" />
143
151
  );
144
152
  expect(toJSON()).toBeDefined();
145
153
  });
@@ -154,7 +162,7 @@ describe("ConsentFormEditor", () => {
154
162
  state.isFormLoading = true;
155
163
  const {rerender, getByTestId} = renderWithTheme(
156
164
  <ConsentFormEditor
157
- api={makeApi() as any}
165
+ api={makeApi() as unknown as AdminApi}
158
166
  baseUrl="/admin"
159
167
  id="f1"
160
168
  supportedLocales={supportedLocales}
@@ -180,7 +188,7 @@ describe("ConsentFormEditor", () => {
180
188
  };
181
189
  rerender(
182
190
  <ConsentFormEditor
183
- api={makeApi() as any}
191
+ api={makeApi() as unknown as AdminApi}
184
192
  baseUrl="/admin"
185
193
  id="f1"
186
194
  supportedLocales={supportedLocales}
@@ -192,23 +200,23 @@ describe("ConsentFormEditor", () => {
192
200
 
193
201
  it("renders create mode with default values", () => {
194
202
  const {getByTestId} = renderWithTheme(
195
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
203
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" />
196
204
  );
197
205
  expect(getByTestId("consent-form-title-input")).toBeDefined();
198
206
  });
199
207
 
200
208
  it("fails validation without title or slug", async () => {
201
209
  const {getByTestId} = renderWithTheme(
202
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
210
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" />
203
211
  );
204
212
  await press(getByTestId("consent-form-save-button"));
205
213
  expect(createCalls.length).toBe(0);
206
214
  });
207
215
 
208
216
  it("auto-slugs titles and creates a form on save", async () => {
209
- const onSave = mock((_: any) => undefined);
217
+ const onSave = mock((_: unknown) => undefined);
210
218
  const {getByTestId} = renderWithTheme(
211
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" onSave={onSave} />
219
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" onSave={onSave} />
212
220
  );
213
221
  await act(async () => {
214
222
  fireEvent.changeText(getByTestId("consent-form-title-input"), "Hello World");
@@ -216,7 +224,7 @@ describe("ConsentFormEditor", () => {
216
224
  });
217
225
  await press(getByTestId("consent-form-save-button"));
218
226
  expect(createCalls.length).toBe(1);
219
- expect(createCalls[0].slug).toBe("hello-world");
227
+ expect((createCalls[0] as Record<string, unknown>).slug).toBe("hello-world");
220
228
  expect(onSave).toHaveBeenCalled();
221
229
  });
222
230
 
@@ -225,7 +233,7 @@ describe("ConsentFormEditor", () => {
225
233
  throw new Error("conflict");
226
234
  };
227
235
  const {getByTestId} = renderWithTheme(
228
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
236
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" />
229
237
  );
230
238
  await act(async () => {
231
239
  fireEvent.changeText(getByTestId("consent-form-title-input"), "Title");
@@ -237,7 +245,7 @@ describe("ConsentFormEditor", () => {
237
245
  });
238
246
  await press(getByTestId("consent-form-save-button"));
239
247
  expect(createCalls.length).toBe(1);
240
- expect(createCalls[0].slug).toBe("custom-slug");
248
+ expect((createCalls[0] as Record<string, unknown>).slug).toBe("custom-slug");
241
249
  });
242
250
 
243
251
  it("loads form data in edit mode and saves an update", async () => {
@@ -259,7 +267,7 @@ describe("ConsentFormEditor", () => {
259
267
  };
260
268
  const {getByTestId} = renderWithTheme(
261
269
  <ConsentFormEditor
262
- api={makeApi() as any}
270
+ api={makeApi() as unknown as AdminApi}
263
271
  baseUrl="/admin"
264
272
  hasAiSupport
265
273
  id="f1"
@@ -268,14 +276,16 @@ describe("ConsentFormEditor", () => {
268
276
  );
269
277
  await press(getByTestId("consent-form-save-button"));
270
278
  expect(updateCalls.length).toBe(1);
271
- expect(updateCalls[0].id).toBe("f1");
272
- expect(updateCalls[0].body.title).toBe("Privacy");
279
+ expect((updateCalls[0] as Record<string, unknown>).id).toBe("f1");
280
+ expect(
281
+ ((updateCalls[0] as Record<string, unknown>).body as Record<string, unknown>).title
282
+ ).toBe("Privacy");
273
283
  });
274
284
 
275
285
  it("publishes a form on publish press", async () => {
276
286
  state.formData = {slug: "s", title: "T"};
277
287
  const {getByTestId} = renderWithTheme(
278
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" id="f1" />
288
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" id="f1" />
279
289
  );
280
290
  await press(getByTestId("consent-form-publish-button"));
281
291
  expect(publishCalls).toEqual(["f1"]);
@@ -287,7 +297,7 @@ describe("ConsentFormEditor", () => {
287
297
  throw new Error("fail");
288
298
  };
289
299
  const {getByTestId} = renderWithTheme(
290
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" id="f1" />
300
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" id="f1" />
291
301
  );
292
302
  await press(getByTestId("consent-form-publish-button"));
293
303
  expect(publishCalls.length).toBe(1);
@@ -295,7 +305,7 @@ describe("ConsentFormEditor", () => {
295
305
 
296
306
  it("opens the AI generate modal in create mode", async () => {
297
307
  const {getByTestId, toJSON} = renderWithTheme(
298
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
308
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" hasAiSupport />
299
309
  );
300
310
  await press(getByTestId("consent-form-generate-button"));
301
311
  expect(toJSON()).toBeDefined();
@@ -303,7 +313,7 @@ describe("ConsentFormEditor", () => {
303
313
 
304
314
  it("adds, edits, and removes checkboxes", async () => {
305
315
  const {getByTestId, queryByTestId} = renderWithTheme(
306
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
316
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" />
307
317
  );
308
318
  await press(getByTestId("consent-form-add-checkbox-button"));
309
319
  expect(getByTestId("consent-form-checkbox-0")).toBeDefined();
@@ -327,7 +337,7 @@ describe("ConsentFormEditor", () => {
327
337
  title: "T",
328
338
  };
329
339
  const {getByTestId} = renderWithTheme(
330
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" id="f1" />
340
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" id="f1" />
331
341
  );
332
342
  expect(getByTestId("consent-form-decline-button-text-input")).toBeDefined();
333
343
  });
@@ -335,7 +345,7 @@ describe("ConsentFormEditor", () => {
335
345
  it("renders and switches segmented control in multi-locale mode", async () => {
336
346
  const {toJSON} = renderWithTheme(
337
347
  <ConsentFormEditor
338
- api={makeApi() as any}
348
+ api={makeApi() as unknown as AdminApi}
339
349
  baseUrl="/admin"
340
350
  hasAiSupport
341
351
  supportedLocales={["en", "es"]}
@@ -347,7 +357,11 @@ describe("ConsentFormEditor", () => {
347
357
  it("calls onCancel when cancel is pressed", async () => {
348
358
  const onCancel = mock(() => undefined);
349
359
  const {getByTestId} = renderWithTheme(
350
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" onCancel={onCancel} />
360
+ <ConsentFormEditor
361
+ api={makeApi() as unknown as AdminApi}
362
+ baseUrl="/admin"
363
+ onCancel={onCancel}
364
+ />
351
365
  );
352
366
  await press(getByTestId("consent-form-cancel-button"));
353
367
  expect(onCancel).toHaveBeenCalled();
@@ -357,16 +371,18 @@ describe("ConsentFormEditor", () => {
357
371
  // Create mode has no id → publish button shouldn't exist, but exercising
358
372
  // the handler via the save flow at least touches the non-id path.
359
373
  const {queryByTestId} = renderWithTheme(
360
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
374
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" hasAiSupport />
361
375
  );
362
376
  expect(queryByTestId("consent-form-publish-button")).toBeNull();
363
377
  });
364
378
 
365
379
  it("wires all three mutations (generate/publish/translate) to the right URLs", () => {
366
- renderWithTheme(<ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />);
380
+ renderWithTheme(
381
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" hasAiSupport />
382
+ );
367
383
  // The mock invokes each mutation's query lambda, so mutationSpecs contains
368
384
  // the resolved request descriptors for generate, publish, and translate.
369
- const urls = mutationSpecs.map((s: any) => s.url).sort();
385
+ const urls = mutationSpecs.map((s: unknown) => (s as Record<string, unknown>).url).sort();
370
386
  expect(urls).toContain("/consent-forms/generate");
371
387
  expect(urls).toContain("/consent-forms/form-id/publish");
372
388
  expect(urls).toContain("/consent-forms/translate");
@@ -374,7 +390,7 @@ describe("ConsentFormEditor", () => {
374
390
 
375
391
  it("refuses to save without a slug even when title is present", async () => {
376
392
  const {getByTestId} = renderWithTheme(
377
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" />
393
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" />
378
394
  );
379
395
  // Set a title so slug would auto-generate.
380
396
  await act(async () => {
@@ -392,23 +408,23 @@ describe("ConsentFormEditor", () => {
392
408
 
393
409
  it("generates AI content into the active locale and closes the modal", async () => {
394
410
  const {getByTestId, UNSAFE_root} = renderWithTheme(
395
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
411
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" hasAiSupport />
396
412
  );
397
413
  await press(getByTestId("consent-form-generate-button"));
398
414
  // Find the Modal's primary "Generate" button and invoke it directly. The
399
415
  // description input lives inside the Modal's portal and may not be
400
416
  // mounted; invoking primaryButtonOnClick still exercises handleGenerate.
401
417
  const modals = UNSAFE_root.findAll(
402
- (n: any) => typeof n.props?.primaryButtonOnClick === "function"
418
+ (n: ReactTestInstance) => typeof n.props?.primaryButtonOnClick === "function"
403
419
  );
404
420
  expect(modals.length).toBeGreaterThan(0);
405
421
  await act(async () => {
406
- (modals[0] as any).props.primaryButtonOnClick();
422
+ (modals[0] as ReactTestInstance).props.primaryButtonOnClick();
407
423
  await new Promise((r) => setTimeout(r, 150));
408
424
  });
409
425
  expect(generateCalls.length).toBe(1);
410
- expect(generateCalls[0].locale).toBe("en");
411
- expect(generateCalls[0].type).toBe("agreement");
426
+ expect((generateCalls[0] as Record<string, unknown>).locale).toBe("en");
427
+ expect((generateCalls[0] as Record<string, unknown>).type).toBe("agreement");
412
428
  });
413
429
 
414
430
  it("surfaces generation errors via toast.catch", async () => {
@@ -416,14 +432,14 @@ describe("ConsentFormEditor", () => {
416
432
  throw new Error("boom");
417
433
  };
418
434
  const {getByTestId, UNSAFE_root} = renderWithTheme(
419
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
435
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" hasAiSupport />
420
436
  );
421
437
  await press(getByTestId("consent-form-generate-button"));
422
438
  const modals = UNSAFE_root.findAll(
423
- (n: any) => typeof n.props?.primaryButtonOnClick === "function"
439
+ (n: ReactTestInstance) => typeof n.props?.primaryButtonOnClick === "function"
424
440
  );
425
441
  await act(async () => {
426
- (modals[0] as any).props.primaryButtonOnClick();
442
+ (modals[0] as ReactTestInstance).props.primaryButtonOnClick();
427
443
  await new Promise((r) => setTimeout(r, 150));
428
444
  });
429
445
  expect(generateCalls.length).toBe(1);
@@ -438,7 +454,7 @@ describe("ConsentFormEditor", () => {
438
454
  };
439
455
  const {UNSAFE_root} = renderWithTheme(
440
456
  <ConsentFormEditor
441
- api={makeApi() as any}
457
+ api={makeApi() as unknown as AdminApi}
442
458
  baseUrl="/admin"
443
459
  hasAiSupport
444
460
  id="f1"
@@ -447,25 +463,26 @@ describe("ConsentFormEditor", () => {
447
463
  );
448
464
  // Switch to non-default locale (es) so the Translate button appears.
449
465
  const segmented = UNSAFE_root.findAll(
450
- (n: any) => typeof n.props?.onChange === "function" && Array.isArray(n.props?.items)
466
+ (n: ReactTestInstance) =>
467
+ typeof n.props?.onChange === "function" && Array.isArray(n.props?.items)
451
468
  );
452
469
  expect(segmented.length).toBeGreaterThan(0);
453
470
  await act(async () => {
454
- (segmented[0] as any).props.onChange(1);
471
+ (segmented[0] as ReactTestInstance).props.onChange(1);
455
472
  await new Promise((r) => setTimeout(r, 50));
456
473
  });
457
- // Now find the translate button and press it.
474
+ // Find the translate button and press it
458
475
  const translateButton = UNSAFE_root.findAll(
459
- (n: any) => n.props?.testID === "consent-form-translate-es-button"
476
+ (n: ReactTestInstance) => n.props?.testID === "consent-form-translate-es-button"
460
477
  );
461
478
  expect(translateButton.length).toBeGreaterThan(0);
462
479
  await act(async () => {
463
- (translateButton[0] as any).props.onClick();
480
+ (translateButton[0] as ReactTestInstance).props.onClick();
464
481
  await new Promise((r) => setTimeout(r, 150));
465
482
  });
466
483
  expect(translateCalls.length).toBe(1);
467
- expect(translateCalls[0].toLocale).toBe("es");
468
- expect(translateCalls[0].content).toBe("Hello world");
484
+ expect((translateCalls[0] as Record<string, unknown>).toLocale).toBe("es");
485
+ expect((translateCalls[0] as Record<string, unknown>).content).toBe("Hello world");
469
486
  });
470
487
 
471
488
  it("short-circuits translation when source locale has empty content", async () => {
@@ -477,7 +494,7 @@ describe("ConsentFormEditor", () => {
477
494
  };
478
495
  const {UNSAFE_root} = renderWithTheme(
479
496
  <ConsentFormEditor
480
- api={makeApi() as any}
497
+ api={makeApi() as unknown as AdminApi}
481
498
  baseUrl="/admin"
482
499
  hasAiSupport
483
500
  id="f1"
@@ -485,17 +502,18 @@ describe("ConsentFormEditor", () => {
485
502
  />
486
503
  );
487
504
  const segmented = UNSAFE_root.findAll(
488
- (n: any) => typeof n.props?.onChange === "function" && Array.isArray(n.props?.items)
505
+ (n: ReactTestInstance) =>
506
+ typeof n.props?.onChange === "function" && Array.isArray(n.props?.items)
489
507
  );
490
508
  await act(async () => {
491
- (segmented[0] as any).props.onChange(1);
509
+ (segmented[0] as ReactTestInstance).props.onChange(1);
492
510
  await new Promise((r) => setTimeout(r, 50));
493
511
  });
494
512
  const translateButton = UNSAFE_root.findAll(
495
- (n: any) => n.props?.testID === "consent-form-translate-es-button"
513
+ (n: ReactTestInstance) => n.props?.testID === "consent-form-translate-es-button"
496
514
  );
497
515
  await act(async () => {
498
- (translateButton[0] as any).props.onClick();
516
+ (translateButton[0] as ReactTestInstance).props.onClick();
499
517
  await new Promise((r) => setTimeout(r, 150));
500
518
  });
501
519
  expect(translateCalls.length).toBe(0);
@@ -513,7 +531,7 @@ describe("ConsentFormEditor", () => {
513
531
  };
514
532
  const {UNSAFE_root} = renderWithTheme(
515
533
  <ConsentFormEditor
516
- api={makeApi() as any}
534
+ api={makeApi() as unknown as AdminApi}
517
535
  baseUrl="/admin"
518
536
  hasAiSupport
519
537
  id="f1"
@@ -521,17 +539,18 @@ describe("ConsentFormEditor", () => {
521
539
  />
522
540
  );
523
541
  const segmented = UNSAFE_root.findAll(
524
- (n: any) => typeof n.props?.onChange === "function" && Array.isArray(n.props?.items)
542
+ (n: ReactTestInstance) =>
543
+ typeof n.props?.onChange === "function" && Array.isArray(n.props?.items)
525
544
  );
526
545
  await act(async () => {
527
- (segmented[0] as any).props.onChange(1);
546
+ (segmented[0] as ReactTestInstance).props.onChange(1);
528
547
  await new Promise((r) => setTimeout(r, 50));
529
548
  });
530
549
  const translateButton = UNSAFE_root.findAll(
531
- (n: any) => n.props?.testID === "consent-form-translate-es-button"
550
+ (n: ReactTestInstance) => n.props?.testID === "consent-form-translate-es-button"
532
551
  );
533
552
  await act(async () => {
534
- (translateButton[0] as any).props.onClick();
553
+ (translateButton[0] as ReactTestInstance).props.onClick();
535
554
  await new Promise((r) => setTimeout(r, 150));
536
555
  });
537
556
  expect(translateCalls.length).toBe(1);
@@ -539,19 +558,19 @@ describe("ConsentFormEditor", () => {
539
558
 
540
559
  it("dismisses the generate modal via secondary action", async () => {
541
560
  const {getByTestId, UNSAFE_root} = renderWithTheme(
542
- <ConsentFormEditor api={makeApi() as any} baseUrl="/admin" hasAiSupport />
561
+ <ConsentFormEditor api={makeApi() as unknown as AdminApi} baseUrl="/admin" hasAiSupport />
543
562
  );
544
563
  await press(getByTestId("consent-form-generate-button"));
545
564
  const modals = UNSAFE_root.findAll(
546
- (n: any) => typeof n.props?.secondaryButtonOnClick === "function"
565
+ (n: ReactTestInstance) => typeof n.props?.secondaryButtonOnClick === "function"
547
566
  );
548
567
  expect(modals.length).toBeGreaterThan(0);
549
568
  await act(async () => {
550
- (modals[0] as any).props.secondaryButtonOnClick();
569
+ (modals[0] as ReactTestInstance).props.secondaryButtonOnClick();
551
570
  await new Promise((r) => setTimeout(r, 50));
552
571
  });
553
572
  await act(async () => {
554
- (modals[0] as any).props.onDismiss?.();
573
+ (modals[0] as ReactTestInstance).props.onDismiss?.();
555
574
  await new Promise((r) => setTimeout(r, 50));
556
575
  });
557
576
  expect(generateCalls.length).toBe(0);
@@ -73,6 +73,49 @@ const slugify = (text: string): string => {
73
73
  .replace(/^-+|-+$/g, "");
74
74
  };
75
75
 
76
+ /**
77
+ * Cache the enhanced api per api object. `injectEndpoints` is a side-effecting
78
+ * operation against the global RTK Query slice — calling it on every render of
79
+ * every editor instance is wasteful, and with `overrideExisting: false` would
80
+ * log a re-injection warning. WeakMap-by-api lets the GC reclaim entries when
81
+ * the api is unreachable.
82
+ */
83
+ const enhancedApiCache = new WeakMap<AdminApi, unknown>();
84
+
85
+ const getEnhancedApi = (api: AdminApi): unknown => {
86
+ const cached = enhancedApiCache.get(api);
87
+ if (cached) {
88
+ return cached;
89
+ }
90
+ const enhanced = api.injectEndpoints({
91
+ endpoints: (build: EndpointBuilder) => ({
92
+ generateConsentContent: build.mutation({
93
+ query: (body: {type: string; description: string; locale: string}) => ({
94
+ body,
95
+ method: "POST",
96
+ url: `${CONSENT_FORM_ROUTE}/generate`,
97
+ }),
98
+ }),
99
+ publishConsentForm: build.mutation({
100
+ query: (formId: string) => ({
101
+ method: "POST",
102
+ url: `${CONSENT_FORM_ROUTE}/${formId}/publish`,
103
+ }),
104
+ }),
105
+ translateConsentContent: build.mutation({
106
+ query: (body: {content: string; fromLocale: string; toLocale: string}) => ({
107
+ body,
108
+ method: "POST",
109
+ url: `${CONSENT_FORM_ROUTE}/translate`,
110
+ }),
111
+ }),
112
+ }),
113
+ overrideExisting: false,
114
+ });
115
+ enhancedApiCache.set(api, enhanced);
116
+ return enhanced;
117
+ };
118
+
76
119
  export const ConsentFormEditor: React.FC<ConsentFormEditorProps> = ({
77
120
  baseUrl,
78
121
  api,
@@ -125,8 +168,6 @@ export const ConsentFormEditor: React.FC<ConsentFormEditorProps> = ({
125
168
  const [generateType, setGenerateType] = useState(type);
126
169
 
127
170
  const routePath = `${baseUrl}${CONSENT_FORM_ROUTE}`;
128
- // publish/generate/translate are registered directly under /consent-forms, not the admin base path
129
- const consentApiPath = CONSENT_FORM_ROUTE;
130
171
 
131
172
  const {useReadQuery, useCreateMutation, useUpdateMutation} = useAdminApi(
132
173
  api,
@@ -134,38 +175,8 @@ export const ConsentFormEditor: React.FC<ConsentFormEditorProps> = ({
134
175
  "ConsentForm"
135
176
  );
136
177
 
137
- const enhancedApi = useMemo(
138
- () =>
139
- api.injectEndpoints({
140
- endpoints: (build: EndpointBuilder) => ({
141
- generateConsentContent: build.mutation({
142
- query: (body: {type: string; description: string; locale: string}) => ({
143
- body,
144
- method: "POST",
145
- url: `${consentApiPath}/generate`,
146
- }),
147
- }),
148
- publishConsentForm: build.mutation({
149
- query: (formId: string) => ({
150
- method: "POST",
151
- url: `${consentApiPath}/${formId}/publish`,
152
- }),
153
- }),
154
- translateConsentContent: build.mutation({
155
- query: (body: {content: string; fromLocale: string; toLocale: string}) => ({
156
- body,
157
- method: "POST",
158
- url: `${consentApiPath}/translate`,
159
- }),
160
- }),
161
- }),
162
- overrideExisting: true,
163
- }),
164
- [api, consentApiPath]
165
- );
166
-
167
178
  // biome-ignore lint/suspicious/noExplicitAny: dynamic hook lookup on RTK Query enhanced API
168
- const enhanced = enhancedApi as any;
179
+ const enhanced = getEnhancedApi(api) as any;
169
180
  const [publishConsentForm, {isLoading: isPublishing}] = enhanced.usePublishConsentFormMutation();
170
181
  const [generateContent, {isLoading: isGenerating}] = enhanced.useGenerateConsentContentMutation();
171
182
  const [translateContent, {isLoading: isTranslating}] =