@terreno/ui 57.20.0 → 58.0.0-beta.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.
- package/.ai/guidelines/core.md +1 -1
- package/CHANGELOG.md +93 -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 +71 -39
- 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 +6 -6
- 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 +9 -9
- 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 +4 -4
- 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 +9 -9
- 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/TapToEdit.d.ts +1 -1
- package/dist/TapToEdit.d.ts.map +1 -1
- package/dist/TapToEdit.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 +4 -4
- package/dist/TextField.js.map +1 -1
- package/dist/Theme.d.ts.map +1 -1
- package/dist/Theme.js +16 -0
- package/dist/Theme.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 +40 -38
- package/src/ActionSheet.test.tsx +13 -13
- package/src/ActionSheet.tsx +18 -5
- package/src/AddressField.test.tsx +9 -3
- package/src/AnnouncementNavigator.test.tsx +219 -319
- package/src/AnnouncementNavigator.tsx +25 -20
- package/src/Avatar.tsx +2 -9
- package/src/Badge.test.tsx +1 -1
- package/src/Banner.test.tsx +0 -1
- package/src/Box.test.tsx +85 -77
- package/src/Box.tsx +4 -2
- package/src/Button.test.tsx +11 -6
- package/src/Card.test.tsx +2 -2
- package/src/Common.ts +79 -44
- package/src/ConflictSheet.test.tsx +5 -5
- package/src/ConsentFormScreen.test.tsx +3 -1
- package/src/ConsentFormScreen.tsx +2 -1
- package/src/ConsentNavigator.test.tsx +166 -243
- package/src/ConsentNavigator.tsx +9 -13
- package/src/CustomSelectField.test.tsx +16 -4
- package/src/CustomSelectField.tsx +3 -3
- package/src/DataTable.test.tsx +73 -5
- package/src/DataTable.tsx +72 -19
- package/src/DataTableVirtualizationRegression.test.tsx +1 -0
- package/src/DateTimeActionSheet.test.tsx +2 -0
- package/src/DateTimeField.test.tsx +3 -12
- package/src/DateTimeField.tsx +10 -10
- package/src/DismissButton.test.tsx +22 -4
- package/src/DropdownPanel.tsx +0 -6
- package/src/EmailField.test.tsx +13 -22
- package/src/EmojiSelector.test.tsx +2 -2
- package/src/ErrorBoundary.test.tsx +5 -3
- package/src/Field.test.tsx +20 -44
- package/src/FilterAccordion.tsx +3 -3
- package/src/FilterBoolean.tsx +3 -3
- package/src/FilterSelectMenu.tsx +1 -1
- package/src/GPTChat.test.tsx +24 -16
- package/src/GPTChat.tsx +10 -9
- package/src/Heading.test.tsx +1 -1
- package/src/Heading.tsx +7 -14
- package/src/HeightField.tsx +17 -10
- package/src/Hyperlink.tsx +6 -3
- package/src/Icon.test.tsx +2 -2
- package/src/Image.test.tsx +1 -1
- package/src/Link.test.tsx +13 -6
- package/src/MarkdownEditorField.tsx +10 -2
- package/src/MarkdownView.test.tsx +10 -6
- package/src/MobileAddressAutoComplete.test.tsx +2 -1
- package/src/Modal.test.tsx +10 -3
- package/src/ModalSheet.test.tsx +11 -10
- package/src/MultiselectField.test.tsx +17 -3
- package/src/NumberField.test.tsx +14 -14
- package/src/NumberPickerActionSheet.test.tsx +1 -1
- package/src/OfflineBanner.tsx +3 -2
- package/src/OpenAPIContext.test.tsx +4 -4
- package/src/OpenAPIRenderRegression.test.tsx +1 -1
- package/src/OtpField.tsx +6 -5
- package/src/P0RenderRegression.test.tsx +7 -1
- package/src/P1RenderRegression.test.tsx +1 -0
- package/src/P2ButtonRenderRegression.test.tsx +1 -0
- package/src/Page.test.tsx +1 -1
- package/src/PhoneNumberField.test.tsx +14 -38
- package/src/PickerSelect.tsx +12 -11
- package/src/Popover.test.tsx +2 -2
- package/src/ProvisionalConsentSigner.test.tsx +140 -113
- package/src/ProvisionalConsentSigner.tsx +5 -4
- package/src/RadioField.test.tsx +7 -2
- package/src/SegmentedControl.test.tsx +21 -12
- package/src/SidebarNavigation.native.tsx +2 -2
- package/src/SidebarNavigation.test.tsx +18 -6
- package/src/SidebarNavigation.tsx +2 -2
- package/src/Signature.test.tsx +20 -23
- package/src/Skeleton.tsx +1 -1
- package/src/Slider.test.tsx +1 -1
- package/src/SplitPage.test.tsx +15 -5
- package/src/TapToEdit.test.tsx +40 -17
- package/src/TapToEdit.tsx +1 -1
- package/src/TerrenoFontProvider.test.tsx +1 -1
- package/src/Text.test.tsx +4 -4
- package/src/Text.tsx +25 -51
- package/src/TextField.test.tsx +82 -9
- package/src/TextField.tsx +6 -5
- package/src/Theme.test.tsx +49 -0
- package/src/Theme.tsx +16 -0
- package/src/ThumbsUpDownFeedback.tsx +3 -3
- package/src/Toast.test.tsx +5 -5
- package/src/ToastNotifications.test.tsx +73 -70
- package/src/ToastNotifications.tsx +3 -2
- package/src/ToastNotifications.webModal.test.tsx +6 -3
- package/src/Tooltip.test.tsx +3 -3
- package/src/Tooltip.tsx +2 -1
- package/src/UnifiedAddressAutoComplete.test.tsx +1 -1
- package/src/UserInactivity.test.tsx +1 -0
- package/src/WebAddressAutocomplete.test.tsx +12 -12
- package/src/WebAddressAutocomplete.tsx +3 -3
- package/src/WebDropdownMenu.test.tsx +16 -15
- package/src/WebDropdownMenu.tsx +16 -5
- package/src/__snapshots__/Card.test.tsx.snap +1 -1
- package/src/__snapshots__/DataTable.test.tsx.snap +13 -0
- package/src/__snapshots__/DismissButton.test.tsx.snap +3 -3
- package/src/__snapshots__/Field.test.tsx.snap +4 -2
- package/src/__snapshots__/Heading.test.tsx.snap +1 -1
- package/src/__snapshots__/Image.test.tsx.snap +1 -1
- package/src/__snapshots__/Page.test.tsx.snap +2 -2
- package/src/__snapshots__/Text.test.tsx.snap +1 -1
- package/src/announcementPlatform.ts +10 -0
- package/src/asks/AskCard.test.tsx +20 -15
- package/src/asks/AskCard.tsx +2 -2
- package/src/asks/AskFiles.test.tsx +5 -1
- package/src/asks/askFileRefs.test.ts +1 -1
- package/src/asks/askSummary.test.ts +2 -2
- package/src/benchmarks/importTiming.ts +1 -1
- package/src/blocks/BlocksView.test.tsx +20 -11
- package/src/bunSetup.ts +43 -1
- package/src/dataTableColumnWidths.test.ts +266 -0
- package/src/dataTableColumnWidths.ts +272 -0
- package/src/dataTableFilters.test.tsx +1 -1
- package/src/index.tsx +9 -2
- package/src/isolated/ListDetailLayout.isolated.tsx +2 -4
- package/src/login/LoginScreen.test.tsx +6 -6
- package/src/login/LoginScreen.tsx +2 -2
- package/src/requestClient.test.ts +288 -0
- package/src/requestClient.ts +228 -0
- package/src/security/PasswordCard.test.tsx +3 -3
- package/src/security/PasswordCard.tsx +7 -5
- package/src/security/TwoFactorSettingsCard.tsx +10 -3
- package/src/signUp/SignUpScreen.test.tsx +4 -4
- package/src/signUp/SignUpScreen.tsx +2 -2
- package/src/table/Table.tsx +1 -2
- package/src/table/TableTitle.tsx +2 -1
- package/src/table/tableContext.test.tsx +5 -5
- package/src/test-utils.tsx +132 -1
- package/src/testing/reactDomServer.d.ts +7 -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
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import {useCallback, useEffect, useRef, useState} from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One request made through a {@link TerrenoRequestClient}. A narrow subset of the
|
|
5
|
+
* `@terreno/syncdb/api` `ApiRequestArgs` shape, so a syncdb `ApiClient` satisfies
|
|
6
|
+
* {@link TerrenoRequestClient} without `@terreno/ui` depending on syncdb.
|
|
7
|
+
*/
|
|
8
|
+
export interface TerrenoRequestArgs {
|
|
9
|
+
/** Path or absolute URL. Relative paths are resolved against the client's base URL. */
|
|
10
|
+
url: string;
|
|
11
|
+
/** HTTP method; defaults to `GET`. */
|
|
12
|
+
method?: string;
|
|
13
|
+
/** Query-string parameters. `undefined` values are omitted. */
|
|
14
|
+
params?: Record<string, unknown>;
|
|
15
|
+
/** JSON request body. */
|
|
16
|
+
body?: Record<string, unknown> | null;
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
/** Unwrap `{data}` response envelopes (default `true`). */
|
|
19
|
+
unwrap?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Minimal request client the consent and announcement components use. Pass the app's
|
|
24
|
+
* `@terreno/syncdb/api` client (`createApiClient(...)`), or any object with a matching
|
|
25
|
+
* `request` method. Failed requests should reject with an error carrying the HTTP
|
|
26
|
+
* `status` so 401/403 responses fall through to the app's auth handling.
|
|
27
|
+
*/
|
|
28
|
+
export interface TerrenoRequestClient {
|
|
29
|
+
request: <T = unknown>(args: TerrenoRequestArgs) => Promise<T>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type TagListener = () => void;
|
|
33
|
+
|
|
34
|
+
const tagListenersByClient = new WeakMap<TerrenoRequestClient, Map<string, Set<TagListener>>>();
|
|
35
|
+
|
|
36
|
+
/** Listen for invalidations of `tag` on `client`. Returns unsubscribe. */
|
|
37
|
+
const subscribeClientTag = ({
|
|
38
|
+
client,
|
|
39
|
+
listener,
|
|
40
|
+
tag,
|
|
41
|
+
}: {
|
|
42
|
+
client: TerrenoRequestClient;
|
|
43
|
+
listener: TagListener;
|
|
44
|
+
tag: string;
|
|
45
|
+
}): (() => void) => {
|
|
46
|
+
let byTag = tagListenersByClient.get(client);
|
|
47
|
+
if (!byTag) {
|
|
48
|
+
byTag = new Map();
|
|
49
|
+
tagListenersByClient.set(client, byTag);
|
|
50
|
+
}
|
|
51
|
+
let listeners = byTag.get(tag);
|
|
52
|
+
if (!listeners) {
|
|
53
|
+
listeners = new Set();
|
|
54
|
+
byTag.set(tag, listeners);
|
|
55
|
+
}
|
|
56
|
+
listeners.add(listener);
|
|
57
|
+
return (): void => {
|
|
58
|
+
listeners.delete(listener);
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Refetch every mounted {@link useClientQuery} on `client` that provides one of `tags`.
|
|
64
|
+
* Mutations call this after success, the way RTK `invalidatesTags` did.
|
|
65
|
+
*/
|
|
66
|
+
export const invalidateClientTags = ({
|
|
67
|
+
client,
|
|
68
|
+
tags,
|
|
69
|
+
}: {
|
|
70
|
+
client: TerrenoRequestClient;
|
|
71
|
+
tags: readonly string[];
|
|
72
|
+
}): void => {
|
|
73
|
+
const byTag = tagListenersByClient.get(client);
|
|
74
|
+
if (!byTag) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
for (const tag of tags) {
|
|
78
|
+
for (const listener of [...(byTag.get(tag) ?? [])]) {
|
|
79
|
+
listener();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export interface ClientQueryResult<T> {
|
|
85
|
+
data: T | undefined;
|
|
86
|
+
error: unknown;
|
|
87
|
+
/** True while a request is in flight and none has succeeded yet. */
|
|
88
|
+
isLoading: boolean;
|
|
89
|
+
/** True while any request is in flight. */
|
|
90
|
+
isFetching: boolean;
|
|
91
|
+
/** Fetch again. Joins a request that is already in flight. */
|
|
92
|
+
refetch: () => Promise<void>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Run `fetcher` on mount and whenever its identity changes, keeping loading / error /
|
|
97
|
+
* data in React state. `providesTag` refetches the query when a mutation invalidates it.
|
|
98
|
+
*/
|
|
99
|
+
export const useClientQuery = <T>({
|
|
100
|
+
client,
|
|
101
|
+
fetcher,
|
|
102
|
+
providesTag,
|
|
103
|
+
}: {
|
|
104
|
+
client: TerrenoRequestClient;
|
|
105
|
+
/** Memoize with `useCallback`; a new identity triggers a new request. */
|
|
106
|
+
fetcher: () => Promise<T>;
|
|
107
|
+
providesTag?: string;
|
|
108
|
+
}): ClientQueryResult<T> => {
|
|
109
|
+
const [data, setData] = useState<T | undefined>(undefined);
|
|
110
|
+
const [hasData, setHasData] = useState(false);
|
|
111
|
+
const [error, setError] = useState<unknown>(undefined);
|
|
112
|
+
const [isFetching, setIsFetching] = useState(true);
|
|
113
|
+
const generationRef = useRef(0);
|
|
114
|
+
const inFlightRef = useRef<Promise<void> | undefined>(undefined);
|
|
115
|
+
const isMountedRef = useRef(true);
|
|
116
|
+
|
|
117
|
+
const run = useCallback((): Promise<void> => {
|
|
118
|
+
generationRef.current += 1;
|
|
119
|
+
const generation = generationRef.current;
|
|
120
|
+
setIsFetching(true);
|
|
121
|
+
setError(undefined);
|
|
122
|
+
const promise = fetcher().then(
|
|
123
|
+
(result) => {
|
|
124
|
+
if (generation !== generationRef.current || !isMountedRef.current) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
setData(result);
|
|
128
|
+
setHasData(true);
|
|
129
|
+
setIsFetching(false);
|
|
130
|
+
},
|
|
131
|
+
(fetchError: unknown) => {
|
|
132
|
+
if (generation !== generationRef.current || !isMountedRef.current) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
setError(fetchError);
|
|
136
|
+
setIsFetching(false);
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
const tracked = promise.finally(() => {
|
|
140
|
+
if (inFlightRef.current === tracked) {
|
|
141
|
+
inFlightRef.current = undefined;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
inFlightRef.current = tracked;
|
|
145
|
+
return tracked;
|
|
146
|
+
}, [fetcher]);
|
|
147
|
+
|
|
148
|
+
const refetch = useCallback((): Promise<void> => {
|
|
149
|
+
return inFlightRef.current ?? run();
|
|
150
|
+
}, [run]);
|
|
151
|
+
|
|
152
|
+
// Fetch on mount and whenever the fetcher (client, URL, or params) changes.
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
isMountedRef.current = true;
|
|
155
|
+
void run();
|
|
156
|
+
return (): void => {
|
|
157
|
+
isMountedRef.current = false;
|
|
158
|
+
inFlightRef.current = undefined;
|
|
159
|
+
};
|
|
160
|
+
}, [run]);
|
|
161
|
+
|
|
162
|
+
// Refetch with a fresh request when a mutation invalidates this query's tag.
|
|
163
|
+
useEffect(() => {
|
|
164
|
+
if (!providesTag) {
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
return subscribeClientTag({
|
|
168
|
+
client,
|
|
169
|
+
listener: () => {
|
|
170
|
+
void run();
|
|
171
|
+
},
|
|
172
|
+
tag: providesTag,
|
|
173
|
+
});
|
|
174
|
+
}, [client, providesTag, run]);
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
data,
|
|
178
|
+
error,
|
|
179
|
+
isFetching,
|
|
180
|
+
isLoading: isFetching && !hasData,
|
|
181
|
+
refetch,
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export interface ClientMutationResult<A> {
|
|
186
|
+
/** Run the mutation; rejects with the request error. */
|
|
187
|
+
mutate: (args: A) => Promise<void>;
|
|
188
|
+
error: unknown;
|
|
189
|
+
isLoading: boolean;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Track loading / error state for a request that changes server state. `mutate` keeps a
|
|
194
|
+
* stable identity across renders so it is safe in effect dependency lists.
|
|
195
|
+
*/
|
|
196
|
+
export const useClientMutation = <A>({
|
|
197
|
+
run,
|
|
198
|
+
}: {
|
|
199
|
+
run: (args: A) => Promise<void>;
|
|
200
|
+
}): ClientMutationResult<A> => {
|
|
201
|
+
const [error, setError] = useState<unknown>(undefined);
|
|
202
|
+
const [pendingCount, setPendingCount] = useState(0);
|
|
203
|
+
const runRef = useRef(run);
|
|
204
|
+
runRef.current = run;
|
|
205
|
+
|
|
206
|
+
const mutate = useCallback(async (args: A): Promise<void> => {
|
|
207
|
+
setPendingCount((count) => count + 1);
|
|
208
|
+
setError(undefined);
|
|
209
|
+
try {
|
|
210
|
+
await runRef.current(args);
|
|
211
|
+
} catch (mutationError) {
|
|
212
|
+
setError(mutationError);
|
|
213
|
+
throw mutationError;
|
|
214
|
+
} finally {
|
|
215
|
+
setPendingCount((count) => count - 1);
|
|
216
|
+
}
|
|
217
|
+
}, []);
|
|
218
|
+
|
|
219
|
+
return {error, isLoading: pendingCount > 0, mutate};
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/** Accept both bare payloads and `{data}` envelopes (when a client does not unwrap). */
|
|
223
|
+
export const unwrapDataEnvelope = <T>(value: unknown): T | undefined => {
|
|
224
|
+
if (value && typeof value === "object" && !Array.isArray(value) && "data" in value) {
|
|
225
|
+
return (value as {data?: T}).data;
|
|
226
|
+
}
|
|
227
|
+
return value as T | undefined;
|
|
228
|
+
};
|
|
@@ -2,7 +2,7 @@ import {describe, expect, it, mock, spyOn} from "bun:test";
|
|
|
2
2
|
import {act, fireEvent, render, waitFor} from "@testing-library/react-native";
|
|
3
3
|
import type {ReactElement} from "react";
|
|
4
4
|
import {ThemeProvider} from "../Theme";
|
|
5
|
-
import {renderWithTheme} from "../test-utils";
|
|
5
|
+
import {elementProps, renderWithTheme} from "../test-utils";
|
|
6
6
|
import {PasswordCard} from "./PasswordCard";
|
|
7
7
|
import type {PasswordCardSubmission} from "./securityTypes";
|
|
8
8
|
|
|
@@ -18,11 +18,11 @@ const press = async (element: Parameters<typeof fireEvent.press>[0]): Promise<vo
|
|
|
18
18
|
const renderTrackingFocus = (ui: ReactElement): ReturnType<typeof render> & {focused: string[]} => {
|
|
19
19
|
const focused: string[] = [];
|
|
20
20
|
const result = render(ui, {
|
|
21
|
-
createNodeMock: (element: ReactElement
|
|
21
|
+
createNodeMock: (element: ReactElement) => ({
|
|
22
22
|
blur: (): void => {},
|
|
23
23
|
clear: (): void => {},
|
|
24
24
|
focus: (): void => {
|
|
25
|
-
focused.push(element.
|
|
25
|
+
focused.push(elementProps<{testID?: string}>(element).testID ?? "");
|
|
26
26
|
},
|
|
27
27
|
isFocused: (): boolean => false,
|
|
28
28
|
}),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {type FC, useCallback, useEffect, useRef, useState} from "react";
|
|
2
|
-
import {type
|
|
2
|
+
import {type TextInputInstance, View} from "react-native";
|
|
3
3
|
|
|
4
4
|
import {Box} from "../Box";
|
|
5
5
|
import {Button} from "../Button";
|
|
@@ -98,16 +98,18 @@ export const PasswordCard: FC<PasswordCardProps> = ({
|
|
|
98
98
|
const [successMessage, setSuccessMessage] = useState<string | undefined>(undefined);
|
|
99
99
|
const isSubmittingRef = useRef(false);
|
|
100
100
|
const previousModeRef = useRef(mode);
|
|
101
|
-
const inputRefs = useRef<Partial<Record<keyof PasswordFieldErrors,
|
|
101
|
+
const inputRefs = useRef<Partial<Record<keyof PasswordFieldErrors, TextInputInstance | null>>>(
|
|
102
|
+
{}
|
|
103
|
+
);
|
|
102
104
|
const formRef = useRef<FormContainerHandle>(null);
|
|
103
105
|
|
|
104
|
-
const setCurrentInputRef = useCallback((ref:
|
|
106
|
+
const setCurrentInputRef = useCallback((ref: TextInputInstance | null): void => {
|
|
105
107
|
inputRefs.current.currentPassword = ref;
|
|
106
108
|
}, []);
|
|
107
|
-
const setNewInputRef = useCallback((ref:
|
|
109
|
+
const setNewInputRef = useCallback((ref: TextInputInstance | null): void => {
|
|
108
110
|
inputRefs.current.newPassword = ref;
|
|
109
111
|
}, []);
|
|
110
|
-
const setConfirmInputRef = useCallback((ref:
|
|
112
|
+
const setConfirmInputRef = useCallback((ref: TextInputInstance | null): void => {
|
|
111
113
|
inputRefs.current.confirmPassword = ref;
|
|
112
114
|
}, []);
|
|
113
115
|
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import * as Clipboard from "expo-clipboard";
|
|
2
2
|
import {type FC, type ReactElement, useCallback, useEffect, useRef, useState} from "react";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
AccessibilityInfo,
|
|
5
|
+
Platform,
|
|
6
|
+
Share,
|
|
7
|
+
type TextInputInstance,
|
|
8
|
+
View,
|
|
9
|
+
type ViewInstance,
|
|
10
|
+
} from "react-native";
|
|
4
11
|
import QRCode from "react-native-qrcode-svg";
|
|
5
12
|
|
|
6
13
|
import {Box} from "../Box";
|
|
@@ -223,8 +230,8 @@ export const TwoFactorSettingsCard: FC<TwoFactorSettingsCardProps> = ({
|
|
|
223
230
|
const [pendingAction, setPendingAction] = useState<PendingAction | undefined>(undefined);
|
|
224
231
|
const [actionError, setActionError] = useState<string | undefined>(undefined);
|
|
225
232
|
const [copyFeedback, setCopyFeedback] = useState<CopyFeedback | undefined>(undefined);
|
|
226
|
-
const passwordInputRef = useRef<
|
|
227
|
-
const stepHeadingRef = useRef<
|
|
233
|
+
const passwordInputRef = useRef<TextInputInstance | null>(null);
|
|
234
|
+
const stepHeadingRef = useRef<ViewInstance | null>(null);
|
|
228
235
|
const isFirstRenderRef = useRef(true);
|
|
229
236
|
|
|
230
237
|
const isBusy = disabled || pendingAction !== undefined;
|
|
@@ -4,7 +4,7 @@ import type {ReactElement} from "react";
|
|
|
4
4
|
import {Platform} from "react-native";
|
|
5
5
|
import {TextField} from "../TextField";
|
|
6
6
|
import {ThemeProvider} from "../Theme";
|
|
7
|
-
import {renderWithTheme} from "../test-utils";
|
|
7
|
+
import {elementProps, renderWithTheme} from "../test-utils";
|
|
8
8
|
import {defaultPasswordRequirements} from "./passwordPresets";
|
|
9
9
|
import {SignUpScreen} from "./SignUpScreen";
|
|
10
10
|
|
|
@@ -177,14 +177,14 @@ const renderWithHostMocks = (
|
|
|
177
177
|
): ReturnType<typeof render> & {focused: string[]} => {
|
|
178
178
|
const focused: string[] = [];
|
|
179
179
|
const result = render(ui, {
|
|
180
|
-
createNodeMock: (element: ReactElement
|
|
180
|
+
createNodeMock: (element: ReactElement) => {
|
|
181
181
|
if (element.type === "form") {
|
|
182
182
|
return requestSubmit ? {requestSubmit} : {};
|
|
183
183
|
}
|
|
184
184
|
return {
|
|
185
185
|
blur: (): void => {},
|
|
186
186
|
focus: (): void => {
|
|
187
|
-
focused.push(element.
|
|
187
|
+
focused.push(elementProps<{testID?: string}>(element).testID ?? "");
|
|
188
188
|
},
|
|
189
189
|
};
|
|
190
190
|
},
|
|
@@ -216,7 +216,7 @@ describe("SignUpScreen keyboard", () => {
|
|
|
216
216
|
});
|
|
217
217
|
|
|
218
218
|
it("submits once on Enter in the last field", async () => {
|
|
219
|
-
const onSubmit = mock(() => Promise.resolve());
|
|
219
|
+
const onSubmit = mock((_values: Record<string, string>) => Promise.resolve());
|
|
220
220
|
const {getByTestId} = renderWithTheme(
|
|
221
221
|
<SignUpScreen
|
|
222
222
|
fields={defaultFields}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {FC} from "react";
|
|
2
2
|
import {useCallback, useRef, useState} from "react";
|
|
3
|
-
import type {
|
|
3
|
+
import type {TextInputInstance} from "react-native";
|
|
4
4
|
|
|
5
5
|
import {Box} from "../Box";
|
|
6
6
|
import {Button} from "../Button";
|
|
@@ -94,7 +94,7 @@ export const SignUpScreen: FC<SignUpScreenProps> = ({
|
|
|
94
94
|
const isSubmitDisabled = loading || !requiredFieldsFilled || !allRequirementsMet;
|
|
95
95
|
|
|
96
96
|
const formRef = useRef<FormContainerHandle>(null);
|
|
97
|
-
const inputRefs = useRef<Record<string,
|
|
97
|
+
const inputRefs = useRef<Record<string, TextInputInstance | null>>({});
|
|
98
98
|
// Blocks a second submission (Enter plus a press, or a double Enter) before `loading` lands.
|
|
99
99
|
const isSubmittingRef = useRef(false);
|
|
100
100
|
|
package/src/table/Table.tsx
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import React, {Children, type ReactElement, useEffect} from "react";
|
|
2
|
-
import {ScrollView} from "react-native";
|
|
3
|
-
import type {DimensionValue} from "react-native/Libraries/StyleSheet/StyleSheetTypes";
|
|
2
|
+
import {type DimensionValue, ScrollView} from "react-native";
|
|
4
3
|
|
|
5
4
|
import {Box} from "../Box";
|
|
6
5
|
import type {ColumnSortInterface, TableProps, TableRowProps} from "../Common";
|
package/src/table/TableTitle.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import type {FC} from "react";
|
|
|
2
2
|
import {Text} from "react-native";
|
|
3
3
|
|
|
4
4
|
import type {TableTitleProps} from "../Common";
|
|
5
|
+
import {DATA_TABLE_HEADER_FONT_SIZE} from "../dataTableColumnWidths";
|
|
5
6
|
import {useTheme} from "../Theme";
|
|
6
7
|
|
|
7
8
|
export const TableTitle: FC<TableTitleProps> = ({title, align = "left"}) => {
|
|
@@ -17,7 +18,7 @@ export const TableTitle: FC<TableTitleProps> = ({title, align = "left"}) => {
|
|
|
17
18
|
color: theme.text.primary,
|
|
18
19
|
flexWrap: "wrap",
|
|
19
20
|
fontFamily: "text",
|
|
20
|
-
fontSize:
|
|
21
|
+
fontSize: DATA_TABLE_HEADER_FONT_SIZE,
|
|
21
22
|
fontWeight: "700",
|
|
22
23
|
lineHeight: 16,
|
|
23
24
|
overflow: "hidden",
|
|
@@ -37,7 +37,7 @@ describe("tableContext", () => {
|
|
|
37
37
|
expect(ctx.hasDrawerContents).toBe(false);
|
|
38
38
|
expect(typeof ctx.setSortColumn).toBe("function");
|
|
39
39
|
// Invoke default setSortColumn to cover the no-op default
|
|
40
|
-
expect(() => ctx.setSortColumn({column: 0, direction: "asc"})).not.toThrow();
|
|
40
|
+
expect(() => ctx.setSortColumn!({column: 0, direction: "asc"})).not.toThrow();
|
|
41
41
|
});
|
|
42
42
|
|
|
43
43
|
it("passes provided values through the provider", () => {
|
|
@@ -46,7 +46,7 @@ describe("tableContext", () => {
|
|
|
46
46
|
render(
|
|
47
47
|
<TableContextProvider
|
|
48
48
|
alternateRowBackground={false}
|
|
49
|
-
borderStyle="
|
|
49
|
+
borderStyle="none"
|
|
50
50
|
columns={[100, 200]}
|
|
51
51
|
hasDrawerContents
|
|
52
52
|
page={3}
|
|
@@ -62,9 +62,9 @@ describe("tableContext", () => {
|
|
|
62
62
|
expect(ctx.hasDrawerContents).toBe(true);
|
|
63
63
|
expect(ctx.stickyHeader).toBe(false);
|
|
64
64
|
expect(ctx.alternateRowBackground).toBe(false);
|
|
65
|
-
expect(ctx.borderStyle).toBe("
|
|
65
|
+
expect(ctx.borderStyle).toBe("none");
|
|
66
66
|
expect(ctx.sortColumn).toEqual({column: 1, direction: "desc"});
|
|
67
|
-
ctx.setSortColumn({column: 0, direction: "asc"});
|
|
67
|
+
ctx.setSortColumn!({column: 0, direction: "asc"});
|
|
68
68
|
expect(setSortColumn).toHaveBeenCalled();
|
|
69
69
|
});
|
|
70
70
|
|
|
@@ -73,7 +73,7 @@ describe("tableContext", () => {
|
|
|
73
73
|
const Consumer = () => {
|
|
74
74
|
const {setSortColumn: setter} = useTableContext();
|
|
75
75
|
useEffect(() => {
|
|
76
|
-
setter({column: 2, direction: "desc"});
|
|
76
|
+
setter!({column: 2, direction: "desc"});
|
|
77
77
|
}, [setter]);
|
|
78
78
|
return <Text>ok</Text>;
|
|
79
79
|
};
|
package/src/test-utils.tsx
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import {mock} from "bun:test";
|
|
2
2
|
import {render} from "@testing-library/react-native";
|
|
3
|
+
import type {JestNativeMatchers} from "@testing-library/react-native/build/matchers/types";
|
|
3
4
|
import type React from "react";
|
|
4
5
|
import {View} from "react-native";
|
|
6
|
+
import type {ReactTestInstance, ReactTestRendererJSON} from "react-test-renderer";
|
|
5
7
|
|
|
6
8
|
import type {CustomIconProps, IconRegistryMap} from "./Common";
|
|
7
9
|
import {IconRegistryProvider} from "./IconRegistry";
|
|
10
|
+
import type {TerrenoRequestArgs, TerrenoRequestClient} from "./requestClient";
|
|
8
11
|
import {ThemeProvider} from "./Theme";
|
|
9
12
|
|
|
10
13
|
// Register a custom icon name for tests. test-utils.tsx is excluded from the
|
|
@@ -15,6 +18,12 @@ declare module "./Common" {
|
|
|
15
18
|
}
|
|
16
19
|
}
|
|
17
20
|
|
|
21
|
+
// @testing-library/react-native registers its matchers (toHaveStyle, toHaveProp, ...) on the
|
|
22
|
+
// global expect at import time; expose their types on bun:test's expect as well.
|
|
23
|
+
declare module "bun:test" {
|
|
24
|
+
interface Matchers<T> extends JestNativeMatchers<void> {}
|
|
25
|
+
}
|
|
26
|
+
|
|
18
27
|
export const TEST_CUSTOM_ICON_TEST_ID = "test-custom-icon";
|
|
19
28
|
|
|
20
29
|
export const TestCustomIcon: React.FC<CustomIconProps> = ({size, testID}) => {
|
|
@@ -42,7 +51,7 @@ export const renderWithIcons = (
|
|
|
42
51
|
|
|
43
52
|
export const createCommonMocks = () => ({
|
|
44
53
|
onBlur: mock(() => {}),
|
|
45
|
-
onChange: mock(() => {}),
|
|
54
|
+
onChange: mock((_value: string) => {}),
|
|
46
55
|
onEnter: mock(() => {}),
|
|
47
56
|
onFocus: mock(() => {}),
|
|
48
57
|
onIconClick: mock(() => {}),
|
|
@@ -57,3 +66,125 @@ export const teardownComponentTest = () => {
|
|
|
57
66
|
// In Bun, mocks are automatically cleaned up
|
|
58
67
|
// No-op for compatibility
|
|
59
68
|
};
|
|
69
|
+
|
|
70
|
+
/** Handler for one fake route; return the response payload or throw to fail the request. */
|
|
71
|
+
export type FakeRouteHandler = (
|
|
72
|
+
args: TerrenoRequestArgs,
|
|
73
|
+
pathParams: Record<string, string>
|
|
74
|
+
) => unknown | Promise<unknown>;
|
|
75
|
+
|
|
76
|
+
export interface FakeRequestClient {
|
|
77
|
+
client: TerrenoRequestClient;
|
|
78
|
+
/** Every request in call order. */
|
|
79
|
+
calls: TerrenoRequestArgs[];
|
|
80
|
+
/** Requests whose method and URL match `route` (for example `"POST /consents/respond"`). */
|
|
81
|
+
callsTo: (route: string) => TerrenoRequestArgs[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Error shaped like a failed request: carries the HTTP `status`. */
|
|
85
|
+
export const fakeHttpError = (status: number, title = `HTTP ${status}`): Error =>
|
|
86
|
+
Object.assign(new Error(title), {data: {title}, status});
|
|
87
|
+
|
|
88
|
+
const routePattern = (route: string): {method: string; regex: RegExp; names: string[]} => {
|
|
89
|
+
const [method, path] = route.split(" ");
|
|
90
|
+
const names: string[] = [];
|
|
91
|
+
const source = path
|
|
92
|
+
.split("/")
|
|
93
|
+
.map((segment) => {
|
|
94
|
+
if (segment.startsWith(":")) {
|
|
95
|
+
names.push(segment.slice(1));
|
|
96
|
+
return "([^/]+)";
|
|
97
|
+
}
|
|
98
|
+
return segment.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
99
|
+
})
|
|
100
|
+
.join("/");
|
|
101
|
+
return {method: method.toUpperCase(), names, regex: new RegExp(`^${source}$`)};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const matchRoute = (
|
|
105
|
+
route: string,
|
|
106
|
+
args: TerrenoRequestArgs
|
|
107
|
+
): Record<string, string> | undefined => {
|
|
108
|
+
const {method, names, regex} = routePattern(route);
|
|
109
|
+
if ((args.method ?? "GET").toUpperCase() !== method) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
const match = regex.exec(args.url);
|
|
113
|
+
if (!match) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
return Object.fromEntries(names.map((name, index) => [name, match[index + 1] ?? ""]));
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* In-memory {@link TerrenoRequestClient}. Routes are `"METHOD /path/:param"` keys; a request
|
|
121
|
+
* with no matching route rejects with a 404 error.
|
|
122
|
+
*/
|
|
123
|
+
export const createFakeRequestClient = (
|
|
124
|
+
routes: Record<string, FakeRouteHandler>
|
|
125
|
+
): FakeRequestClient => {
|
|
126
|
+
const calls: TerrenoRequestArgs[] = [];
|
|
127
|
+
const request = async <T,>(args: TerrenoRequestArgs): Promise<T> => {
|
|
128
|
+
calls.push(args);
|
|
129
|
+
for (const [route, handler] of Object.entries(routes)) {
|
|
130
|
+
const pathParams = matchRoute(route, args);
|
|
131
|
+
if (pathParams) {
|
|
132
|
+
return (await handler(args, pathParams)) as T;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
throw fakeHttpError(404, `No fake route for ${args.method ?? "GET"} ${args.url}`);
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
calls,
|
|
139
|
+
callsTo: (route: string): TerrenoRequestArgs[] =>
|
|
140
|
+
calls.filter((args) => matchRoute(route, args) !== undefined),
|
|
141
|
+
client: {request},
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Host element name (e.g. "View") of a test-renderer node. The react-native mock in bunSetup.ts
|
|
147
|
+
* renders string host types that React's DOM-only JSX typings do not list, so read it as a string.
|
|
148
|
+
*/
|
|
149
|
+
const hostTypeName = (node: ReactTestInstance): string | undefined => {
|
|
150
|
+
const type: unknown = node.type;
|
|
151
|
+
return typeof type === "string" ? type : undefined;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const isHostType = (node: ReactTestInstance, name: string): boolean => {
|
|
155
|
+
return hostTypeName(node) === name;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/** Typed equivalent of `root.findByType("View")` for string host types. */
|
|
159
|
+
export const findByHostType = (root: ReactTestInstance, name: string): ReactTestInstance => {
|
|
160
|
+
return root.find((node) => isHostType(node, name));
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/** Typed equivalent of `root.findAllByType("View")` for string host types. */
|
|
164
|
+
export const findAllByHostType = (
|
|
165
|
+
root: ReactTestInstance,
|
|
166
|
+
name: string,
|
|
167
|
+
options?: {deep: boolean}
|
|
168
|
+
): ReactTestInstance[] => {
|
|
169
|
+
return root.findAll((node) => isHostType(node, name), options);
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Props of a React element handed to `createNodeMock` or returned from a render method. React
|
|
174
|
+
* types element props as `unknown`; tests read the few host props they inspect through this.
|
|
175
|
+
*/
|
|
176
|
+
export const elementProps = <P extends object = Record<string, unknown>>(
|
|
177
|
+
element: React.ReactElement
|
|
178
|
+
): Partial<P> => {
|
|
179
|
+
return (element.props ?? {}) as Partial<P>;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/** Narrows a `toJSON()` result to its single root node; fails when the render has several roots. */
|
|
183
|
+
export const rootJSONNode = (
|
|
184
|
+
tree: ReactTestRendererJSON | ReactTestRendererJSON[] | null
|
|
185
|
+
): ReactTestRendererJSON | null => {
|
|
186
|
+
if (Array.isArray(tree)) {
|
|
187
|
+
throw new Error(`Expected a single root node, got ${tree.length}`);
|
|
188
|
+
}
|
|
189
|
+
return tree;
|
|
190
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// ResponsiveBreakpoint.test.tsx renders through react-dom/server to cover the SSR path, and the
|
|
2
|
+
// workspace does not install @types/react-dom. Declare only the entry point the tests use.
|
|
3
|
+
declare module "react-dom/server" {
|
|
4
|
+
import type {ReactNode} from "react";
|
|
5
|
+
|
|
6
|
+
export const renderToString: (element: ReactNode) => string;
|
|
7
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {Platform} from "react-native";
|
|
2
|
+
|
|
3
|
+
import type {TextSize} from "./Common";
|
|
4
|
+
|
|
5
|
+
interface TextFontSizeAndWeight {
|
|
6
|
+
size: number;
|
|
7
|
+
weight: "regular" | "medium";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const fontSizeAndWeightWeb: Record<TextSize, TextFontSizeAndWeight> = {
|
|
11
|
+
"2xl": {size: 48, weight: "medium"},
|
|
12
|
+
lg: {size: 18, weight: "medium"},
|
|
13
|
+
md: {size: 16, weight: "regular"},
|
|
14
|
+
sm: {size: 12, weight: "regular"},
|
|
15
|
+
xl: {size: 20, weight: "medium"},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const fontSizeAndWeightMobile: Record<TextSize, TextFontSizeAndWeight> = {
|
|
19
|
+
"2xl": {size: 40, weight: "medium"},
|
|
20
|
+
lg: {size: 16, weight: "medium"},
|
|
21
|
+
md: {size: 14, weight: "regular"},
|
|
22
|
+
sm: {size: 10, weight: "regular"},
|
|
23
|
+
xl: {size: 18, weight: "medium"},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Font size and weight for each `Text` size on the current platform. Shared by `Text` and by
|
|
28
|
+
* anything that has to predict how wide `Text` will render, such as DataTable auto widths.
|
|
29
|
+
*/
|
|
30
|
+
export const textFontSizes: Record<TextSize, TextFontSizeAndWeight> =
|
|
31
|
+
Platform.OS === "web" ? fontSizeAndWeightWeb : fontSizeAndWeightMobile;
|