@malloy-publisher/sdk 0.3.0 → 0.3.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.
Files changed (156) hide show
  1. package/README.md +8 -0
  2. package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +134 -0
  3. package/dist/DocumentStorageProvider-tGvodI8f.es.js +24268 -0
  4. package/dist/RenderedResult-CPi6uQWq.cjs.js +115 -0
  5. package/dist/RenderedResult-mDE-4u8S.es.js +542 -0
  6. package/dist/ServerProvider-CH71GsoL.cjs.js +1 -0
  7. package/dist/{ServerProvider-DSK9rfSY.es.js → ServerProvider-X2Ma0HRs.es.js} +486 -521
  8. package/dist/builder/index.cjs.js +105 -0
  9. package/dist/builder/index.d.ts +2 -0
  10. package/dist/builder/index.es.js +9561 -0
  11. package/dist/builder-entry.d.ts +8 -0
  12. package/dist/client/index.cjs.js +1 -1
  13. package/dist/client/index.es.js +1 -1
  14. package/dist/components/Dashboard/Dashboard.d.ts +19 -14
  15. package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
  16. package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
  17. package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
  18. package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
  19. package/dist/components/Dashboard/TileCard.d.ts +44 -0
  20. package/dist/components/Dashboard/index.d.ts +2 -0
  21. package/dist/components/Dashboard/promoteMeasureRow.d.ts +7 -3
  22. package/dist/components/Dashboard/telemetry.d.ts +43 -0
  23. package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
  24. package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
  25. package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
  26. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
  27. package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
  28. package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
  29. package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
  30. package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
  31. package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
  32. package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
  33. package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
  34. package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
  35. package/dist/components/DashboardBuilder/controls.d.ts +157 -0
  36. package/dist/components/DashboardBuilder/diff.d.ts +35 -0
  37. package/dist/components/DashboardBuilder/document.d.ts +218 -0
  38. package/dist/components/DashboardBuilder/layout.d.ts +71 -0
  39. package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
  40. package/dist/components/DashboardBuilder/preview.d.ts +60 -0
  41. package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
  42. package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
  43. package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
  44. package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
  45. package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
  46. package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
  47. package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
  48. package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
  49. package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
  50. package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -0
  51. package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +7 -0
  52. package/dist/components/DocumentStorage/index.d.ts +1 -1
  53. package/dist/components/FloatingIconButton.d.ts +16 -0
  54. package/dist/components/Prose.d.ts +46 -0
  55. package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
  56. package/dist/components/RenderedResult/index.d.ts +1 -0
  57. package/dist/components/drill/index.d.ts +1 -1
  58. package/dist/components/drill/resolveDrill.d.ts +22 -0
  59. package/dist/components/drill/useDrill.d.ts +11 -2
  60. package/dist/components/index.d.ts +1 -0
  61. package/dist/components/styles.d.ts +0 -4
  62. package/dist/hooks/index.d.ts +2 -0
  63. package/dist/hooks/useDocumentControls.d.ts +70 -0
  64. package/dist/hooks/useQueryResult.d.ts +40 -0
  65. package/dist/index.cjs.js +23 -270
  66. package/dist/index.es.js +16977 -41293
  67. package/dist/utils/malloyLiteral.d.ts +11 -0
  68. package/package.json +24 -9
  69. package/src/builder-entry.ts +55 -0
  70. package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
  71. package/src/components/Dashboard/Dashboard.tsx +165 -175
  72. package/src/components/Dashboard/DashboardGrid.tsx +124 -0
  73. package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
  74. package/src/components/Dashboard/DashboardTile.tsx +66 -123
  75. package/src/components/Dashboard/ExploreDialog.tsx +84 -0
  76. package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
  77. package/src/components/Dashboard/RowsDialog.tsx +234 -0
  78. package/src/components/Dashboard/TileCard.tsx +120 -0
  79. package/src/components/Dashboard/index.ts +2 -0
  80. package/src/components/Dashboard/promoteMeasureRow.spec.ts +70 -0
  81. package/src/components/Dashboard/promoteMeasureRow.ts +33 -2
  82. package/src/components/Dashboard/telemetry.ts +44 -0
  83. package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
  84. package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
  85. package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
  86. package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
  87. package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
  88. package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
  89. package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
  90. package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
  91. package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
  92. package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
  93. package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
  94. package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
  95. package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
  96. package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
  97. package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
  98. package/src/components/DashboardBuilder/catalog.ts +225 -0
  99. package/src/components/DashboardBuilder/controls.spec.ts +243 -0
  100. package/src/components/DashboardBuilder/controls.ts +398 -0
  101. package/src/components/DashboardBuilder/diff.spec.ts +44 -0
  102. package/src/components/DashboardBuilder/diff.ts +93 -0
  103. package/src/components/DashboardBuilder/document.ts +209 -0
  104. package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
  105. package/src/components/DashboardBuilder/layout.ts +143 -0
  106. package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
  107. package/src/components/DashboardBuilder/malloyText.ts +151 -0
  108. package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
  109. package/src/components/DashboardBuilder/preview.spec.ts +149 -0
  110. package/src/components/DashboardBuilder/preview.ts +172 -0
  111. package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
  112. package/src/components/DashboardBuilder/readDocument.ts +467 -0
  113. package/src/components/DashboardBuilder/sortable.tsx +151 -0
  114. package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
  115. package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
  116. package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
  117. package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
  118. package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
  119. package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
  120. package/src/components/DashboardBuilder/useDraft.ts +43 -0
  121. package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
  122. package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
  123. package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
  124. package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
  125. package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
  126. package/src/components/DashboardBuilder/useTileResize.ts +110 -0
  127. package/src/components/DashboardBuilder/withGaps.spec.ts +96 -0
  128. package/src/components/DocumentStorage/DocumentStorageProvider.tsx +12 -0
  129. package/src/components/DocumentStorage/index.ts +1 -0
  130. package/src/components/Environment/About.tsx +4 -36
  131. package/src/components/FloatingIconButton.tsx +41 -0
  132. package/src/components/Model/Model.tsx +6 -23
  133. package/src/components/Model/ModelCell.tsx +33 -72
  134. package/src/components/Notebook/Notebook.tsx +25 -88
  135. package/src/components/Notebook/NotebookCell.tsx +35 -183
  136. package/src/components/Prose.spec.tsx +85 -0
  137. package/src/components/Prose.tsx +179 -0
  138. package/src/components/QueryResult/QueryResult.tsx +15 -43
  139. package/src/components/RenderedResult/ResultContainer.tsx +4 -12
  140. package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
  141. package/src/components/RenderedResult/ResultPanel.tsx +73 -0
  142. package/src/components/RenderedResult/index.ts +1 -0
  143. package/src/components/drill/index.ts +1 -0
  144. package/src/components/drill/resolveDrill.ts +22 -1
  145. package/src/components/drill/useDrill.spec.tsx +94 -0
  146. package/src/components/drill/useDrill.tsx +52 -12
  147. package/src/components/given/GivensPanel.tsx +1 -1
  148. package/src/components/index.ts +6 -0
  149. package/src/components/styles.ts +0 -37
  150. package/src/hooks/index.ts +13 -0
  151. package/src/hooks/useDocumentControls.ts +170 -0
  152. package/src/hooks/useQueryResult.ts +82 -0
  153. package/src/utils/malloyLiteral.spec.ts +43 -0
  154. package/src/utils/malloyLiteral.ts +76 -0
  155. package/vite.config.ts +5 -0
  156. package/dist/ServerProvider-CGnLjzag.cjs.js +0 -1
