@malloy-publisher/sdk 0.0.239 → 0.0.241
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/bunfig.toml +4 -0
- package/dist/ServerProvider-D40U1tBk.cjs.js +1 -0
- package/dist/{ServerProvider-kzoOMXNx.es.js → ServerProvider-FQ4I4dKu.es.js} +250 -241
- package/dist/client/api.d.ts +181 -109
- 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 +70 -68
- package/dist/components/{PageViewer/PageViewer.d.ts → DataAppViewer/DataAppViewer.d.ts} +6 -6
- package/dist/components/DataAppViewer/index.d.ts +1 -0
- package/dist/components/Package/ContentTypeIcon.d.ts +1 -1
- package/dist/components/ServerProvider.d.ts +2 -2
- package/dist/components/index.d.ts +1 -1
- package/dist/{core-DtPGM_JB.cjs.js → core-2tUy-45C.cjs.js} +1 -1
- package/dist/{core-BGHaIj_q.es.js → core-KFsKleo0.es.js} +1 -1
- package/dist/hooks/useGivensForm.d.ts +8 -4
- package/dist/{index-B2gBd2gP.cjs.js → index-C0EfXZ-k.cjs.js} +31 -31
- package/dist/{index-CEteIhTc.es.js → index-D2sq6csh.es.js} +1825 -1829
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.es.js +34 -34
- package/dist/utils/{pageEmbed.d.ts → dataAppEmbed.d.ts} +7 -7
- package/package.json +7 -4
- package/src/components/{PageViewer/PageViewer.tsx → DataAppViewer/DataAppViewer.tsx} +41 -36
- package/src/components/DataAppViewer/index.ts +1 -0
- package/src/components/Package/ContentTypeIcon.tsx +2 -2
- package/src/components/Package/Package.tsx +28 -30
- package/src/components/ServerProvider.tsx +2 -2
- package/src/components/given/GivenInput.spec.tsx +206 -0
- package/src/components/given/GivenInput.tsx +6 -1
- package/src/components/index.ts +1 -1
- package/src/hooks/useGivensForm.spec.ts +99 -0
- package/src/hooks/useGivensForm.ts +14 -5
- package/src/index.ts +1 -1
- package/src/utils/{pageEmbed.spec.ts → dataAppEmbed.spec.ts} +1 -1
- package/src/utils/{pageEmbed.ts → dataAppEmbed.ts} +9 -9
- package/test/README.md +130 -0
- package/test/setup.ts +26 -0
- package/tsconfig.json +12 -1
- package/tsconfig.spec.json +22 -0
- package/dist/ServerProvider-nJNqhxy6.cjs.js +0 -1
- package/dist/components/PageViewer/index.d.ts +0 -1
- package/src/components/PageViewer/index.ts +0 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { describe, expect, it, mock } from "bun:test";
|
|
2
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import type { GivenValue } from "../../hooks/useGivensForm";
|
|
4
|
+
import { GivenInput } from "./GivenInput";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Covers the three-state contract GivenInput's own doc comment specifies:
|
|
8
|
+
* unset vs. an explicit override (including `""` and `false`) vs. a revert.
|
|
9
|
+
* None of it had any verification before this suite.
|
|
10
|
+
*
|
|
11
|
+
* The distinction is not cosmetic: unset omits the given from the request so
|
|
12
|
+
* the server applies the model default, while an explicit `""` is sent
|
|
13
|
+
* verbatim. The only thing telling a user which state they are in is whether
|
|
14
|
+
* the clear (x) affordance is showing, so each test below asserts on it.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const clearButtons = () => screen.queryAllByLabelText("clear value");
|
|
18
|
+
|
|
19
|
+
describe("GivenInput: unset vs. explicit override", () => {
|
|
20
|
+
it("unset shows the model default as a placeholder and offers no revert", () => {
|
|
21
|
+
render(
|
|
22
|
+
<GivenInput
|
|
23
|
+
given={{ name: "region", type: "string", default: "'WN'" }}
|
|
24
|
+
value={undefined}
|
|
25
|
+
onChange={() => {}}
|
|
26
|
+
/>,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const input = screen.getByLabelText("region") as HTMLInputElement;
|
|
30
|
+
expect(input.value).toBe("");
|
|
31
|
+
expect(input.placeholder).toBe("WN");
|
|
32
|
+
// Nothing to revert to: the widget is already showing the default.
|
|
33
|
+
expect(clearButtons()).toHaveLength(0);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("an explicit empty string is an override, not an unset", () => {
|
|
37
|
+
render(
|
|
38
|
+
<GivenInput
|
|
39
|
+
given={{ name: "region", type: "string", default: "'WN'" }}
|
|
40
|
+
value=""
|
|
41
|
+
onChange={() => {}}
|
|
42
|
+
/>,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const input = screen.getByLabelText("region") as HTMLInputElement;
|
|
46
|
+
expect(input.value).toBe("");
|
|
47
|
+
// The ghost default is suppressed and the revert appears, which is the
|
|
48
|
+
// only thing distinguishing this from the unset case above: both render
|
|
49
|
+
// an empty box.
|
|
50
|
+
expect(input.placeholder).toBe("");
|
|
51
|
+
expect(clearButtons()).toHaveLength(1);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("the revert affordance reverts rather than blanking the field", () => {
|
|
55
|
+
// Typed, not `mock(() => {})`: an untyped zero-arg mock makes
|
|
56
|
+
// `mock.calls` an array of empty tuples, so reading `[0][0]` below does
|
|
57
|
+
// not compile and nothing constrains what the component passes.
|
|
58
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
59
|
+
render(
|
|
60
|
+
<GivenInput
|
|
61
|
+
given={{ name: "region", type: "string" }}
|
|
62
|
+
value="CA"
|
|
63
|
+
onChange={onChange}
|
|
64
|
+
/>,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
fireEvent.click(clearButtons()[0]);
|
|
68
|
+
|
|
69
|
+
// null, not "". `useGivensForm` deletes the key on null, which is what
|
|
70
|
+
// returns the given to "use the model default".
|
|
71
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
72
|
+
expect(onChange.mock.calls[0][0]).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("GivenInput: boolean is three-state, not truthy/falsy", () => {
|
|
77
|
+
it("unset reflects the model default so the box matches what will run", () => {
|
|
78
|
+
render(
|
|
79
|
+
<GivenInput
|
|
80
|
+
given={{ name: "active", type: "boolean", default: "true" }}
|
|
81
|
+
value={undefined}
|
|
82
|
+
onChange={() => {}}
|
|
83
|
+
/>,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
const box = screen.getByLabelText("active") as HTMLInputElement;
|
|
87
|
+
expect(box.checked).toBe(true);
|
|
88
|
+
expect(clearButtons()).toHaveLength(0);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("an explicit false is an override, distinguishable from unset", () => {
|
|
92
|
+
render(
|
|
93
|
+
<GivenInput
|
|
94
|
+
given={{ name: "active", type: "boolean", default: "true" }}
|
|
95
|
+
value={false}
|
|
96
|
+
onChange={() => {}}
|
|
97
|
+
/>,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const box = screen.getByLabelText("active") as HTMLInputElement;
|
|
101
|
+
expect(box.checked).toBe(false);
|
|
102
|
+
// Guards the `typeof value === "boolean"` test in the component. A
|
|
103
|
+
// truthiness check would read `false` as unset and hide this revert,
|
|
104
|
+
// stranding the user on a value the server is being sent.
|
|
105
|
+
expect(clearButtons()).toHaveLength(1);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("GivenInput: helper text", () => {
|
|
110
|
+
it("surfaces a description annotation verbatim", () => {
|
|
111
|
+
render(
|
|
112
|
+
<GivenInput
|
|
113
|
+
given={{
|
|
114
|
+
name: "since",
|
|
115
|
+
type: "string",
|
|
116
|
+
annotations: ['#(description="Earliest report date")'],
|
|
117
|
+
}}
|
|
118
|
+
value={undefined}
|
|
119
|
+
onChange={() => {}}
|
|
120
|
+
/>,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
expect(screen.getByText("Earliest report date")).toBeDefined();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("shows an explicit empty-string default as (empty), not as no default", () => {
|
|
127
|
+
render(
|
|
128
|
+
<GivenInput
|
|
129
|
+
given={{ name: "region", type: "string", default: "''" }}
|
|
130
|
+
value={undefined}
|
|
131
|
+
onChange={() => {}}
|
|
132
|
+
/>,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
// Guards the `!== undefined` test in the component: `is ''` renders as
|
|
136
|
+
// "", and a truthiness check would drop the caption entirely, making a
|
|
137
|
+
// real empty-string default look like no default at all.
|
|
138
|
+
expect(screen.getByText("Default: (empty)")).toBeDefined();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("GivenInput: number", () => {
|
|
143
|
+
it("treats 0 as a value, not as absent", () => {
|
|
144
|
+
render(
|
|
145
|
+
<GivenInput
|
|
146
|
+
given={{ name: "limit", type: "number" }}
|
|
147
|
+
value={0}
|
|
148
|
+
onChange={() => {}}
|
|
149
|
+
/>,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const input = screen.getByLabelText("limit") as HTMLInputElement;
|
|
153
|
+
expect(input.value).toBe("0");
|
|
154
|
+
expect(clearButtons()).toHaveLength(1);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("still offers a revert when the value is not a number", () => {
|
|
158
|
+
// Reachable through the public component API: GivenInput and GivenValue
|
|
159
|
+
// are both published SDK exports, and GivenValue is a union permitting a
|
|
160
|
+
// string while `type` says number.
|
|
161
|
+
render(
|
|
162
|
+
<GivenInput
|
|
163
|
+
given={{ name: "limit", type: "number" }}
|
|
164
|
+
value={"12abc"}
|
|
165
|
+
onChange={() => {}}
|
|
166
|
+
/>,
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
// The box reads empty because the component substitutes "" for a
|
|
170
|
+
// non-number, and passing the raw string instead would not help: a
|
|
171
|
+
// browser discards it anyway on input[type=number] (checked in
|
|
172
|
+
// Chromium). So the revert is the user's only route out, and it used to
|
|
173
|
+
// be hidden too, because it keyed off the rendered string being
|
|
174
|
+
// non-empty rather than off a value being overridden.
|
|
175
|
+
expect(clearButtons()).toHaveLength(1);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe("GivenInput: which types are actually reachable", () => {
|
|
180
|
+
it("renders a multi-value widget only for the array<...> spelling", () => {
|
|
181
|
+
render(
|
|
182
|
+
<GivenInput
|
|
183
|
+
given={{ name: "tags", type: "array<string>" }}
|
|
184
|
+
value={undefined}
|
|
185
|
+
onChange={() => {}}
|
|
186
|
+
/>,
|
|
187
|
+
);
|
|
188
|
+
expect(screen.queryAllByRole("combobox")).toHaveLength(1);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("falls back to a text box for the bare array type the server emits", () => {
|
|
192
|
+
// The array<...> branch is unreachable twice over. Malloy's grammar emits
|
|
193
|
+
// only scalar parameter types, so no array given exists to send; and if
|
|
194
|
+
// one did, `given.ts` renders the wire type as the bare discriminator
|
|
195
|
+
// "array", which does not match the component's `startsWith("array<")`.
|
|
196
|
+
render(
|
|
197
|
+
<GivenInput
|
|
198
|
+
given={{ name: "tags", type: "array" }}
|
|
199
|
+
value={undefined}
|
|
200
|
+
onChange={() => {}}
|
|
201
|
+
/>,
|
|
202
|
+
);
|
|
203
|
+
expect(screen.queryAllByRole("combobox")).toHaveLength(0);
|
|
204
|
+
expect(screen.getByLabelText("tags")).toBeDefined();
|
|
205
|
+
});
|
|
206
|
+
});
|
|
@@ -141,6 +141,11 @@ export function GivenInput({ given, value, onChange }: GivenInputProps) {
|
|
|
141
141
|
|
|
142
142
|
if (type === "number") {
|
|
143
143
|
const num = typeof value === "number" ? value : "";
|
|
144
|
+
// Track whether a value is OVERRIDDEN, the way the string branch below
|
|
145
|
+
// does, rather than whether the box happens to render non-empty. A
|
|
146
|
+
// non-number renders blank (see `num` above), and keying the revert off
|
|
147
|
+
// that blank left the user no way to clear it.
|
|
148
|
+
const isOverridden = value !== undefined && value !== null;
|
|
144
149
|
return (
|
|
145
150
|
<TextField
|
|
146
151
|
label={label}
|
|
@@ -154,7 +159,7 @@ export function GivenInput({ given, value, onChange }: GivenInputProps) {
|
|
|
154
159
|
helperText={helperNode}
|
|
155
160
|
slotProps={{
|
|
156
161
|
input: {
|
|
157
|
-
endAdornment:
|
|
162
|
+
endAdornment: isOverridden && (
|
|
158
163
|
<ClearAdornment onClear={() => onChange(null)} />
|
|
159
164
|
),
|
|
160
165
|
},
|
package/src/components/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { AnalyzePackageButton } from "./AnalyzePackageButton";
|
|
2
2
|
export { useRouterClickHandler } from "./click_helper";
|
|
3
|
+
export * from "./DataAppViewer";
|
|
3
4
|
export * from "./Environment";
|
|
4
5
|
export * from "./filter";
|
|
5
6
|
export * from "./Home";
|
|
@@ -8,7 +9,6 @@ export * from "./Materializations";
|
|
|
8
9
|
export * from "./Model";
|
|
9
10
|
export * from "./Notebook";
|
|
10
11
|
export * from "./Package";
|
|
11
|
-
export * from "./PageViewer";
|
|
12
12
|
export * from "./QueryResult";
|
|
13
13
|
export * from "./RenderedResult";
|
|
14
14
|
export { ServerProvider, useServer } from "./ServerProvider";
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { act, renderHook } from "@testing-library/react";
|
|
3
|
+
import type { Given } from "../client";
|
|
4
|
+
import { useGivensForm } from "./useGivensForm";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook-testing pattern for this package: `renderHook` to mount the hook,
|
|
8
|
+
* `act()` around anything that sets state, then assert on `result.current`.
|
|
9
|
+
* No JSX is involved, so this is a plain `.spec.ts`.
|
|
10
|
+
*
|
|
11
|
+
* A migration note, since #935 replaces this hook with `useGivensState`:
|
|
12
|
+
* these tests describe behaviours a replacement has to preserve, not an API
|
|
13
|
+
* contract worth keeping, so rewrite them alongside the hook. The last one
|
|
14
|
+
* matters most. It guards a defect that was live here, and a replacement that
|
|
15
|
+
* rebuilds its state on every run of the sync effect will reintroduce it.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const givens = (...names: string[]): Given[] =>
|
|
19
|
+
names.map((name) => ({ name, type: "string" }));
|
|
20
|
+
|
|
21
|
+
describe("useGivensForm", () => {
|
|
22
|
+
it("starts with no overrides, so every given uses its model default", () => {
|
|
23
|
+
const { result } = renderHook(() => useGivensForm(givens("region")));
|
|
24
|
+
expect(result.current.givenValues.size).toBe(0);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("records an override and reports it as active", () => {
|
|
28
|
+
const { result } = renderHook(() => useGivensForm(givens("region")));
|
|
29
|
+
|
|
30
|
+
act(() => result.current.updateGiven("region", "CA"));
|
|
31
|
+
|
|
32
|
+
expect(result.current.givenValues.get("region")).toBe("CA");
|
|
33
|
+
expect([...result.current.getActiveGivens()]).toEqual([["region", "CA"]]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("treats null as a revert, deleting the key rather than storing null", () => {
|
|
37
|
+
const { result } = renderHook(() => useGivensForm(givens("region")));
|
|
38
|
+
|
|
39
|
+
act(() => result.current.updateGiven("region", "CA"));
|
|
40
|
+
act(() => result.current.updateGiven("region", null));
|
|
41
|
+
|
|
42
|
+
// Deleted, not set to null. A stored null would still be an entry, and
|
|
43
|
+
// the request builder would send the given rather than omitting it.
|
|
44
|
+
expect(result.current.givenValues.has("region")).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("keeps an explicit empty string, which is an override not a revert", () => {
|
|
48
|
+
const { result } = renderHook(() => useGivensForm(givens("region")));
|
|
49
|
+
|
|
50
|
+
act(() => result.current.updateGiven("region", ""));
|
|
51
|
+
|
|
52
|
+
expect(result.current.givenValues.get("region")).toBe("");
|
|
53
|
+
expect(result.current.getActiveGivens().has("region")).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("clearAll drops every override", () => {
|
|
57
|
+
const { result } = renderHook(() => useGivensForm(givens("a", "b")));
|
|
58
|
+
|
|
59
|
+
act(() => result.current.updateGiven("a", "1"));
|
|
60
|
+
act(() => result.current.updateGiven("b", "2"));
|
|
61
|
+
act(() => result.current.clearAll());
|
|
62
|
+
|
|
63
|
+
expect(result.current.givenValues.size).toBe(0);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("drops overrides for givens the model no longer declares", () => {
|
|
67
|
+
const { result, rerender } = renderHook(
|
|
68
|
+
({ declared }) => useGivensForm(declared),
|
|
69
|
+
{ initialProps: { declared: givens("a", "b") } },
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
act(() => result.current.updateGiven("a", "1"));
|
|
73
|
+
act(() => result.current.updateGiven("b", "2"));
|
|
74
|
+
|
|
75
|
+
// The model reloaded and "b" is gone.
|
|
76
|
+
rerender({ declared: givens("a") });
|
|
77
|
+
|
|
78
|
+
expect(result.current.givenValues.get("a")).toBe("1");
|
|
79
|
+
expect(result.current.givenValues.has("b")).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("settles when `givens` is a fresh array on every render", () => {
|
|
83
|
+
// This hook is a published SDK export, so a consumer can reasonably call
|
|
84
|
+
// it with an array literal instead of a memoized one. The sync effect
|
|
85
|
+
// keys on `givens` identity, so an unmemoized array re-fires it on every
|
|
86
|
+
// render; if it returns a new Map each time, state identity changes,
|
|
87
|
+
// that re-renders, and the two drive each other until React throws
|
|
88
|
+
// "Maximum update depth exceeded". In-repo callers are shielded only
|
|
89
|
+
// because `useModelGivens` happens to memoize.
|
|
90
|
+
let renders = 0;
|
|
91
|
+
const { result } = renderHook(() => {
|
|
92
|
+
renders++;
|
|
93
|
+
return useGivensForm([{ name: "region", type: "string" }]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(result.current.givenValues.size).toBe(0);
|
|
97
|
+
expect(renders).toBeLessThan(5);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -32,12 +32,16 @@ export interface UseGivensFormResult {
|
|
|
32
32
|
* Manages user-supplied values for model `given:` parameters.
|
|
33
33
|
*
|
|
34
34
|
* Mirrors the shape of [`useDimensionFilters`](./useDimensionFilters.ts):
|
|
35
|
-
*
|
|
36
|
-
*
|
|
35
|
+
* keyed by given name, with no source qualifier, because givens are
|
|
36
|
+
* model-level rather than per-source. One entry per OVERRIDE, not per
|
|
37
|
+
* declared given: the map starts empty and a missing key means "use the
|
|
38
|
+
* model default".
|
|
37
39
|
*
|
|
38
40
|
* When `givens` (the introspected list) changes (e.g., after the notebook
|
|
39
|
-
* loads), existing user overrides are preserved for matching names and
|
|
40
|
-
*
|
|
41
|
+
* loads), existing user overrides are preserved for matching names and
|
|
42
|
+
* overrides for names that are no longer declared are dropped. A newly
|
|
43
|
+
* declared given gets no entry: the map holds overrides only, and a missing
|
|
44
|
+
* key already means "use the model default".
|
|
41
45
|
*/
|
|
42
46
|
export function useGivensForm(givens: Given[]): UseGivensFormResult {
|
|
43
47
|
const [givenValues, setGivenValues] = useState<Map<string, GivenValue>>(
|
|
@@ -56,7 +60,12 @@ export function useGivensForm(givens: Given[]): UseGivensFormResult {
|
|
|
56
60
|
prev.forEach((value, name) => {
|
|
57
61
|
if (declared.has(name)) next.set(name, value);
|
|
58
62
|
});
|
|
59
|
-
|
|
63
|
+
// `next` is `prev` minus undeclared names, so equal sizes means
|
|
64
|
+
// nothing was dropped. Return `prev` itself in that case: handing back
|
|
65
|
+
// a fresh Map every time changes state identity on every run, and a
|
|
66
|
+
// caller passing an unmemoized `givens` array then re-renders, which
|
|
67
|
+
// re-fires this effect, until React gives up.
|
|
68
|
+
return next.size === prev.size ? prev : next;
|
|
60
69
|
});
|
|
61
70
|
}, [givens]);
|
|
62
71
|
|
package/src/index.ts
CHANGED
|
@@ -8,5 +8,5 @@ export * from "./utils/formatting";
|
|
|
8
8
|
// How a package's own files are addressed. Exported because a host that renders
|
|
9
9
|
// its own "this path is not a model" state has to point at the URL that does
|
|
10
10
|
// serve the file, and this is the one place that string is built.
|
|
11
|
-
export { packageFileUrl } from "./utils/
|
|
11
|
+
export { packageFileUrl } from "./utils/dataAppEmbed";
|
|
12
12
|
export * from "./constants/docLinks";
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
// Shared contract for serving and embedding in-package HTML
|
|
1
|
+
// Shared contract for serving and embedding in-package HTML data apps.
|
|
2
2
|
//
|
|
3
|
-
// Both the Publisher SPA host (
|
|
3
|
+
// Both the Publisher SPA host (DataAppViewer.tsx, Package.tsx) and the
|
|
4
4
|
// build-step-free browser runtime (packages/server/src/runtime/publisher.js)
|
|
5
5
|
// speak this protocol. publisher.js cannot import from here — it ships as
|
|
6
6
|
// standalone vanilla JS — so it carries a cross-reference comment pointing at
|
|
7
7
|
// this file as the single source of truth. Keep the two in sync.
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
* postMessage `type` an embedded
|
|
11
|
-
* height changes, so the host can resize the iframe to avoid nested
|
|
12
|
-
* Payload shape is {@link PublisherResizeMessage}.
|
|
10
|
+
* postMessage `type` an embedded data app emits to its host frame as its
|
|
11
|
+
* content height changes, so the host can resize the iframe to avoid nested
|
|
12
|
+
* scrollbars. Payload shape is {@link PublisherResizeMessage}.
|
|
13
13
|
*/
|
|
14
14
|
export const PUBLISHER_RESIZE_MESSAGE_TYPE = "publisher:resize";
|
|
15
15
|
|
|
16
|
-
/** Resize message posted by an embedded
|
|
16
|
+
/** Resize message posted by an embedded data app's publisher.js runtime. */
|
|
17
17
|
export interface PublisherResizeMessage {
|
|
18
18
|
type: typeof PUBLISHER_RESIZE_MESSAGE_TYPE;
|
|
19
19
|
/** Content height in CSS pixels. */
|
|
@@ -38,8 +38,8 @@ export function isPublisherResizeMessage(
|
|
|
38
38
|
*
|
|
39
39
|
* The static-file routes live off the server root, not under the API prefix,
|
|
40
40
|
* and the data origin can differ from the SPA origin in multi-host
|
|
41
|
-
* deployments — so a
|
|
42
|
-
* with
|
|
41
|
+
* deployments — so a data app's standalone URL is `serverBaseUrl(server)`
|
|
42
|
+
* joined with its root-relative `resource`.
|
|
43
43
|
*/
|
|
44
44
|
export function serverBaseUrl(server: string): string {
|
|
45
45
|
return server.replace(/\/api\/v0\/?$/, "");
|
|
@@ -49,7 +49,7 @@ export function serverBaseUrl(server: string): string {
|
|
|
49
49
|
* Absolute URL of a file inside a package, as Publisher serves it:
|
|
50
50
|
* `<data origin>/environments/<env>/packages/<pkg>/<path>`.
|
|
51
51
|
*
|
|
52
|
-
* The one home for this string. Both the
|
|
52
|
+
* The one home for this string. Both the data app viewer's iframe and the
|
|
53
53
|
* "this is not a model" page point at it, and hand-building it in each place is
|
|
54
54
|
* how the two drift.
|
|
55
55
|
*
|
package/test/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Testing React components and hooks in the SDK
|
|
2
|
+
|
|
3
|
+
`bun test` in this package runs with a DOM, so components and hooks can be
|
|
4
|
+
rendered and asserted on. Before this existed every SDK spec was a pure
|
|
5
|
+
function, and anything touching React was untestable.
|
|
6
|
+
|
|
7
|
+
Run them the usual way:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
cd packages/sdk && bun test # whole package
|
|
11
|
+
cd packages/sdk && bun test src/components/given # one directory
|
|
12
|
+
cd packages/sdk && bun test -t "explicit false" # one test by name
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Run them from this package, not from the repo root. Bun reads `bunfig.toml`
|
|
16
|
+
from the working directory, so a spec invoked from the root gets no preload and
|
|
17
|
+
fails with `ReferenceError: document is not defined`. `bun run test:sdk` at the
|
|
18
|
+
root is fine; it changes directory first, and so does CI.
|
|
19
|
+
|
|
20
|
+
## How the DOM gets there
|
|
21
|
+
|
|
22
|
+
`bunfig.toml` preloads `test/setup.ts`, which registers happy-dom onto
|
|
23
|
+
`globalThis` and installs Testing Library's `cleanup` in an `afterEach`. There
|
|
24
|
+
is no per-file setup: write a spec and it has a document.
|
|
25
|
+
|
|
26
|
+
Two details in that file are load-bearing. Testing Library is imported
|
|
27
|
+
dynamically, after the registrator runs, because it binds to `document` at
|
|
28
|
+
module scope and a static import would hoist above the line that creates one.
|
|
29
|
+
And the setup file lives outside `src/` because `vite-plugin-dts` generates
|
|
30
|
+
declarations for everything under `src/`, so a setup file in there would ship a
|
|
31
|
+
stray `.d.ts` in `dist/`.
|
|
32
|
+
|
|
33
|
+
happy-dom rather than jsdom because `@happy-dom/global-registrator` is designed
|
|
34
|
+
to install into an existing runtime, which is exactly what a Bun preload can do.
|
|
35
|
+
jsdom expects to own the test environment and offers no equivalent hook, so
|
|
36
|
+
using it would have meant bringing in a second test runner alongside `bun test`.
|
|
37
|
+
|
|
38
|
+
## Testing a component
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
import { describe, expect, it, mock } from "bun:test";
|
|
42
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
43
|
+
import type { GivenValue } from "../../hooks/useGivensForm";
|
|
44
|
+
import { GivenInput } from "./GivenInput";
|
|
45
|
+
|
|
46
|
+
it("reverts rather than blanking the field", () => {
|
|
47
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
48
|
+
render(<GivenInput given={{ name: "region" }} value="CA" onChange={onChange} />);
|
|
49
|
+
|
|
50
|
+
fireEvent.click(screen.getByLabelText("clear value"));
|
|
51
|
+
|
|
52
|
+
expect(onChange.mock.calls[0][0]).toBeNull();
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Type the mock's parameter. `mock(() => {})` is a zero-argument mock, so
|
|
57
|
+
`mock.calls` is an array of empty tuples: reading `[0][0]` does not compile,
|
|
58
|
+
and nothing constrains what the component actually passes you.
|
|
59
|
+
|
|
60
|
+
Query by what a user can perceive (`getByLabelText`, `getByRole`,
|
|
61
|
+
`getByText`) rather than by class name or component internals, so a test
|
|
62
|
+
survives a restyle. Use `queryAllBy*` when asserting something is absent;
|
|
63
|
+
`getBy*` throws instead of returning empty.
|
|
64
|
+
|
|
65
|
+
`src/components/given/GivenInput.spec.tsx` is the worked example.
|
|
66
|
+
|
|
67
|
+
## Testing a hook
|
|
68
|
+
|
|
69
|
+
No JSX is needed, so a hook spec is a plain `.spec.ts`.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { act, renderHook } from "@testing-library/react";
|
|
73
|
+
|
|
74
|
+
const { result, rerender } = renderHook(
|
|
75
|
+
({ declared }) => useGivensForm(declared),
|
|
76
|
+
{ initialProps: { declared: [{ name: "region" }] } },
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
act(() => result.current.updateGiven("region", "CA"));
|
|
80
|
+
expect(result.current.givenValues.get("region")).toBe("CA");
|
|
81
|
+
|
|
82
|
+
rerender({ declared: [] }); // props change, effects flush
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`result.current` is re-read after every update, so hold onto the object rather
|
|
86
|
+
than destructuring off it. Wrap anything that sets state in `act()`.
|
|
87
|
+
|
|
88
|
+
`src/hooks/useGivensForm.spec.ts` is the worked example, and its last test is
|
|
89
|
+
worth copying: it renders a hook with a deliberately unmemoized argument to
|
|
90
|
+
prove the hook settles instead of re-rendering forever. That is a real defect
|
|
91
|
+
class here, and it is invisible to a test that only ever passes stable inputs.
|
|
92
|
+
|
|
93
|
+
## Where specs live, and what not to break
|
|
94
|
+
|
|
95
|
+
Co-locate a spec with what it tests, matching the existing `*.spec.ts` files.
|
|
96
|
+
|
|
97
|
+
`tsconfig.json` excludes test files, because that config drives the declaration
|
|
98
|
+
build and anything left in it emits a stray `.d.ts` into `dist/`, which ships.
|
|
99
|
+
`bun test` discovers four name patterns, and both configs list all four with
|
|
100
|
+
the extension left open, so `.tsx`/`.mts`/`.cts` cannot slip past:
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
*.spec.* *.test.* *_spec.* *_test.*
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Excluded is not unchecked. `tsconfig.spec.json` includes the same four plus
|
|
107
|
+
everything in `test/`, and the root `typecheck:sdk` runs it, so a type error in
|
|
108
|
+
a spec fails the gate. Without it there would be no gate at all: `bun test`
|
|
109
|
+
strips types without checking them, and eslint in this package is not
|
|
110
|
+
type-aware.
|
|
111
|
+
|
|
112
|
+
Keep the two lists in step, and if you add a pattern, put it in both and check
|
|
113
|
+
`dist/` afterwards.
|
|
114
|
+
|
|
115
|
+
## One thing happy-dom will let you get wrong
|
|
116
|
+
|
|
117
|
+
happy-dom does not implement the HTML value-sanitization algorithm. Set
|
|
118
|
+
`"12abc"` on a raw `<input type="number">` and happy-dom keeps it, where a real
|
|
119
|
+
browser discards it and leaves the field empty (checked against Chromium). So a
|
|
120
|
+
test that hands a number input a non-numeric value and asserts on what it
|
|
121
|
+
displays can pass here and be false in production.
|
|
122
|
+
|
|
123
|
+
Note where that does *not* bite: it only fires if your code passes the bad
|
|
124
|
+
value through to the DOM. `GivenInput` substitutes `""` first, so both engines
|
|
125
|
+
agree there, and its non-numeric test asserts on the revert affordance rather
|
|
126
|
+
than the displayed value.
|
|
127
|
+
|
|
128
|
+
The general form of that trap: happy-dom is a fast approximation, not a
|
|
129
|
+
browser. Behaviour that depends on layout, real CSS, or the finer points of
|
|
130
|
+
form-control semantics belongs in the Playwright suite in `packages/app`.
|
package/test/setup.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOM environment for `bun test` in this package.
|
|
3
|
+
*
|
|
4
|
+
* Registered via `preload` in bunfig.toml, so it runs once before any spec and
|
|
5
|
+
* every spec in the package gets the same globals. Lives outside `src/` on
|
|
6
|
+
* purpose: `vite-plugin-dts` emits declarations for everything under `src/`, so
|
|
7
|
+
* a setup file in there would ship a stray `.d.ts` into `dist/`.
|
|
8
|
+
*
|
|
9
|
+
* happy-dom rather than jsdom because `@happy-dom/global-registrator` installs
|
|
10
|
+
* onto globalThis, which is what a Bun preload can do; jsdom expects to own the
|
|
11
|
+
* test runner's environment and has no equivalent hook. See test/README.md.
|
|
12
|
+
*/
|
|
13
|
+
import { GlobalRegistrator } from "@happy-dom/global-registrator";
|
|
14
|
+
import { afterEach } from "bun:test";
|
|
15
|
+
|
|
16
|
+
GlobalRegistrator.register();
|
|
17
|
+
|
|
18
|
+
// Imported dynamically, AFTER register(). Testing Library binds to `document`
|
|
19
|
+
// at module scope, so a static import would be hoisted above the line that
|
|
20
|
+
// creates it and every query would resolve against an undefined document.
|
|
21
|
+
const { cleanup } = await import("@testing-library/react");
|
|
22
|
+
|
|
23
|
+
// Unmount between tests. Without this each render appends to the same <body>,
|
|
24
|
+
// so `getByLabelText` starts throwing "found multiple elements" on the second
|
|
25
|
+
// test that renders the same widget.
|
|
26
|
+
afterEach(cleanup);
|
package/tsconfig.json
CHANGED
|
@@ -27,5 +27,16 @@
|
|
|
27
27
|
"forceConsistentCasingInFileNames": true
|
|
28
28
|
},
|
|
29
29
|
"include": ["src", "k6-tests"], // Specifies the directory to include when searching for TypeScript files.
|
|
30
|
-
|
|
30
|
+
// Keep test files out of the build; anything left in emits a stray .d.ts.
|
|
31
|
+
// These are the four name patterns `bun test` discovers, each left open on
|
|
32
|
+
// the extension so .tsx/.mts/.cts cannot slip past. tsconfig.spec.json
|
|
33
|
+
// includes the same four, so excluded does not mean unchecked.
|
|
34
|
+
"exclude": [
|
|
35
|
+
"src/**/__docs__",
|
|
36
|
+
"src/**/__test__",
|
|
37
|
+
"**/*.spec.*",
|
|
38
|
+
"**/*.test.*",
|
|
39
|
+
"**/*_spec.*",
|
|
40
|
+
"**/*_test.*"
|
|
41
|
+
]
|
|
31
42
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Typechecks the test files tsconfig.json excludes from the build.
|
|
2
|
+
//
|
|
3
|
+
// They must be excluded there, because that config drives the declaration
|
|
4
|
+
// build. But excluded also meant unchecked: `bun test` strips types without
|
|
5
|
+
// checking them and eslint here is not type-aware, so a type error in a spec
|
|
6
|
+
// reached no gate. The root `typecheck:sdk` runs this config as its second
|
|
7
|
+
// half. Keep the four patterns in step with the exclude list next door.
|
|
8
|
+
{
|
|
9
|
+
"extends": "./tsconfig.json",
|
|
10
|
+
"compilerOptions": {
|
|
11
|
+
"noEmit": true,
|
|
12
|
+
"declaration": false
|
|
13
|
+
},
|
|
14
|
+
"include": [
|
|
15
|
+
"src/**/*.spec.*",
|
|
16
|
+
"src/**/*.test.*",
|
|
17
|
+
"src/**/*_spec.*",
|
|
18
|
+
"src/**/*_test.*",
|
|
19
|
+
"test/**/*"
|
|
20
|
+
],
|
|
21
|
+
"exclude": []
|
|
22
|
+
}
|