@malloy-publisher/sdk 0.2.0 → 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/dist/ServerProvider-Cn_LhKda.cjs.js +1 -0
- package/dist/{ServerProvider-DUFPY6Rv.es.js → ServerProvider-sfabtriY.es.js} +110 -106
- package/dist/client/api.d.ts +62 -15
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +14 -13
- package/dist/components/Dashboard/Dashboard.d.ts +14 -1
- package/dist/components/Dashboard/DashboardTile.d.ts +16 -7
- package/dist/{core-hLahnv0W.cjs.js → core-CqRKhtoy.cjs.js} +1 -1
- package/dist/{core-lb28vQkR.es.js → core-DyU47reh.es.js} +1 -1
- package/dist/hooks/useSuggestOptions.d.ts +1 -1
- package/dist/{index-txw04ZbN.es.js → index-CAkFE4Ol.es.js} +13455 -13248
- package/dist/index-DD4b_bt0.cjs.js +356 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +9 -9
- package/dist/theme/buildTableCssVars.d.ts +17 -1
- package/package.json +1 -1
- package/src/components/Connections/AddConnectionDialog.tsx +102 -23
- package/src/components/Connections/EditConnectionDialog.tsx +134 -33
- package/src/components/Connections/common.ts +30 -2
- package/src/components/Dashboard/Dashboard.spec.tsx +216 -0
- package/src/components/Dashboard/Dashboard.tsx +77 -17
- package/src/components/Dashboard/DashboardTile.spec.tsx +81 -0
- package/src/components/Dashboard/DashboardTile.tsx +58 -12
- package/src/components/Dashboard/tileGridColumn.spec.ts +35 -0
- package/src/components/Notebook/Notebook.spec.tsx +88 -0
- package/src/components/Notebook/Notebook.tsx +1 -0
- package/src/components/Package/Package.spec.tsx +113 -0
- package/src/components/Package/Package.tsx +29 -3
- package/src/components/RenderedResult/RenderedResult.tsx +47 -7
- package/src/hooks/useSuggestOptions.dom.spec.ts +91 -0
- package/src/hooks/useSuggestOptions.ts +6 -1
- package/src/theme/buildTableCssVars.spec.ts +19 -0
- package/src/theme/buildTableCssVars.ts +31 -0
- package/test/README.md +44 -0
- package/test/serverProvider.tsx +72 -0
- package/dist/ServerProvider-CTopDUWW.cjs.js +0 -1
- package/dist/index-DvEanVgc.cjs.js +0 -339
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A notebook's suggest queries are version-scoped like the rest of it.
|
|
6
|
+
*
|
|
7
|
+
* The notebook already versioned its own fetch and its cell execution while
|
|
8
|
+
* its dropdowns ran unversioned, so a versioned notebook filled its controls
|
|
9
|
+
* from whatever the latest version happened to hold. The forwarding is a bare
|
|
10
|
+
* trailing optional argument, which is the kind that goes missing without a
|
|
11
|
+
* test noticing, so it is asserted here rather than only on the hook.
|
|
12
|
+
*/
|
|
13
|
+
import { beforeEach, expect, it, mock } from "bun:test";
|
|
14
|
+
import { render, waitFor } from "@testing-library/react";
|
|
15
|
+
import {
|
|
16
|
+
cacheKeys,
|
|
17
|
+
clearCache,
|
|
18
|
+
mockServerProvider,
|
|
19
|
+
pending,
|
|
20
|
+
serverWrapper,
|
|
21
|
+
} from "../../../test/serverProvider";
|
|
22
|
+
import type { RawNotebook } from "../../client";
|
|
23
|
+
|
|
24
|
+
const NOTEBOOK: RawNotebook = {
|
|
25
|
+
notebookCells: [],
|
|
26
|
+
sources: [
|
|
27
|
+
{
|
|
28
|
+
name: "orders",
|
|
29
|
+
givens: [
|
|
30
|
+
{
|
|
31
|
+
name: "REGION",
|
|
32
|
+
control: "select",
|
|
33
|
+
suggest: { source: "orders", dimension: "region" },
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const getNotebook = mock(
|
|
41
|
+
(
|
|
42
|
+
_environmentName: string,
|
|
43
|
+
_packageName: string,
|
|
44
|
+
_notebookPath: string,
|
|
45
|
+
_versionId?: string,
|
|
46
|
+
) => Promise.resolve({ data: NOTEBOOK }),
|
|
47
|
+
);
|
|
48
|
+
const executeQueryModel = mock(
|
|
49
|
+
(
|
|
50
|
+
_environmentName: string,
|
|
51
|
+
_packageName: string,
|
|
52
|
+
_modelPath: string,
|
|
53
|
+
_request: { versionId?: string },
|
|
54
|
+
) => pending(),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
mockServerProvider({
|
|
58
|
+
notebooks: { getNotebook },
|
|
59
|
+
models: { executeQueryModel },
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const { default: Notebook } = await import("./Notebook");
|
|
63
|
+
|
|
64
|
+
const URI =
|
|
65
|
+
"publisher://environments/env/packages/pkg/models/notebooks/ops.malloynb";
|
|
66
|
+
|
|
67
|
+
beforeEach(() => {
|
|
68
|
+
clearCache();
|
|
69
|
+
getNotebook.mockClear();
|
|
70
|
+
executeQueryModel.mockClear();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("runs its suggest queries against the version it was opened at", async () => {
|
|
74
|
+
render(<Notebook resourceUri={`${URI}?versionId=v2`} />, {
|
|
75
|
+
wrapper: serverWrapper,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
79
|
+
expect(executeQueryModel.mock.calls[0][3].versionId).toBe("v2");
|
|
80
|
+
expect(cacheKeys("givenSuggest")[0]).toContain('"v2"');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("sends none when the notebook was opened without one", async () => {
|
|
84
|
+
render(<Notebook resourceUri={URI} />, { wrapper: serverWrapper });
|
|
85
|
+
|
|
86
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
87
|
+
expect(executeQueryModel.mock.calls[0][3].versionId).toBeUndefined();
|
|
88
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The dashboards listing follows the package's other listings; the data apps
|
|
6
|
+
* do not.
|
|
7
|
+
*
|
|
8
|
+
* Two exceptions that look alike and are not: `/data-apps` serves static files
|
|
9
|
+
* and declares no `versionId` in the spec, while `list-dashboards` declares one
|
|
10
|
+
* exactly as `list-notebooks` does. Pinned as a pair, because the cost of
|
|
11
|
+
* confusing them is a listing that answers for the wrong version.
|
|
12
|
+
*/
|
|
13
|
+
import { beforeEach, expect, it, mock } from "bun:test";
|
|
14
|
+
import { render, screen, waitFor } from "@testing-library/react";
|
|
15
|
+
import {
|
|
16
|
+
cacheKeys,
|
|
17
|
+
clearCache,
|
|
18
|
+
mockServerProvider,
|
|
19
|
+
pending,
|
|
20
|
+
serverWrapper,
|
|
21
|
+
} from "../../../test/serverProvider";
|
|
22
|
+
|
|
23
|
+
const listDashboards = mock(
|
|
24
|
+
(_environmentName: string, _packageName: string, _versionId?: string) =>
|
|
25
|
+
pending(),
|
|
26
|
+
);
|
|
27
|
+
const listDataApps = mock((_environmentName: string, _packageName: string) =>
|
|
28
|
+
pending(),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
mockServerProvider({
|
|
32
|
+
packages: { getPackage: pending },
|
|
33
|
+
// Resolved, not pending: the page withholds every section until the
|
|
34
|
+
// notebooks and dashboards listings have both settled.
|
|
35
|
+
notebooks: { listNotebooks: () => Promise.resolve({ data: [] }) },
|
|
36
|
+
models: { listModels: pending },
|
|
37
|
+
databases: { listDatabases: pending },
|
|
38
|
+
dataApps: { listDataApps },
|
|
39
|
+
dashboards: { listDashboards },
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const { default: Package } = await import("./Package");
|
|
43
|
+
|
|
44
|
+
const packageAt = (versionId?: string) => (
|
|
45
|
+
<Package
|
|
46
|
+
resourceUri={
|
|
47
|
+
versionId === undefined
|
|
48
|
+
? "publisher://environments/env/packages/pkg"
|
|
49
|
+
: `publisher://environments/env/packages/pkg?versionId=${versionId}`
|
|
50
|
+
}
|
|
51
|
+
/>
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
clearCache();
|
|
56
|
+
listDashboards.mockClear();
|
|
57
|
+
listDataApps.mockClear();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("lists a version's dashboards, and keys the listing on it", async () => {
|
|
61
|
+
render(packageAt("v2"), { wrapper: serverWrapper });
|
|
62
|
+
|
|
63
|
+
await waitFor(() => expect(listDashboards).toHaveBeenCalled());
|
|
64
|
+
expect(listDashboards.mock.calls[0]).toEqual(["env", "pkg", "v2"]);
|
|
65
|
+
expect(cacheKeys("dashboards")[0]).toContain('"v2"');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("keys an unversioned listing beside the package's other listings", async () => {
|
|
69
|
+
render(packageAt(), { wrapper: serverWrapper });
|
|
70
|
+
|
|
71
|
+
await waitFor(() => expect(listDashboards).toHaveBeenCalled());
|
|
72
|
+
expect(listDashboards.mock.calls[0]).toEqual(["env", "pkg", undefined]);
|
|
73
|
+
expect(cacheKeys("dashboards")[0]).toBe(
|
|
74
|
+
'["dashboards","env","pkg",null,"http://localhost/api/v0"]',
|
|
75
|
+
);
|
|
76
|
+
expect(cacheKeys("notebooks")[0]).toBe(
|
|
77
|
+
'["notebooks","env","pkg",null,"http://localhost/api/v0"]',
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("still leaves the data apps unversioned", async () => {
|
|
82
|
+
render(packageAt("v2"), { wrapper: serverWrapper });
|
|
83
|
+
|
|
84
|
+
await waitFor(() => expect(listDataApps).toHaveBeenCalled());
|
|
85
|
+
expect(listDataApps.mock.calls[0]).toEqual(["env", "pkg"]);
|
|
86
|
+
expect(cacheKeys("data-apps")[0]).not.toContain('"v2"');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("says so when the dashboards listing fails, rather than showing none", async () => {
|
|
90
|
+
// Sending a version gives a host that refuses the parameter a way to fail
|
|
91
|
+
// this listing alone. Falling back to an empty list would report that as
|
|
92
|
+
// "no dashboards", which is a wrong answer wearing a right one's clothes.
|
|
93
|
+
listDashboards.mockImplementationOnce(() =>
|
|
94
|
+
Promise.reject({ response: { status: 501 } }),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
render(packageAt("v2"), { wrapper: serverWrapper });
|
|
98
|
+
|
|
99
|
+
expect(await screen.findByText(/Could not list/)).toBeDefined();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("keeps showing no section at all for an older server with no route", async () => {
|
|
103
|
+
// A 404 is a Publisher that predates the endpoint, which genuinely has no
|
|
104
|
+
// dashboards to list. Unchanged: no error, no section.
|
|
105
|
+
listDashboards.mockImplementationOnce(() =>
|
|
106
|
+
Promise.reject({ response: { status: 404 } }),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
render(packageAt(), { wrapper: serverWrapper });
|
|
110
|
+
|
|
111
|
+
await waitFor(() => expect(listDashboards).toHaveBeenCalled());
|
|
112
|
+
expect(screen.queryAllByText(/Could not list/)).toHaveLength(0);
|
|
113
|
+
});
|
|
@@ -5,6 +5,7 @@ import ArrowBackIcon from "@mui/icons-material/ArrowBack";
|
|
|
5
5
|
import CloseIcon from "@mui/icons-material/Close";
|
|
6
6
|
import OpenInNewIcon from "@mui/icons-material/OpenInNew";
|
|
7
7
|
import {
|
|
8
|
+
Alert,
|
|
8
9
|
Box,
|
|
9
10
|
Container,
|
|
10
11
|
Dialog,
|
|
@@ -130,15 +131,14 @@ export default function Package({
|
|
|
130
131
|
});
|
|
131
132
|
const dataApps = dataAppsQuery.data?.data ?? [];
|
|
132
133
|
|
|
133
|
-
// No versionId, for the same reason as data apps: the dashboards endpoint
|
|
134
|
-
// takes only env + package.
|
|
135
134
|
const dashboardsQuery = useQueryWithApiError({
|
|
136
|
-
queryKey: ["dashboards", environmentName, packageName],
|
|
135
|
+
queryKey: ["dashboards", environmentName, packageName, versionId],
|
|
137
136
|
queryFn: async () => {
|
|
138
137
|
try {
|
|
139
138
|
return await apiClients.dashboards.listDashboards(
|
|
140
139
|
environmentName,
|
|
141
140
|
packageName,
|
|
141
|
+
versionId,
|
|
142
142
|
);
|
|
143
143
|
} catch (e) {
|
|
144
144
|
// Non-fatal for the same reasons as the data-apps list above: an
|
|
@@ -287,6 +287,32 @@ export default function Package({
|
|
|
287
287
|
{/* First: the at-a-glance artifact a visitor most likely wants,
|
|
288
288
|
ahead of the notebooks and models it is built on. Hidden when
|
|
289
289
|
empty, like Data Apps. */}
|
|
290
|
+
{/* A listing that FAILED renders identically to a package with
|
|
291
|
+
no dashboards: `dashboards` falls back to `[]` and the
|
|
292
|
+
section hides itself. Only `pkgQuery` reaches the error page,
|
|
293
|
+
so nothing else here would say a word. Now that the listing
|
|
294
|
+
carries a `versionId`, a host that resolves it on some routes
|
|
295
|
+
and refuses it on others has a new way in, and "this package
|
|
296
|
+
has no dashboards" is the wrong thing to conclude from a
|
|
297
|
+
transport error. The 404 above stays swallowed: that one is
|
|
298
|
+
an older Publisher with no route, which genuinely has none. */}
|
|
299
|
+
{/* A listing that FAILED renders identically to a package with
|
|
300
|
+
no dashboards: `dashboards` falls back to `[]` and the
|
|
301
|
+
section hides itself. Only `pkgQuery` reaches the error page,
|
|
302
|
+
so nothing else here would say a word. Now that the listing
|
|
303
|
+
carries a `versionId`, a host that resolves it on some routes
|
|
304
|
+
and refuses it on others has a new way in, and "this package
|
|
305
|
+
has no dashboards" is the wrong thing to conclude from a
|
|
306
|
+
transport error. The 404 above stays swallowed: that one is
|
|
307
|
+
an older Publisher with no route, which genuinely has none. */}
|
|
308
|
+
{dashboardsQuery.isError && (
|
|
309
|
+
<Box sx={{ mb: 4 }}>
|
|
310
|
+
<Alert severity="warning">
|
|
311
|
+
Could not list this package's dashboards, so any it
|
|
312
|
+
has are missing from this page.
|
|
313
|
+
</Alert>
|
|
314
|
+
</Box>
|
|
315
|
+
)}
|
|
290
316
|
{dashboards.length > 0 && (
|
|
291
317
|
<PackageSection title="Dashboards" count={dashboards.length}>
|
|
292
318
|
{dashboards.map((dashboard) => {
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
// Copyright (c) Credible Data Inc.
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
|
-
import { Box } from "@mui/material";
|
|
4
|
+
import { Box, useTheme } from "@mui/material";
|
|
5
5
|
import React, {
|
|
6
6
|
Suspense,
|
|
7
7
|
useCallback,
|
|
8
8
|
useEffect,
|
|
9
9
|
useLayoutEffect,
|
|
10
|
+
useMemo,
|
|
10
11
|
useRef,
|
|
11
12
|
} from "react";
|
|
12
13
|
import { buildMalloyExplicitTheme } from "../../theme/buildMalloyExplicitTheme";
|
|
13
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
buildTableCssVars,
|
|
16
|
+
DASHBOARD_CARD_PADDING_PX,
|
|
17
|
+
type DashboardCardGeometry,
|
|
18
|
+
} from "../../theme/buildTableCssVars";
|
|
14
19
|
import { buildVegaThemeOverride } from "../../theme/buildVegaThemeOverride";
|
|
15
20
|
import { readChartAnnotations } from "../../theme/readChartAnnotations";
|
|
16
21
|
import { resolveTheme } from "../../theme/resolveTheme";
|
|
@@ -153,8 +158,12 @@ async function extractChartThemeOverride(parsed: unknown) {
|
|
|
153
158
|
}
|
|
154
159
|
}
|
|
155
160
|
|
|
156
|
-
function applyTableCssVars(
|
|
157
|
-
|
|
161
|
+
function applyTableCssVars(
|
|
162
|
+
element: HTMLElement,
|
|
163
|
+
theme: ResolvedTheme,
|
|
164
|
+
card: DashboardCardGeometry,
|
|
165
|
+
): void {
|
|
166
|
+
const vars = buildTableCssVars(theme, card);
|
|
158
167
|
for (const [key, value] of Object.entries(vars)) {
|
|
159
168
|
element.style.setProperty(key, value);
|
|
160
169
|
}
|
|
@@ -174,6 +183,18 @@ function applyTableCssVars(element: HTMLElement, theme: ResolvedTheme): void {
|
|
|
174
183
|
* than the renderer's own so they win regardless of stylesheet order.
|
|
175
184
|
*/
|
|
176
185
|
const PUBLISHER_RENDERER_OVERRIDES_CSS = `
|
|
186
|
+
/* Card geometry, through the renderer's OWN theme vars rather than over the top
|
|
187
|
+
of them. It declares --malloy-theme--dashboard-* on .malloy-render itself and
|
|
188
|
+
passes each through to the --malloy-render--* the card CSS reads, so a value
|
|
189
|
+
inherited from our wrapper loses to that declaration however early it is set:
|
|
190
|
+
this has to be a rule on the same element, at higher specificity. Doubling the
|
|
191
|
+
class does that, and then no !important is needed — which matters, because
|
|
192
|
+
!important here also lands on .dashboard-item-borderless and repaints a card
|
|
193
|
+
the author asked to have no card. */
|
|
194
|
+
.malloy-render.malloy-render {
|
|
195
|
+
--malloy-theme--dashboard-card-radius: var(--publisher-dashboard-card-radius);
|
|
196
|
+
--malloy-theme--dashboard-card-padding: var(--publisher-dashboard-card-padding);
|
|
197
|
+
}
|
|
177
198
|
/* dashboard.css hardcodes background: #f7f9fc on .malloy-dashboard
|
|
178
199
|
and .dashboard-row-header, which would paint light grey in dark
|
|
179
200
|
mode and also bleed an operator-picked palette.background across
|
|
@@ -208,11 +229,16 @@ div.malloy-render .malloy-dashboard .dashboard-row-header {
|
|
|
208
229
|
background: var(--publisher-dashboard-root) !important;
|
|
209
230
|
background-color: var(--publisher-dashboard-root) !important;
|
|
210
231
|
}
|
|
211
|
-
.malloy-render .malloy-dashboard .dashboard-item {
|
|
232
|
+
.malloy-render .malloy-dashboard .dashboard-item:not(.dashboard-item-borderless) {
|
|
212
233
|
/* Tile padding around each chart / table. Uses our custom
|
|
213
234
|
--malloy-render--tile-background so the operator can theme the
|
|
214
235
|
tile separately from the table header row (which paints from
|
|
215
|
-
--malloy-render--table-pinned-background below).
|
|
236
|
+
--malloy-render--table-pinned-background below).
|
|
237
|
+
|
|
238
|
+
A flat bordered card rather than the renderer's shadow ring, which is what
|
|
239
|
+
makes it match the composite tile's Paper. :not(borderless) because
|
|
240
|
+
# borderless asks for no card at all, and an !important background and
|
|
241
|
+
border drew one anyway. */
|
|
216
242
|
background: var(--malloy-render--tile-background) !important;
|
|
217
243
|
color: var(--malloy-render--table-body-color) !important;
|
|
218
244
|
box-shadow: none !important;
|
|
@@ -340,6 +366,19 @@ function RenderedResultInner({
|
|
|
340
366
|
// bails, so overlapping renders can't leave two charts or leak a viz.
|
|
341
367
|
const renderGenRef = useRef(0);
|
|
342
368
|
const { theme: baseTheme, layers, mode } = usePublisherTheme();
|
|
369
|
+
// The host app's card radius, so a dashboard card reads as a card of the app
|
|
370
|
+
// it is embedded in rather than as the renderer's 8px on a 4px page. Both
|
|
371
|
+
// cards take it from here; see DashboardTile, which is the other one.
|
|
372
|
+
const cardRadius = useTheme().shape.borderRadius;
|
|
373
|
+
// Memoized because it is a dependency of the render effect below, and a fresh
|
|
374
|
+
// object each render would re-render the chart on every render.
|
|
375
|
+
const cardGeometry = useMemo<DashboardCardGeometry>(
|
|
376
|
+
() => ({
|
|
377
|
+
radius: `${cardRadius}px`,
|
|
378
|
+
padding: `${DASHBOARD_CARD_PADDING_PX}px`,
|
|
379
|
+
}),
|
|
380
|
+
[cardRadius],
|
|
381
|
+
);
|
|
343
382
|
|
|
344
383
|
// Dispose the last live viz on unmount only. Deliberately NOT done in the
|
|
345
384
|
// render effect's cleanup: a re-run must keep the old chart until the new
|
|
@@ -453,7 +492,7 @@ function RenderedResultInner({
|
|
|
453
492
|
// child of the container, so writing the new per-chart CSS vars there
|
|
454
493
|
// would repaint the old chart's chrome to the new theme before it is
|
|
455
494
|
// swapped out. Scoping the vars to this stage keeps each chart stable.
|
|
456
|
-
applyTableCssVars(stage, effectiveTheme);
|
|
495
|
+
applyTableCssVars(stage, effectiveTheme, cardGeometry);
|
|
457
496
|
if (previous) {
|
|
458
497
|
element.style.position = "relative";
|
|
459
498
|
stage.style.position = "absolute";
|
|
@@ -757,6 +796,7 @@ function RenderedResultInner({
|
|
|
757
796
|
baseTheme,
|
|
758
797
|
layers,
|
|
759
798
|
mode,
|
|
799
|
+
cardGeometry,
|
|
760
800
|
]);
|
|
761
801
|
|
|
762
802
|
// Malloy renderer requires explicit pixel height to render visualizations
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a suggest query sends and what it is cached under.
|
|
6
|
+
*
|
|
7
|
+
* Separate from the pure-function tests next door because `mock.module` has to
|
|
8
|
+
* run before the hook is imported, and a static import there would hoist above
|
|
9
|
+
* it.
|
|
10
|
+
*/
|
|
11
|
+
import { beforeEach, expect, it, mock } from "bun:test";
|
|
12
|
+
import { renderHook, waitFor } from "@testing-library/react";
|
|
13
|
+
import {
|
|
14
|
+
cacheKeys,
|
|
15
|
+
clearCache,
|
|
16
|
+
mockServerProvider,
|
|
17
|
+
pending,
|
|
18
|
+
serverWrapper,
|
|
19
|
+
} from "../../test/serverProvider";
|
|
20
|
+
import type { Given } from "../client";
|
|
21
|
+
|
|
22
|
+
const executeQueryModel = mock(
|
|
23
|
+
(
|
|
24
|
+
_environmentName: string,
|
|
25
|
+
_packageName: string,
|
|
26
|
+
_modelPath: string,
|
|
27
|
+
_request: { versionId?: string },
|
|
28
|
+
) => pending(),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
mockServerProvider({ models: { executeQueryModel } });
|
|
32
|
+
|
|
33
|
+
const { useSuggestOptions } = await import("./useSuggestOptions");
|
|
34
|
+
|
|
35
|
+
const SPECS: Given[] = [
|
|
36
|
+
{
|
|
37
|
+
name: "REGION",
|
|
38
|
+
control: "select",
|
|
39
|
+
suggest: { source: "orders", dimension: "region" },
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
const renderSuggest = (versionId?: string) =>
|
|
44
|
+
renderHook(
|
|
45
|
+
() =>
|
|
46
|
+
useSuggestOptions(
|
|
47
|
+
"env",
|
|
48
|
+
"pkg",
|
|
49
|
+
"dashboards/ops.malloy",
|
|
50
|
+
SPECS,
|
|
51
|
+
versionId,
|
|
52
|
+
),
|
|
53
|
+
{ wrapper: serverWrapper },
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
beforeEach(() => {
|
|
57
|
+
clearCache();
|
|
58
|
+
executeQueryModel.mockClear();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("sends the version with the suggest query and keys on it", async () => {
|
|
62
|
+
renderSuggest("v2");
|
|
63
|
+
|
|
64
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
65
|
+
expect(executeQueryModel.mock.calls[0][3].versionId).toBe("v2");
|
|
66
|
+
expect(cacheKeys("givenSuggest")[0]).toContain('"v2"');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("sends and keys nothing when no version was given", async () => {
|
|
70
|
+
renderSuggest();
|
|
71
|
+
|
|
72
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
73
|
+
expect(executeQueryModel.mock.calls[0][3].versionId).toBeUndefined();
|
|
74
|
+
// `useQueries` goes through the context client rather than
|
|
75
|
+
// `useQueryWithApiError`, so this key carries no trailing server.
|
|
76
|
+
expect(cacheKeys("givenSuggest")[0]).toBe(
|
|
77
|
+
'["givenSuggest","env","pkg",null,"dashboards/ops.malloy","REGION",' +
|
|
78
|
+
'null,"orders","region"]',
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("keeps two versions' option lists apart", async () => {
|
|
83
|
+
const first = renderSuggest("v1");
|
|
84
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalledTimes(1));
|
|
85
|
+
first.unmount();
|
|
86
|
+
|
|
87
|
+
renderSuggest("v2");
|
|
88
|
+
|
|
89
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalledTimes(2));
|
|
90
|
+
expect(new Set(cacheKeys("givenSuggest")).size).toBe(2);
|
|
91
|
+
});
|
|
@@ -86,6 +86,9 @@ export function useSuggestOptions(
|
|
|
86
86
|
packageName: string,
|
|
87
87
|
modelPath: string | undefined,
|
|
88
88
|
specs: Given[],
|
|
89
|
+
// Trailing and optional because this hook is exported from the package, so
|
|
90
|
+
// a slot beside `packageName` where it belongs would break every caller.
|
|
91
|
+
versionId?: string,
|
|
89
92
|
): {
|
|
90
93
|
options: Map<string, string[]>;
|
|
91
94
|
isLoading: boolean;
|
|
@@ -122,6 +125,7 @@ export function useSuggestOptions(
|
|
|
122
125
|
"givenSuggest",
|
|
123
126
|
environmentName,
|
|
124
127
|
packageName,
|
|
128
|
+
versionId,
|
|
125
129
|
modelPath,
|
|
126
130
|
spec.name,
|
|
127
131
|
spec.suggest?.query,
|
|
@@ -140,13 +144,14 @@ export function useSuggestOptions(
|
|
|
140
144
|
packageName,
|
|
141
145
|
modelPath as string,
|
|
142
146
|
suggest.query !== undefined
|
|
143
|
-
? { queryName: suggest.query, compactJson: true }
|
|
147
|
+
? { queryName: suggest.query, compactJson: true, versionId }
|
|
144
148
|
: {
|
|
145
149
|
query: buildSuggestQuery(
|
|
146
150
|
suggest.source as string,
|
|
147
151
|
suggest.dimension as string,
|
|
148
152
|
),
|
|
149
153
|
compactJson: true,
|
|
154
|
+
versionId,
|
|
150
155
|
},
|
|
151
156
|
);
|
|
152
157
|
return readOptionValues(
|
|
@@ -55,6 +55,25 @@ describe("buildTableCssVars", () => {
|
|
|
55
55
|
);
|
|
56
56
|
});
|
|
57
57
|
|
|
58
|
+
// The card geometry does NOT come from the instance theme: the radius is the
|
|
59
|
+
// host app's MUI `shape.borderRadius`, passed in, so a dashboard card reads as
|
|
60
|
+
// a card of the app it is embedded in. The default is the renderer's own, so a
|
|
61
|
+
// caller that passes nothing gets today's look.
|
|
62
|
+
it("carries the caller's card geometry into the publisher namespace", () => {
|
|
63
|
+
const vars = buildTableCssVars(resolveTheme([], "light"), {
|
|
64
|
+
radius: "4px",
|
|
65
|
+
padding: "20px",
|
|
66
|
+
});
|
|
67
|
+
expect(vars["--publisher-dashboard-card-radius"]).toBe("4px");
|
|
68
|
+
expect(vars["--publisher-dashboard-card-padding"]).toBe("20px");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("defaults the card geometry to the renderer's own", () => {
|
|
72
|
+
const vars = buildTableCssVars(resolveTheme([], "light"));
|
|
73
|
+
expect(vars["--publisher-dashboard-card-radius"]).toBe("8px");
|
|
74
|
+
expect(vars["--publisher-dashboard-card-padding"]).toBe("20px");
|
|
75
|
+
});
|
|
76
|
+
|
|
58
77
|
it("emits the font-family token consumed by the renderer", () => {
|
|
59
78
|
const t = resolveTheme(
|
|
60
79
|
[{ font: { family: "Roboto, sans-serif" } }],
|
|
@@ -3,6 +3,29 @@
|
|
|
3
3
|
|
|
4
4
|
import type { ResolvedTheme } from "./types";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Padding inside a dashboard card, around the result. The renderer's own
|
|
8
|
+
* default, shared with the composite tile so the two cards cannot drift.
|
|
9
|
+
*/
|
|
10
|
+
export const DASHBOARD_CARD_PADDING_PX = 20;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Geometry the two dashboard cards agree on. Not part of {@link ResolvedTheme}
|
|
14
|
+
* because the radius comes from the HOST's MUI `shape.borderRadius` rather than
|
|
15
|
+
* from the instance theme: a dashboard card should read as a card of the app it
|
|
16
|
+
* is embedded in, and pinning it to a constant is what made the renderer's 8px
|
|
17
|
+
* the only 8px card on a 4px page.
|
|
18
|
+
*/
|
|
19
|
+
export interface DashboardCardGeometry {
|
|
20
|
+
radius: string;
|
|
21
|
+
padding: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const DEFAULT_CARD_GEOMETRY: DashboardCardGeometry = {
|
|
25
|
+
radius: "8px",
|
|
26
|
+
padding: `${DASHBOARD_CARD_PADDING_PX}px`,
|
|
27
|
+
};
|
|
28
|
+
|
|
6
29
|
/**
|
|
7
30
|
* Build the CSS variable map applied to the wrapper element around
|
|
8
31
|
* `<malloy-render>`. The values here power two things only:
|
|
@@ -28,6 +51,7 @@ import type { ResolvedTheme } from "./types";
|
|
|
28
51
|
*/
|
|
29
52
|
export function buildTableCssVars(
|
|
30
53
|
theme: ResolvedTheme,
|
|
54
|
+
card: DashboardCardGeometry = DEFAULT_CARD_GEOMETRY,
|
|
31
55
|
): Record<string, string> {
|
|
32
56
|
return {
|
|
33
57
|
"--malloy-render--font-family": theme.font.family,
|
|
@@ -63,5 +87,12 @@ export function buildTableCssVars(
|
|
|
63
87
|
// .publisher-drill rule in injectRendererOverrides: the renderer
|
|
64
88
|
// has no notion of drill, so the affordance is entirely ours.
|
|
65
89
|
"--publisher-drill-link": theme.drillLink,
|
|
90
|
+
// Card geometry, in the publisher namespace because the renderer's own
|
|
91
|
+
// `--malloy-theme--dashboard-*` are declared ON `.malloy-render` and so
|
|
92
|
+
// beat anything inherited from this wrapper. injectRendererOverrides
|
|
93
|
+
// redeclares them there at raised specificity reading these, which is why
|
|
94
|
+
// that route needs no !important.
|
|
95
|
+
"--publisher-dashboard-card-radius": card.radius,
|
|
96
|
+
"--publisher-dashboard-card-padding": card.padding,
|
|
66
97
|
};
|
|
67
98
|
}
|
package/test/README.md
CHANGED
|
@@ -101,6 +101,50 @@ as props, the way the page hosts do through the URL. Several defects in that
|
|
|
101
101
|
hook only appear on the second half of that loop, so a spec that treats the
|
|
102
102
|
props as constant cannot see them.
|
|
103
103
|
|
|
104
|
+
## Testing something that talks to the server
|
|
105
|
+
|
|
106
|
+
Anything that calls `useServer()` needs `ServerProvider` replaced, because the
|
|
107
|
+
real one builds axios clients against `window.location`. `test/serverProvider.tsx`
|
|
108
|
+
is the one way to do it:
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
import { cacheKeys, clearCache, mockServerProvider, pending, serverWrapper }
|
|
112
|
+
from "../../../test/serverProvider";
|
|
113
|
+
|
|
114
|
+
const getDashboard = mock(() => pending());
|
|
115
|
+
mockServerProvider({ dashboards: { getDashboard } });
|
|
116
|
+
|
|
117
|
+
// AFTER the line above, never a static import: it would hoist past the stub.
|
|
118
|
+
const { default: Dashboard } = await import("./Dashboard");
|
|
119
|
+
|
|
120
|
+
render(<Dashboard resourceUri={URI} dashboard="ops" />, { wrapper: serverWrapper });
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Three things that file exists to get right.
|
|
124
|
+
|
|
125
|
+
`mock.module` is **process-global and cannot be undone**. `bun test` runs every
|
|
126
|
+
spec in one process, so a stub registered here is the `ServerProvider` every
|
|
127
|
+
later spec sees, in file order. Nothing in Bun scopes it to a file. That is
|
|
128
|
+
survivable only while every spec registers the same shape, which is why the
|
|
129
|
+
stub lives in one place rather than being hand-rolled per file: a spec that
|
|
130
|
+
inherits someone else's stub still gets `{ server, apiClients }`, and fails on
|
|
131
|
+
a missing client rather than on a missing property of `undefined`. If you ever
|
|
132
|
+
need a spec to see the REAL `ServerProvider`, it cannot share a process with
|
|
133
|
+
these — that would need its own `bun test` invocation.
|
|
134
|
+
|
|
135
|
+
`serverWrapper` provides `globalQueryClient`, the same instance `ServerProvider`
|
|
136
|
+
provides in production. It is not decoration: `useQueryWithApiError` passes that
|
|
137
|
+
client to `useQuery` explicitly while `useQueries` resolves one from context, so
|
|
138
|
+
a spec that supplies a fresh client sees only half its component's queries.
|
|
139
|
+
Sharing one client is also why `clearCache()` belongs in `beforeEach` — the
|
|
140
|
+
cache outlives the test that filled it.
|
|
141
|
+
|
|
142
|
+
`cacheKeys(prefix)` reads the query keys back out as strings. Assert on the
|
|
143
|
+
whole key, not just on the value you added: a key is the only thing standing
|
|
144
|
+
between two requests that differ, so "the version is in there somewhere" passes
|
|
145
|
+
for a key that also grew a slot it should not have. Note the two key shapes —
|
|
146
|
+
`useQueryWithApiError` appends the server URL, plain `useQueries` does not.
|
|
147
|
+
|
|
104
148
|
## Where specs live, and what not to break
|
|
105
149
|
|
|
106
150
|
Co-locate a spec with what it tests, matching the existing `*.spec.ts` files.
|