@malloy-publisher/sdk 0.0.382-dev → 0.2.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/README.md +115 -95
- package/TREE_SHAKING.md +16 -11
- package/bunfig.toml +7 -0
- package/dist/ServerProvider-Cn_LhKda.cjs.js +1 -0
- package/dist/ServerProvider-sfabtriY.es.js +4076 -0
- package/dist/client/api.d.ts +3773 -1515
- package/dist/client/configuration.d.ts +1 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.es.js +86 -59
- package/dist/components/Connections/EditConnectionDialog.d.ts +2 -1
- package/dist/components/Connections/common.d.ts +8 -2
- package/dist/components/Dashboard/Dashboard.d.ts +62 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +57 -0
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/DataAppViewer/DataAppViewer.d.ts +23 -0
- package/dist/components/DataAppViewer/index.d.ts +1 -0
- package/dist/components/Environment/Environment.d.ts +6 -0
- package/dist/components/Environment/index.d.ts +2 -0
- package/dist/components/Home/AddEnvironmentDialog.d.ts +1 -0
- package/dist/components/Home/DeleteEnvironmentDialog.d.ts +5 -0
- package/dist/components/Home/EditEnvironmentDialog.d.ts +7 -0
- package/dist/components/Home/Home.d.ts +2 -2
- package/dist/components/Materializations/CreateEnvMaterializationDialog.d.ts +14 -0
- package/dist/components/Materializations/CreateMaterializationDialog.d.ts +10 -0
- package/dist/components/Materializations/DeleteMaterializationDialog.d.ts +7 -0
- package/dist/components/Materializations/EnvironmentMaterializations.d.ts +7 -0
- package/dist/components/Materializations/ManifestView.d.ts +13 -0
- package/dist/components/Materializations/MaterializationDetailDialog.d.ts +9 -0
- package/dist/components/Materializations/MaterializationRunsList.d.ts +15 -0
- package/dist/components/Materializations/Materializations.d.ts +7 -0
- package/dist/components/Materializations/ScheduleCard.d.ts +20 -0
- package/dist/components/Materializations/SectionLabel.d.ts +10 -0
- package/dist/components/Materializations/SetScheduleDialog.d.ts +11 -0
- package/dist/components/Materializations/TriggerChip.d.ts +10 -0
- package/dist/components/Materializations/cron.d.ts +23 -0
- package/dist/components/Materializations/index.d.ts +2 -0
- package/dist/components/Materializations/utils.d.ts +46 -0
- package/dist/components/Notebook/Notebook.d.ts +47 -4
- package/dist/components/Notebook/NotebookCell.d.ts +37 -1
- package/dist/components/Notebook/runPlan.d.ts +29 -0
- package/dist/components/Notebook/types.d.ts +10 -0
- package/dist/components/Package/ContentTypeIcon.d.ts +31 -0
- package/dist/components/Package/Package.d.ts +1 -4
- package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
- package/dist/components/RenderedResult/ResultContainer.d.ts +5 -1
- package/dist/components/RenderedResult/renderLogs.d.ts +6 -0
- package/dist/components/ResultsDialog.d.ts +6 -1
- package/dist/components/ServerProvider.d.ts +28 -4
- package/dist/components/click_helper.d.ts +10 -1
- package/dist/components/drill/index.d.ts +3 -0
- package/dist/components/drill/markDrillableCells.d.ts +55 -0
- package/dist/components/drill/resolveDrill.d.ts +139 -0
- package/dist/components/drill/useDrill.d.ts +63 -0
- package/dist/components/given/GivenInput.d.ts +52 -0
- package/dist/components/given/GivensPanel.d.ts +47 -0
- package/dist/components/given/filterValue.d.ts +97 -0
- package/dist/components/given/index.d.ts +2 -0
- package/dist/components/given/paramCodec.d.ts +60 -0
- package/dist/components/given/utils.d.ts +17 -0
- package/dist/components/highlighter.d.ts +6 -1
- package/dist/components/index.d.ts +7 -2
- package/dist/components/styles.d.ts +47 -0
- package/dist/constants/docLinks.d.ts +12 -0
- package/dist/{core-YNpOLuB1.cjs.js → core-CqRKhtoy.cjs.js} +1 -1
- package/dist/{core-CjeTkq8O.es.js → core-DyU47reh.es.js} +1 -1
- package/dist/github-dark-BKL9xNgN.cjs.js +1 -0
- package/dist/github-dark-DenFmJkN.es.js +4 -0
- package/dist/hooks/givenValue.d.ts +10 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/useDimensionFiltersFromSpec.d.ts +3 -3
- package/dist/hooks/useDimensionFiltersQuery.d.ts +3 -3
- package/dist/hooks/useDimensionalFilterRangeData.d.ts +4 -4
- package/dist/hooks/useGivensState.d.ts +64 -0
- package/dist/hooks/useModelGivens.d.ts +10 -0
- package/dist/hooks/useSuggestOptions.d.ts +78 -0
- package/dist/{index-DRDu9kIV.es.js → index-CAkFE4Ol.es.js} +41278 -28504
- package/dist/index-DD4b_bt0.cjs.js +356 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.es.js +79 -48
- package/dist/malloy-explorer.css +12 -1
- package/dist/sdk.css +1 -1
- package/dist/theme/ThemeContext.d.ts +45 -0
- package/dist/theme/buildMalloyExplicitTheme.d.ts +34 -0
- package/dist/theme/buildTableCssVars.d.ts +41 -0
- package/dist/theme/buildVegaThemeOverride.d.ts +16 -0
- package/dist/theme/defaults.d.ts +7 -0
- package/dist/theme/index.d.ts +8 -0
- package/dist/theme/keys.d.ts +20 -0
- package/dist/theme/readChartAnnotations.d.ts +23 -0
- package/dist/theme/resolveTheme.d.ts +22 -0
- package/dist/theme/types.d.ts +70 -0
- package/dist/utils/dataAppEmbed.d.ts +44 -0
- package/dist/utils/formatting.d.ts +1 -1
- package/dist/utils/malloyString.d.ts +7 -0
- package/dist/utils/parsing.d.ts +2 -2
- package/dist/utils/queryClient.d.ts +5 -0
- package/package.json +19 -8
- package/src/client-entry.ts +3 -0
- package/src/components/AnalyzePackageButton.tsx +9 -3
- package/src/components/ApiErrorDisplay.tsx +3 -0
- package/src/components/Connections/AddConnectionDialog.tsx +132 -25
- package/src/components/Connections/DeleteConnectionDialog.tsx +14 -3
- package/src/components/Connections/EditConnectionDialog.tsx +167 -37
- package/src/components/Connections/common.ts +94 -3
- package/src/components/DOMElement/DOMElement.tsx +3 -0
- package/src/components/DOMElement/index.ts +3 -0
- package/src/components/Dashboard/Dashboard.spec.tsx +216 -0
- package/src/components/Dashboard/Dashboard.tsx +458 -0
- package/src/components/Dashboard/DashboardTile.spec.tsx +81 -0
- package/src/components/Dashboard/DashboardTile.tsx +199 -0
- package/src/components/Dashboard/index.ts +5 -0
- package/src/components/Dashboard/tileGridColumn.spec.ts +35 -0
- package/src/components/DataAppViewer/DataAppViewer.tsx +214 -0
- package/src/components/DataAppViewer/index.ts +4 -0
- package/src/components/{Project → Environment}/About.tsx +11 -4
- package/src/components/Environment/AddPackageDialog.tsx +202 -0
- package/src/components/{Project → Environment}/ConnectionExplorer.tsx +17 -14
- package/src/components/Environment/Connections.tsx +391 -0
- package/src/components/{Project → Environment}/DeletePackageDialog.tsx +8 -4
- package/src/components/{Project → Environment}/EditPackageDialog.tsx +21 -13
- package/src/components/Environment/Environment.tsx +89 -0
- package/src/components/Environment/Packages.tsx +206 -0
- package/src/components/Environment/index.ts +5 -0
- package/src/components/Home/{AddProjectDialog.tsx → AddEnvironmentDialog.tsx} +25 -19
- package/src/components/Home/{DeleteProjectDialog.tsx → DeleteEnvironmentDialog.tsx} +20 -15
- package/src/components/Home/{EditProjectDialog.tsx → EditEnvironmentDialog.tsx} +35 -28
- package/src/components/Home/Home.tsx +386 -408
- package/src/components/Home/index.ts +3 -0
- package/src/components/Loading.tsx +3 -0
- package/src/components/Materializations/CreateEnvMaterializationDialog.tsx +179 -0
- package/src/components/Materializations/CreateMaterializationDialog.tsx +112 -0
- package/src/components/Materializations/DeleteMaterializationDialog.tsx +109 -0
- package/src/components/Materializations/EnvironmentMaterializations.tsx +267 -0
- package/src/components/Materializations/ManifestView.tsx +87 -0
- package/src/components/Materializations/MaterializationDetailDialog.tsx +259 -0
- package/src/components/Materializations/MaterializationRunsList.tsx +256 -0
- package/src/components/Materializations/Materializations.tsx +355 -0
- package/src/components/Materializations/ScheduleCard.tsx +231 -0
- package/src/components/Materializations/SectionLabel.tsx +30 -0
- package/src/components/Materializations/SetScheduleDialog.tsx +162 -0
- package/src/components/Materializations/TriggerChip.tsx +44 -0
- package/src/components/Materializations/cron.ts +124 -0
- package/src/components/Materializations/index.ts +5 -0
- package/src/components/Materializations/utils.ts +145 -0
- package/src/components/Model/Model.tsx +44 -2
- package/src/components/Model/ModelCell.tsx +14 -5
- package/src/components/Model/ModelExplorer.tsx +3 -0
- package/src/components/Model/ModelExplorerDialog.tsx +4 -1
- package/src/components/Model/SourcesExplorer.tsx +9 -6
- package/src/components/Model/index.ts +3 -0
- package/src/components/Model/useModelData.ts +6 -3
- package/src/components/Notebook/Notebook.spec.tsx +88 -0
- package/src/components/Notebook/Notebook.tsx +476 -339
- package/src/components/Notebook/NotebookCell.tsx +264 -25
- package/src/components/Notebook/index.ts +3 -0
- package/src/components/Notebook/runPlan.spec.ts +155 -0
- package/src/components/Notebook/runPlan.ts +67 -0
- package/src/components/Notebook/types.ts +14 -0
- package/src/components/Package/ContentTypeIcon.spec.ts +91 -0
- package/src/components/Package/ContentTypeIcon.tsx +177 -0
- package/src/components/Package/FileTreeView.tsx +3 -0
- package/src/components/Package/Models.tsx +7 -4
- package/src/components/Package/Package.spec.tsx +113 -0
- package/src/components/Package/Package.tsx +692 -59
- package/src/components/Package/index.ts +3 -0
- package/src/components/QueryResult/QueryResult.tsx +17 -8
- package/src/components/QueryResult/index.ts +3 -0
- package/src/components/RenderedResult/RenderedResult.tsx +725 -67
- package/src/components/RenderedResult/ResultContainer.tsx +71 -2
- package/src/components/RenderedResult/index.ts +3 -0
- package/src/components/RenderedResult/renderLogs.spec.ts +58 -0
- package/src/components/RenderedResult/renderLogs.ts +43 -0
- package/src/components/ResultsDialog.tsx +13 -1
- package/src/components/ServerProvider.tsx +175 -51
- package/src/components/Workbook/BrowserWorkbookStorage.ts +3 -0
- package/src/components/Workbook/EditableMalloyCell.tsx +3 -0
- package/src/components/Workbook/ModelPicker.tsx +7 -4
- package/src/components/Workbook/MutableCell.tsx +10 -5
- package/src/components/Workbook/Workbook.tsx +8 -5
- package/src/components/Workbook/WorkbookList.tsx +3 -0
- package/src/components/Workbook/WorkbookManager.ts +3 -0
- package/src/components/Workbook/WorkbookStorage.ts +3 -0
- package/src/components/Workbook/WorkbookStorageProvider.tsx +3 -0
- package/src/components/Workbook/index.ts +3 -0
- package/src/components/click_helper.ts +17 -1
- package/src/components/drill/index.ts +28 -0
- package/src/components/drill/markDrillableCells.dom.spec.ts +313 -0
- package/src/components/drill/markDrillableCells.spec.ts +109 -0
- package/src/components/drill/markDrillableCells.ts +314 -0
- package/src/components/drill/resolveDrill.spec.ts +449 -0
- package/src/components/drill/resolveDrill.ts +375 -0
- package/src/components/drill/useDrill.tsx +203 -0
- package/src/components/filter/DimensionFilter.tsx +3 -0
- package/src/components/filter/index.ts +3 -0
- package/src/components/filter/utils.ts +16 -5
- package/src/components/given/GivenInput.spec.tsx +689 -0
- package/src/components/given/GivenInput.tsx +801 -0
- package/src/components/given/GivensPanel.spec.tsx +175 -0
- package/src/components/given/GivensPanel.tsx +195 -0
- package/src/components/given/filterValue.spec.ts +364 -0
- package/src/components/given/filterValue.ts +207 -0
- package/src/components/given/index.ts +9 -0
- package/src/components/given/paramCodec.spec.ts +857 -0
- package/src/components/given/paramCodec.ts +483 -0
- package/src/components/given/utils.spec.ts +60 -0
- package/src/components/given/utils.ts +79 -0
- package/src/components/highlighter.ts +22 -5
- package/src/components/index.ts +10 -2
- package/src/components/styles.ts +82 -33
- package/src/constants/docLinks.spec.ts +54 -0
- package/src/constants/docLinks.ts +46 -0
- package/src/hooks/givenValue.ts +13 -0
- package/src/hooks/index.ts +13 -0
- package/src/hooks/useDimensionFilters.ts +4 -7
- package/src/hooks/useDimensionFiltersFromSpec.ts +8 -5
- package/src/hooks/useDimensionFiltersQuery.ts +10 -13
- package/src/hooks/useDimensionalFilterRangeData.ts +11 -14
- package/src/hooks/useGivensState.spec.ts +449 -0
- package/src/hooks/useGivensState.ts +340 -0
- package/src/hooks/useModelGivens.ts +29 -0
- package/src/hooks/useQueryWithApiError.ts +3 -0
- package/src/hooks/useRawQueryData.ts +6 -3
- package/src/hooks/useSuggestOptions.dom.spec.ts +91 -0
- package/src/hooks/useSuggestOptions.spec.ts +112 -0
- package/src/hooks/useSuggestOptions.ts +268 -0
- package/src/index.ts +10 -1
- package/src/react-app-env.d.ts +3 -0
- package/src/theme/ThemeContext.tsx +86 -0
- package/src/theme/buildMalloyExplicitTheme.ts +61 -0
- package/src/theme/buildTableCssVars.spec.ts +86 -0
- package/src/theme/buildTableCssVars.ts +98 -0
- package/src/theme/buildVegaThemeOverride.spec.ts +56 -0
- package/src/theme/buildVegaThemeOverride.ts +47 -0
- package/src/theme/defaults.ts +78 -0
- package/src/theme/index.ts +11 -0
- package/src/theme/keys.ts +32 -0
- package/src/theme/readChartAnnotations.spec.ts +58 -0
- package/src/theme/readChartAnnotations.ts +69 -0
- package/src/theme/resolveTheme.spec.ts +160 -0
- package/src/theme/resolveTheme.ts +121 -0
- package/src/theme/types.ts +79 -0
- package/src/utils/dataAppEmbed.spec.ts +63 -0
- package/src/utils/dataAppEmbed.ts +84 -0
- package/src/utils/formatting.spec.ts +25 -21
- package/src/utils/formatting.ts +11 -7
- package/src/utils/malloyString.spec.ts +24 -0
- package/src/utils/malloyString.ts +12 -0
- package/src/utils/parsing.spec.ts +26 -19
- package/src/utils/parsing.ts +11 -6
- package/src/utils/queryClient.ts +17 -0
- package/test/README.md +185 -0
- package/test/serverProvider.tsx +72 -0
- package/test/setup.ts +29 -0
- package/tsconfig.json +12 -1
- package/tsconfig.spec.json +22 -0
- package/vite.config.ts +3 -0
- package/dist/ServerProvider-lOQXDlqB.cjs.js +0 -1
- package/dist/ServerProvider-on-8FH5Z.es.js +0 -3060
- package/dist/components/Home/AddProjectDialog.d.ts +0 -1
- package/dist/components/Home/DeleteProjectDialog.d.ts +0 -5
- package/dist/components/Home/EditProjectDialog.d.ts +0 -7
- package/dist/components/Package/Config.d.ts +0 -5
- package/dist/components/Package/Databases.d.ts +0 -5
- package/dist/components/Package/Notebooks.d.ts +0 -6
- package/dist/components/Project/Project.d.ts +0 -6
- package/dist/components/Project/index.d.ts +0 -2
- package/dist/index-BOLBP6_i.cjs.js +0 -233
- package/src/components/Package/Config.tsx +0 -97
- package/src/components/Package/Connections.tsx +0 -373
- package/src/components/Package/Databases.tsx +0 -228
- package/src/components/Package/Notebooks.tsx +0 -77
- package/src/components/Project/AddPackageDialog.tsx +0 -159
- package/src/components/Project/Packages.tsx +0 -191
- package/src/components/Project/Project.tsx +0 -52
- package/src/components/Project/index.ts +0 -2
- /package/dist/components/{Project → Environment}/About.d.ts +0 -0
- /package/dist/components/{Project → Environment}/AddPackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/ConnectionExplorer.d.ts +0 -0
- /package/dist/components/{Package → Environment}/Connections.d.ts +0 -0
- /package/dist/components/{Project → Environment}/DeletePackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/EditPackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/Packages.d.ts +0 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { act, renderHook } from "@testing-library/react";
|
|
5
|
+
import { describe, expect, it } from "bun:test";
|
|
6
|
+
import { useGivensState } from "./useGivensState";
|
|
7
|
+
|
|
8
|
+
const TYPES = new Map<string, string | undefined>([
|
|
9
|
+
["REGION", "filter<string>"],
|
|
10
|
+
["MIN_AMOUNT", "number"],
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
const entries = (values: Map<string, unknown>) => Object.fromEntries(values);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A host that behaves like `NotebookPage` and `DashboardPage`: it keeps the
|
|
17
|
+
* applied values in its URL and feeds that URL straight back in as `params`.
|
|
18
|
+
*
|
|
19
|
+
* Testing the hook without that loop misses most of what it does. Every Reset
|
|
20
|
+
* defect below is invisible to a test that treats `params` as a constant,
|
|
21
|
+
* because the second half of each one happens when the values the hook just
|
|
22
|
+
* reported arrive back through the front door.
|
|
23
|
+
*/
|
|
24
|
+
function renderWithUrlHost(options: {
|
|
25
|
+
autorun: boolean;
|
|
26
|
+
startingValues?: Record<string, string>;
|
|
27
|
+
initialUrl?: Record<string, string>;
|
|
28
|
+
}) {
|
|
29
|
+
let url: Record<string, string> = options.initialUrl ?? {};
|
|
30
|
+
const urlWrites: Record<string, string>[] = [];
|
|
31
|
+
// Mounted with no starting values, then given them a render later. That is
|
|
32
|
+
// how it really happens: they arrive with the notebook, after the first
|
|
33
|
+
// render, and the hook's report guard is seeded for exactly that order.
|
|
34
|
+
// Handing them over at mount instead makes the first render look like the
|
|
35
|
+
// steady state and hides whether they ever reach the URL.
|
|
36
|
+
const arrival: { starting?: Record<string, string> } = {};
|
|
37
|
+
const view = renderHook(
|
|
38
|
+
({
|
|
39
|
+
p,
|
|
40
|
+
s,
|
|
41
|
+
}: {
|
|
42
|
+
p: Record<string, string>;
|
|
43
|
+
s: Record<string, string> | undefined;
|
|
44
|
+
}) =>
|
|
45
|
+
useGivensState({
|
|
46
|
+
declaredTypes: TYPES,
|
|
47
|
+
startingValues: s,
|
|
48
|
+
params: p,
|
|
49
|
+
onParamsChange: (next) => {
|
|
50
|
+
url = next;
|
|
51
|
+
urlWrites.push(next);
|
|
52
|
+
},
|
|
53
|
+
autorun: options.autorun,
|
|
54
|
+
}),
|
|
55
|
+
{ initialProps: { p: url, s: arrival.starting } },
|
|
56
|
+
);
|
|
57
|
+
// Let the reported values propagate back through the host, repeatedly, so a
|
|
58
|
+
// change that keeps provoking another URL write shows up as more writes
|
|
59
|
+
// rather than as a stable-looking end state.
|
|
60
|
+
const settle = () => {
|
|
61
|
+
for (let i = 0; i < 5; i++)
|
|
62
|
+
act(() => view.rerender({ p: url, s: arrival.starting }));
|
|
63
|
+
};
|
|
64
|
+
settle();
|
|
65
|
+
arrival.starting = options.startingValues;
|
|
66
|
+
settle();
|
|
67
|
+
return {
|
|
68
|
+
get current() {
|
|
69
|
+
return view.result.current;
|
|
70
|
+
},
|
|
71
|
+
settle,
|
|
72
|
+
urlWrites,
|
|
73
|
+
get url() {
|
|
74
|
+
return url;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
describe("useGivensState: where the controls start", () => {
|
|
80
|
+
it("takes the document's starting values when the URL is empty", () => {
|
|
81
|
+
const host = renderWithUrlHost({
|
|
82
|
+
autorun: true,
|
|
83
|
+
startingValues: { REGION: "West" },
|
|
84
|
+
});
|
|
85
|
+
expect(entries(host.current.applied)).toEqual({ REGION: "West" });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("lets the URL win, so a shared link shows what the sender saw", () => {
|
|
89
|
+
const host = renderWithUrlHost({
|
|
90
|
+
autorun: true,
|
|
91
|
+
startingValues: { REGION: "West" },
|
|
92
|
+
initialUrl: { REGION: "Northeast" },
|
|
93
|
+
});
|
|
94
|
+
expect(entries(host.current.applied)).toEqual({ REGION: "Northeast" });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("reads each value as its declared type", () => {
|
|
98
|
+
const host = renderWithUrlHost({
|
|
99
|
+
autorun: true,
|
|
100
|
+
initialUrl: { REGION: "West", MIN_AMOUNT: "100" },
|
|
101
|
+
});
|
|
102
|
+
expect(host.current.applied.get("REGION")).toBe("West");
|
|
103
|
+
expect(host.current.applied.get("MIN_AMOUNT")).toBe(100);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("ignores a parameter the model does not declare", () => {
|
|
107
|
+
// A tracking tag on the page URL must not reach a query, which would
|
|
108
|
+
// fail outright on an unknown given.
|
|
109
|
+
const host = renderWithUrlHost({
|
|
110
|
+
autorun: true,
|
|
111
|
+
initialUrl: { REGION: "West", utm_source: "slack" },
|
|
112
|
+
});
|
|
113
|
+
expect(entries(host.current.applied)).toEqual({ REGION: "West" });
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("reports the starting values outward once, and then stops", () => {
|
|
117
|
+
const host = renderWithUrlHost({
|
|
118
|
+
autorun: true,
|
|
119
|
+
startingValues: { REGION: "West" },
|
|
120
|
+
});
|
|
121
|
+
expect(host.urlWrites).toEqual([{ REGION: "West" }]);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("useGivensState: editing", () => {
|
|
126
|
+
it("applies immediately under autorun", () => {
|
|
127
|
+
const host = renderWithUrlHost({ autorun: true });
|
|
128
|
+
act(() => host.current.setGiven("REGION", "Northeast"));
|
|
129
|
+
host.settle();
|
|
130
|
+
|
|
131
|
+
expect(entries(host.current.applied)).toEqual({ REGION: "Northeast" });
|
|
132
|
+
expect(host.current.pending).toBe(false);
|
|
133
|
+
expect(host.url).toEqual({ REGION: "Northeast" });
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("batches behind Apply when autorun is off", () => {
|
|
137
|
+
const host = renderWithUrlHost({ autorun: false });
|
|
138
|
+
act(() => host.current.setGiven("REGION", "Northeast"));
|
|
139
|
+
act(() => host.current.setGiven("MIN_AMOUNT", 100));
|
|
140
|
+
host.settle();
|
|
141
|
+
|
|
142
|
+
expect(entries(host.current.draft)).toEqual({
|
|
143
|
+
REGION: "Northeast",
|
|
144
|
+
MIN_AMOUNT: 100,
|
|
145
|
+
});
|
|
146
|
+
expect(entries(host.current.applied)).toEqual({});
|
|
147
|
+
expect(host.current.pending).toBe(true);
|
|
148
|
+
// Two edits, and the URL has not moved: nothing has been run yet.
|
|
149
|
+
expect(host.urlWrites).toEqual([]);
|
|
150
|
+
|
|
151
|
+
act(() => host.current.apply());
|
|
152
|
+
host.settle();
|
|
153
|
+
|
|
154
|
+
expect(entries(host.current.applied)).toEqual({
|
|
155
|
+
REGION: "Northeast",
|
|
156
|
+
MIN_AMOUNT: 100,
|
|
157
|
+
});
|
|
158
|
+
expect(host.current.pending).toBe(false);
|
|
159
|
+
// One write for two edits, which is the whole point of batching.
|
|
160
|
+
expect(host.urlWrites).toEqual([
|
|
161
|
+
{ REGION: "Northeast", MIN_AMOUNT: "100" },
|
|
162
|
+
]);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("keeps the other starting values when one control is edited", () => {
|
|
166
|
+
const host = renderWithUrlHost({
|
|
167
|
+
autorun: true,
|
|
168
|
+
startingValues: { REGION: "West", MIN_AMOUNT: "5" },
|
|
169
|
+
});
|
|
170
|
+
act(() => host.current.setGiven("REGION", "Northeast"));
|
|
171
|
+
host.settle();
|
|
172
|
+
|
|
173
|
+
expect(entries(host.current.applied)).toEqual({
|
|
174
|
+
REGION: "Northeast",
|
|
175
|
+
MIN_AMOUNT: 5,
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("drops an override on null, back to the model default", () => {
|
|
180
|
+
const host = renderWithUrlHost({
|
|
181
|
+
autorun: true,
|
|
182
|
+
initialUrl: { REGION: "West" },
|
|
183
|
+
});
|
|
184
|
+
act(() => host.current.setGiven("REGION", null));
|
|
185
|
+
host.settle();
|
|
186
|
+
|
|
187
|
+
expect(host.current.applied.has("REGION")).toBe(false);
|
|
188
|
+
expect(host.url).toEqual({});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("drops values for givens the model no longer declares", () => {
|
|
192
|
+
// What a package reload can do while the page is open. Sending a given
|
|
193
|
+
// the model no longer declares fails the query outright.
|
|
194
|
+
let url: Record<string, string> = { REGION: "West", MIN_AMOUNT: "100" };
|
|
195
|
+
const { result, rerender } = renderHook(
|
|
196
|
+
({ types }: { types: Map<string, string | undefined> }) =>
|
|
197
|
+
useGivensState({
|
|
198
|
+
declaredTypes: types,
|
|
199
|
+
params: url,
|
|
200
|
+
onParamsChange: (next) => {
|
|
201
|
+
url = next;
|
|
202
|
+
},
|
|
203
|
+
autorun: true,
|
|
204
|
+
}),
|
|
205
|
+
{ initialProps: { types: TYPES } },
|
|
206
|
+
);
|
|
207
|
+
expect(result.current.applied.size).toBe(2);
|
|
208
|
+
|
|
209
|
+
const reloaded = new Map([["REGION", "filter<string>"]]);
|
|
210
|
+
act(() => rerender({ types: reloaded }));
|
|
211
|
+
|
|
212
|
+
expect(entries(result.current.applied)).toEqual({ REGION: "West" });
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe("useGivensState: Reset", () => {
|
|
217
|
+
// Two symptoms were reported against #935's `clearAll`, and they turned out
|
|
218
|
+
// to be one defect: it cleared to an EMPTY map rather than to the starting
|
|
219
|
+
// point, and it only committed under autorun. Each test below fails against
|
|
220
|
+
// that version, in the way named in its comment.
|
|
221
|
+
|
|
222
|
+
it("puts the document's starting values back, and runs once", () => {
|
|
223
|
+
// Was: cleared to empty, the empty state went out to the URL, the
|
|
224
|
+
// starting values then flowed back in from `initial`, and the queries
|
|
225
|
+
// ran a second time. Two URL writes where there should be one.
|
|
226
|
+
const host = renderWithUrlHost({
|
|
227
|
+
autorun: true,
|
|
228
|
+
startingValues: { REGION: "West" },
|
|
229
|
+
});
|
|
230
|
+
act(() => host.current.setGiven("REGION", "Northeast"));
|
|
231
|
+
host.settle();
|
|
232
|
+
const writesBefore = host.urlWrites.length;
|
|
233
|
+
|
|
234
|
+
act(() => host.current.reset());
|
|
235
|
+
host.settle();
|
|
236
|
+
|
|
237
|
+
expect(entries(host.current.applied)).toEqual({ REGION: "West" });
|
|
238
|
+
expect(host.url).toEqual({ REGION: "West" });
|
|
239
|
+
expect(host.urlWrites.length - writesBefore).toBe(1);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("takes effect with autorun off, instead of doing nothing at all", () => {
|
|
243
|
+
// Was: `applied` was left untouched whenever autorun was false, so the
|
|
244
|
+
// URL never changed and no query re-ran. Reset was inert.
|
|
245
|
+
const host = renderWithUrlHost({ autorun: false });
|
|
246
|
+
act(() => host.current.setGiven("REGION", "Northeast"));
|
|
247
|
+
act(() => host.current.apply());
|
|
248
|
+
host.settle();
|
|
249
|
+
expect(host.url).toEqual({ REGION: "Northeast" });
|
|
250
|
+
const writesBefore = host.urlWrites.length;
|
|
251
|
+
|
|
252
|
+
act(() => host.current.reset());
|
|
253
|
+
host.settle();
|
|
254
|
+
|
|
255
|
+
expect(entries(host.current.applied)).toEqual({});
|
|
256
|
+
expect(host.url).toEqual({});
|
|
257
|
+
expect(host.urlWrites.length - writesBefore).toBe(1);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("leaves nothing pending, so Apply does not light up after a Reset", () => {
|
|
261
|
+
const host = renderWithUrlHost({
|
|
262
|
+
autorun: false,
|
|
263
|
+
startingValues: { REGION: "West" },
|
|
264
|
+
});
|
|
265
|
+
act(() => host.current.setGiven("REGION", "Northeast"));
|
|
266
|
+
host.settle();
|
|
267
|
+
expect(host.current.pending).toBe(true);
|
|
268
|
+
|
|
269
|
+
act(() => host.current.reset());
|
|
270
|
+
host.settle();
|
|
271
|
+
|
|
272
|
+
expect(host.current.pending).toBe(false);
|
|
273
|
+
expect(entries(host.current.draft)).toEqual({ REGION: "West" });
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("clears a URL-carried value rather than treating it as the start", () => {
|
|
277
|
+
// The URL is the thing being reset away from, so a link's values are not
|
|
278
|
+
// the starting point even though they win on arrival.
|
|
279
|
+
const host = renderWithUrlHost({
|
|
280
|
+
autorun: true,
|
|
281
|
+
startingValues: { REGION: "West" },
|
|
282
|
+
initialUrl: { REGION: "Northeast" },
|
|
283
|
+
});
|
|
284
|
+
expect(entries(host.current.applied)).toEqual({ REGION: "Northeast" });
|
|
285
|
+
|
|
286
|
+
act(() => host.current.reset());
|
|
287
|
+
host.settle();
|
|
288
|
+
|
|
289
|
+
expect(entries(host.current.applied)).toEqual({ REGION: "West" });
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
describe("useGivensState: settling", () => {
|
|
294
|
+
it("stops reporting once the host echoes the values back", () => {
|
|
295
|
+
// The hook reports applied values out for the host to put in the URL,
|
|
296
|
+
// and the host feeds that URL back in. Without the `sameParams` guard
|
|
297
|
+
// that is a loop.
|
|
298
|
+
const host = renderWithUrlHost({
|
|
299
|
+
autorun: true,
|
|
300
|
+
initialUrl: { REGION: "West" },
|
|
301
|
+
});
|
|
302
|
+
host.settle();
|
|
303
|
+
host.settle();
|
|
304
|
+
expect(host.urlWrites).toEqual([]);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("settles when its inputs are fresh objects on every render", () => {
|
|
308
|
+
// Carried over from `useGivensForm.spec.ts`, which this hook replaces.
|
|
309
|
+
// That hook infinite-looped on an unmemoized argument: its effect
|
|
310
|
+
// returned a fresh Map every run, which changed state identity, which
|
|
311
|
+
// re-rendered, until React gave up with "Maximum update depth exceeded"
|
|
312
|
+
// after 645k warnings. This is a published SDK export, so a consumer can
|
|
313
|
+
// reasonably pass an object literal.
|
|
314
|
+
let renders = 0;
|
|
315
|
+
const { result } = renderHook(() => {
|
|
316
|
+
renders++;
|
|
317
|
+
return useGivensState({
|
|
318
|
+
declaredTypes: new Map([["REGION", "filter<string>"]]),
|
|
319
|
+
startingValues: { REGION: "West" },
|
|
320
|
+
params: {},
|
|
321
|
+
onParamsChange: () => {},
|
|
322
|
+
autorun: true,
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
expect(entries(result.current.applied)).toEqual({ REGION: "West" });
|
|
327
|
+
expect(renders).toBeLessThan(5);
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
describe("useGivensState: swapping documents", () => {
|
|
332
|
+
it("drops the previous notebook's applied values on navigation", () => {
|
|
333
|
+
// Navigating between notebooks reuses this hook's component, and the
|
|
334
|
+
// edits used to be keyed by their starting VALUES alone. Two notebooks
|
|
335
|
+
// that both start empty produced the same key, so the first one's
|
|
336
|
+
// applied values stayed active and filtered the second, while
|
|
337
|
+
// `lastReported` (already equal to them) suppressed the report that
|
|
338
|
+
// would have shown them in the URL.
|
|
339
|
+
const reported: Record<string, string>[] = [];
|
|
340
|
+
const { result, rerender } = renderHook(
|
|
341
|
+
(props: { doc: string; params: Record<string, string> }) =>
|
|
342
|
+
useGivensState({
|
|
343
|
+
declaredTypes: new Map([["REGION", "string"]]),
|
|
344
|
+
params: props.params,
|
|
345
|
+
documentKey: props.doc,
|
|
346
|
+
onParamsChange: (next) => reported.push(next),
|
|
347
|
+
autorun: true,
|
|
348
|
+
}),
|
|
349
|
+
{ initialProps: { doc: "a.malloynb", params: {} } },
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
act(() => result.current.setGiven("REGION", "West"));
|
|
353
|
+
expect(entries(result.current.applied)).toEqual({ REGION: "West" });
|
|
354
|
+
|
|
355
|
+
// The host writes it to the URL and feeds it straight back in.
|
|
356
|
+
rerender({ doc: "a.malloynb", params: { REGION: "West" } });
|
|
357
|
+
expect(entries(result.current.applied)).toEqual({ REGION: "West" });
|
|
358
|
+
|
|
359
|
+
rerender({ doc: "b.malloynb", params: {} });
|
|
360
|
+
expect(entries(result.current.applied)).toEqual({});
|
|
361
|
+
expect(reported.at(-1)).toEqual({});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("keeps edits to the givens a model reload did not drop", () => {
|
|
365
|
+
// `prune` exists to drop values for givens that no longer exist while
|
|
366
|
+
// keeping the rest. It never got the chance: `initialKey` was built from
|
|
367
|
+
// `initial`, which is already filtered by `declaredTypes`, so removing
|
|
368
|
+
// one given re-keyed everything and discarded the reader's edits to the
|
|
369
|
+
// givens that survived. A declaration change is not a new starting point.
|
|
370
|
+
const { result, rerender } = renderHook(
|
|
371
|
+
(props: { types: Map<string, string> }) =>
|
|
372
|
+
useGivensState({
|
|
373
|
+
declaredTypes: props.types,
|
|
374
|
+
startingValues: { STATUS: "open" },
|
|
375
|
+
documentKey: "orders.malloynb",
|
|
376
|
+
autorun: true,
|
|
377
|
+
}),
|
|
378
|
+
{
|
|
379
|
+
initialProps: {
|
|
380
|
+
types: new Map([
|
|
381
|
+
["REGION", "string"],
|
|
382
|
+
["STATUS", "string"],
|
|
383
|
+
]),
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
act(() => result.current.setGiven("REGION", "east"));
|
|
389
|
+
expect(entries(result.current.applied)).toEqual({
|
|
390
|
+
STATUS: "open",
|
|
391
|
+
REGION: "east",
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
// The model author removes STATUS and the package reloads.
|
|
395
|
+
rerender({ types: new Map([["REGION", "string"]]) });
|
|
396
|
+
expect(entries(result.current.applied)).toEqual({ REGION: "east" });
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it("is not re-keyed by the ORDER a host lists its parameters in", () => {
|
|
400
|
+
// `URLSearchParams` iterates in the query string's own order, so a host
|
|
401
|
+
// rewriting `?A=1&B=2` as `?B=2&A=1` would otherwise look like a new
|
|
402
|
+
// starting point and throw the reader's edits away for nothing.
|
|
403
|
+
const { result, rerender } = renderHook(
|
|
404
|
+
(props: { params: Record<string, string> }) =>
|
|
405
|
+
useGivensState({
|
|
406
|
+
declaredTypes: new Map([
|
|
407
|
+
["A", "string"],
|
|
408
|
+
["B", "string"],
|
|
409
|
+
]),
|
|
410
|
+
params: props.params,
|
|
411
|
+
documentKey: "orders.malloynb",
|
|
412
|
+
autorun: true,
|
|
413
|
+
}),
|
|
414
|
+
{ initialProps: { params: { A: "1", B: "2" } } },
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
act(() => result.current.setGiven("A", "edited"));
|
|
418
|
+
rerender({ params: { B: "2", A: "1" } });
|
|
419
|
+
expect(entries(result.current.applied)).toEqual({ A: "edited", B: "2" });
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it("does not resurrect an abandoned edit when the key returns", () => {
|
|
423
|
+
// The edits were only IGNORED on a key mismatch, never discarded, and the
|
|
424
|
+
// key space is small enough to revisit: notebook a -> b -> a from a clean
|
|
425
|
+
// link matched the abandoned edits again and re-applied a filter the URL
|
|
426
|
+
// did not carry, to a reader who had just opened it fresh. Browser Back
|
|
427
|
+
// and a drill pushing an earlier URL do the same.
|
|
428
|
+
const { result, rerender } = renderHook(
|
|
429
|
+
(props: { doc: string; params: Record<string, string> }) =>
|
|
430
|
+
useGivensState({
|
|
431
|
+
declaredTypes: new Map([["REGION", "string"]]),
|
|
432
|
+
params: props.params,
|
|
433
|
+
documentKey: props.doc,
|
|
434
|
+
autorun: true,
|
|
435
|
+
}),
|
|
436
|
+
{ initialProps: { doc: "a.malloynb", params: {} } },
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
act(() => result.current.setGiven("REGION", "East"));
|
|
440
|
+
// The host echoes it into the URL, then the reader navigates away.
|
|
441
|
+
rerender({ doc: "a.malloynb", params: { REGION: "East" } });
|
|
442
|
+
rerender({ doc: "b.malloynb", params: {} });
|
|
443
|
+
expect(entries(result.current.applied)).toEqual({});
|
|
444
|
+
|
|
445
|
+
// Back to the first notebook from a link carrying no query string.
|
|
446
|
+
rerender({ doc: "a.malloynb", params: {} });
|
|
447
|
+
expect(entries(result.current.applied)).toEqual({});
|
|
448
|
+
});
|
|
449
|
+
});
|