@terreno/ui 57.21.0 → 58.0.0-beta.2
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/.ai/guidelines/core.md +1 -1
- package/CHANGELOG.md +89 -0
- package/dist/ActionSheet.d.ts +9 -9
- package/dist/ActionSheet.d.ts.map +1 -1
- package/dist/ActionSheet.js +11 -4
- package/dist/ActionSheet.js.map +1 -1
- package/dist/AnnouncementNavigator.d.ts +7 -1
- package/dist/AnnouncementNavigator.d.ts.map +1 -1
- package/dist/AnnouncementNavigator.js +15 -16
- package/dist/AnnouncementNavigator.js.map +1 -1
- package/dist/Avatar.d.ts.map +1 -1
- package/dist/Avatar.js +1 -1
- package/dist/Avatar.js.map +1 -1
- package/dist/Box.d.ts.map +1 -1
- package/dist/Box.js +3 -1
- package/dist/Box.js.map +1 -1
- package/dist/Common.d.ts +16 -12
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/ConsentFormScreen.d.ts.map +1 -1
- package/dist/ConsentFormScreen.js.map +1 -1
- package/dist/ConsentNavigator.d.ts +5 -3
- package/dist/ConsentNavigator.d.ts.map +1 -1
- package/dist/ConsentNavigator.js +4 -4
- package/dist/ConsentNavigator.js.map +1 -1
- package/dist/CustomSelectField.js.map +1 -1
- package/dist/DataTable.d.ts.map +1 -1
- package/dist/DataTable.js +37 -12
- package/dist/DataTable.js.map +1 -1
- package/dist/DateTimeField.js.map +1 -1
- package/dist/DropdownPanel.d.ts +0 -5
- package/dist/DropdownPanel.d.ts.map +1 -1
- package/dist/DropdownPanel.js +0 -5
- package/dist/DropdownPanel.js.map +1 -1
- package/dist/FilterAccordion.d.ts.map +1 -1
- package/dist/FilterAccordion.js.map +1 -1
- package/dist/FilterBoolean.d.ts.map +1 -1
- package/dist/FilterBoolean.js.map +1 -1
- package/dist/FilterSelectMenu.js +1 -1
- package/dist/FilterSelectMenu.js.map +1 -1
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js.map +1 -1
- package/dist/Heading.d.ts.map +1 -1
- package/dist/Heading.js +6 -12
- package/dist/Heading.js.map +1 -1
- package/dist/HeightField.d.ts.map +1 -1
- package/dist/HeightField.js +1 -1
- package/dist/HeightField.js.map +1 -1
- package/dist/Hyperlink.d.ts.map +1 -1
- package/dist/Hyperlink.js +6 -1
- package/dist/Hyperlink.js.map +1 -1
- package/dist/Image.d.ts +1 -1
- package/dist/MarkdownEditorField.d.ts.map +1 -1
- package/dist/MarkdownEditorField.js +1 -1
- package/dist/MarkdownEditorField.js.map +1 -1
- package/dist/OfflineBanner.d.ts +3 -2
- package/dist/OfflineBanner.d.ts.map +1 -1
- package/dist/OfflineBanner.js +3 -2
- package/dist/OfflineBanner.js.map +1 -1
- package/dist/OtpField.d.ts.map +1 -1
- package/dist/OtpField.js +6 -3
- package/dist/OtpField.js.map +1 -1
- package/dist/PickerSelect.d.ts +4 -4
- package/dist/PickerSelect.d.ts.map +1 -1
- package/dist/PickerSelect.js.map +1 -1
- package/dist/ProvisionalConsentSigner.d.ts +3 -2
- package/dist/ProvisionalConsentSigner.d.ts.map +1 -1
- package/dist/ProvisionalConsentSigner.js +2 -2
- package/dist/ProvisionalConsentSigner.js.map +1 -1
- package/dist/SidebarNavigation.js +2 -2
- package/dist/SidebarNavigation.js.map +1 -1
- package/dist/SidebarNavigation.native.js +2 -2
- package/dist/SidebarNavigation.native.js.map +1 -1
- package/dist/Skeleton.js.map +1 -1
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +27 -50
- package/dist/Text.js.map +1 -1
- package/dist/TextField.d.ts.map +1 -1
- package/dist/TextField.js.map +1 -1
- package/dist/ThumbsUpDownFeedback.d.ts.map +1 -1
- package/dist/ThumbsUpDownFeedback.js.map +1 -1
- package/dist/ToastNotifications.d.ts.map +1 -1
- package/dist/ToastNotifications.js.map +1 -1
- package/dist/Tooltip.d.ts.map +1 -1
- package/dist/Tooltip.js.map +1 -1
- package/dist/Unifier.d.ts +1 -1
- package/dist/WebAddressAutocomplete.js.map +1 -1
- package/dist/WebDropdownMenu.d.ts +3 -3
- package/dist/WebDropdownMenu.d.ts.map +1 -1
- package/dist/WebDropdownMenu.js +1 -1
- package/dist/WebDropdownMenu.js.map +1 -1
- package/dist/announcementPlatform.d.ts +5 -0
- package/dist/announcementPlatform.d.ts.map +1 -1
- package/dist/announcementPlatform.js +9 -0
- package/dist/announcementPlatform.js.map +1 -1
- package/dist/asks/AskCard.d.ts +2 -2
- package/dist/asks/AskCard.d.ts.map +1 -1
- package/dist/dataTableColumnWidths.d.ts +76 -0
- package/dist/dataTableColumnWidths.d.ts.map +1 -0
- package/dist/dataTableColumnWidths.js +181 -0
- package/dist/dataTableColumnWidths.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/login/LoginScreen.js.map +1 -1
- package/dist/requestClient.d.ts +71 -0
- package/dist/requestClient.d.ts.map +1 -0
- package/dist/requestClient.js +139 -0
- package/dist/requestClient.js.map +1 -0
- package/dist/security/PasswordCard.d.ts.map +1 -1
- package/dist/security/PasswordCard.js.map +1 -1
- package/dist/security/TwoFactorSettingsCard.d.ts.map +1 -1
- package/dist/security/TwoFactorSettingsCard.js +1 -1
- package/dist/security/TwoFactorSettingsCard.js.map +1 -1
- package/dist/signUp/SignUpScreen.js.map +1 -1
- package/dist/table/Table.d.ts.map +1 -1
- package/dist/table/Table.js.map +1 -1
- package/dist/table/TableTitle.d.ts.map +1 -1
- package/dist/table/TableTitle.js +2 -1
- package/dist/table/TableTitle.js.map +1 -1
- package/dist/textFontSizes.d.ts +12 -0
- package/dist/textFontSizes.d.ts.map +1 -0
- package/dist/textFontSizes.js +21 -0
- package/dist/textFontSizes.js.map +1 -0
- package/dist/useAcknowledgeAnnouncement.d.ts +8 -63
- package/dist/useAcknowledgeAnnouncement.d.ts.map +1 -1
- package/dist/useAcknowledgeAnnouncement.js +31 -80
- package/dist/useAcknowledgeAnnouncement.js.map +1 -1
- package/dist/useAnnouncements.d.ts +14 -47
- package/dist/useAnnouncements.d.ts.map +1 -1
- package/dist/useAnnouncements.js +30 -57
- package/dist/useAnnouncements.js.map +1 -1
- package/dist/useConsentForms.d.ts +6 -38
- package/dist/useConsentForms.d.ts.map +1 -1
- package/dist/useConsentForms.js +25 -48
- package/dist/useConsentForms.js.map +1 -1
- package/dist/useProvisionalConsent.d.ts +7 -58
- package/dist/useProvisionalConsent.d.ts.map +1 -1
- package/dist/useProvisionalConsent.js +57 -67
- package/dist/useProvisionalConsent.js.map +1 -1
- package/dist/useSubmitConsent.d.ts +9 -39
- package/dist/useSubmitConsent.d.ts.map +1 -1
- package/dist/useSubmitConsent.js +10 -39
- package/dist/useSubmitConsent.js.map +1 -1
- package/package.json +38 -38
- package/src/ActionSheet.test.tsx +4 -4
- package/src/ActionSheet.tsx +18 -5
- package/src/AnnouncementNavigator.test.tsx +215 -315
- package/src/AnnouncementNavigator.tsx +25 -20
- package/src/Avatar.tsx +2 -9
- package/src/Box.test.tsx +3 -3
- package/src/Box.tsx +4 -2
- package/src/Common.ts +19 -15
- package/src/ConsentFormScreen.tsx +2 -1
- package/src/ConsentNavigator.test.tsx +166 -242
- package/src/ConsentNavigator.tsx +9 -13
- package/src/CustomSelectField.tsx +3 -3
- package/src/DataTable.test.tsx +69 -1
- package/src/DataTable.tsx +72 -19
- package/src/DateTimeField.tsx +4 -4
- package/src/DropdownPanel.tsx +0 -6
- package/src/FilterAccordion.tsx +3 -3
- package/src/FilterBoolean.tsx +3 -3
- package/src/FilterSelectMenu.tsx +1 -1
- package/src/GPTChat.tsx +10 -9
- package/src/Heading.tsx +7 -14
- package/src/HeightField.tsx +9 -2
- package/src/Hyperlink.tsx +6 -3
- package/src/Link.test.tsx +2 -2
- package/src/MarkdownEditorField.tsx +10 -2
- package/src/MarkdownView.test.tsx +10 -6
- package/src/Modal.test.tsx +3 -1
- package/src/ModalSheet.test.tsx +6 -6
- package/src/OfflineBanner.tsx +3 -2
- package/src/OtpField.test.tsx +44 -0
- package/src/OtpField.tsx +8 -4
- package/src/PickerSelect.tsx +3 -2
- package/src/ProvisionalConsentSigner.test.tsx +140 -113
- package/src/ProvisionalConsentSigner.tsx +5 -4
- package/src/SidebarNavigation.native.tsx +2 -2
- package/src/SidebarNavigation.tsx +2 -2
- package/src/Skeleton.tsx +1 -1
- package/src/TapToEdit.test.tsx +2 -2
- package/src/Text.test.tsx +1 -1
- package/src/Text.tsx +25 -51
- package/src/TextField.tsx +2 -1
- package/src/ThumbsUpDownFeedback.tsx +3 -3
- package/src/ToastNotifications.tsx +3 -2
- package/src/ToastNotifications.webModal.test.tsx +4 -1
- package/src/Tooltip.tsx +2 -1
- package/src/UserInactivity.test.tsx +2 -3
- package/src/WebAddressAutocomplete.tsx +3 -3
- package/src/WebDropdownMenu.tsx +16 -5
- package/src/__snapshots__/DataTable.test.tsx.snap +13 -0
- package/src/announcementPlatform.ts +10 -0
- package/src/asks/AskCard.test.tsx +14 -12
- package/src/asks/AskCard.tsx +2 -2
- package/src/bunSetup.ts +42 -0
- package/src/dataTableColumnWidths.test.ts +266 -0
- package/src/dataTableColumnWidths.ts +272 -0
- package/src/index.tsx +9 -2
- package/src/login/LoginScreen.tsx +2 -2
- package/src/requestClient.test.ts +288 -0
- package/src/requestClient.ts +228 -0
- package/src/security/PasswordCard.tsx +7 -5
- package/src/security/TwoFactorSettingsCard.tsx +10 -3
- package/src/signUp/SignUpScreen.tsx +2 -2
- package/src/table/Table.tsx +1 -2
- package/src/table/TableTitle.tsx +2 -1
- package/src/test-utils.tsx +76 -0
- package/src/textFontSizes.ts +31 -0
- package/src/useAcknowledgeAnnouncement.test.ts +101 -193
- package/src/useAcknowledgeAnnouncement.ts +69 -147
- package/src/useAnnouncements.test.ts +122 -186
- package/src/useAnnouncements.ts +49 -105
- package/src/useConsentForms.test.ts +59 -112
- package/src/useConsentForms.ts +37 -87
- package/src/useProvisionalConsent.test.ts +74 -150
- package/src/useProvisionalConsent.ts +79 -125
- package/src/useSubmitConsent.test.ts +59 -85
- package/src/useSubmitConsent.ts +22 -77
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
-
import {renderHook} from "@testing-library/react-native";
|
|
1
|
+
import {describe, expect, it, mock, spyOn} from "bun:test";
|
|
2
|
+
import {act, renderHook, waitFor} from "@testing-library/react-native";
|
|
3
3
|
|
|
4
|
+
import {createFakeRequestClient, fakeHttpError} from "./test-utils";
|
|
4
5
|
import type {ConsentFormPublic} from "./useConsentForms";
|
|
5
6
|
import {
|
|
6
7
|
applyConsentVariables,
|
|
@@ -8,25 +9,15 @@ import {
|
|
|
8
9
|
resolveConsentLocale,
|
|
9
10
|
useConsentForms,
|
|
10
11
|
} from "./useConsentForms";
|
|
12
|
+
import {useSubmitConsent} from "./useSubmitConsent";
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
const makeForm = (id: string): ConsentFormPublic =>
|
|
15
|
+
({id, title: `Form ${id}`}) as unknown as ConsentFormPublic;
|
|
13
16
|
|
|
14
17
|
interface GlobalThisWithNavigator {
|
|
15
18
|
navigator: {language: string} | undefined;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
|
-
interface MockQueryDef {
|
|
19
|
-
onQueryStarted?: (
|
|
20
|
-
_arg: unknown,
|
|
21
|
-
helpers: {queryFulfilled: Promise<{data: Partial<ConsentFormPublic>[]}> | Promise<never>}
|
|
22
|
-
) => Promise<void>;
|
|
23
|
-
query: () => string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface MockInjectOpts {
|
|
27
|
-
endpoints: (build: {query: (def: MockQueryDef) => string}) => Record<string, unknown>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
21
|
mock.module("expo-localization", () => ({
|
|
31
22
|
getLocales: () => [{languageTag: "es-ES"}],
|
|
32
23
|
}));
|
|
@@ -102,112 +93,68 @@ describe("detectLocale", () => {
|
|
|
102
93
|
});
|
|
103
94
|
|
|
104
95
|
describe("useConsentForms", () => {
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
injectEndpoints: mock((opts: MockInjectOpts) => {
|
|
116
|
-
// Call endpoint builder to exercise provides/onQueryStarted
|
|
117
|
-
const build = {
|
|
118
|
-
query: mock((def: MockQueryDef) => {
|
|
119
|
-
// Call onQueryStarted with a successful result
|
|
120
|
-
if (def.onQueryStarted) {
|
|
121
|
-
void def.onQueryStarted(undefined, {
|
|
122
|
-
queryFulfilled: Promise.resolve({data: [{id: "1"}]}),
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
// Also exercise the URL builder
|
|
126
|
-
def.query();
|
|
127
|
-
return "pending-query";
|
|
128
|
-
}),
|
|
129
|
-
};
|
|
130
|
-
opts.endpoints(build);
|
|
131
|
-
return {useGetPendingConsentsQuery};
|
|
132
|
-
}),
|
|
133
|
-
})),
|
|
134
|
-
};
|
|
135
|
-
return {api, refetch};
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
it("returns an array of forms when response is an array", () => {
|
|
139
|
-
const {api} = buildApi({data: [{id: "1", title: "Form 1"} as unknown as ConsentFormPublic]});
|
|
140
|
-
const {result} = renderHook(() => useConsentForms(api as unknown as ConsentFormsApi));
|
|
141
|
-
expect(result.current.forms).toBeDefined();
|
|
142
|
-
expect(Array.isArray(result.current.forms)).toBe(true);
|
|
96
|
+
it("returns forms from a bare array response", async () => {
|
|
97
|
+
const {client, calls} = createFakeRequestClient({
|
|
98
|
+
"GET /consents/pending": () => [makeForm("1")],
|
|
99
|
+
});
|
|
100
|
+
const {result} = renderHook(() => useConsentForms(client));
|
|
101
|
+
expect(result.current.isLoading).toBe(true);
|
|
102
|
+
expect(result.current.forms).toEqual([]);
|
|
103
|
+
await waitFor(() => expect(result.current.forms).toHaveLength(1));
|
|
104
|
+
expect(result.current.isLoading).toBe(false);
|
|
105
|
+
expect(calls[0]?.url).toBe("/consents/pending");
|
|
143
106
|
});
|
|
144
107
|
|
|
145
|
-
it("unwraps
|
|
146
|
-
const {
|
|
147
|
-
|
|
148
|
-
|
|
108
|
+
it("unwraps a {data} envelope and prefixes baseUrl", async () => {
|
|
109
|
+
const {client, calls} = createFakeRequestClient({
|
|
110
|
+
"GET /api/consents/pending": () => ({data: [makeForm("2")]}),
|
|
111
|
+
});
|
|
112
|
+
const {result} = renderHook(() => useConsentForms(client, "/api"));
|
|
113
|
+
await waitFor(() => expect(result.current.forms[0]?.id).toBe("2"));
|
|
114
|
+
expect(calls[0]?.url).toBe("/api/consents/pending");
|
|
149
115
|
});
|
|
150
116
|
|
|
151
|
-
it("returns empty array when no data
|
|
152
|
-
const {
|
|
153
|
-
const {result} = renderHook(() => useConsentForms(
|
|
117
|
+
it("returns an empty array when the envelope has no data", async () => {
|
|
118
|
+
const {client} = createFakeRequestClient({"GET /consents/pending": () => ({})});
|
|
119
|
+
const {result} = renderHook(() => useConsentForms(client));
|
|
120
|
+
await waitFor(() => expect(result.current.isLoading).toBe(false));
|
|
154
121
|
expect(result.current.forms).toEqual([]);
|
|
155
|
-
expect(result.current.isLoading).toBe(true);
|
|
156
122
|
});
|
|
157
123
|
|
|
158
|
-
it("
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
opts.endpoints(build);
|
|
174
|
-
return {
|
|
175
|
-
useGetPendingConsentsQuery: () => ({
|
|
176
|
-
data: undefined,
|
|
177
|
-
error: "error",
|
|
178
|
-
isLoading: false,
|
|
179
|
-
refetch,
|
|
180
|
-
}),
|
|
181
|
-
};
|
|
182
|
-
}),
|
|
183
|
-
})),
|
|
184
|
-
};
|
|
185
|
-
const {result} = renderHook(() => useConsentForms(api as unknown as ConsentFormsApi));
|
|
186
|
-
expect(result.current.error).toBe("error");
|
|
124
|
+
it("surfaces request errors and logs a warning", async () => {
|
|
125
|
+
const warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
126
|
+
const failure = fakeHttpError(500);
|
|
127
|
+
const {client} = createFakeRequestClient({
|
|
128
|
+
"GET /consents/pending": () => {
|
|
129
|
+
throw failure;
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
const {result} = renderHook(() => useConsentForms(client));
|
|
133
|
+
await waitFor(() => expect(result.current.error).toBe(failure));
|
|
134
|
+
expect(result.current.forms).toEqual([]);
|
|
135
|
+
expect(warn).toHaveBeenCalledWith("[useConsentForms] Failed to fetch pending consent forms", {
|
|
136
|
+
error: failure,
|
|
137
|
+
});
|
|
138
|
+
warn.mockRestore();
|
|
187
139
|
});
|
|
188
140
|
|
|
189
|
-
it("
|
|
190
|
-
let
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
141
|
+
it("refetches when a consent submission invalidates pending consents", async () => {
|
|
142
|
+
let forms = [makeForm("1")];
|
|
143
|
+
const {client, callsTo} = createFakeRequestClient({
|
|
144
|
+
"GET /consents/pending": () => forms,
|
|
145
|
+
"POST /consents/respond": () => ({agreed: true}),
|
|
146
|
+
});
|
|
147
|
+
const {result} = renderHook(() => ({
|
|
148
|
+
pending: useConsentForms(client),
|
|
149
|
+
submit: useSubmitConsent(client),
|
|
197
150
|
}));
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const {rerender} = renderHook(() => useConsentForms(api as unknown as ConsentFormsApi));
|
|
207
|
-
rerender(undefined);
|
|
208
|
-
rerender(undefined);
|
|
209
|
-
// The hook reuses the cached enhanced api after the first render so the
|
|
210
|
-
// dev-mode RTK warning about re-injecting endpoints never fires.
|
|
211
|
-
expect(injectCallCount).toBe(1);
|
|
151
|
+
await waitFor(() => expect(result.current.pending.forms).toHaveLength(1));
|
|
152
|
+
|
|
153
|
+
forms = [];
|
|
154
|
+
await act(async () => {
|
|
155
|
+
await result.current.submit.submit({agreed: true, consentFormId: "1", locale: "en"});
|
|
156
|
+
});
|
|
157
|
+
await waitFor(() => expect(result.current.pending.forms).toHaveLength(0));
|
|
158
|
+
expect(callsTo("GET /consents/pending")).toHaveLength(2);
|
|
212
159
|
});
|
|
213
160
|
});
|
package/src/useConsentForms.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {getLocales} from "expo-localization";
|
|
2
|
+
import {useCallback} from "react";
|
|
3
|
+
|
|
4
|
+
import {type TerrenoRequestClient, useClientQuery} from "./requestClient";
|
|
5
|
+
|
|
1
6
|
export interface ConsentFormPublic {
|
|
2
7
|
id: string;
|
|
3
8
|
title: string;
|
|
@@ -19,45 +24,6 @@ export interface ConsentFormPublic {
|
|
|
19
24
|
provisionalSignature?: {signedAt: string; signedByName: string};
|
|
20
25
|
}
|
|
21
26
|
|
|
22
|
-
import {getLocales} from "expo-localization";
|
|
23
|
-
|
|
24
|
-
interface ConsentFormsResponse {
|
|
25
|
-
data?: ConsentFormPublic[];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface ConsentFormsQueryBuilder {
|
|
29
|
-
query: (options: {
|
|
30
|
-
onQueryStarted?: (
|
|
31
|
-
_arg: unknown,
|
|
32
|
-
helpers: {queryFulfilled: Promise<ConsentFormsResponse>}
|
|
33
|
-
) => Promise<void>;
|
|
34
|
-
providesTags: string[];
|
|
35
|
-
query: () => string;
|
|
36
|
-
}) => unknown;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface ConsentFormsHookState {
|
|
40
|
-
data?: ConsentFormPublic[] | ConsentFormsResponse;
|
|
41
|
-
error: unknown;
|
|
42
|
-
isLoading: boolean;
|
|
43
|
-
refetch: () => void | Promise<void>;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
interface ConsentFormsEnhancedApi {
|
|
47
|
-
useGetPendingConsentsQuery: () => ConsentFormsHookState;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
interface ConsentFormsApiWithTags {
|
|
51
|
-
injectEndpoints: (options: {
|
|
52
|
-
endpoints: (build: ConsentFormsQueryBuilder) => {getPendingConsents: unknown};
|
|
53
|
-
overrideExisting: boolean;
|
|
54
|
-
}) => ConsentFormsEnhancedApi;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
interface ConsentFormsApi {
|
|
58
|
-
enhanceEndpoints: (options: {addTagTypes: string[]}) => ConsentFormsApiWithTags;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
27
|
/** Replace `{{name}}` placeholders in consent content; unknown placeholders stay as written. */
|
|
62
28
|
export const applyConsentVariables = (
|
|
63
29
|
content: string,
|
|
@@ -100,56 +66,40 @@ export const detectLocale = (): string => {
|
|
|
100
66
|
return "en";
|
|
101
67
|
};
|
|
102
68
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*/
|
|
110
|
-
const enhancedApiCache = new WeakMap<ConsentFormsApi, Map<string, ConsentFormsEnhancedApi>>();
|
|
111
|
-
|
|
112
|
-
const getEnhancedApi = (api: ConsentFormsApi, base: string): ConsentFormsEnhancedApi => {
|
|
113
|
-
let byBase = enhancedApiCache.get(api);
|
|
114
|
-
if (!byBase) {
|
|
115
|
-
byBase = new Map();
|
|
116
|
-
enhancedApiCache.set(api, byBase);
|
|
117
|
-
}
|
|
118
|
-
const cached = byBase.get(base);
|
|
119
|
-
if (cached) {
|
|
120
|
-
return cached;
|
|
121
|
-
}
|
|
122
|
-
const apiWithConsentTags = api.enhanceEndpoints({addTagTypes: ["PendingConsents"]});
|
|
123
|
-
const enhanced = apiWithConsentTags.injectEndpoints({
|
|
124
|
-
endpoints: (build) => ({
|
|
125
|
-
getPendingConsents: build.query({
|
|
126
|
-
async onQueryStarted(_arg: unknown, {queryFulfilled}) {
|
|
127
|
-
console.info("[useConsentForms] Fetching pending consent forms");
|
|
128
|
-
try {
|
|
129
|
-
const result = await queryFulfilled;
|
|
130
|
-
console.info("[useConsentForms] Pending consent forms fetched", {
|
|
131
|
-
count: result?.data?.length ?? 0,
|
|
132
|
-
});
|
|
133
|
-
} catch (error) {
|
|
134
|
-
console.warn("[useConsentForms] Failed to fetch pending consent forms", {error});
|
|
135
|
-
}
|
|
136
|
-
},
|
|
137
|
-
providesTags: ["PendingConsents"],
|
|
138
|
-
query: () => `${base}/consents/pending`,
|
|
139
|
-
}),
|
|
140
|
-
}),
|
|
141
|
-
overrideExisting: false,
|
|
142
|
-
});
|
|
143
|
-
byBase.set(base, enhanced);
|
|
144
|
-
return enhanced;
|
|
145
|
-
};
|
|
69
|
+
export interface UseConsentFormsResult {
|
|
70
|
+
error: unknown;
|
|
71
|
+
forms: ConsentFormPublic[];
|
|
72
|
+
isLoading: boolean;
|
|
73
|
+
refetch: () => Promise<void>;
|
|
74
|
+
}
|
|
146
75
|
|
|
147
|
-
|
|
76
|
+
/** Fetch the signed-in user's pending consent forms (`GET /consents/pending`). */
|
|
77
|
+
export const useConsentForms = (
|
|
78
|
+
client: TerrenoRequestClient,
|
|
79
|
+
baseUrl?: string
|
|
80
|
+
): UseConsentFormsResult => {
|
|
148
81
|
const base = baseUrl || "";
|
|
149
|
-
const enhancedApi = getEnhancedApi(api, base);
|
|
150
82
|
|
|
151
|
-
const
|
|
152
|
-
|
|
83
|
+
const fetchPendingConsents = useCallback(async (): Promise<ConsentFormPublic[]> => {
|
|
84
|
+
console.info("[useConsentForms] Fetching pending consent forms");
|
|
85
|
+
try {
|
|
86
|
+
const result = await client.request<ConsentFormPublic[] | {data?: ConsentFormPublic[]}>({
|
|
87
|
+
url: `${base}/consents/pending`,
|
|
88
|
+
});
|
|
89
|
+
const forms = Array.isArray(result) ? result : (result?.data ?? []);
|
|
90
|
+
console.info("[useConsentForms] Pending consent forms fetched", {count: forms.length});
|
|
91
|
+
return forms;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.warn("[useConsentForms] Failed to fetch pending consent forms", {error});
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}, [base, client]);
|
|
97
|
+
|
|
98
|
+
const {data, error, isLoading, refetch} = useClientQuery({
|
|
99
|
+
client,
|
|
100
|
+
fetcher: fetchPendingConsents,
|
|
101
|
+
providesTag: "PendingConsents",
|
|
102
|
+
});
|
|
153
103
|
|
|
154
|
-
return {error, forms, isLoading, refetch};
|
|
104
|
+
return {error, forms: data ?? [], isLoading, refetch};
|
|
155
105
|
};
|
|
@@ -1,23 +1,13 @@
|
|
|
1
|
-
import {describe, expect, it
|
|
2
|
-
import {act, renderHook} from "@testing-library/react-native";
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import {act, renderHook, waitFor} from "@testing-library/react-native";
|
|
3
3
|
|
|
4
|
+
import {createFakeRequestClient, fakeHttpError} from "./test-utils";
|
|
4
5
|
import {
|
|
5
|
-
type ProvisionalConsentApi,
|
|
6
6
|
type ProvisionalConsentStatusRow,
|
|
7
7
|
provisionalSignerName,
|
|
8
8
|
useProvisionalConsent,
|
|
9
9
|
} from "./useProvisionalConsent";
|
|
10
10
|
|
|
11
|
-
interface CapturedQuery {
|
|
12
|
-
providesTags: string[];
|
|
13
|
-
query: (userId: string) => string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface CapturedMutation {
|
|
17
|
-
invalidatesTags: string[];
|
|
18
|
-
query: (args: Record<string, unknown>) => {body: unknown; method: string; url: string};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
11
|
const row: ProvisionalConsentStatusRow = {
|
|
22
12
|
form: {
|
|
23
13
|
active: true,
|
|
@@ -41,126 +31,68 @@ const row: ProvisionalConsentStatusRow = {
|
|
|
41
31
|
status: "pending",
|
|
42
32
|
};
|
|
43
33
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
signResult
|
|
47
|
-
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
queriedUserIds: string[];
|
|
55
|
-
signedBodies: unknown[];
|
|
56
|
-
} => {
|
|
57
|
-
const captured: CapturedQuery[] = [];
|
|
58
|
-
const capturedMutations: CapturedMutation[] = [];
|
|
59
|
-
const addTagTypes: string[][] = [];
|
|
60
|
-
const queriedUserIds: string[] = [];
|
|
34
|
+
/** A fake request client: the status route returns `status`, signing and voiding run `signResult` / `voidResult`. */
|
|
35
|
+
const buildClient = ({
|
|
36
|
+
signResult = () => Promise.resolve({}),
|
|
37
|
+
status = {data: []},
|
|
38
|
+
voidResult = () => Promise.resolve({}),
|
|
39
|
+
}: {
|
|
40
|
+
signResult?: () => Promise<unknown>;
|
|
41
|
+
status?: unknown | (() => Promise<unknown>);
|
|
42
|
+
voidResult?: () => Promise<unknown>;
|
|
43
|
+
} = {}) => {
|
|
61
44
|
const signedBodies: unknown[] = [];
|
|
62
45
|
const voidedArgs: unknown[] = [];
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return def;
|
|
78
|
-
},
|
|
79
|
-
query: (def: CapturedQuery) => {
|
|
80
|
-
captured.push(def);
|
|
81
|
-
return def;
|
|
82
|
-
},
|
|
83
|
-
});
|
|
84
|
-
return {
|
|
85
|
-
useGetConsentStatusQuery: (userId: string) => {
|
|
86
|
-
queriedUserIds.push(userId);
|
|
87
|
-
return {
|
|
88
|
-
data: state.data,
|
|
89
|
-
error: state.error,
|
|
90
|
-
isLoading: state.isLoading ?? false,
|
|
91
|
-
refetch: () => {},
|
|
92
|
-
};
|
|
93
|
-
},
|
|
94
|
-
useSignConsentProvisionallyMutation: () => [
|
|
95
|
-
(body: unknown) => {
|
|
96
|
-
signedBodies.push(body);
|
|
97
|
-
return {unwrap: signResult};
|
|
98
|
-
},
|
|
99
|
-
{isLoading: false},
|
|
100
|
-
],
|
|
101
|
-
useVoidProvisionalConsentMutation: () => [
|
|
102
|
-
(args: unknown) => {
|
|
103
|
-
voidedArgs.push(args);
|
|
104
|
-
return {unwrap: voidResult};
|
|
105
|
-
},
|
|
106
|
-
{isLoading: false},
|
|
107
|
-
],
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
),
|
|
111
|
-
};
|
|
112
|
-
}),
|
|
113
|
-
} as unknown as ProvisionalConsentApi;
|
|
114
|
-
return {addTagTypes, api, captured, capturedMutations, queriedUserIds, signedBodies, voidedArgs};
|
|
46
|
+
const fake = createFakeRequestClient({
|
|
47
|
+
"GET /consent-responses/status": async () =>
|
|
48
|
+
typeof status === "function" ? (status as () => Promise<unknown>)() : status,
|
|
49
|
+
"GET /v2/consent-responses/status": async () => status,
|
|
50
|
+
"POST /consent-responses/:id/void": async (args, params) => {
|
|
51
|
+
voidedArgs.push({reason: (args.body as {reason: string}).reason, responseId: params.id});
|
|
52
|
+
return voidResult();
|
|
53
|
+
},
|
|
54
|
+
"POST /consent-responses/provisional": async (args) => {
|
|
55
|
+
signedBodies.push(args.body);
|
|
56
|
+
return signResult();
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
return {...fake, signedBodies, voidedArgs};
|
|
115
60
|
};
|
|
116
61
|
|
|
117
62
|
describe("useProvisionalConsent", () => {
|
|
118
|
-
it("
|
|
119
|
-
const {
|
|
63
|
+
it("requests the status endpoint for the user and returns the rows", async () => {
|
|
64
|
+
const {client, callsTo} = buildClient({status: {data: [row]}});
|
|
120
65
|
|
|
121
|
-
const {result} = renderHook(() =>
|
|
122
|
-
useProvisionalConsent({api, baseUrl: "/api", userId: "user 1"})
|
|
123
|
-
);
|
|
66
|
+
const {result} = renderHook(() => useProvisionalConsent({client, userId: "user 1"}));
|
|
124
67
|
|
|
125
|
-
expect(
|
|
126
|
-
expect(
|
|
127
|
-
expect(captured[0].providesTags).toEqual(["ConsentStatus"]);
|
|
128
|
-
expect(addTagTypes[0]).toEqual(["ConsentStatus", "PendingConsents"]);
|
|
129
|
-
expect(queriedUserIds).toContain("user 1");
|
|
130
|
-
expect(result.current.statuses).toEqual([row]);
|
|
68
|
+
expect(result.current.isLoading).toBe(true);
|
|
69
|
+
await waitFor(() => expect(result.current.statuses).toEqual([row]));
|
|
131
70
|
expect(result.current.isLoading).toBe(false);
|
|
71
|
+
expect(callsTo("GET /consent-responses/status")[0]?.params).toEqual({userId: "user 1"});
|
|
132
72
|
});
|
|
133
73
|
|
|
134
|
-
it("
|
|
135
|
-
const {
|
|
74
|
+
it("prefixes the base URL", async () => {
|
|
75
|
+
const {client, callsTo} = buildClient({status: {data: [row]}});
|
|
136
76
|
|
|
137
|
-
renderHook(() => useProvisionalConsent({
|
|
138
|
-
renderHook(() => useProvisionalConsent({api, userId: "b"}));
|
|
139
|
-
renderHook(() => useProvisionalConsent({api, baseUrl: "/v2", userId: "a"}));
|
|
77
|
+
const {result} = renderHook(() => useProvisionalConsent({baseUrl: "/v2", client, userId: "a"}));
|
|
140
78
|
|
|
141
|
-
expect(
|
|
142
|
-
expect(
|
|
143
|
-
expect(captured[1].query("a")).toBe("/v2/consent-responses/status?userId=a");
|
|
79
|
+
await waitFor(() => expect(result.current.statuses).toEqual([row]));
|
|
80
|
+
expect(callsTo("GET /v2/consent-responses/status")).toHaveLength(1);
|
|
144
81
|
});
|
|
145
82
|
|
|
146
|
-
it("
|
|
147
|
-
const {
|
|
148
|
-
renderHook(() => useProvisionalConsent({
|
|
149
|
-
|
|
150
|
-
expect(capturedMutations).toHaveLength(2);
|
|
151
|
-
expect(capturedMutations[0].invalidatesTags).toEqual(["ConsentStatus", "PendingConsents"]);
|
|
152
|
-
const body = {consentFormId: "form-1", note: "Phone", userId: "a"};
|
|
153
|
-
expect(capturedMutations[0].query(body)).toEqual({
|
|
154
|
-
body,
|
|
155
|
-
method: "POST",
|
|
156
|
-
url: "/api/consent-responses/provisional",
|
|
157
|
-
});
|
|
83
|
+
it("accepts an unwrapped array response", async () => {
|
|
84
|
+
const {client} = buildClient({status: [row]});
|
|
85
|
+
const {result} = renderHook(() => useProvisionalConsent({client, userId: "a"}));
|
|
86
|
+
await waitFor(() => expect(result.current.statuses).toEqual([row]));
|
|
158
87
|
});
|
|
159
88
|
|
|
160
|
-
it("signs for the hook's user and
|
|
89
|
+
it("signs for the hook's user, unwraps the envelope, and refetches the statuses", async () => {
|
|
161
90
|
const response = {_id: "response-1", agreedAt: "2026-10-20T15:30:00.000Z"};
|
|
162
|
-
const {
|
|
163
|
-
|
|
91
|
+
const {client, callsTo, signedBodies} = buildClient({
|
|
92
|
+
signResult: () => Promise.resolve({data: response}),
|
|
93
|
+
});
|
|
94
|
+
const {result} = renderHook(() => useProvisionalConsent({client, userId: "member-1"}));
|
|
95
|
+
await waitFor(() => expect(result.current.isLoading).toBe(false));
|
|
164
96
|
|
|
165
97
|
let signed: unknown;
|
|
166
98
|
await act(async () => {
|
|
@@ -172,12 +104,13 @@ describe("useProvisionalConsent", () => {
|
|
|
172
104
|
]);
|
|
173
105
|
expect(signed).toEqual(response);
|
|
174
106
|
expect(result.current.isSigning).toBe(false);
|
|
107
|
+
await waitFor(() => expect(callsTo("GET /consent-responses/status").length).toBeGreaterThan(1));
|
|
175
108
|
});
|
|
176
109
|
|
|
177
110
|
it("returns an already-unwrapped sign response as is", async () => {
|
|
178
111
|
const response = {_id: "response-2", agreedAt: "2026-10-20T15:30:00.000Z"};
|
|
179
|
-
const {
|
|
180
|
-
const {result} = renderHook(() => useProvisionalConsent({
|
|
112
|
+
const {client} = buildClient({signResult: () => Promise.resolve(response)});
|
|
113
|
+
const {result} = renderHook(() => useProvisionalConsent({client, userId: "member-1"}));
|
|
181
114
|
|
|
182
115
|
let signed: unknown;
|
|
183
116
|
await act(async () => {
|
|
@@ -187,55 +120,46 @@ describe("useProvisionalConsent", () => {
|
|
|
187
120
|
});
|
|
188
121
|
|
|
189
122
|
it("rejects with the server error when signing fails", async () => {
|
|
190
|
-
const error =
|
|
191
|
-
const {
|
|
192
|
-
const {result} = renderHook(() => useProvisionalConsent({
|
|
123
|
+
const error = fakeHttpError(400, "Consent form is not active");
|
|
124
|
+
const {client} = buildClient({signResult: () => Promise.reject(error)});
|
|
125
|
+
const {result} = renderHook(() => useProvisionalConsent({client, userId: "member-1"}));
|
|
193
126
|
|
|
194
|
-
await
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const {api, capturedMutations} = buildApi({data: []});
|
|
199
|
-
renderHook(() => useProvisionalConsent({api, baseUrl: "/api", userId: "a"}));
|
|
200
|
-
|
|
201
|
-
expect(capturedMutations[1].invalidatesTags).toEqual(["ConsentStatus", "PendingConsents"]);
|
|
202
|
-
expect(capturedMutations[1].query({reason: "Wrong member", responseId: "resp 1"})).toEqual({
|
|
203
|
-
body: {reason: "Wrong member"},
|
|
204
|
-
method: "POST",
|
|
205
|
-
url: "/api/consent-responses/resp%201/void",
|
|
127
|
+
await act(async () => {
|
|
128
|
+
await expect(result.current.sign({consentFormId: "form-1", note: "Phone"})).rejects.toBe(
|
|
129
|
+
error
|
|
130
|
+
);
|
|
206
131
|
});
|
|
207
132
|
});
|
|
208
133
|
|
|
209
|
-
it("voids a signature
|
|
134
|
+
it("voids a signature by response id, unwraps the response, and refetches", async () => {
|
|
210
135
|
const response = {_id: "response-1", voidedAt: "2026-10-21T09:00:00.000Z"};
|
|
211
|
-
const {
|
|
212
|
-
Promise.resolve({data: response})
|
|
213
|
-
);
|
|
214
|
-
const {result} = renderHook(() => useProvisionalConsent({
|
|
136
|
+
const {callsTo, client, voidedArgs} = buildClient({
|
|
137
|
+
voidResult: () => Promise.resolve({data: response}),
|
|
138
|
+
});
|
|
139
|
+
const {result} = renderHook(() => useProvisionalConsent({client, userId: "member-1"}));
|
|
140
|
+
await waitFor(() => expect(result.current.isLoading).toBe(false));
|
|
215
141
|
|
|
216
142
|
let voided: unknown;
|
|
217
143
|
await act(async () => {
|
|
218
|
-
voided = await result.current.voidSignature({reason: "Wrong member", responseId: "
|
|
144
|
+
voided = await result.current.voidSignature({reason: "Wrong member", responseId: "resp 1"});
|
|
219
145
|
});
|
|
220
146
|
|
|
221
|
-
expect(voidedArgs).toEqual([{reason: "Wrong member", responseId: "
|
|
147
|
+
expect(voidedArgs).toEqual([{reason: "Wrong member", responseId: "resp%201"}]);
|
|
148
|
+
expect(callsTo("POST /consent-responses/:id/void")[0]?.url).toBe(
|
|
149
|
+
"/consent-responses/resp%201/void"
|
|
150
|
+
);
|
|
222
151
|
expect(voided).toEqual(response);
|
|
223
152
|
expect(result.current.isVoiding).toBe(false);
|
|
224
153
|
});
|
|
225
154
|
|
|
226
|
-
it("
|
|
227
|
-
const
|
|
228
|
-
const {
|
|
229
|
-
|
|
230
|
-
});
|
|
155
|
+
it("returns no statuses while loading and passes the load error through", async () => {
|
|
156
|
+
const error = fakeHttpError(405, "Method not allowed");
|
|
157
|
+
const {client} = buildClient({status: () => Promise.reject(error)});
|
|
158
|
+
const {result} = renderHook(() => useProvisionalConsent({client, userId: "a"}));
|
|
231
159
|
|
|
232
|
-
it("returns no statuses while loading and passes the error through", () => {
|
|
233
|
-
const error = {status: 405};
|
|
234
|
-
const {api} = buildApi({error, isLoading: true});
|
|
235
|
-
const {result} = renderHook(() => useProvisionalConsent({api, userId: "a"}));
|
|
236
160
|
expect(result.current.statuses).toEqual([]);
|
|
237
161
|
expect(result.current.isLoading).toBe(true);
|
|
238
|
-
expect(result.current.error).toBe(error);
|
|
162
|
+
await waitFor(() => expect(result.current.error).toBe(error));
|
|
239
163
|
});
|
|
240
164
|
});
|
|
241
165
|
|