@@ -0,0 +1,858 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import {
5
+ fireEvent,
6
+ render,
7
+ screen,
8
+ waitFor,
9
+ within,
10
+ } from "@testing-library/react";
11
+ import { describe, expect, it, mock } from "bun:test";
12
+ import type { DashboardEvent } from "../Dashboard/telemetry";
13
+ import { DashboardBuilder } from "./DashboardBuilder";
14
+ import { openDocument } from "./testing/fixtures";
15
+
16
+ const SOURCE = `## artifact { title="Storefront" tiles=["a -> by_cat", "a -> by_brand"] } dashboard { columns=12 }
17
+ import "../data_app.malloy"
18
+
19
+ source: a is scoped_orders extend {
20
+ // Kept, because a splice never rewrites what it did not change.
21
+ # colspan=6
22
+ # label="By category"
23
+ view: by_cat is by_category + { where: cat ~ $CATEGORY }
24
+
25
+ # colspan=6
26
+ view: by_brand is by_brand_view
27
+ }`;
28
+
29
+ const mount = async (
30
+ onSave?: (source: string) => Promise<void> | void,
31
+ onEvent?: (event: DashboardEvent) => void,
32
+ ) => {
33
+ const document = await openDocument(SOURCE);
34
+ return render(
35
+ <DashboardBuilder
36
+ source={SOURCE}
37
+ document={document}
38
+ {...(onSave ? { onSave } : {})}
39
+ {...(onEvent ? { onEvent } : {})}
40
+ />,
41
+ );
42
+ };
43
+
44
+ const tile = (name: string) => screen.getByLabelText(`Tile ${name}`);
45
+
46
+ /**
47
+ * Select a tile AND open its settings.
48
+ *
49
+ * A tile's presentation is edited from a popover on the tile itself rather than
50
+ * a panel under the page, so reaching those controls means opening it. Width
51
+ * and position are not in here at all — they are dragged.
52
+ */
53
+ const selectTile = (name: string) => fireEvent.click(tile(name));
54
+
55
+ /**
56
+ * Retitle a tile from its menu.
57
+ *
58
+ * The one on-tile edit a test can drive by clicking. Width and position are
59
+ * dragged, which needs real element geometry that jsdom does not provide, a
60
+ * tile's row is set by dragging it into a gap, and filters are configured
61
+ * only from the strip under the header.
62
+ */
63
+ const retitle = (title: string, next: string) => {
64
+ fireEvent.click(screen.getByLabelText(`Settings for ${title}`));
65
+ const field = screen.getByLabelText("Tile title");
66
+ fireEvent.change(field, { target: { value: next } });
67
+ fireEvent.keyDown(field, { key: "Escape" });
68
+ };
69
+ /**
70
+ * A named button, hidden or not. A window's exit transition never ends under
71
+ * the test runner, so once any window has been open the rest of the page reads
72
+ * as hidden to assistive tech; the header's buttons have to be reached through
73
+ * that, and it costs nothing when no window was.
74
+ */
75
+ const button = (name: string) =>
76
+ screen.getByRole("button", { name, hidden: true });
77
+
78
+ /**
79
+ * Every style rule the grid puts on a tile's item — its column, and the
80
+ * minimum that lets it narrow.
81
+ *
82
+ * The layout is responsive (one column on a phone, the grid above `md`), so
83
+ * `grid-column` lands in a media rule rather than on the element — reading
84
+ * `style.gridColumn` would report nothing and pass whatever the component did.
85
+ */
86
+ const itemStyleOf = (name: string): string => {
87
+ const box = tile(name).parentElement;
88
+ const className = Array.from(box?.classList ?? []).find((each) =>
89
+ each.startsWith("css-"),
90
+ );
91
+ const declarations: string[] = [];
92
+ const collect = (rules: CSSRuleList | undefined) => {
93
+ for (const rule of Array.from(rules ?? [])) {
94
+ if ("cssRules" in rule) collect((rule as CSSGroupingRule).cssRules);
95
+ const text = rule.cssText;
96
+ if (className && text.startsWith(`.${className} `))
97
+ declarations.push(text);
98
+ }
99
+ };
100
+ for (const sheet of Array.from(document.styleSheets))
101
+ collect(sheet.cssRules);
102
+ return declarations.join(" ");
103
+ };
104
+
105
+ describe("DashboardBuilder", () => {
106
+ it("shows a tile per entry, titled as a reader would see it", async () => {
107
+ await mount();
108
+ // The labelled tile shows its label; the unlabelled one falls back to the
109
+ // view name, which is what the dashboard itself does.
110
+ expect(within(tile("by_cat")).getByText("By category")).toBeDefined();
111
+ expect(within(tile("by_brand")).getByText("by_brand")).toBeDefined();
112
+ expect(screen.getByText("Storefront")).toBeDefined();
113
+ });
114
+
115
+ // The point of taking the rule from `Dashboard` rather than restating it:
116
+ // what you arrange is what a reader sees.
117
+ it("lays tiles out on the dashboard's own grid rule", async () => {
118
+ await mount();
119
+ expect(itemStyleOf("by_cat")).toContain("grid-column: span 6");
120
+ });
121
+
122
+ // Narrowing a tile has to narrow it. A grid item's minimum width is its
123
+ // content's, and a chart's content is an SVG as wide as the tile WAS — so
124
+ // without an explicit zero the tile could grow but never shrink, and the
125
+ // renderer never redrew for a size change it never saw.
126
+ it("lets a tile narrow below whatever it holds", async () => {
127
+ await mount();
128
+ expect(itemStyleOf("by_cat")).toContain("min-width: 0;");
129
+ });
130
+
131
+ // Filters are configured in ONE place, the strip: a control's window is
132
+ // where a tile is bound or unbound. Nothing on the tile edits a binding.
133
+ it("unbinds a tile from the control's window, and nowhere else", async () => {
134
+ let written: string | undefined;
135
+ await mount((source) => {
136
+ written = source;
137
+ });
138
+ expect(
139
+ within(tile("by_cat")).queryByLabelText(/^Remove filter /),
140
+ ).toBeNull();
141
+
142
+ fireEvent.click(screen.getByLabelText("Edit filter CATEGORY"));
143
+ fireEvent.click(screen.getByLabelText("Filter By category"));
144
+ fireEvent.click(screen.getByRole("button", { name: "Apply" }));
145
+ fireEvent.click(
146
+ screen.getByRole("button", { name: "Save changes", hidden: true }),
147
+ );
148
+ await waitFor(() => expect(written).toBeDefined());
149
+ expect(written).toContain("view: by_cat is by_category\n");
150
+ expect(written).not.toContain("$CATEGORY");
151
+ });
152
+
153
+ it("offers no resize for a tile the model owns", async () => {
154
+ const source = `## artifact { title="T" tiles=["orders -> by_brand"] }\nimport { orders } from '../orders.malloy'`;
155
+ const document = await openDocument(source);
156
+ render(<DashboardBuilder source={source} document={document} />);
157
+ selectTile("by_brand");
158
+ // Its tags live on the model's source, which the builder does not write,
159
+ // so it gets no resize handle — offering one would offer a drag the
160
+ // writer then refuses. It keeps its grip: order is this file's own
161
+ // `tiles=[…]` array, which it owns for every tile.
162
+ expect(within(tile("by_brand")).queryByLabelText(/^Resize /)).toBeNull();
163
+ expect(within(tile("by_brand")).getByLabelText(/^Move /)).toBeDefined();
164
+ });
165
+
166
+ // `renderTile` hands over the WHOLE tile, card and heading included, because
167
+ // a real `DashboardTile` draws both. Filling a card of ours would show a card
168
+ // in a card under two titles, which is the opposite of the point.
169
+ it("hands the whole tile to renderTile, adding no heading of its own", async () => {
170
+ const document = await openDocument(SOURCE);
171
+ render(
172
+ <DashboardBuilder
173
+ source={SOURCE}
174
+ document={document}
175
+ renderTile={(each) => <article>a real {each.name}</article>}
176
+ />,
177
+ );
178
+ expect(within(tile("by_cat")).getByText(/a real by_cat/)).toBeDefined();
179
+ // The label is the caller's to draw now, so the surface does not also
180
+ // print it, and the placeholder is gone.
181
+ expect(screen.queryByText("By category")).toBeNull();
182
+ expect(screen.queryByText(/a → by_cat/)).toBeNull();
183
+ });
184
+
185
+ // Selecting is a thing you do while arranging, so it must not move what you
186
+ // are arranging: an outline is drawn outside the box and takes no space.
187
+ it("marks selection without resizing the tile", async () => {
188
+ const document = await openDocument(SOURCE);
189
+ render(
190
+ <DashboardBuilder
191
+ source={SOURCE}
192
+ document={document}
193
+ renderTile={(each) => <article>a real {each.name}</article>}
194
+ />,
195
+ );
196
+ const before = itemStyleOf("by_cat");
197
+ selectTile("by_cat");
198
+ expect(tile("by_cat").getAttribute("aria-current")).toBe("true");
199
+ expect(itemStyleOf("by_cat")).toBe(before);
200
+ });
201
+
202
+ // The whole card is the drag target, so a press anywhere on it is how a
203
+ // move begins — and, below the drag threshold, how a tile is selected. The
204
+ // drag itself needs geometry jsdom does not have; the press it can drive.
205
+ it("selects a tile from a press anywhere on it, not only the grip", async () => {
206
+ await mount();
207
+ fireEvent.pointerDown(tile("by_brand"), { button: 0 });
208
+ expect(tile("by_brand").getAttribute("aria-current")).toBe("true");
209
+ // Released without travelling: a click, so nothing was reordered and the
210
+ // page is left as it was.
211
+ fireEvent.pointerUp(window);
212
+ expect(tile("by_brand").getAttribute("aria-current")).toBe("true");
213
+ });
214
+
215
+ it("ignores a press with any button but the primary", async () => {
216
+ await mount();
217
+ fireEvent.pointerDown(tile("by_brand"), { button: 2 });
218
+ expect(tile("by_brand").getAttribute("aria-current")).toBe("false");
219
+ });
220
+ });
221
+
222
+ describe("DashboardBuilder: the dashboard's filters", () => {
223
+ // The convention, end to end through the surface: a filter added here is a
224
+ // declaration in THIS file plus a binding on each ticked tile, and the save
225
+ // writes exactly that.
226
+ it("adds a filter declared in this dashboard and writes it to the file", async () => {
227
+ let written: string | undefined;
228
+ await mount((source) => {
229
+ written = source;
230
+ });
231
+ // The file binds CATEGORY without declaring it, so that chip is the
232
+ // model's: it can still be opened, and taken off every tile from there.
233
+ expect(screen.getByLabelText("Edit filter CATEGORY")).toBeDefined();
234
+
235
+ fireEvent.click(button("Add filter"));
236
+ fireEvent.change(screen.getByLabelText("Field to filter"), {
237
+ target: { value: "brand" },
238
+ });
239
+ fireEvent.change(screen.getByLabelText("Control label"), {
240
+ target: { value: "Brand" },
241
+ });
242
+ fireEvent.click(
243
+ screen.getByRole("button", { name: "Add filter", hidden: false }),
244
+ );
245
+
246
+ // Declared here, and bound on both tiles.
247
+ expect(screen.getByLabelText("Edit filter BRAND")).toBeDefined();
248
+
249
+ // The window's exit transition hides the rest of the page from assistive
250
+ // tech until it ends, and the test runner never ends it; the header's
251
+ // button has to be reached through that.
252
+ fireEvent.click(
253
+ screen.getByRole("button", { name: "Save changes", hidden: true }),
254
+ );
255
+ await waitFor(() => expect(written).toBeDefined());
256
+ expect(written).toContain(
257
+ '# label="Brand" control=select suggest { source=scoped_orders dimension=brand }\n' +
258
+ "given: BRAND :: filter<string> is f''",
259
+ );
260
+ expect(written).toContain(
261
+ "view: by_cat is by_category + { where: cat ~ $CATEGORY, where: brand ~ $BRAND }",
262
+ );
263
+ expect(written).toContain(
264
+ "view: by_brand is by_brand_view + { where: brand ~ $BRAND }",
265
+ );
266
+ });
267
+
268
+ it("binds a given the model offers, comparing the way its type needs", async () => {
269
+ let written: string | undefined;
270
+ const document = await openDocument(SOURCE);
271
+ render(
272
+ <DashboardBuilder
273
+ source={SOURCE}
274
+ document={document}
275
+ givens={[
276
+ {
277
+ name: "SINCE",
278
+ type: "date",
279
+ label: "Since",
280
+ field: "created_at",
281
+ },
282
+ ]}
283
+ onSave={(source) => {
284
+ written = source;
285
+ }}
286
+ />,
287
+ );
288
+ fireEvent.click(button("Add filter"));
289
+ fireEvent.click(screen.getByRole("button", { name: "From the model" }));
290
+ fireEvent.click(
291
+ screen.getByRole("button", { name: "Add filter", hidden: false }),
292
+ );
293
+
294
+ fireEvent.click(
295
+ screen.getByRole("button", { name: "Save changes", hidden: true }),
296
+ );
297
+ await waitFor(() => expect(written).toBeDefined());
298
+ // A `date` is a value, not a filter expression: `>=`, and no declaration
299
+ // of ours, since the model's is the one that binds.
300
+ expect(written).toContain("where: created_at >= $SINCE");
301
+ expect(written).not.toContain("given: SINCE");
302
+ });
303
+
304
+ it("takes a control it declared off the dashboard, bindings and all", async () => {
305
+ let written: string | undefined;
306
+ const source = SOURCE.replace(
307
+ 'import "../data_app.malloy"',
308
+ 'import "../data_app.malloy"\n\n# label="Category"\ngiven: CATEGORY :: filter<string> is f\'\'',
309
+ );
310
+ const document = await openDocument(source);
311
+ render(
312
+ <DashboardBuilder
313
+ source={source}
314
+ document={document}
315
+ onSave={(next) => {
316
+ written = next;
317
+ }}
318
+ />,
319
+ );
320
+ // From the control's window, which is the one place a control is removed.
321
+ fireEvent.click(screen.getByLabelText("Edit filter CATEGORY"));
322
+ fireEvent.click(screen.getByLabelText("Remove control CATEGORY"));
323
+ expect(screen.queryByLabelText("Edit filter CATEGORY")).toBeNull();
324
+
325
+ // One history entry for the whole removal.
326
+ fireEvent.click(button("Undo"));
327
+ expect(screen.getByLabelText("Edit filter CATEGORY")).toBeDefined();
328
+ fireEvent.click(button("Redo"));
329
+
330
+ fireEvent.click(button("Save changes"));
331
+ await waitFor(() => expect(written).toBeDefined());
332
+ expect(written).not.toContain("CATEGORY");
333
+ expect(written).toContain("view: by_cat is by_category\n");
334
+ });
335
+ });
336
+
337
+ describe("DashboardBuilder: fields, when the package is known", () => {
338
+ const catalog = {
339
+ sources: [
340
+ {
341
+ name: "scoped_orders",
342
+ modelPath: "data_app.malloy",
343
+ views: [],
344
+ givens: [],
345
+ fields: [
346
+ { name: "cat", kind: "dimension" as const, type: "string_type" },
347
+ {
348
+ name: "brand",
349
+ kind: "dimension" as const,
350
+ type: "string_type",
351
+ },
352
+ {
353
+ name: "products.category",
354
+ kind: "dimension" as const,
355
+ type: "string_type",
356
+ },
357
+ {
358
+ name: "sale_price",
359
+ kind: "dimension" as const,
360
+ type: "number_type",
361
+ },
362
+ ],
363
+ },
364
+ ],
365
+ };
366
+ const mountWithCatalog = async () => {
367
+ const document = await openDocument(SOURCE);
368
+ return render(
369
+ <DashboardBuilder
370
+ source={SOURCE}
371
+ document={document}
372
+ catalog={catalog}
373
+ onSave={() => undefined}
374
+ />,
375
+ );
376
+ };
377
+
378
+ // A binding to a field the source does not have fails at package load,
379
+ // which is the worst place for it. Here it is marked and cannot be applied.
380
+ it("holds Apply while a ticked tile names a field the source does not have", async () => {
381
+ await mountWithCatalog();
382
+ fireEvent.click(screen.getByLabelText("Edit filter CATEGORY"));
383
+ const field = screen.getByLabelText("Field to filter");
384
+ expect((field as HTMLInputElement).value).toBe("cat");
385
+ expect(button("Apply")).toHaveProperty("disabled", false);
386
+
387
+ fireEvent.change(field, { target: { value: "nope" } });
388
+ expect(screen.getByText("Not a field of scoped_orders.")).toBeDefined();
389
+ expect(button("Apply")).toHaveProperty("disabled", true);
390
+
391
+ fireEvent.change(field, { target: { value: "products.category" } });
392
+ expect(screen.queryByText("Not a field of scoped_orders.")).toBeNull();
393
+ expect(button("Apply")).toHaveProperty("disabled", false);
394
+ });
395
+
396
+ it("holds Apply while a ticked tile has no field at all", async () => {
397
+ await mountWithCatalog();
398
+ fireEvent.click(button("Add filter"));
399
+ // Both tiles are ticked and the field is empty: nothing to bind with.
400
+ expect(
401
+ screen.getByRole("button", { name: "Add filter", hidden: false }),
402
+ ).toHaveProperty("disabled", true);
403
+ fireEvent.change(screen.getByLabelText("Field to filter"), {
404
+ target: { value: "brand" },
405
+ });
406
+ expect(
407
+ screen.getByRole("button", { name: "Add filter", hidden: false }),
408
+ ).toHaveProperty("disabled", false);
409
+ });
410
+
411
+ it("switches a new control to the kind its field wants, and marks a field it cannot compare", async () => {
412
+ await mountWithCatalog();
413
+ fireEvent.click(button("Add filter"));
414
+ // A pick-list over a number makes no sense; picking a number field
415
+ // turns the new control into a number range rather than refusing it.
416
+ fireEvent.change(screen.getByLabelText("Field to filter"), {
417
+ target: { value: "sale_price" },
418
+ });
419
+ const kind = () =>
420
+ screen.getByRole("combobox", { name: /Kind of control/ });
421
+ const submit = () =>
422
+ screen.getByRole("button", { name: "Add filter", hidden: false });
423
+ expect(kind().textContent).toBe("Number range");
424
+ expect(submit()).toHaveProperty("disabled", false);
425
+ // But an EXPLICIT kind the field cannot take is a problem, said plainly.
426
+ fireEvent.mouseDown(kind());
427
+ fireEvent.click(screen.getByRole("option", { name: /Pick one/ }));
428
+ expect(
429
+ screen.getByText("sale_price is a number; this filter compares text."),
430
+ ).toBeDefined();
431
+ expect(submit()).toHaveProperty("disabled", true);
432
+ });
433
+
434
+ it("marks a control whose binding names a missing field, on its chip", async () => {
435
+ const source = SOURCE.replace(
436
+ "where: cat ~ $CATEGORY",
437
+ "where: catgory ~ $CATEGORY",
438
+ );
439
+ const document = await openDocument(source);
440
+ render(
441
+ <DashboardBuilder
442
+ source={source}
443
+ document={document}
444
+ catalog={catalog}
445
+ />,
446
+ );
447
+ const chip = screen.getByLabelText("Edit filter CATEGORY");
448
+ expect(chip.className).toContain("colorWarning");
449
+ });
450
+ });
451
+
452
+ describe("DashboardBuilder: a control the model declares", () => {
453
+ // Its declaration is not ours to delete, but a control is a given some tile
454
+ // binds, so taking it off the dashboard is unbinding every tile.
455
+ it("can still be taken off the dashboard", async () => {
456
+ let written: string | undefined;
457
+ await mount((source) => {
458
+ written = source;
459
+ });
460
+ // From the control's window, which is the one place a control is removed.
461
+ fireEvent.click(screen.getByLabelText("Edit filter CATEGORY"));
462
+ fireEvent.click(screen.getByLabelText("Remove control CATEGORY"));
463
+ expect(screen.queryByLabelText("Edit filter CATEGORY")).toBeNull();
464
+ fireEvent.click(button("Save changes"));
465
+ await waitFor(() => expect(written).toBeDefined());
466
+ expect(written).toContain("view: by_cat is by_category\n");
467
+ expect(written).not.toContain("$CATEGORY");
468
+ });
469
+ });
470
+
471
+ describe("DashboardBuilder: tiles added and removed", () => {
472
+ // A catalog whose source the fixture's extension is built on, with a view
473
+ // to put on the page. `scoped_orders` is reachable because `a` extends it.
474
+ const catalogWithViews = {
475
+ sources: [
476
+ {
477
+ name: "scoped_orders",
478
+ modelPath: "data_app.malloy",
479
+ views: [
480
+ {
481
+ name: "sales_by_state",
482
+ description: "Revenue by state",
483
+ chart: "shape_map",
484
+ },
485
+ ],
486
+ givens: [],
487
+ fields: [],
488
+ },
489
+ ],
490
+ };
491
+ const mountWithCatalog = async (onSave: (source: string) => void) => {
492
+ const document = await openDocument(SOURCE);
493
+ return render(
494
+ <DashboardBuilder
495
+ source={SOURCE}
496
+ document={document}
497
+ catalog={catalogWithViews}
498
+ onSave={onSave}
499
+ />,
500
+ );
501
+ };
502
+ const submitDialog = (name: string) =>
503
+ fireEvent.click(screen.getByRole("button", { name, hidden: false }));
504
+
505
+ // A structural save shows the file's diff first: declarations move, and
506
+ // the file cannot say whose comment sits beside them.
507
+ it("adds a tile from the catalog, and saves it through the diff", async () => {
508
+ let written: string | undefined;
509
+ await mountWithCatalog((source) => {
510
+ written = source;
511
+ });
512
+ fireEvent.click(button("Add tile"));
513
+ fireEvent.click(screen.getByLabelText("View sales_by_state"));
514
+ fireEvent.change(screen.getByLabelText("Tile title"), {
515
+ target: { value: "Revenue by state" },
516
+ });
517
+ submitDialog("Add tile");
518
+ expect(tile("sales_by_state_tile")).toBeDefined();
519
+
520
+ fireEvent.click(
521
+ screen.getByRole("button", { name: "Save changes", hidden: true }),
522
+ );
523
+ await waitFor(() =>
524
+ expect(screen.getByLabelText("File changes")).toBeDefined(),
525
+ );
526
+ // Nothing written until the diff is approved.
527
+ expect(written).toBeUndefined();
528
+ fireEvent.click(screen.getByRole("button", { name: "Save this" }));
529
+ await waitFor(() => expect(written).toBeDefined());
530
+ expect(written).toContain(
531
+ 'tiles=["a -> by_cat", "a -> by_brand", "a -> sales_by_state_tile"]',
532
+ );
533
+ expect(written).toContain(
534
+ ' # colspan=6\n # label="Revenue by state"\n view: sales_by_state_tile is sales_by_state\n}',
535
+ );
536
+ });
537
+
538
+ it("removes a tile from its menu, and saves it through the diff", async () => {
539
+ let written: string | undefined;
540
+ await mountWithCatalog((source) => {
541
+ written = source;
542
+ });
543
+ fireEvent.click(screen.getByLabelText("Settings for By category"));
544
+ fireEvent.click(screen.getByRole("button", { name: "Remove tile" }));
545
+ expect(screen.queryByLabelText("Tile by_cat")).toBeNull();
546
+
547
+ fireEvent.click(
548
+ screen.getByRole("button", { name: "Save changes", hidden: true }),
549
+ );
550
+ await waitFor(() =>
551
+ expect(screen.getByLabelText("File changes")).toBeDefined(),
552
+ );
553
+ fireEvent.click(screen.getByRole("button", { name: "Save this" }));
554
+ await waitFor(() => expect(written).toBeDefined());
555
+ expect(written).toContain('tiles=["a -> by_brand"]');
556
+ expect(written).not.toContain("view: by_cat");
557
+ // The comment above the removed tile is still in the file.
558
+ expect(written).toContain(
559
+ " // Kept, because a splice never rewrites what it did not change.",
560
+ );
561
+ });
562
+
563
+ it("keeps editing when the diff is declined", async () => {
564
+ let written: string | undefined;
565
+ await mountWithCatalog((source) => {
566
+ written = source;
567
+ });
568
+ fireEvent.click(screen.getByLabelText("Settings for By category"));
569
+ fireEvent.click(screen.getByRole("button", { name: "Remove tile" }));
570
+ fireEvent.click(
571
+ screen.getByRole("button", { name: "Save changes", hidden: true }),
572
+ );
573
+ await waitFor(() =>
574
+ expect(screen.getByLabelText("File changes")).toBeDefined(),
575
+ );
576
+ fireEvent.click(screen.getByRole("button", { name: "Keep editing" }));
577
+ expect(written).toBeUndefined();
578
+ expect(
579
+ screen.getByRole("button", { name: "Save changes", hidden: true }),
580
+ ).toBeDefined();
581
+ });
582
+ });
583
+
584
+ describe("DashboardBuilder: a tile's own settings", () => {
585
+ it("retitles a tile from its menu, as one history entry", async () => {
586
+ await mount();
587
+ fireEvent.click(screen.getByLabelText("Settings for By category"));
588
+ fireEvent.change(screen.getByLabelText("Tile title"), {
589
+ target: { value: "Revenue by category" },
590
+ });
591
+ fireEvent.change(screen.getByLabelText("Tile subtitle"), {
592
+ target: { value: "Net of returns" },
593
+ });
594
+ // Closing commits, once.
595
+ fireEvent.keyDown(screen.getByLabelText("Tile title"), { key: "Escape" });
596
+ expect(
597
+ within(tile("by_cat")).getByText("Revenue by category"),
598
+ ).toBeDefined();
599
+ expect(within(tile("by_cat")).getByText("Net of returns")).toBeDefined();
600
+ fireEvent.click(button("Undo"));
601
+ expect(within(tile("by_cat")).getByText("By category")).toBeDefined();
602
+ expect(within(tile("by_cat")).queryByText("Net of returns")).toBeNull();
603
+ });
604
+
605
+ it("offers no title to a tile the model owns", async () => {
606
+ const source = `## artifact { title="T" tiles=["orders -> by_brand"] }\nimport { orders } from '../orders.malloy'`;
607
+ const document = await openDocument(source);
608
+ render(<DashboardBuilder source={source} document={document} />);
609
+ fireEvent.click(screen.getByLabelText("Settings for by_brand"));
610
+ expect(screen.queryByLabelText("Tile title")).toBeNull();
611
+ expect(screen.getByText(/Declared on its source/)).toBeDefined();
612
+ });
613
+ });
614
+
615
+ describe("DashboardBuilder: widths and the page's own settings", () => {
616
+ it("sets a tile to a fraction of the grid from its menu", async () => {
617
+ await mount();
618
+ fireEvent.click(screen.getByLabelText("Settings for By category"));
619
+ const third = screen.getByRole("button", { name: "Width ⅓" });
620
+ fireEvent.click(third);
621
+ expect(third.getAttribute("aria-pressed")).toBe("true");
622
+ fireEvent.keyDown(screen.getByLabelText("Tile title"), { key: "Escape" });
623
+ // 12 columns, a third is 4.
624
+ expect(itemStyleOf("by_cat")).toContain("grid-column: span 4");
625
+ expect(itemStyleOf("by_brand")).toContain("grid-column: span 6");
626
+ });
627
+
628
+ it("lays every tile the file owns to one width, as one history entry", async () => {
629
+ await mount();
630
+ fireEvent.click(button("Layout"));
631
+ fireEvent.click(screen.getByRole("menuitem", { name: "A quarter" }));
632
+ expect(itemStyleOf("by_cat")).toContain("grid-column: span 3");
633
+ expect(itemStyleOf("by_brand")).toContain("grid-column: span 3");
634
+ fireEvent.click(button("Undo"));
635
+ expect(itemStyleOf("by_cat")).toContain("grid-column: span 6");
636
+ expect(itemStyleOf("by_brand")).toContain("grid-column: span 6");
637
+ });
638
+
639
+ it("edits the page's title, width and autorun, and writes them to the tag", async () => {
640
+ let saved = "";
641
+ await mount((source) => {
642
+ saved = source;
643
+ });
644
+ fireEvent.click(button("Settings"));
645
+ fireEvent.change(screen.getByLabelText("Dashboard title"), {
646
+ target: { value: "Storefront, weekly" },
647
+ });
648
+ fireEvent.change(screen.getByLabelText("Dashboard description"), {
649
+ target: { value: "What sold, and where." },
650
+ });
651
+ fireEvent.click(screen.getByLabelText("Run as controls change"));
652
+ // Closing commits, once.
653
+ fireEvent.keyDown(screen.getByLabelText("Dashboard title"), {
654
+ key: "Escape",
655
+ });
656
+ expect(screen.getByText("Storefront, weekly")).toBeDefined();
657
+ // The popover's own field still holds the text (its exit transition
658
+ // never ends under the test runner), so look for the page's copy.
659
+ expect(
660
+ screen.getAllByText("What sold, and where.").length,
661
+ ).toBeGreaterThan(0);
662
+
663
+ fireEvent.click(button("Save changes"));
664
+ await waitFor(() => expect(saved).not.toBe(""));
665
+ expect(saved).toContain(
666
+ '## artifact { title="Storefront, weekly" tiles=["a -> by_cat", "a -> by_brand"] autorun=false } dashboard { columns=12 }',
667
+ );
668
+ expect(saved).toContain('##" What sold, and where.');
669
+
670
+ fireEvent.click(button("Undo"));
671
+ expect(screen.queryByText("Storefront, weekly")).toBeNull();
672
+ expect(screen.getByText("Storefront")).toBeDefined();
673
+ });
674
+ });
675
+
676
+ describe("DashboardBuilder: clickable cells", () => {
677
+ const WITH_DIMENSION = `## artifact { title="T" tiles=["a -> x"] } dashboard { columns=12 }
678
+ import "../data_app.malloy"
679
+
680
+ source: a is scoped_orders extend {
681
+ # label="Category"
682
+ dimension: cat is products.category
683
+
684
+ # colspan=6
685
+ view: x is { group_by: cat aggregate: total_revenue }
686
+ }`;
687
+
688
+ it("puts a drill on a dimension this file declares, and writes the tag", async () => {
689
+ let saved = "";
690
+ const doc = await openDocument(WITH_DIMENSION);
691
+ render(
692
+ <DashboardBuilder
693
+ source={WITH_DIMENSION}
694
+ document={doc}
695
+ dashboards={["regions"]}
696
+ givens={[{ name: "CATEGORY" }]}
697
+ onSave={(source) => {
698
+ saved = source;
699
+ }}
700
+ />,
701
+ );
702
+ fireEvent.click(screen.getByLabelText("Settings for x"));
703
+ fireEvent.click(button("Clickable cells…"));
704
+
705
+ fireEvent.mouseDown(
706
+ screen.getByRole("combobox", { name: /Clicks go to/, hidden: true }),
707
+ );
708
+ fireEvent.click(
709
+ screen.getByRole("option", { name: /This dashboard/, hidden: true }),
710
+ );
711
+ fireEvent.click(
712
+ screen.getByRole("option", { name: /regions/, hidden: true }),
713
+ );
714
+ fireEvent.keyDown(document.body, { key: "Escape" });
715
+ fireEvent.mouseDown(
716
+ screen.getByRole("combobox", {
717
+ name: /Sets the control/,
718
+ hidden: true,
719
+ }),
720
+ );
721
+ fireEvent.click(
722
+ screen.getByRole("option", { name: "CATEGORY", hidden: true }),
723
+ );
724
+ fireEvent.click(button("Apply"));
725
+
726
+ fireEvent.click(button("Save changes"));
727
+ await waitFor(() => expect(saved).not.toBe(""));
728
+ expect(saved).toContain(
729
+ ` # label="Category"
730
+ # drill { to=["self", "regions"] given=CATEGORY }
731
+ dimension: cat is products.category`,
732
+ );
733
+ });
734
+
735
+ it("explains itself when the source declares no dimension here", async () => {
736
+ await mount();
737
+ fireEvent.click(screen.getByLabelText("Settings for By category"));
738
+ fireEvent.click(button("Clickable cells…"));
739
+ expect(
740
+ screen.getByText(/declares no dimensions of its own/),
741
+ ).toBeDefined();
742
+ expect(button("Apply").hasAttribute("disabled")).toBe(true);
743
+ });
744
+ });
745
+
746
+ describe("DashboardBuilder: keyboard", () => {
747
+ it("undoes and redoes from the keyboard, and never from inside a text field", async () => {
748
+ await mount();
749
+ retitle("By category", "Renamed");
750
+ expect(within(tile("by_cat")).getByText("Renamed")).toBeDefined();
751
+
752
+ fireEvent.keyDown(window, { key: "z", ctrlKey: true, metaKey: true });
753
+ expect(within(tile("by_cat")).getByText("By category")).toBeDefined();
754
+ fireEvent.keyDown(window, {
755
+ key: "z",
756
+ ctrlKey: true,
757
+ metaKey: true,
758
+ shiftKey: true,
759
+ });
760
+ expect(within(tile("by_cat")).getByText("Renamed")).toBeDefined();
761
+
762
+ // Typing in a field is typing, not editing the document.
763
+ fireEvent.click(screen.getByLabelText("Settings for Renamed"));
764
+ const field = screen.getByLabelText("Tile title");
765
+ fireEvent.keyDown(field, { key: "z", ctrlKey: true, metaKey: true });
766
+ expect(within(tile("by_cat")).getByText("Renamed")).toBeDefined();
767
+ });
768
+
769
+ it("nudges the selected tile's width with the arrow keys", async () => {
770
+ await mount();
771
+ selectTile("by_cat");
772
+ fireEvent.keyDown(window, { key: "ArrowLeft" });
773
+ expect(itemStyleOf("by_cat")).toContain("grid-column: span 5");
774
+ fireEvent.keyDown(window, { key: "ArrowRight" });
775
+ fireEvent.keyDown(window, { key: "ArrowRight" });
776
+ expect(itemStyleOf("by_cat")).toContain("grid-column: span 7");
777
+ });
778
+ });
779
+
780
+ describe("DashboardBuilder: history", () => {
781
+ it("enables undo only once there is something to undo", async () => {
782
+ await mount();
783
+ expect(button("Undo")).toHaveProperty("disabled", true);
784
+ retitle("By category", "Renamed");
785
+ expect(button("Undo")).toHaveProperty("disabled", false);
786
+ });
787
+
788
+ it("takes a change back and puts it forward again", async () => {
789
+ await mount();
790
+ retitle("By category", "Renamed");
791
+ expect(within(tile("by_cat")).getByText("Renamed")).toBeDefined();
792
+
793
+ fireEvent.click(button("Undo"));
794
+ expect(within(tile("by_cat")).getByText("By category")).toBeDefined();
795
+
796
+ fireEvent.click(button("Redo"));
797
+ expect(within(tile("by_cat")).getByText("Renamed")).toBeDefined();
798
+ });
799
+ });
800
+
801
+ describe("DashboardBuilder: saving", () => {
802
+ it("offers no save button when there is nowhere to save", async () => {
803
+ await mount();
804
+ expect(screen.queryByRole("button", { name: /Save|Saved/ })).toBeNull();
805
+ });
806
+
807
+ it("writes the change into the file, comment and all, and says so", async () => {
808
+ let written: string | undefined;
809
+ const onEvent = mock((_event: DashboardEvent) => {});
810
+ await mount((source) => {
811
+ written = source;
812
+ }, onEvent);
813
+
814
+ retitle("By category", "Renamed");
815
+ fireEvent.click(button("Save changes"));
816
+
817
+ await waitFor(() => expect(written).toBeDefined());
818
+ // The tag is rewritten in place; the declaration is otherwise untouched,
819
+ // and so is the comment in the block.
820
+ expect(written).toContain('# label="Renamed"');
821
+ expect(written).not.toContain('# label="By category"');
822
+ expect(written).toContain(
823
+ " view: by_cat is by_category + { where: cat ~ $CATEGORY }",
824
+ );
825
+ expect(written).toContain(
826
+ " // Kept, because a splice never rewrites what it did not change.",
827
+ );
828
+ await waitFor(() => expect(button("Saved")).toBeDefined());
829
+ expect(onEvent).toHaveBeenCalledTimes(1);
830
+ expect(onEvent.mock.calls[0][0]).toMatchObject({
831
+ type: "dashboard.saved",
832
+ tiles: 2,
833
+ structural: false,
834
+ });
835
+ });
836
+
837
+ // The rule the writer rests on, shown through the surface: the work stays on
838
+ // screen and the reason is visible.
839
+ it("keeps the edit on screen when a save is refused, and says why", async () => {
840
+ const onEvent = mock((_event: DashboardEvent) => {});
841
+ await mount(() => {
842
+ throw new Error("disk full");
843
+ }, onEvent);
844
+ retitle("By category", "Renamed");
845
+ fireEvent.click(button("Save changes"));
846
+
847
+ await waitFor(() =>
848
+ expect(screen.getByRole("alert").textContent).toContain("disk full"),
849
+ );
850
+ expect(onEvent.mock.calls[0][0]).toMatchObject({
851
+ type: "dashboard.save_refused",
852
+ reason: expect.stringContaining("disk full"),
853
+ });
854
+ // The edit is still on screen and still unsaved.
855
+ expect(within(tile("by_cat")).getByText("Renamed")).toBeDefined();
856
+ expect(button("Save changes")).toBeDefined();
857
+ });
858
+ });