@malloy-publisher/sdk 0.9.16 → 0.10.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/{RenderedResult-5hOs5XnV.cjs.js → RenderedResult-B5f4qOjp.cjs.js} +1 -1
- package/dist/{RenderedResult-DxmHGZNQ.es.js → RenderedResult-vf6tu8tf.es.js} +2 -2
- package/dist/{ServerProvider-D7neZcTk.es.js → ServerProvider-B6RLAbgV.es.js} +48 -24
- package/dist/builder/index.cjs.js +17 -17
- package/dist/builder/index.es.js +278 -266
- package/dist/client/api.d.ts +64 -28
- package/dist/client/index.es.js +1 -1
- package/dist/components/ReportBuilder/TextTileBody.d.ts +9 -2
- package/dist/components/legacy/malloynb/CellRunnerView.d.ts +6 -6
- package/dist/hooks/useSettled.d.ts +5 -8
- package/dist/hooks/useSettledDraft.d.ts +33 -0
- package/dist/index.cjs.js +10 -10
- package/dist/index.es.js +3769 -3779
- package/dist/useCompiledDocument-C995sQ3j.cjs.js +142 -0
- package/dist/{useCompiledDocument-CaYP_OYG.es.js → useCompiledDocument-CLbTFtsJ.es.js} +5449 -5294
- package/dist/utils/malloyLiteral.d.ts +2 -0
- package/package.json +11 -16
- package/src/components/ReportBuilder/BuilderGrid.tsx +1 -0
- package/src/components/ReportBuilder/TextTileBody.tsx +24 -4
- package/src/components/ReportBuilder/placement.spec.ts +27 -0
- package/src/components/ReportBuilder/placement.ts +8 -4
- package/src/components/given/GivenInput.spec.tsx +447 -2
- package/src/components/given/GivenInput.tsx +192 -80
- package/src/components/given/GivensPanel.spec.tsx +59 -0
- package/src/components/given/GivensPanel.tsx +14 -2
- package/src/components/legacy/malloynb/CellRunnerView.tsx +6 -6
- package/src/hooks/useDimensionFilters.ts +1 -35
- package/src/hooks/useDimensionFiltersQuery.ts +2 -36
- package/src/hooks/useDimensionalFilterRangeData.ts +1 -35
- package/src/hooks/useGivensState.spec.ts +106 -1
- package/src/hooks/useGivensState.ts +48 -13
- package/src/hooks/useSettled.ts +5 -8
- package/src/hooks/useSettledDraft.ts +127 -0
- package/src/utils/malloyLiteral.spec.ts +12 -1
- package/src/utils/malloyLiteral.ts +32 -0
- package/vite.config.ts +0 -2
- package/.eslintrc +0 -36
- package/dist/components/DOMElement/DOMElement.d.ts +0 -5
- package/dist/components/DOMElement/index.d.ts +0 -1
- package/dist/components/Package/FileTreeView.d.ts +0 -9
- package/dist/useCompiledDocument-zYEfLogf.cjs.js +0 -142
- package/src/components/DOMElement/DOMElement.tsx +0 -22
- package/src/components/DOMElement/index.ts +0 -4
- package/src/components/Package/FileTreeView.tsx +0 -244
|
@@ -9,3 +9,5 @@
|
|
|
9
9
|
* value, or one the type cannot spell.
|
|
10
10
|
*/
|
|
11
11
|
export declare function malloyLiteral(value: unknown, type?: string): string | undefined;
|
|
12
|
+
/** A filter value as a Malloy literal; unlike `malloyLiteral`, a Date is spelled as a local-time day. */
|
|
13
|
+
export declare function formatMalloyValue(value: string | number | boolean | Date | null | undefined, isDate?: boolean): string;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@malloy-publisher/sdk",
|
|
3
3
|
"description": "Malloy Publisher SDK",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.10.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs.js",
|
|
7
7
|
"module": "dist/index.es.js",
|
|
@@ -82,18 +82,17 @@
|
|
|
82
82
|
"generate-api-types": "bunx openapi-generator-cli generate -i ../../api-doc.yaml -g typescript-axios -o src/client/"
|
|
83
83
|
},
|
|
84
84
|
"peerDependencies": {
|
|
85
|
-
"@malloydata/malloy": "^0.0.
|
|
85
|
+
"@malloydata/malloy": "^0.0.436",
|
|
86
86
|
"@malloydata/malloy-explorer": "0.0.338-dev260215172810",
|
|
87
|
-
"@malloydata/malloy-filter": "^0.0.
|
|
88
|
-
"@malloydata/malloy-interfaces": "^0.0.
|
|
89
|
-
"@malloydata/malloy-query-builder": "^0.0.
|
|
90
|
-
"@malloydata/malloy-tag": "^0.0.
|
|
91
|
-
"@malloydata/render": "^0.0.
|
|
87
|
+
"@malloydata/malloy-filter": "^0.0.436",
|
|
88
|
+
"@malloydata/malloy-interfaces": "^0.0.436",
|
|
89
|
+
"@malloydata/malloy-query-builder": "^0.0.436",
|
|
90
|
+
"@malloydata/malloy-tag": "^0.0.436",
|
|
91
|
+
"@malloydata/render": "^0.0.436",
|
|
92
92
|
"@mui/icons-material": ">=7.1.1",
|
|
93
93
|
"@mui/material": ">=7.1.1",
|
|
94
94
|
"@mui/system": ">=7.1.1",
|
|
95
95
|
"@mui/x-date-pickers": ">=7.1.1",
|
|
96
|
-
"@mui/x-tree-view": ">=7.16.0",
|
|
97
96
|
"@tanstack/react-query": ">=5.59.16",
|
|
98
97
|
"axios": ">=1.7.7",
|
|
99
98
|
"react": ">=19.1.0",
|
|
@@ -107,7 +106,6 @@
|
|
|
107
106
|
"@dnd-kit/react": "^0.5.0",
|
|
108
107
|
"@emotion/react": "^11.14.0",
|
|
109
108
|
"@emotion/styled": "^11.14.0",
|
|
110
|
-
"@react-spring/web": "^10.0.1",
|
|
111
109
|
"cron-parser": "^5.6.2",
|
|
112
110
|
"cronstrue": "^2.50.0",
|
|
113
111
|
"dayjs": "^1.11.13",
|
|
@@ -115,25 +113,22 @@
|
|
|
115
113
|
},
|
|
116
114
|
"devDependencies": {
|
|
117
115
|
"@happy-dom/global-registrator": "^20.11.1",
|
|
118
|
-
"@malloydata/malloy": "^0.0.
|
|
116
|
+
"@malloydata/malloy": "^0.0.436",
|
|
119
117
|
"@malloydata/malloy-explorer": "0.0.338-dev260215172810",
|
|
120
|
-
"@malloydata/malloy-filter": "^0.0.
|
|
121
|
-
"@malloydata/malloy-query-builder": "^0.0.
|
|
122
|
-
"@malloydata/malloy-tag": "^0.0.
|
|
118
|
+
"@malloydata/malloy-filter": "^0.0.436",
|
|
119
|
+
"@malloydata/malloy-query-builder": "^0.0.436",
|
|
120
|
+
"@malloydata/malloy-tag": "^0.0.436",
|
|
123
121
|
"@openapitools/openapi-generator-cli": "^2.40.1",
|
|
124
122
|
"@shikijs/langs": "^1.16.3",
|
|
125
123
|
"@shikijs/themes": "^1.16.3",
|
|
126
124
|
"@testing-library/dom": "^10.4.1",
|
|
127
125
|
"@testing-library/react": "^16.3.2",
|
|
128
126
|
"@types/bun": "^1.2.21",
|
|
129
|
-
"@types/k6": "^1.0.2",
|
|
130
127
|
"@types/react": "^19.1.7",
|
|
131
128
|
"@types/react-dom": "^19.1.6",
|
|
132
129
|
"@typescript-eslint/eslint-plugin": "8.16.0",
|
|
133
130
|
"@typescript-eslint/parser": "8.16.0",
|
|
134
131
|
"@vitejs/plugin-react": "^4.4.1",
|
|
135
|
-
"ajv": "^8.12.0",
|
|
136
|
-
"archiver": "^7.0.1",
|
|
137
132
|
"eslint-config-prettier": "9.1.0",
|
|
138
133
|
"eslint-plugin-prettier": "5.2.1",
|
|
139
134
|
"eslint-plugin-react": "7.37.2",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Copyright (c) Credible Data Inc.
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
|
+
import { Box } from "@mui/material";
|
|
4
5
|
import { TextTileFrame } from "../Report/ReportPage";
|
|
5
6
|
import { InlineMarkdown } from "./InlineMarkdown";
|
|
6
7
|
import type { TextTile } from "./document";
|
|
@@ -8,17 +9,25 @@ import type { TextTile } from "./document";
|
|
|
8
9
|
export const TEXT_TILE_PLACEHOLDER =
|
|
9
10
|
"Write markdown: # heading, **bold**, - list";
|
|
10
11
|
|
|
11
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* A text tile as the builder draws it: the reader's prose frame, its markdown edited in place.
|
|
14
|
+
*
|
|
15
|
+
* `sized`: the tile has a set height (a `rowspan`, or a placed report's rows), so prose taller
|
|
16
|
+
* than it scrolls inside, as the reader scrolls it, rather than running on under the tiles
|
|
17
|
+
* below. Left to flow, the frame is as tall as the prose and nothing clips.
|
|
18
|
+
*/
|
|
12
19
|
export function TextTileBody({
|
|
13
20
|
tile,
|
|
21
|
+
sized = false,
|
|
14
22
|
onChange,
|
|
15
23
|
}: {
|
|
16
24
|
tile: TextTile;
|
|
25
|
+
sized?: boolean;
|
|
17
26
|
onChange: (markdown: string) => void;
|
|
18
27
|
}) {
|
|
19
|
-
|
|
20
|
-
// Overflow shown: the open editor's ring and hint stand a few
|
|
21
|
-
// outside the prose, which a clipping frame cut off.
|
|
28
|
+
const body = (
|
|
29
|
+
// Overflow shown on the frame itself: the open editor's ring and hint stand a few
|
|
30
|
+
// pixels outside the prose, which a clipping frame cut off.
|
|
22
31
|
<TextTileFrame sx={{ overflow: "visible" }}>
|
|
23
32
|
<InlineMarkdown
|
|
24
33
|
markdown={tile.markdown}
|
|
@@ -27,4 +36,15 @@ export function TextTileBody({
|
|
|
27
36
|
/>
|
|
28
37
|
</TextTileFrame>
|
|
29
38
|
);
|
|
39
|
+
if (!sized) return body;
|
|
40
|
+
return (
|
|
41
|
+
// The scroller sits outside the frame, with the ring's few pixels of room folded in,
|
|
42
|
+
// so the ring still shows and the prose still stops at the tile's edge.
|
|
43
|
+
<Box
|
|
44
|
+
data-testid="text-tile-scroller"
|
|
45
|
+
sx={{ minHeight: 0, overflow: "auto", p: "4px", m: "-4px" }}
|
|
46
|
+
>
|
|
47
|
+
{body}
|
|
48
|
+
</Box>
|
|
49
|
+
);
|
|
30
50
|
}
|
|
@@ -126,6 +126,33 @@ describe("placeWhereDrawn", () => {
|
|
|
126
126
|
// A row break means nothing once tiles are placed.
|
|
127
127
|
expect(placeWhereDrawn(tiles, boxes, 12, track)[0].break).toBeUndefined();
|
|
128
128
|
});
|
|
129
|
+
|
|
130
|
+
it("rounds a text tile's bottom edge up, so its prose keeps every line", () => {
|
|
131
|
+
const track = trackWidth(1200, 12);
|
|
132
|
+
// A heading and two lines: 124px, under a row and a half, which rounded to one row
|
|
133
|
+
// of 80px and left the last line under the tiles placed beneath.
|
|
134
|
+
const intro: DashboardTile = {
|
|
135
|
+
kind: "text",
|
|
136
|
+
name: "intro",
|
|
137
|
+
markdown: "# Hi\n\ntwo lines",
|
|
138
|
+
colspan: 12,
|
|
139
|
+
};
|
|
140
|
+
const tiles: DashboardTile[] = [intro, tile("trend", { colspan: 12 })];
|
|
141
|
+
const boxes = [
|
|
142
|
+
{ top: 0, left: 0, width: 1200, height: 124 },
|
|
143
|
+
{ top: 140, left: 0, width: 1200, height: 272 },
|
|
144
|
+
];
|
|
145
|
+
const placed = where(placeWhereDrawn(tiles, boxes, 12, track));
|
|
146
|
+
expect(placed[0]).toEqual({
|
|
147
|
+
name: "intro",
|
|
148
|
+
row: 1,
|
|
149
|
+
col: 1,
|
|
150
|
+
colspan: 12,
|
|
151
|
+
rowspan: 2,
|
|
152
|
+
});
|
|
153
|
+
// The chart below starts where it was drawn; a text tile that grew a row is settled under it.
|
|
154
|
+
expect(placed[1].row).toBeGreaterThanOrEqual(3);
|
|
155
|
+
});
|
|
129
156
|
});
|
|
130
157
|
|
|
131
158
|
describe("inReadingOrder", () => {
|
|
@@ -244,10 +244,14 @@ export function placeWhereDrawn(
|
|
|
244
244
|
// Its top and bottom are each taken to the nearest row line, rather
|
|
245
245
|
// than its height rounded on its own: tiles that touched still touch,
|
|
246
246
|
// so a whole page's rounding never adds up and pushes rows down. No
|
|
247
|
-
// edge moves more than half a row.
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
247
|
+
// edge moves more than half a row. Except a text tile's bottom, which
|
|
248
|
+
// only ever moves down: a chart redraws at whatever height it gets,
|
|
249
|
+
// but prose cannot shrink, and a bottom rounded up by half a row left
|
|
250
|
+
// its last line scrolling out of a one-row tile.
|
|
251
|
+
const bottom = (box.top + box.height + GRID_GAP_PX) / ROW_STEP;
|
|
252
|
+
const lastRow = isTextTile(tile)
|
|
253
|
+
? Math.ceil(bottom - 0.02)
|
|
254
|
+
: Math.round(bottom);
|
|
251
255
|
return {
|
|
252
256
|
...rest,
|
|
253
257
|
...cell,
|
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
// Copyright (c) Credible Data Inc.
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
afterEach,
|
|
6
|
+
beforeEach,
|
|
7
|
+
describe,
|
|
8
|
+
expect,
|
|
9
|
+
it,
|
|
10
|
+
jest,
|
|
11
|
+
mock,
|
|
12
|
+
} from "bun:test";
|
|
13
|
+
import {
|
|
14
|
+
act,
|
|
15
|
+
cleanup,
|
|
16
|
+
fireEvent,
|
|
17
|
+
render,
|
|
18
|
+
screen,
|
|
19
|
+
} from "@testing-library/react";
|
|
20
|
+
import { useState } from "react";
|
|
6
21
|
import type { Given } from "../../client";
|
|
7
22
|
import type { GivenValue } from "../../hooks/givenValue";
|
|
23
|
+
import { GIVEN_SETTLE_MS } from "../../hooks/useSettled";
|
|
8
24
|
import { GivenInput } from "./GivenInput";
|
|
9
25
|
|
|
10
26
|
/**
|
|
@@ -994,3 +1010,432 @@ describe("GivenInput: the range slider's commits", () => {
|
|
|
994
1010
|
expect(onChange).toHaveBeenCalledWith(null);
|
|
995
1011
|
});
|
|
996
1012
|
});
|
|
1013
|
+
|
|
1014
|
+
// Bun runs it; its typings do not declare it.
|
|
1015
|
+
const advance = (ms: number) =>
|
|
1016
|
+
(
|
|
1017
|
+
jest as unknown as { advanceTimersByTime(ms: number): void }
|
|
1018
|
+
).advanceTimersByTime(ms);
|
|
1019
|
+
|
|
1020
|
+
/** A host that feeds each commit back in as `value`, the way a dashboard does. */
|
|
1021
|
+
function Host({
|
|
1022
|
+
given,
|
|
1023
|
+
initial,
|
|
1024
|
+
onChange,
|
|
1025
|
+
}: {
|
|
1026
|
+
given: Given;
|
|
1027
|
+
initial: GivenValue | undefined;
|
|
1028
|
+
onChange: (next: GivenValue) => void;
|
|
1029
|
+
}) {
|
|
1030
|
+
const [value, setValue] = useState<GivenValue | undefined>(initial);
|
|
1031
|
+
return (
|
|
1032
|
+
<GivenInput
|
|
1033
|
+
given={given}
|
|
1034
|
+
value={value}
|
|
1035
|
+
onChange={(next) => {
|
|
1036
|
+
onChange(next);
|
|
1037
|
+
setValue(next);
|
|
1038
|
+
}}
|
|
1039
|
+
/>
|
|
1040
|
+
);
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
describe("GivenInput: typing commits once the field settles", () => {
|
|
1044
|
+
beforeEach(() => {
|
|
1045
|
+
jest.useFakeTimers();
|
|
1046
|
+
});
|
|
1047
|
+
afterEach(() => {
|
|
1048
|
+
jest.useRealTimers();
|
|
1049
|
+
cleanup();
|
|
1050
|
+
});
|
|
1051
|
+
|
|
1052
|
+
const text: Given = { name: "brand", type: "filter<string>" };
|
|
1053
|
+
const type = (input: HTMLElement, value: string) => {
|
|
1054
|
+
fireEvent.focus(input);
|
|
1055
|
+
fireEvent.change(input, { target: { value } });
|
|
1056
|
+
};
|
|
1057
|
+
|
|
1058
|
+
it("sends one change for a burst of keystrokes, after the burst", () => {
|
|
1059
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1060
|
+
render(<Host given={text} initial={undefined} onChange={onChange} />);
|
|
1061
|
+
const input = screen.getByLabelText("brand") as HTMLInputElement;
|
|
1062
|
+
for (const value of ["F", "Fo", "For", "Ford"]) {
|
|
1063
|
+
type(input, value);
|
|
1064
|
+
act(() => advance(GIVEN_SETTLE_MS - 1));
|
|
1065
|
+
}
|
|
1066
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1067
|
+
act(() => advance(1));
|
|
1068
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1069
|
+
expect(onChange).toHaveBeenCalledWith("Ford");
|
|
1070
|
+
act(() => advance(GIVEN_SETTLE_MS * 3));
|
|
1071
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1072
|
+
});
|
|
1073
|
+
|
|
1074
|
+
it("commits on Enter without waiting", () => {
|
|
1075
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1076
|
+
render(<Host given={text} initial={undefined} onChange={onChange} />);
|
|
1077
|
+
const input = screen.getByLabelText("brand");
|
|
1078
|
+
type(input, "Ford");
|
|
1079
|
+
fireEvent.keyDown(input, { key: "Enter" });
|
|
1080
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1081
|
+
expect(onChange).toHaveBeenCalledWith("Ford");
|
|
1082
|
+
act(() => advance(GIVEN_SETTLE_MS * 3));
|
|
1083
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1084
|
+
});
|
|
1085
|
+
|
|
1086
|
+
it("commits a draft that settled mid-composition once the composition ends", () => {
|
|
1087
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1088
|
+
render(<Host given={text} initial={undefined} onChange={onChange} />);
|
|
1089
|
+
const input = screen.getByLabelText("brand");
|
|
1090
|
+
fireEvent.focus(input);
|
|
1091
|
+
fireEvent.compositionStart(input);
|
|
1092
|
+
fireEvent.change(input, { target: { value: "日本" } });
|
|
1093
|
+
act(() => advance(GIVEN_SETTLE_MS * 2));
|
|
1094
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1095
|
+
fireEvent.compositionEnd(input);
|
|
1096
|
+
act(() => advance(GIVEN_SETTLE_MS));
|
|
1097
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1098
|
+
expect(onChange).toHaveBeenCalledWith("日本");
|
|
1099
|
+
});
|
|
1100
|
+
|
|
1101
|
+
it("commits a pending draft when the field unmounts", () => {
|
|
1102
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1103
|
+
const view = render(
|
|
1104
|
+
<Host given={text} initial={undefined} onChange={onChange} />,
|
|
1105
|
+
);
|
|
1106
|
+
type(screen.getByLabelText("brand"), "Ford");
|
|
1107
|
+
view.unmount();
|
|
1108
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1109
|
+
expect(onChange).toHaveBeenCalledWith("Ford");
|
|
1110
|
+
});
|
|
1111
|
+
|
|
1112
|
+
it("follows the host while focused when nothing has been typed", () => {
|
|
1113
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1114
|
+
const view = render(
|
|
1115
|
+
<GivenInput given={text} value="East" onChange={onChange} />,
|
|
1116
|
+
);
|
|
1117
|
+
const input = screen.getByLabelText("brand") as HTMLInputElement;
|
|
1118
|
+
fireEvent.focus(input);
|
|
1119
|
+
view.rerender(
|
|
1120
|
+
<GivenInput given={text} value="West" onChange={onChange} />,
|
|
1121
|
+
);
|
|
1122
|
+
expect(input.value).toBe("West");
|
|
1123
|
+
fireEvent.blur(input);
|
|
1124
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1125
|
+
});
|
|
1126
|
+
|
|
1127
|
+
it("reverts an emptied number once, and an untouched blur sends nothing more", () => {
|
|
1128
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1129
|
+
render(
|
|
1130
|
+
<Host
|
|
1131
|
+
given={{ name: "n", type: "number" }}
|
|
1132
|
+
initial={5}
|
|
1133
|
+
onChange={onChange}
|
|
1134
|
+
/>,
|
|
1135
|
+
);
|
|
1136
|
+
const input = screen.getByLabelText("n");
|
|
1137
|
+
type(input, "");
|
|
1138
|
+
fireEvent.blur(input);
|
|
1139
|
+
fireEvent.focus(input);
|
|
1140
|
+
fireEvent.blur(input);
|
|
1141
|
+
fireEvent.focus(input);
|
|
1142
|
+
fireEvent.keyDown(input, { key: "Enter" });
|
|
1143
|
+
act(() => advance(GIVEN_SETTLE_MS * 3));
|
|
1144
|
+
expect(onChange.mock.calls.map((c) => c[0])).toEqual([null]);
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
it("keeps a backspace typed while the host still shows the older commit", () => {
|
|
1148
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1149
|
+
const view = render(
|
|
1150
|
+
<GivenInput given={text} value="ab" onChange={onChange} />,
|
|
1151
|
+
);
|
|
1152
|
+
const input = screen.getByLabelText("brand") as HTMLInputElement;
|
|
1153
|
+
type(input, "abc");
|
|
1154
|
+
act(() => advance(GIVEN_SETTLE_MS));
|
|
1155
|
+
expect(onChange).toHaveBeenCalledWith("abc");
|
|
1156
|
+
// The host has not adopted "abc" yet when the reader backspaces.
|
|
1157
|
+
fireEvent.change(input, { target: { value: "ab" } });
|
|
1158
|
+
view.rerender(
|
|
1159
|
+
<GivenInput given={text} value="abc" onChange={onChange} />,
|
|
1160
|
+
);
|
|
1161
|
+
expect(input.value).toBe("ab");
|
|
1162
|
+
act(() => advance(GIVEN_SETTLE_MS));
|
|
1163
|
+
expect(onChange).toHaveBeenLastCalledWith("ab");
|
|
1164
|
+
});
|
|
1165
|
+
|
|
1166
|
+
it("does not send a settled draft again when the control unmounts before the host echoes it", () => {
|
|
1167
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1168
|
+
const view = render(
|
|
1169
|
+
<GivenInput given={text} value="ab" onChange={onChange} />,
|
|
1170
|
+
);
|
|
1171
|
+
type(screen.getByLabelText("brand"), "abc");
|
|
1172
|
+
act(() => advance(GIVEN_SETTLE_MS));
|
|
1173
|
+
view.unmount();
|
|
1174
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1175
|
+
});
|
|
1176
|
+
|
|
1177
|
+
it("commits a backspace back to the stale committed value when the host echo is late", () => {
|
|
1178
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1179
|
+
const view = render(
|
|
1180
|
+
<GivenInput given={text} value="ab" onChange={onChange} />,
|
|
1181
|
+
);
|
|
1182
|
+
const input = screen.getByLabelText("brand") as HTMLInputElement;
|
|
1183
|
+
fireEvent.focus(input);
|
|
1184
|
+
type(input, "abc");
|
|
1185
|
+
act(() => advance(GIVEN_SETTLE_MS));
|
|
1186
|
+
fireEvent.change(input, { target: { value: "ab" } });
|
|
1187
|
+
fireEvent.blur(input);
|
|
1188
|
+
expect(onChange).toHaveBeenLastCalledWith("ab");
|
|
1189
|
+
view.rerender(
|
|
1190
|
+
<GivenInput given={text} value="abc" onChange={onChange} />,
|
|
1191
|
+
);
|
|
1192
|
+
view.rerender(<GivenInput given={text} value="ab" onChange={onChange} />);
|
|
1193
|
+
expect(input.value).toBe("ab");
|
|
1194
|
+
expect(onChange).toHaveBeenCalledTimes(2);
|
|
1195
|
+
});
|
|
1196
|
+
|
|
1197
|
+
it("sends an explicit empty value when an unset field is typed in and emptied", () => {
|
|
1198
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1199
|
+
render(<Host given={text} initial={undefined} onChange={onChange} />);
|
|
1200
|
+
const input = screen.getByLabelText("brand");
|
|
1201
|
+
fireEvent.focus(input);
|
|
1202
|
+
fireEvent.blur(input);
|
|
1203
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1204
|
+
type(input, "x");
|
|
1205
|
+
type(input, "");
|
|
1206
|
+
act(() => advance(GIVEN_SETTLE_MS));
|
|
1207
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1208
|
+
expect(onChange).toHaveBeenCalledWith("");
|
|
1209
|
+
});
|
|
1210
|
+
|
|
1211
|
+
it("does not send the draft after a clear that swaps the control", () => {
|
|
1212
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1213
|
+
render(
|
|
1214
|
+
<Host
|
|
1215
|
+
given={{
|
|
1216
|
+
name: "n",
|
|
1217
|
+
type: "filter<number>",
|
|
1218
|
+
rangeMin: 0,
|
|
1219
|
+
rangeMax: 10,
|
|
1220
|
+
}}
|
|
1221
|
+
initial="!= 3"
|
|
1222
|
+
onChange={onChange}
|
|
1223
|
+
/>,
|
|
1224
|
+
);
|
|
1225
|
+
const input = screen.getByRole("textbox");
|
|
1226
|
+
type(input, "!= 4");
|
|
1227
|
+
fireEvent.click(clearButtons()[0]);
|
|
1228
|
+
expect(onChange.mock.calls.map((c) => c[0])).toEqual([null]);
|
|
1229
|
+
});
|
|
1230
|
+
|
|
1231
|
+
it("commits on blur without waiting", () => {
|
|
1232
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1233
|
+
render(<Host given={text} initial={undefined} onChange={onChange} />);
|
|
1234
|
+
const input = screen.getByLabelText("brand");
|
|
1235
|
+
type(input, "Ford");
|
|
1236
|
+
fireEvent.blur(input);
|
|
1237
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1238
|
+
expect(onChange).toHaveBeenCalledWith("Ford");
|
|
1239
|
+
act(() => advance(GIVEN_SETTLE_MS * 3));
|
|
1240
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1241
|
+
});
|
|
1242
|
+
|
|
1243
|
+
it("does not overwrite typed text when the value changes under a focused field", () => {
|
|
1244
|
+
const view = render(
|
|
1245
|
+
<GivenInput given={text} value={undefined} onChange={() => {}} />,
|
|
1246
|
+
);
|
|
1247
|
+
const input = screen.getByLabelText("brand") as HTMLInputElement;
|
|
1248
|
+
type(input, "Fordo");
|
|
1249
|
+
// The URL echoing back an earlier keystroke.
|
|
1250
|
+
view.rerender(
|
|
1251
|
+
<GivenInput given={text} value="Ford" onChange={() => {}} />,
|
|
1252
|
+
);
|
|
1253
|
+
expect(input.value).toBe("Fordo");
|
|
1254
|
+
});
|
|
1255
|
+
|
|
1256
|
+
it("shows a new value once the field is no longer focused", () => {
|
|
1257
|
+
const view = render(
|
|
1258
|
+
<GivenInput given={text} value="Ford" onChange={() => {}} />,
|
|
1259
|
+
);
|
|
1260
|
+
const input = screen.getByLabelText("brand") as HTMLInputElement;
|
|
1261
|
+
view.rerender(
|
|
1262
|
+
<GivenInput given={text} value="Audi" onChange={() => {}} />,
|
|
1263
|
+
);
|
|
1264
|
+
expect(input.value).toBe("Audi");
|
|
1265
|
+
});
|
|
1266
|
+
|
|
1267
|
+
it("never commits on mount, for an unset filter given", () => {
|
|
1268
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1269
|
+
render(<GivenInput given={text} value={undefined} onChange={onChange} />);
|
|
1270
|
+
act(() => advance(GIVEN_SETTLE_MS * 3));
|
|
1271
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1272
|
+
});
|
|
1273
|
+
|
|
1274
|
+
it("clears without sending the half-typed text first", () => {
|
|
1275
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1276
|
+
render(<Host given={text} initial="Audi" onChange={onChange} />);
|
|
1277
|
+
const input = screen.getByLabelText("brand") as HTMLInputElement;
|
|
1278
|
+
type(input, "Ford");
|
|
1279
|
+
fireEvent.click(clearButtons()[0]);
|
|
1280
|
+
fireEvent.blur(input);
|
|
1281
|
+
act(() => advance(GIVEN_SETTLE_MS * 3));
|
|
1282
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1283
|
+
expect(onChange.mock.calls[0][0]).toBeNull();
|
|
1284
|
+
expect(input.value).toBe("");
|
|
1285
|
+
});
|
|
1286
|
+
|
|
1287
|
+
it("sends one change for a burst typed into a number field", () => {
|
|
1288
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1289
|
+
render(
|
|
1290
|
+
<Host
|
|
1291
|
+
given={{ name: "n", type: "number" }}
|
|
1292
|
+
initial={undefined}
|
|
1293
|
+
onChange={onChange}
|
|
1294
|
+
/>,
|
|
1295
|
+
);
|
|
1296
|
+
const input = screen.getByLabelText("n");
|
|
1297
|
+
for (const value of ["1", "12", "123"]) {
|
|
1298
|
+
type(input, value);
|
|
1299
|
+
act(() => advance(100));
|
|
1300
|
+
}
|
|
1301
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1302
|
+
act(() => advance(GIVEN_SETTLE_MS));
|
|
1303
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1304
|
+
expect(onChange).toHaveBeenCalledWith(123);
|
|
1305
|
+
});
|
|
1306
|
+
});
|
|
1307
|
+
|
|
1308
|
+
describe("GivenInput: a date typed into the field", () => {
|
|
1309
|
+
beforeEach(() => {
|
|
1310
|
+
jest.useFakeTimers();
|
|
1311
|
+
});
|
|
1312
|
+
afterEach(() => {
|
|
1313
|
+
jest.useRealTimers();
|
|
1314
|
+
cleanup();
|
|
1315
|
+
});
|
|
1316
|
+
|
|
1317
|
+
it("commits once it settles, not for each edit", () => {
|
|
1318
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1319
|
+
render(
|
|
1320
|
+
<Host
|
|
1321
|
+
given={{ name: "d", type: "date" }}
|
|
1322
|
+
initial={undefined}
|
|
1323
|
+
onChange={onChange}
|
|
1324
|
+
/>,
|
|
1325
|
+
);
|
|
1326
|
+
const input = screen.getByRole("textbox") as HTMLInputElement;
|
|
1327
|
+
fireEvent.focus(input);
|
|
1328
|
+
fireEvent.change(input, { target: { value: "01/15/2024" } });
|
|
1329
|
+
fireEvent.change(input, { target: { value: "01/16/2024" } });
|
|
1330
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1331
|
+
act(() => advance(GIVEN_SETTLE_MS));
|
|
1332
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1333
|
+
expect((onChange.mock.calls[0][0] as Date).toISOString()).toBe(
|
|
1334
|
+
"2024-01-16T00:00:00.000Z",
|
|
1335
|
+
);
|
|
1336
|
+
// The committed day is UTC and the typed one is local, equal days that
|
|
1337
|
+
// are different instants outside UTC: blurring must not send it again.
|
|
1338
|
+
fireEvent.blur(input);
|
|
1339
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1340
|
+
});
|
|
1341
|
+
|
|
1342
|
+
it("commits at once when the field is not focused, as a calendar pick is", () => {
|
|
1343
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1344
|
+
render(
|
|
1345
|
+
<Host
|
|
1346
|
+
given={{ name: "d", type: "date" }}
|
|
1347
|
+
initial={undefined}
|
|
1348
|
+
onChange={onChange}
|
|
1349
|
+
/>,
|
|
1350
|
+
);
|
|
1351
|
+
const input = screen.getByRole("textbox") as HTMLInputElement;
|
|
1352
|
+
fireEvent.change(input, { target: { value: "01/15/2024" } });
|
|
1353
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1354
|
+
});
|
|
1355
|
+
|
|
1356
|
+
it("keeps focus on the field when the clear button is pressed", () => {
|
|
1357
|
+
render(
|
|
1358
|
+
<Host
|
|
1359
|
+
given={{ name: "d", type: "date" }}
|
|
1360
|
+
initial={new Date("2024-01-16T00:00:00Z")}
|
|
1361
|
+
onChange={() => {}}
|
|
1362
|
+
/>,
|
|
1363
|
+
);
|
|
1364
|
+
const clear = screen.getByRole("button", { name: "Clear" });
|
|
1365
|
+
expect(fireEvent.mouseDown(clear)).toBe(false);
|
|
1366
|
+
});
|
|
1367
|
+
|
|
1368
|
+
it("sends one change for a custom range edited in a burst", () => {
|
|
1369
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1370
|
+
render(
|
|
1371
|
+
<Host
|
|
1372
|
+
given={{ name: "p", type: "filter<timestamp>" }}
|
|
1373
|
+
initial="2024-01-01 to 2024-02-01"
|
|
1374
|
+
onChange={onChange}
|
|
1375
|
+
/>,
|
|
1376
|
+
);
|
|
1377
|
+
const to = screen.getByLabelText("To") as HTMLInputElement;
|
|
1378
|
+
fireEvent.focus(to);
|
|
1379
|
+
fireEvent.change(to, { target: { value: "01/15/2024" } });
|
|
1380
|
+
fireEvent.change(to, { target: { value: "01/20/2024" } });
|
|
1381
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1382
|
+
act(() => advance(GIVEN_SETTLE_MS));
|
|
1383
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1384
|
+
});
|
|
1385
|
+
});
|
|
1386
|
+
|
|
1387
|
+
describe("GivenInput: a slider commits on release, not on every move", () => {
|
|
1388
|
+
afterEach(cleanup);
|
|
1389
|
+
|
|
1390
|
+
it("sends nothing while the thumb is dragged and one change on release", () => {
|
|
1391
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1392
|
+
render(
|
|
1393
|
+
<GivenInput
|
|
1394
|
+
given={{ name: "n", type: "number", rangeMin: 0, rangeMax: 10 }}
|
|
1395
|
+
value={2}
|
|
1396
|
+
onChange={onChange}
|
|
1397
|
+
/>,
|
|
1398
|
+
);
|
|
1399
|
+
const root = document.querySelector(".MuiSlider-root") as HTMLElement;
|
|
1400
|
+
root.getBoundingClientRect = () =>
|
|
1401
|
+
({
|
|
1402
|
+
left: 0,
|
|
1403
|
+
width: 100,
|
|
1404
|
+
top: 0,
|
|
1405
|
+
height: 10,
|
|
1406
|
+
right: 100,
|
|
1407
|
+
bottom: 10,
|
|
1408
|
+
}) as DOMRect;
|
|
1409
|
+
fireEvent.mouseDown(root, { clientX: 30, button: 0 });
|
|
1410
|
+
fireEvent.mouseMove(document, { clientX: 50, buttons: 1 });
|
|
1411
|
+
fireEvent.mouseMove(document, { clientX: 70, buttons: 1 });
|
|
1412
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1413
|
+
fireEvent.mouseUp(document, { clientX: 70, buttons: 1 });
|
|
1414
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
1415
|
+
expect(onChange).toHaveBeenCalledWith(7);
|
|
1416
|
+
});
|
|
1417
|
+
|
|
1418
|
+
it("sends nothing for a press that moves nothing", () => {
|
|
1419
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
1420
|
+
render(
|
|
1421
|
+
<GivenInput
|
|
1422
|
+
given={{ name: "n", type: "number", rangeMin: 0, rangeMax: 10 }}
|
|
1423
|
+
value={undefined}
|
|
1424
|
+
onChange={onChange}
|
|
1425
|
+
/>,
|
|
1426
|
+
);
|
|
1427
|
+
const root = document.querySelector(".MuiSlider-root") as HTMLElement;
|
|
1428
|
+
root.getBoundingClientRect = () =>
|
|
1429
|
+
({
|
|
1430
|
+
left: 0,
|
|
1431
|
+
width: 100,
|
|
1432
|
+
top: 0,
|
|
1433
|
+
height: 10,
|
|
1434
|
+
right: 100,
|
|
1435
|
+
bottom: 10,
|
|
1436
|
+
}) as DOMRect;
|
|
1437
|
+
fireEvent.mouseDown(root, { clientX: 0, button: 0 });
|
|
1438
|
+
fireEvent.mouseUp(document, { clientX: 0, buttons: 1 });
|
|
1439
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
1440
|
+
});
|
|
1441
|
+
});
|