@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,272 @@
|
|
|
1
|
+
import {Platform} from "react-native";
|
|
2
|
+
|
|
3
|
+
import type {DataTableCellData, DataTableColumn} from "./Common";
|
|
4
|
+
import {textFontSizes} from "./textFontSizes";
|
|
5
|
+
|
|
6
|
+
// Layout values DataTable renders with. DataTable.tsx and TableTitle import these, so the width
|
|
7
|
+
// estimate below and the rendered table cannot drift apart.
|
|
8
|
+
|
|
9
|
+
/** Padding on every side of each header and body cell. */
|
|
10
|
+
export const DATA_TABLE_CELL_PADDING = 16;
|
|
11
|
+
/** Horizontal padding on each header and body cell (both sides). */
|
|
12
|
+
export const DATA_TABLE_CELL_HORIZONTAL_PADDING = DATA_TABLE_CELL_PADDING * 2;
|
|
13
|
+
/** Font size of the uppercase, bold column title in the header. */
|
|
14
|
+
export const DATA_TABLE_HEADER_FONT_SIZE = 10;
|
|
15
|
+
/** Diameter of the round sort button in a sortable column's header. */
|
|
16
|
+
export const DATA_TABLE_SORT_BUTTON_SIZE = 16;
|
|
17
|
+
/** Gap between the header title and the sort button. */
|
|
18
|
+
export const DATA_TABLE_SORT_BUTTON_MARGIN = 8;
|
|
19
|
+
/** Width of the fixed "more" column drawn when `moreContentComponent` is set. */
|
|
20
|
+
export const DATA_TABLE_MORE_COLUMN_WIDTH = 48;
|
|
21
|
+
/** Border around the table body. */
|
|
22
|
+
export const DATA_TABLE_BORDER_WIDTH = 1;
|
|
23
|
+
|
|
24
|
+
/** Narrowest an auto-sized column starts at, before any fill is added. */
|
|
25
|
+
export const DATA_TABLE_AUTO_MIN_COLUMN_WIDTH = 80;
|
|
26
|
+
/** Widest an auto-sized column starts at, before any fill is added. Longer text wraps or clips. */
|
|
27
|
+
export const DATA_TABLE_AUTO_MAX_COLUMN_WIDTH = 360;
|
|
28
|
+
/** Rows read per column when estimating content width. Spread evenly across the data. */
|
|
29
|
+
export const DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS = 50;
|
|
30
|
+
/** Content width assumed for a custom cell whose value is not a string or number. */
|
|
31
|
+
export const DATA_TABLE_AUTO_CUSTOM_CONTENT_WIDTH = 120;
|
|
32
|
+
|
|
33
|
+
const HEADER_SORT_ICON_WIDTH = DATA_TABLE_SORT_BUTTON_SIZE + DATA_TABLE_SORT_BUTTON_MARGIN;
|
|
34
|
+
// InfoModalIcon and DataTableColumnFilterWeb size themselves; these are their rendered widths.
|
|
35
|
+
const HEADER_INFO_ICON_WIDTH = 26;
|
|
36
|
+
const HEADER_FILTER_ICON_WIDTH = 40;
|
|
37
|
+
const BOOLEAN_ICON_WIDTH = 20;
|
|
38
|
+
|
|
39
|
+
// Font families registered by TerrenoFontProvider, as Text picks them for sm/md cells and
|
|
40
|
+
// TableTitle's bold header.
|
|
41
|
+
const HEADER_FONT_FAMILY = "text-bold";
|
|
42
|
+
const CELL_FONT_FAMILY = "text-regular";
|
|
43
|
+
|
|
44
|
+
// Average glyph width as a share of font size, for Nunito. Native has no synchronous text
|
|
45
|
+
// measurement, so it estimates; web measures with a canvas.
|
|
46
|
+
const REGULAR_CHAR_WIDTH_RATIO = 0.56;
|
|
47
|
+
const BOLD_UPPERCASE_CHAR_WIDTH_RATIO = 0.72;
|
|
48
|
+
|
|
49
|
+
export type DataTableTextSize = "sm" | "md" | "lg";
|
|
50
|
+
|
|
51
|
+
export interface MeasureDataTableTextArgs {
|
|
52
|
+
text: string;
|
|
53
|
+
fontSize: number;
|
|
54
|
+
isHeader: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type MeasureDataTableText = (args: MeasureDataTableTextArgs) => number;
|
|
58
|
+
|
|
59
|
+
export const estimateDataTableTextWidth: MeasureDataTableText = ({text, fontSize, isHeader}) => {
|
|
60
|
+
const ratio = isHeader ? BOLD_UPPERCASE_CHAR_WIDTH_RATIO : REGULAR_CHAR_WIDTH_RATIO;
|
|
61
|
+
return Math.ceil(text.length * fontSize * ratio);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
interface CanvasTextContext {
|
|
65
|
+
font: string;
|
|
66
|
+
measureText: (text: string) => {width: number};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface CanvasDocument {
|
|
70
|
+
createElement?: (tag: string) => unknown;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface CreateDataTableTextMeasurerArgs {
|
|
74
|
+
/** Document to create the measuring canvas from. Defaults to the global document. */
|
|
75
|
+
getDocument?: () => CanvasDocument | undefined;
|
|
76
|
+
/** Whether to measure with a canvas. Defaults to `Platform.OS === "web"`. */
|
|
77
|
+
isWeb?: () => boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const getGlobalDocument = (): CanvasDocument | undefined =>
|
|
81
|
+
(globalThis as {document?: CanvasDocument}).document;
|
|
82
|
+
|
|
83
|
+
const isWebPlatform = (): boolean => Platform.OS === "web";
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Builds a measurer that uses one shared canvas on web (no layout or reflow) and estimates
|
|
87
|
+
* everywhere else, or when no canvas is available.
|
|
88
|
+
*/
|
|
89
|
+
export const createDataTableTextMeasurer = ({
|
|
90
|
+
getDocument = getGlobalDocument,
|
|
91
|
+
isWeb = isWebPlatform,
|
|
92
|
+
}: CreateDataTableTextMeasurerArgs = {}): MeasureDataTableText => {
|
|
93
|
+
let canvasContext: CanvasTextContext | null | undefined;
|
|
94
|
+
|
|
95
|
+
const getCanvasContext = (): CanvasTextContext | null => {
|
|
96
|
+
if (canvasContext !== undefined) {
|
|
97
|
+
return canvasContext;
|
|
98
|
+
}
|
|
99
|
+
const doc = getDocument();
|
|
100
|
+
if (!doc?.createElement) {
|
|
101
|
+
canvasContext = null;
|
|
102
|
+
return canvasContext;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const canvas = doc.createElement("canvas") as {getContext?: (kind: string) => unknown};
|
|
106
|
+
canvasContext = (canvas.getContext?.("2d") as CanvasTextContext | null) ?? null;
|
|
107
|
+
} catch {
|
|
108
|
+
canvasContext = null;
|
|
109
|
+
}
|
|
110
|
+
return canvasContext;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return (args) => {
|
|
114
|
+
const context = isWeb() ? getCanvasContext() : null;
|
|
115
|
+
if (!context) {
|
|
116
|
+
return estimateDataTableTextWidth(args);
|
|
117
|
+
}
|
|
118
|
+
const family = args.isHeader ? HEADER_FONT_FAMILY : CELL_FONT_FAMILY;
|
|
119
|
+
const weight = args.isHeader ? 700 : 400;
|
|
120
|
+
context.font = `${weight} ${args.fontSize}px ${family}, Nunito, sans-serif`;
|
|
121
|
+
const measured = context.measureText(args.isHeader ? args.text.toUpperCase() : args.text).width;
|
|
122
|
+
// Fall back if the canvas is a stub (jsdom returns 0).
|
|
123
|
+
if (!measured && args.text.length > 0) {
|
|
124
|
+
return estimateDataTableTextWidth(args);
|
|
125
|
+
}
|
|
126
|
+
return Math.ceil(measured);
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/** Default measurer: canvas on web, estimate on native. */
|
|
131
|
+
export const measureDataTableTextWidth: MeasureDataTableText = createDataTableTextMeasurer();
|
|
132
|
+
|
|
133
|
+
const sampleRowIndexes = (rowCount: number): number[] => {
|
|
134
|
+
if (rowCount <= DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS) {
|
|
135
|
+
return Array.from({length: rowCount}, (_, index) => index);
|
|
136
|
+
}
|
|
137
|
+
const stride = rowCount / DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS;
|
|
138
|
+
return Array.from({length: DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS}, (_, index) =>
|
|
139
|
+
Math.floor(index * stride)
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const cellText = (
|
|
144
|
+
column: DataTableColumn,
|
|
145
|
+
cell: DataTableCellData,
|
|
146
|
+
formatDate: (value: unknown) => string
|
|
147
|
+
): string | undefined => {
|
|
148
|
+
const {value} = cell;
|
|
149
|
+
if (column.columnType === "date") {
|
|
150
|
+
return formatDate(value);
|
|
151
|
+
}
|
|
152
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
153
|
+
return String(value);
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const clampWidth = (width: number): number =>
|
|
159
|
+
Math.min(DATA_TABLE_AUTO_MAX_COLUMN_WIDTH, Math.max(DATA_TABLE_AUTO_MIN_COLUMN_WIDTH, width));
|
|
160
|
+
|
|
161
|
+
export interface GetDataTableNaturalWidthsArgs {
|
|
162
|
+
columns: DataTableColumn[];
|
|
163
|
+
data: DataTableCellData[][];
|
|
164
|
+
defaultTextSize: DataTableTextSize;
|
|
165
|
+
formatDate: (value: unknown) => string;
|
|
166
|
+
/** Column types drawn by `customColumnComponentMap`; their content is not text we can measure. */
|
|
167
|
+
customColumnTypes?: Set<string>;
|
|
168
|
+
hasFilters?: boolean;
|
|
169
|
+
measureText?: MeasureDataTableText;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Width each column wants on its own: the wider of its header and its sampled content, clamped
|
|
174
|
+
* to a sane range. Columns with an explicit `width` keep it. Runs only when columns or data change.
|
|
175
|
+
*/
|
|
176
|
+
export const getDataTableNaturalWidths = ({
|
|
177
|
+
columns,
|
|
178
|
+
data,
|
|
179
|
+
defaultTextSize,
|
|
180
|
+
formatDate,
|
|
181
|
+
customColumnTypes,
|
|
182
|
+
hasFilters = false,
|
|
183
|
+
measureText = measureDataTableTextWidth,
|
|
184
|
+
}: GetDataTableNaturalWidthsArgs): number[] => {
|
|
185
|
+
const rowIndexes = sampleRowIndexes(data.length);
|
|
186
|
+
|
|
187
|
+
return columns.map((column, columnIndex) => {
|
|
188
|
+
if (column.width !== undefined) {
|
|
189
|
+
return column.width;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let headerWidth = column.title
|
|
193
|
+
? measureText({fontSize: DATA_TABLE_HEADER_FONT_SIZE, isHeader: true, text: column.title})
|
|
194
|
+
: 0;
|
|
195
|
+
if (column.sortable) {
|
|
196
|
+
headerWidth += HEADER_SORT_ICON_WIDTH;
|
|
197
|
+
}
|
|
198
|
+
if (column.infoModalText) {
|
|
199
|
+
headerWidth += HEADER_INFO_ICON_WIDTH;
|
|
200
|
+
}
|
|
201
|
+
if (column.filter && hasFilters && Platform.OS === "web") {
|
|
202
|
+
headerWidth += HEADER_FILTER_ICON_WIDTH;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let contentWidth = 0;
|
|
206
|
+
const isCustom = customColumnTypes?.has(column.columnType) ?? false;
|
|
207
|
+
if (column.columnType === "boolean" && !isCustom) {
|
|
208
|
+
contentWidth = BOOLEAN_ICON_WIDTH;
|
|
209
|
+
} else {
|
|
210
|
+
for (const rowIndex of rowIndexes) {
|
|
211
|
+
const cell = data[rowIndex]?.[columnIndex];
|
|
212
|
+
if (!cell) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const text = cellText(column, cell, formatDate);
|
|
216
|
+
if (text === undefined) {
|
|
217
|
+
if (isCustom) {
|
|
218
|
+
contentWidth = Math.max(contentWidth, DATA_TABLE_AUTO_CUSTOM_CONTENT_WIDTH);
|
|
219
|
+
}
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const fontSize = textFontSizes[cell.textSize ?? defaultTextSize].size;
|
|
223
|
+
contentWidth = Math.max(contentWidth, measureText({fontSize, isHeader: false, text}));
|
|
224
|
+
if (contentWidth >= DATA_TABLE_AUTO_MAX_COLUMN_WIDTH) {
|
|
225
|
+
// Already at the cap; the rest of the sample cannot change the result.
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return clampWidth(Math.max(headerWidth, contentWidth) + DATA_TABLE_CELL_HORIZONTAL_PADDING);
|
|
232
|
+
});
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
export interface FitDataTableColumnWidthsArgs {
|
|
236
|
+
columns: DataTableColumn[];
|
|
237
|
+
naturalWidths: number[];
|
|
238
|
+
/** Width the columns can fill. 0 (not measured yet) keeps the natural widths. */
|
|
239
|
+
availableWidth: number;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Grows auto-sized columns to fill the container, in proportion to their natural widths. When
|
|
244
|
+
* the columns already overflow, they keep their natural widths and the table scrolls
|
|
245
|
+
* horizontally. Columns with an explicit `width` never change.
|
|
246
|
+
*/
|
|
247
|
+
export const fitDataTableColumnWidths = ({
|
|
248
|
+
columns,
|
|
249
|
+
naturalWidths,
|
|
250
|
+
availableWidth,
|
|
251
|
+
}: FitDataTableColumnWidthsArgs): number[] => {
|
|
252
|
+
const autoIndexes = columns.flatMap((column, index) =>
|
|
253
|
+
column.width === undefined ? [index] : []
|
|
254
|
+
);
|
|
255
|
+
const totalNatural = naturalWidths.reduce((sum, width) => sum + width, 0);
|
|
256
|
+
const extra = Math.floor(availableWidth - totalNatural);
|
|
257
|
+
if (autoIndexes.length === 0 || availableWidth <= 0 || extra <= 0) {
|
|
258
|
+
return naturalWidths;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const autoNatural = autoIndexes.reduce((sum, index) => sum + naturalWidths[index], 0);
|
|
262
|
+
const widths = [...naturalWidths];
|
|
263
|
+
let assigned = 0;
|
|
264
|
+
for (const index of autoIndexes) {
|
|
265
|
+
const share = Math.floor((extra * naturalWidths[index]) / autoNatural);
|
|
266
|
+
widths[index] += share;
|
|
267
|
+
assigned += share;
|
|
268
|
+
}
|
|
269
|
+
// Rounding leftovers go to the last auto column so the row is exactly the container width.
|
|
270
|
+
widths[autoIndexes[autoIndexes.length - 1]] += extra - assigned;
|
|
271
|
+
return widths;
|
|
272
|
+
};
|
|
@@ -450,7 +450,7 @@ describe("DataTableAdditionalFiltersWeb", () => {
|
|
|
450
450
|
});
|
|
451
451
|
|
|
452
452
|
it("keeps in-progress drafts when the filters array is recreated", async () => {
|
|
453
|
-
const onApply = mock(() => {});
|
|
453
|
+
const onApply = mock((_values: Record<string, unknown>) => {});
|
|
454
454
|
const departmentFilter = {
|
|
455
455
|
field: "department",
|
|
456
456
|
kind: "choice" as const,
|
package/src/index.tsx
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
// Re-export React Native style types for
|
|
2
|
-
export type {
|
|
1
|
+
// Re-export React Native style and host-instance types for consumer projects.
|
|
2
|
+
export type {
|
|
3
|
+
ScrollViewInstance,
|
|
4
|
+
StyleProp,
|
|
5
|
+
TextInputInstance,
|
|
6
|
+
ViewInstance,
|
|
7
|
+
ViewStyle,
|
|
8
|
+
} from "react-native";
|
|
3
9
|
export * from "./Accordion";
|
|
4
10
|
export * from "./ActionSheet";
|
|
5
11
|
export * from "./AddressField";
|
|
@@ -138,6 +144,7 @@ export type * from "./ProvisionalConsentSigner";
|
|
|
138
144
|
export * from "./pdfHtmlTemplate";
|
|
139
145
|
export * from "./Radio";
|
|
140
146
|
export * from "./RadioField";
|
|
147
|
+
export type {TerrenoRequestArgs, TerrenoRequestClient} from "./requestClient";
|
|
141
148
|
export type * from "./Scorecard";
|
|
142
149
|
export * from "./ScrollView";
|
|
143
150
|
export * from "./SectionDivider";
|
|
@@ -5,7 +5,7 @@ import {useEffect} from "react";
|
|
|
5
5
|
import {Text, useWindowDimensions} from "react-native";
|
|
6
6
|
|
|
7
7
|
import type {ListDetailState} from "../Common";
|
|
8
|
-
import {renderWithTheme} from "../test-utils";
|
|
8
|
+
import {findAllByHostType, renderWithTheme} from "../test-utils";
|
|
9
9
|
|
|
10
10
|
// This file runs in its own process (src/isolated), so it can install route and router doubles
|
|
11
11
|
// for the hooks this layout reads without affecting other suites.
|
|
@@ -126,9 +126,7 @@ describe("ListDetailLayout", () => {
|
|
|
126
126
|
const {getByTestId, queryByTestId} = renderLayout();
|
|
127
127
|
|
|
128
128
|
expect(queryByTestId("list-detail-list-pane")).toBeNull();
|
|
129
|
-
expect(
|
|
130
|
-
getByTestId("list-detail-detail-pane").findAll((node) => node.type === "Stack")
|
|
131
|
-
).toHaveLength(1);
|
|
129
|
+
expect(findAllByHostType(getByTestId("list-detail-detail-pane"), "Stack")).toHaveLength(1);
|
|
132
130
|
expect(getByTestId("split-probe").props.children).toBe("stacked");
|
|
133
131
|
});
|
|
134
132
|
|
|
@@ -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 {LoginScreen} from "./LoginScreen";
|
|
9
9
|
import type {LoginAccountOption} from "./loginTypes";
|
|
10
10
|
|
|
@@ -252,7 +252,7 @@ describe("LoginScreen", () => {
|
|
|
252
252
|
});
|
|
253
253
|
|
|
254
254
|
it("calls onSubmit with form values when submit button is pressed", async () => {
|
|
255
|
-
const onSubmit = mock(() => Promise.resolve());
|
|
255
|
+
const onSubmit = mock((_values: Record<string, string>) => Promise.resolve());
|
|
256
256
|
const {getByTestId} = renderWithTheme(
|
|
257
257
|
<LoginScreen fields={defaultFields} onSubmit={onSubmit} />
|
|
258
258
|
);
|
|
@@ -845,14 +845,14 @@ const renderWithHostMocks = (
|
|
|
845
845
|
): ReturnType<typeof render> & {focused: string[]} => {
|
|
846
846
|
const focused: string[] = [];
|
|
847
847
|
const result = render(ui, {
|
|
848
|
-
createNodeMock: (element: ReactElement
|
|
848
|
+
createNodeMock: (element: ReactElement) => {
|
|
849
849
|
if (element.type === "form") {
|
|
850
850
|
return requestSubmit ? {requestSubmit} : {};
|
|
851
851
|
}
|
|
852
852
|
return {
|
|
853
853
|
blur: (): void => {},
|
|
854
854
|
focus: (): void => {
|
|
855
|
-
focused.push(element.
|
|
855
|
+
focused.push(elementProps<{testID?: string}>(element).testID ?? "");
|
|
856
856
|
},
|
|
857
857
|
};
|
|
858
858
|
},
|
|
@@ -900,7 +900,7 @@ describe("LoginScreen keyboard and autofill", () => {
|
|
|
900
900
|
});
|
|
901
901
|
|
|
902
902
|
it("submits once on Enter in the password field", async () => {
|
|
903
|
-
const onSubmit = mock(() => Promise.resolve());
|
|
903
|
+
const onSubmit = mock((_values: Record<string, string>) => Promise.resolve());
|
|
904
904
|
const {getByTestId} = renderWithTheme(
|
|
905
905
|
<LoginScreen fields={defaultFields} onSubmit={onSubmit} />
|
|
906
906
|
);
|
|
@@ -973,7 +973,7 @@ describe("LoginScreen keyboard and autofill", () => {
|
|
|
973
973
|
Platform.OS = "web";
|
|
974
974
|
try {
|
|
975
975
|
const requestSubmit = mock(() => {});
|
|
976
|
-
const onSubmit = mock(() => Promise.resolve());
|
|
976
|
+
const onSubmit = mock((_values: Record<string, string>) => Promise.resolve());
|
|
977
977
|
const {UNSAFE_getByType, getByTestId} = renderWithHostMocks(
|
|
978
978
|
<LoginScreen fields={defaultFields} onSubmit={onSubmit} />,
|
|
979
979
|
{requestSubmit}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {FC, ReactElement, ReactNode} from "react";
|
|
2
2
|
import {useCallback, useEffect, useRef, useState} from "react";
|
|
3
|
-
import {Platform, Pressable, type
|
|
3
|
+
import {Platform, Pressable, type TextInputInstance, View} from "react-native";
|
|
4
4
|
|
|
5
5
|
import {Avatar} from "../Avatar";
|
|
6
6
|
import {Box} from "../Box";
|
|
@@ -276,7 +276,7 @@ export const LoginScreen: FC<LoginScreenProps> = ({
|
|
|
276
276
|
}, []);
|
|
277
277
|
|
|
278
278
|
const formRef = useRef<FormContainerHandle>(null);
|
|
279
|
-
const inputRefs = useRef<Record<string,
|
|
279
|
+
const inputRefs = useRef<Record<string, TextInputInstance | null>>({});
|
|
280
280
|
const isSubmittingRef = useRef(false);
|
|
281
281
|
|
|
282
282
|
const handlePasskeyPress = useCallback(async (): Promise<void> => {
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {act, renderHook, waitFor} from "@testing-library/react-native";
|
|
3
|
+
import {useCallback} from "react";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
invalidateClientTags,
|
|
7
|
+
type TerrenoRequestClient,
|
|
8
|
+
unwrapDataEnvelope,
|
|
9
|
+
useClientMutation,
|
|
10
|
+
useClientQuery,
|
|
11
|
+
} from "./requestClient";
|
|
12
|
+
import {createFakeRequestClient, fakeHttpError} from "./test-utils";
|
|
13
|
+
|
|
14
|
+
// Mirror of `@terreno/syncdb/api` ApiRequestArgs / ApiClient (ui cannot import syncdb).
|
|
15
|
+
// The assignment below fails to compile if TerrenoRequestClient stops being a structural
|
|
16
|
+
// subset of the syncdb client.
|
|
17
|
+
interface SyncdbApiRequestArgs {
|
|
18
|
+
url: string;
|
|
19
|
+
method?: string;
|
|
20
|
+
body?: BodyInit | Record<string, unknown> | readonly unknown[] | null;
|
|
21
|
+
params?: Record<string, unknown>;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
credentials?: RequestCredentials;
|
|
24
|
+
signal?: AbortSignal;
|
|
25
|
+
timeoutMs?: number;
|
|
26
|
+
parseAs?: "json" | "text" | "blob";
|
|
27
|
+
unwrap?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface SyncdbApiClientShape {
|
|
31
|
+
readonly baseUrl: string | undefined;
|
|
32
|
+
request: <T = unknown>(args: SyncdbApiRequestArgs) => Promise<T>;
|
|
33
|
+
invalidateTags: (tags: readonly string[]) => void;
|
|
34
|
+
reset: () => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const deferred = <T>(): {
|
|
38
|
+
promise: Promise<T>;
|
|
39
|
+
reject: (error: unknown) => void;
|
|
40
|
+
resolve: (value: T) => void;
|
|
41
|
+
} => {
|
|
42
|
+
let resolve: (value: T) => void = () => {};
|
|
43
|
+
let reject: (error: unknown) => void = () => {};
|
|
44
|
+
const promise = new Promise<T>((res, rej) => {
|
|
45
|
+
resolve = res;
|
|
46
|
+
reject = rej;
|
|
47
|
+
});
|
|
48
|
+
return {promise, reject, resolve};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const useCountQuery = (
|
|
52
|
+
client: TerrenoRequestClient,
|
|
53
|
+
providesTag?: string
|
|
54
|
+
): ReturnType<typeof useClientQuery<number>> => {
|
|
55
|
+
const fetcher = useCallback(() => client.request<number>({url: "/count"}), [client]);
|
|
56
|
+
return useClientQuery({client, fetcher, providesTag});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
describe("TerrenoRequestClient", () => {
|
|
60
|
+
it("accepts a syncdb ApiClient structurally", async () => {
|
|
61
|
+
const syncdbClient: SyncdbApiClientShape = {
|
|
62
|
+
baseUrl: "http://api.test",
|
|
63
|
+
invalidateTags: () => {},
|
|
64
|
+
request: async <T>(args: SyncdbApiRequestArgs): Promise<T> => args.url as T,
|
|
65
|
+
reset: () => {},
|
|
66
|
+
};
|
|
67
|
+
const client: TerrenoRequestClient = syncdbClient;
|
|
68
|
+
expect(await client.request<string>({url: "/consents/pending"})).toBe("/consents/pending");
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("useClientQuery", () => {
|
|
73
|
+
it("loads on mount and exposes data", async () => {
|
|
74
|
+
const {client} = createFakeRequestClient({"GET /count": () => 3});
|
|
75
|
+
const {result} = renderHook(() => useCountQuery(client));
|
|
76
|
+
expect(result.current.isLoading).toBe(true);
|
|
77
|
+
await waitFor(() => expect(result.current.data).toBe(3));
|
|
78
|
+
expect(result.current.isLoading).toBe(false);
|
|
79
|
+
expect(result.current.isFetching).toBe(false);
|
|
80
|
+
expect(result.current.error).toBeUndefined();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("surfaces request errors and recovers on refetch", async () => {
|
|
84
|
+
let shouldFail = true;
|
|
85
|
+
const {client} = createFakeRequestClient({
|
|
86
|
+
"GET /count": () => {
|
|
87
|
+
if (shouldFail) {
|
|
88
|
+
throw fakeHttpError(500);
|
|
89
|
+
}
|
|
90
|
+
return 7;
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
const {result} = renderHook(() => useCountQuery(client));
|
|
94
|
+
await waitFor(() => expect((result.current.error as {status?: number})?.status).toBe(500));
|
|
95
|
+
expect(result.current.isLoading).toBe(false);
|
|
96
|
+
|
|
97
|
+
shouldFail = false;
|
|
98
|
+
await act(async () => {
|
|
99
|
+
await result.current.refetch();
|
|
100
|
+
});
|
|
101
|
+
expect(result.current.data).toBe(7);
|
|
102
|
+
expect(result.current.error).toBeUndefined();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("joins an in-flight request on refetch", async () => {
|
|
106
|
+
const pending = deferred<number>();
|
|
107
|
+
const {client, calls} = createFakeRequestClient({"GET /count": () => pending.promise});
|
|
108
|
+
const {result} = renderHook(() => useCountQuery(client));
|
|
109
|
+
let refetchPromise: Promise<void> = Promise.resolve();
|
|
110
|
+
act(() => {
|
|
111
|
+
refetchPromise = result.current.refetch();
|
|
112
|
+
});
|
|
113
|
+
expect(calls).toHaveLength(1);
|
|
114
|
+
await act(async () => {
|
|
115
|
+
pending.resolve(5);
|
|
116
|
+
await refetchPromise;
|
|
117
|
+
});
|
|
118
|
+
expect(result.current.data).toBe(5);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("keeps data while refetching so isLoading stays false", async () => {
|
|
122
|
+
let value = 1;
|
|
123
|
+
const next = deferred<number>();
|
|
124
|
+
const {client} = createFakeRequestClient({
|
|
125
|
+
"GET /count": () => (value === 1 ? value : next.promise),
|
|
126
|
+
});
|
|
127
|
+
const {result} = renderHook(() => useCountQuery(client));
|
|
128
|
+
await waitFor(() => expect(result.current.data).toBe(1));
|
|
129
|
+
value = 2;
|
|
130
|
+
let refetchPromise: Promise<void> = Promise.resolve();
|
|
131
|
+
act(() => {
|
|
132
|
+
refetchPromise = result.current.refetch();
|
|
133
|
+
});
|
|
134
|
+
expect(result.current.isFetching).toBe(true);
|
|
135
|
+
expect(result.current.isLoading).toBe(false);
|
|
136
|
+
await act(async () => {
|
|
137
|
+
next.resolve(2);
|
|
138
|
+
await refetchPromise;
|
|
139
|
+
});
|
|
140
|
+
expect(result.current.data).toBe(2);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("refetches with a fresh request when its tag is invalidated", async () => {
|
|
144
|
+
let value = 1;
|
|
145
|
+
const {client, calls} = createFakeRequestClient({"GET /count": () => value});
|
|
146
|
+
const {result} = renderHook(() => useCountQuery(client, "Counts"));
|
|
147
|
+
await waitFor(() => expect(result.current.data).toBe(1));
|
|
148
|
+
|
|
149
|
+
value = 2;
|
|
150
|
+
await act(async () => {
|
|
151
|
+
invalidateClientTags({client, tags: ["Counts"]});
|
|
152
|
+
});
|
|
153
|
+
await waitFor(() => expect(result.current.data).toBe(2));
|
|
154
|
+
expect(calls).toHaveLength(2);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("ignores invalidations for other tags and other clients", async () => {
|
|
158
|
+
const first = createFakeRequestClient({"GET /count": () => 1});
|
|
159
|
+
const second = createFakeRequestClient({"GET /count": () => 1});
|
|
160
|
+
const {result} = renderHook(() => useCountQuery(first.client, "Counts"));
|
|
161
|
+
await waitFor(() => expect(result.current.data).toBe(1));
|
|
162
|
+
|
|
163
|
+
await act(async () => {
|
|
164
|
+
invalidateClientTags({client: first.client, tags: ["Other"]});
|
|
165
|
+
invalidateClientTags({client: second.client, tags: ["Counts"]});
|
|
166
|
+
});
|
|
167
|
+
expect(first.calls).toHaveLength(1);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("stops listening for invalidations after unmount", async () => {
|
|
171
|
+
const {client, calls} = createFakeRequestClient({"GET /count": () => 1});
|
|
172
|
+
const {result, unmount} = renderHook(() => useCountQuery(client, "Counts"));
|
|
173
|
+
await waitFor(() => expect(result.current.data).toBe(1));
|
|
174
|
+
unmount();
|
|
175
|
+
invalidateClientTags({client, tags: ["Counts"]});
|
|
176
|
+
expect(calls).toHaveLength(1);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("drops a stale response when a newer request was started", async () => {
|
|
180
|
+
const firstResponse = deferred<number>();
|
|
181
|
+
let callCount = 0;
|
|
182
|
+
const {client} = createFakeRequestClient({
|
|
183
|
+
"GET /count": () => {
|
|
184
|
+
callCount += 1;
|
|
185
|
+
return callCount === 1 ? firstResponse.promise : 2;
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
const {result} = renderHook(() => useCountQuery(client, "Counts"));
|
|
189
|
+
await act(async () => {
|
|
190
|
+
invalidateClientTags({client, tags: ["Counts"]});
|
|
191
|
+
});
|
|
192
|
+
await waitFor(() => expect(result.current.data).toBe(2));
|
|
193
|
+
await act(async () => {
|
|
194
|
+
firstResponse.resolve(1);
|
|
195
|
+
await firstResponse.promise;
|
|
196
|
+
});
|
|
197
|
+
expect(result.current.data).toBe(2);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("drops a stale error when a newer request was started", async () => {
|
|
201
|
+
const firstResponse = deferred<number>();
|
|
202
|
+
let callCount = 0;
|
|
203
|
+
const {client} = createFakeRequestClient({
|
|
204
|
+
"GET /count": () => {
|
|
205
|
+
callCount += 1;
|
|
206
|
+
return callCount === 1 ? firstResponse.promise : 2;
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
const {result} = renderHook(() => useCountQuery(client, "Counts"));
|
|
210
|
+
await act(async () => {
|
|
211
|
+
invalidateClientTags({client, tags: ["Counts"]});
|
|
212
|
+
});
|
|
213
|
+
await waitFor(() => expect(result.current.data).toBe(2));
|
|
214
|
+
await act(async () => {
|
|
215
|
+
firstResponse.reject(fakeHttpError(500));
|
|
216
|
+
await firstResponse.promise.catch(() => {});
|
|
217
|
+
});
|
|
218
|
+
expect(result.current.error).toBeUndefined();
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe("useClientMutation", () => {
|
|
223
|
+
it("tracks loading and resolves when the request succeeds", async () => {
|
|
224
|
+
const pending = deferred<void>();
|
|
225
|
+
const run = mock((_value: string) => pending.promise);
|
|
226
|
+
const {result} = renderHook(() => useClientMutation({run}));
|
|
227
|
+
let mutatePromise: Promise<void> = Promise.resolve();
|
|
228
|
+
act(() => {
|
|
229
|
+
mutatePromise = result.current.mutate("a");
|
|
230
|
+
});
|
|
231
|
+
expect(result.current.isLoading).toBe(true);
|
|
232
|
+
await act(async () => {
|
|
233
|
+
pending.resolve();
|
|
234
|
+
await mutatePromise;
|
|
235
|
+
});
|
|
236
|
+
expect(result.current.isLoading).toBe(false);
|
|
237
|
+
expect(run).toHaveBeenCalledWith("a");
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("stores and rethrows the request error", async () => {
|
|
241
|
+
const failure = fakeHttpError(400);
|
|
242
|
+
const {result} = renderHook(() =>
|
|
243
|
+
useClientMutation({
|
|
244
|
+
run: async (): Promise<void> => {
|
|
245
|
+
throw failure;
|
|
246
|
+
},
|
|
247
|
+
})
|
|
248
|
+
);
|
|
249
|
+
let caught: unknown;
|
|
250
|
+
await act(async () => {
|
|
251
|
+
await result.current.mutate(undefined).catch((error: unknown) => {
|
|
252
|
+
caught = error;
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
expect(caught).toBe(failure);
|
|
256
|
+
expect(result.current.error).toBe(failure);
|
|
257
|
+
expect(result.current.isLoading).toBe(false);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("keeps a stable mutate identity and calls the latest run", async () => {
|
|
261
|
+
const firstRun = mock(async (): Promise<void> => {});
|
|
262
|
+
const secondRun = mock(async (): Promise<void> => {});
|
|
263
|
+
const {result, rerender} = renderHook(
|
|
264
|
+
({run}: {run: () => Promise<void>}) => useClientMutation({run}),
|
|
265
|
+
{initialProps: {run: firstRun}}
|
|
266
|
+
);
|
|
267
|
+
const firstMutate = result.current.mutate;
|
|
268
|
+
rerender({run: secondRun});
|
|
269
|
+
expect(result.current.mutate).toBe(firstMutate);
|
|
270
|
+
await act(async () => {
|
|
271
|
+
await result.current.mutate(undefined);
|
|
272
|
+
});
|
|
273
|
+
expect(firstRun).not.toHaveBeenCalled();
|
|
274
|
+
expect(secondRun).toHaveBeenCalled();
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
describe("unwrapDataEnvelope", () => {
|
|
279
|
+
it("unwraps {data} envelopes", () => {
|
|
280
|
+
expect(unwrapDataEnvelope<number[]>({data: [1]})).toEqual([1]);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("returns bare payloads unchanged", () => {
|
|
284
|
+
expect(unwrapDataEnvelope<number[]>([1])).toEqual([1]);
|
|
285
|
+
expect(unwrapDataEnvelope<{current: null}>({current: null})).toEqual({current: null});
|
|
286
|
+
expect(unwrapDataEnvelope(undefined)).toBeUndefined();
|
|
287
|
+
});
|
|
288
|
+
});
|