@opendata-ai/openchart-engine 8.0.0-rc.12 → 8.0.0-rc.3
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/index.d.ts +0 -7
- package/dist/index.js +161 -579
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- package/src/__test-fixtures__/specs.ts +182 -0
- package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +2339 -0
- package/src/__tests__/annotation-behavior.test.ts +721 -0
- package/src/__tests__/axes.test.ts +1579 -0
- package/src/__tests__/compile-animation.test.ts +161 -0
- package/src/__tests__/compile-chart.test.ts +1303 -0
- package/src/__tests__/compile-footnotes.test.ts +277 -0
- package/src/__tests__/compile-layer.test.ts +969 -0
- package/src/__tests__/compile-snapshot.test.ts +156 -0
- package/src/__tests__/compound-labels.test.ts +147 -0
- package/src/__tests__/dimensions.test.ts +918 -0
- package/src/__tests__/encoding-sugar.test.ts +390 -0
- package/src/__tests__/facet.test.ts +215 -0
- package/src/__tests__/helpers/invariants.ts +299 -0
- package/src/__tests__/known-bugs.test.ts +566 -0
- package/src/__tests__/layout-fuzz.test.ts +358 -0
- package/src/__tests__/layout-invariants.test.ts +535 -0
- package/src/__tests__/legend.test.ts +721 -0
- package/src/__tests__/mark-keys.test.ts +294 -0
- package/src/__tests__/ranking-recipes.test.ts +260 -0
- package/src/__tests__/rotation.test.ts +83 -0
- package/src/__tests__/scales.test.ts +323 -0
- package/src/__tests__/series-search.test.ts +92 -0
- package/src/__tests__/spec-sugar.test.ts +801 -0
- package/src/__tests__/you-draw-it.test.ts +160 -0
- package/src/annotations/__tests__/compute.test.ts +1672 -0
- package/src/annotations/__tests__/geometry.test.ts +66 -0
- package/src/annotations/__tests__/placement-fuzz.test.ts +256 -0
- package/src/annotations/__tests__/placement-invariants.test.ts +345 -0
- package/src/annotations/__tests__/placement.test.ts +316 -0
- package/src/annotations/__tests__/resolve-text.test.ts +788 -0
- package/src/annotations/__tests__/rich-text.test.ts +91 -0
- package/src/annotations/__tests__/thinning.test.ts +285 -0
- package/src/annotations/collisions.ts +326 -0
- package/src/annotations/compute.ts +333 -0
- package/src/annotations/constants.ts +162 -0
- package/src/annotations/geometry.ts +370 -0
- package/src/annotations/move.ts +135 -0
- package/src/annotations/placement.ts +636 -0
- package/src/annotations/position.ts +131 -0
- package/src/annotations/resolve-range.ts +117 -0
- package/src/annotations/resolve-refline.ts +157 -0
- package/src/annotations/resolve-text.ts +539 -0
- package/src/annotations/rich-text.ts +65 -0
- package/src/annotations/thinning.ts +190 -0
- package/src/barlist/__tests__/compile-barlist.test.ts +200 -0
- package/src/barlist/compile-barlist.ts +391 -0
- package/src/barlist/types.ts +28 -0
- package/src/charts/__tests__/post-process.test.ts +237 -0
- package/src/charts/__tests__/registry.test.ts +116 -0
- package/src/charts/__tests__/utils.test.ts +195 -0
- package/src/charts/_shared/__tests__/density-filter.test.ts +32 -0
- package/src/charts/_shared/density-filter.ts +26 -0
- package/src/charts/_shared/format-label-value.ts +16 -0
- package/src/charts/bar/__tests__/compute.test.ts +934 -0
- package/src/charts/bar/__tests__/gradient-orient.test.ts +127 -0
- package/src/charts/bar/__tests__/labels.test.ts +292 -0
- package/src/charts/bar/compute.ts +539 -0
- package/src/charts/bar/index.ts +60 -0
- package/src/charts/bar/labels.ts +311 -0
- package/src/charts/beeswarm/__tests__/compute.test.ts +280 -0
- package/src/charts/beeswarm/__tests__/dodge.test.ts +148 -0
- package/src/charts/beeswarm/compute.ts +203 -0
- package/src/charts/beeswarm/dodge.ts +155 -0
- package/src/charts/beeswarm/index.ts +32 -0
- package/src/charts/builtin.ts +75 -0
- package/src/charts/calendar/__tests__/compute.test.ts +417 -0
- package/src/charts/calendar/compute.ts +319 -0
- package/src/charts/calendar/index.ts +32 -0
- package/src/charts/column/__tests__/compute.test.ts +540 -0
- package/src/charts/column/__tests__/labels.test.ts +179 -0
- package/src/charts/column/compute.ts +493 -0
- package/src/charts/column/index.ts +53 -0
- package/src/charts/column/labels.ts +229 -0
- package/src/charts/dot/__tests__/compute.test.ts +380 -0
- package/src/charts/dot/__tests__/labels.test.ts +98 -0
- package/src/charts/dot/compute.ts +285 -0
- package/src/charts/dot/index.ts +62 -0
- package/src/charts/dot/labels.ts +121 -0
- package/src/charts/line/__tests__/compute.test.ts +1194 -0
- package/src/charts/line/__tests__/curves.test.ts +51 -0
- package/src/charts/line/__tests__/labels.test.ts +94 -0
- package/src/charts/line/__tests__/mark-options.test.ts +175 -0
- package/src/charts/line/area.ts +484 -0
- package/src/charts/line/compute.ts +356 -0
- package/src/charts/line/curves.ts +85 -0
- package/src/charts/line/index.ts +176 -0
- package/src/charts/line/labels.ts +177 -0
- package/src/charts/parliament/__tests__/compute.test.ts +411 -0
- package/src/charts/parliament/compute.ts +313 -0
- package/src/charts/parliament/index.ts +32 -0
- package/src/charts/pie/__tests__/compute.test.ts +384 -0
- package/src/charts/pie/__tests__/labels.test.ts +132 -0
- package/src/charts/pie/compute.ts +324 -0
- package/src/charts/pie/index.ts +49 -0
- package/src/charts/pie/labels.ts +143 -0
- package/src/charts/post-process.ts +229 -0
- package/src/charts/range/__tests__/compute.test.ts +486 -0
- package/src/charts/range/compute.ts +273 -0
- package/src/charts/range/index.ts +82 -0
- package/src/charts/range/labels.ts +169 -0
- package/src/charts/rect/__tests__/compute.test.ts +120 -0
- package/src/charts/rect/index.ts +127 -0
- package/src/charts/registry.ts +72 -0
- package/src/charts/rule/index.ts +139 -0
- package/src/charts/scatter/__tests__/compute.test.ts +488 -0
- package/src/charts/scatter/__tests__/trendline.test.ts +191 -0
- package/src/charts/scatter/compute.ts +192 -0
- package/src/charts/scatter/index.ts +46 -0
- package/src/charts/scatter/trendline.ts +100 -0
- package/src/charts/text/__tests__/compute.test.ts +307 -0
- package/src/charts/text/index.ts +141 -0
- package/src/charts/tick/__tests__/compute.test.ts +75 -0
- package/src/charts/tick/index.ts +99 -0
- package/src/charts/utils.ts +170 -0
- package/src/charts/waffle/__tests__/compute.test.ts +305 -0
- package/src/charts/waffle/compute.ts +197 -0
- package/src/charts/waffle/index.ts +31 -0
- package/src/compile/__tests__/color-scale-range.test.ts +158 -0
- package/src/compile/__tests__/color-scale-reverse.test.ts +89 -0
- package/src/compile/__tests__/data-clip.test.ts +127 -0
- package/src/compile/__tests__/fill-patterns.test.ts +154 -0
- package/src/compile/__tests__/size-scale.test.ts +108 -0
- package/src/compile/__tests__/watermark-obstacle.test.ts +93 -0
- package/src/compile/color-scale-range.ts +101 -0
- package/src/compile/data-clip.ts +48 -0
- package/src/compile/fill-patterns.ts +74 -0
- package/src/compile/layer.ts +626 -0
- package/src/compile/size-scale.ts +141 -0
- package/src/compile/spec-sugar.ts +758 -0
- package/src/compile/watermark-obstacle.ts +54 -0
- package/src/compile/you-draw-it.ts +123 -0
- package/src/compile.ts +1450 -0
- package/src/compiler/__tests__/a11y-warnings.test.ts +148 -0
- package/src/compiler/__tests__/animation.test.ts +177 -0
- package/src/compiler/__tests__/compile.test.ts +87 -0
- package/src/compiler/__tests__/fixtures/vl-parity/axis-null.json +19 -0
- package/src/compiler/__tests__/fixtures/vl-parity/color-value.json +20 -0
- package/src/compiler/__tests__/fixtures/vl-parity/count-aggregate.json +22 -0
- package/src/compiler/__tests__/fixtures/vl-parity/data-values.json +19 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-axis-null.json +14 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-count.json +34 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-data-values.json +22 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-legend-null.json +41 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-scheme.json +38 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-sort.json +28 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-theta.json +28 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-title.json +30 -0
- package/src/compiler/__tests__/fixtures/vl-parity/layer-value-def.json +29 -0
- package/src/compiler/__tests__/fixtures/vl-parity/legend-null.json +32 -0
- package/src/compiler/__tests__/fixtures/vl-parity/scheme.json +25 -0
- package/src/compiler/__tests__/fixtures/vl-parity/sort-by-value.json +19 -0
- package/src/compiler/__tests__/fixtures/vl-parity/theta-arc.json +19 -0
- package/src/compiler/__tests__/fixtures/vl-parity/title-subtitle.json +21 -0
- package/src/compiler/__tests__/fixtures/vl-parity/width-height.json +23 -0
- package/src/compiler/__tests__/keys.test.ts +83 -0
- package/src/compiler/__tests__/normalize-highlight.test.ts +103 -0
- package/src/compiler/__tests__/normalize.test.ts +399 -0
- package/src/compiler/__tests__/sparkline-defaults.test.ts +153 -0
- package/src/compiler/__tests__/validate-did-you-mean.test.ts +86 -0
- package/src/compiler/__tests__/validate-highlight.test.ts +112 -0
- package/src/compiler/__tests__/validate-near-miss.test.ts +89 -0
- package/src/compiler/__tests__/validate.test.ts +629 -0
- package/src/compiler/__tests__/vl-parity.test.ts +68 -0
- package/src/compiler/a11y-warnings.ts +110 -0
- package/src/compiler/animation.ts +142 -0
- package/src/compiler/index.ts +47 -0
- package/src/compiler/keys.ts +55 -0
- package/src/compiler/normalize.ts +734 -0
- package/src/compiler/sparkline-defaults.ts +138 -0
- package/src/compiler/types.ts +241 -0
- package/src/compiler/validate.ts +1871 -0
- package/src/endpoint-labels/__tests__/compute.test.ts +587 -0
- package/src/endpoint-labels/compute.ts +621 -0
- package/src/endpoint-labels/constants.ts +54 -0
- package/src/endpoint-labels/format.ts +60 -0
- package/src/endpoint-labels/predict.ts +139 -0
- package/src/format/__tests__/field-format.test.ts +164 -0
- package/src/format/field-format.ts +27 -0
- package/src/geo/__tests__/compile-map.test.ts +832 -0
- package/src/geo/compile-map.ts +1243 -0
- package/src/geo/join.ts +115 -0
- package/src/geo/projections.ts +45 -0
- package/src/geo/types.ts +26 -0
- package/src/graphs/__tests__/community.test.ts +228 -0
- package/src/graphs/__tests__/compile-graph.test.ts +396 -0
- package/src/graphs/__tests__/encoding.test.ts +415 -0
- package/src/graphs/community.ts +92 -0
- package/src/graphs/compile-graph.ts +329 -0
- package/src/graphs/encoding.ts +356 -0
- package/src/graphs/types.ts +106 -0
- package/src/index.ts +177 -0
- package/src/layout/__tests__/metrics.test.ts +97 -0
- package/src/layout/__tests__/scales-highlight.test.ts +192 -0
- package/src/layout/axes/rotation.ts +152 -0
- package/src/layout/axes/thinning.ts +98 -0
- package/src/layout/axes/ticks.ts +409 -0
- package/src/layout/axes.ts +757 -0
- package/src/layout/dimensions.ts +912 -0
- package/src/layout/facet.ts +91 -0
- package/src/layout/gridlines.ts +84 -0
- package/src/layout/metrics.ts +132 -0
- package/src/layout/plan.ts +631 -0
- package/src/layout/scales.ts +1053 -0
- package/src/layout/shared.ts +44 -0
- package/src/legend/__tests__/continuous.test.ts +318 -0
- package/src/legend/__tests__/redundancy.test.ts +108 -0
- package/src/legend/__tests__/size.test.ts +348 -0
- package/src/legend/__tests__/suppression.test.ts +382 -0
- package/src/legend/compute.ts +747 -0
- package/src/legend/continuous.ts +327 -0
- package/src/legend/size.ts +407 -0
- package/src/legend/suppression.ts +226 -0
- package/src/legend/wrap.ts +117 -0
- package/src/sankey/__tests__/compile-sankey.test.ts +458 -0
- package/src/sankey/__tests__/layout.test.ts +165 -0
- package/src/sankey/__tests__/node-label-wrap.test.ts +114 -0
- package/src/sankey/__tests__/node-sort.test.ts +45 -0
- package/src/sankey/compile-sankey.ts +706 -0
- package/src/sankey/layout.ts +184 -0
- package/src/sankey/types.ts +41 -0
- package/src/tables/__tests__/bar-column.test.ts +147 -0
- package/src/tables/__tests__/category-colors.test.ts +230 -0
- package/src/tables/__tests__/compile-table.test.ts +236 -0
- package/src/tables/__tests__/format-cells.test.ts +143 -0
- package/src/tables/__tests__/heatmap.test.ts +148 -0
- package/src/tables/__tests__/pagination.test.ts +78 -0
- package/src/tables/__tests__/search.test.ts +94 -0
- package/src/tables/__tests__/sort.test.ts +107 -0
- package/src/tables/__tests__/sparkline.test.ts +122 -0
- package/src/tables/bar-column.ts +94 -0
- package/src/tables/category-colors.ts +78 -0
- package/src/tables/compile-table.ts +427 -0
- package/src/tables/format-cells.ts +115 -0
- package/src/tables/heatmap.ts +141 -0
- package/src/tables/pagination.ts +46 -0
- package/src/tables/search.ts +66 -0
- package/src/tables/sort.ts +69 -0
- package/src/tables/sparkline.ts +113 -0
- package/src/tables/utils.ts +16 -0
- package/src/tilemap/__tests__/compile-tilemap.test.ts +576 -0
- package/src/tilemap/compile-tilemap.ts +755 -0
- package/src/tilemap/layout.ts +172 -0
- package/src/tilemap/types.ts +33 -0
- package/src/tooltips/__tests__/compute.test.ts +582 -0
- package/src/tooltips/compute.ts +700 -0
- package/src/transforms/__tests__/aggregate.test.ts +159 -0
- package/src/transforms/__tests__/bin.test.ts +88 -0
- package/src/transforms/__tests__/calculate.test.ts +146 -0
- package/src/transforms/__tests__/conditional.test.ts +127 -0
- package/src/transforms/__tests__/filter-relative.test.ts +202 -0
- package/src/transforms/__tests__/filter.test.ts +59 -0
- package/src/transforms/__tests__/fold.test.ts +79 -0
- package/src/transforms/__tests__/index.test.ts +93 -0
- package/src/transforms/__tests__/predicates.test.ts +193 -0
- package/src/transforms/__tests__/timeunit.test.ts +129 -0
- package/src/transforms/__tests__/window.test.ts +286 -0
- package/src/transforms/aggregate.ts +131 -0
- package/src/transforms/bin.ts +87 -0
- package/src/transforms/calculate.ts +60 -0
- package/src/transforms/conditional.ts +46 -0
- package/src/transforms/filter.ts +122 -0
- package/src/transforms/fold.ts +49 -0
- package/src/transforms/index.ts +60 -0
- package/src/transforms/predicates.ts +123 -0
- package/src/transforms/timeunit.ts +88 -0
- package/src/transforms/window.ts +185 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChartSpec,
|
|
3
|
+
GraphSpec,
|
|
4
|
+
LayerSpec,
|
|
5
|
+
MapSpec,
|
|
6
|
+
RangeAnnotation,
|
|
7
|
+
RefLineAnnotation,
|
|
8
|
+
SankeySpec,
|
|
9
|
+
TableSpec,
|
|
10
|
+
TextAnnotation,
|
|
11
|
+
} from '@opendata-ai/openchart-core';
|
|
12
|
+
import { describe, expect, it } from 'vitest';
|
|
13
|
+
import type { NormalizedMapSpec } from '../../geo/types';
|
|
14
|
+
import type { NormalizedSankeySpec } from '../../sankey/types';
|
|
15
|
+
import { normalizeSpec } from '../normalize';
|
|
16
|
+
import type { NormalizedChartSpec, NormalizedGraphSpec, NormalizedTableSpec } from '../types';
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// Test data
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
const lineSpec: ChartSpec = {
|
|
23
|
+
mark: 'line',
|
|
24
|
+
data: [
|
|
25
|
+
{ date: '2020-01-01', value: 10, country: 'US' },
|
|
26
|
+
{ date: '2021-01-01', value: 20, country: 'UK' },
|
|
27
|
+
],
|
|
28
|
+
encoding: {
|
|
29
|
+
x: { field: 'date', type: 'temporal' },
|
|
30
|
+
y: { field: 'value', type: 'quantitative' },
|
|
31
|
+
},
|
|
32
|
+
chrome: {
|
|
33
|
+
title: 'GDP Growth',
|
|
34
|
+
subtitle: { text: 'Over time', style: { fontSize: 16 } },
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
// Tests
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
describe('normalizeSpec', () => {
|
|
43
|
+
describe('chart spec normalization', () => {
|
|
44
|
+
it('fills default values for optionals', () => {
|
|
45
|
+
const warnings: string[] = [];
|
|
46
|
+
const result = normalizeSpec(lineSpec, warnings) as NormalizedChartSpec;
|
|
47
|
+
|
|
48
|
+
expect(result.responsive).toBe(true);
|
|
49
|
+
expect(result.darkMode).toBe('off');
|
|
50
|
+
expect(result.annotations).toEqual([]);
|
|
51
|
+
expect(result.theme).toEqual({});
|
|
52
|
+
expect(result.labels).toEqual({
|
|
53
|
+
density: 'auto',
|
|
54
|
+
format: '',
|
|
55
|
+
prefix: '',
|
|
56
|
+
offsets: undefined,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('watermark defaults to true when not specified', () => {
|
|
61
|
+
const result = normalizeSpec(lineSpec) as NormalizedChartSpec;
|
|
62
|
+
expect(result.watermark).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('watermark respects explicit false', () => {
|
|
66
|
+
const spec: ChartSpec = { ...lineSpec, watermark: false };
|
|
67
|
+
const result = normalizeSpec(spec) as NormalizedChartSpec;
|
|
68
|
+
expect(result.watermark).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('preserves explicit values', () => {
|
|
72
|
+
const spec: ChartSpec = {
|
|
73
|
+
...lineSpec,
|
|
74
|
+
responsive: false,
|
|
75
|
+
darkMode: 'force',
|
|
76
|
+
};
|
|
77
|
+
const result = normalizeSpec(spec) as NormalizedChartSpec;
|
|
78
|
+
|
|
79
|
+
expect(result.responsive).toBe(false);
|
|
80
|
+
expect(result.darkMode).toBe('force');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('normalizes chrome strings to ChromeText objects', () => {
|
|
84
|
+
const result = normalizeSpec(lineSpec) as NormalizedChartSpec;
|
|
85
|
+
|
|
86
|
+
// Plain string becomes ChromeText
|
|
87
|
+
expect(result.chrome.title).toEqual({ text: 'GDP Growth' });
|
|
88
|
+
// ChromeText with style is preserved
|
|
89
|
+
expect(result.chrome.subtitle).toEqual({
|
|
90
|
+
text: 'Over time',
|
|
91
|
+
style: { fontSize: 16 },
|
|
92
|
+
});
|
|
93
|
+
// Undefined fields stay undefined
|
|
94
|
+
expect(result.chrome.source).toBeUndefined();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('infers encoding types from data when not specified', () => {
|
|
98
|
+
const warnings: string[] = [];
|
|
99
|
+
const spec: ChartSpec = {
|
|
100
|
+
mark: 'point',
|
|
101
|
+
data: [
|
|
102
|
+
{ x: 10, y: 20 },
|
|
103
|
+
{ x: 30, y: 40 },
|
|
104
|
+
],
|
|
105
|
+
encoding: {
|
|
106
|
+
// No type specified, should be inferred as quantitative
|
|
107
|
+
x: { field: 'x', type: 'quantitative' },
|
|
108
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentionally omitting `type` to test inference
|
|
109
|
+
y: { field: 'y' } as any,
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const result = normalizeSpec(spec, warnings) as NormalizedChartSpec;
|
|
114
|
+
expect(result.encoding.y?.type).toBe('quantitative');
|
|
115
|
+
expect(warnings.some((w) => w.includes('Inferred'))).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('infers temporal type from date strings', () => {
|
|
119
|
+
const warnings: string[] = [];
|
|
120
|
+
const spec: ChartSpec = {
|
|
121
|
+
mark: 'line',
|
|
122
|
+
data: [
|
|
123
|
+
{ date: '2020-01-01', value: 10 },
|
|
124
|
+
{ date: '2021-06-15', value: 20 },
|
|
125
|
+
],
|
|
126
|
+
encoding: {
|
|
127
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentionally omitting `type` to test inference
|
|
128
|
+
x: { field: 'date' } as any,
|
|
129
|
+
y: { field: 'value', type: 'quantitative' },
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const result = normalizeSpec(spec, warnings) as NormalizedChartSpec;
|
|
134
|
+
expect(result.encoding.x?.type).toBe('temporal');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('warns on type mismatch (temporal declared as nominal)', () => {
|
|
138
|
+
const warnings: string[] = [];
|
|
139
|
+
const spec: ChartSpec = {
|
|
140
|
+
mark: 'line',
|
|
141
|
+
data: [
|
|
142
|
+
{ date: '2020-01-01', value: 10 },
|
|
143
|
+
{ date: '2021-06-15', value: 20 },
|
|
144
|
+
],
|
|
145
|
+
encoding: {
|
|
146
|
+
x: { field: 'date', type: 'nominal' },
|
|
147
|
+
y: { field: 'value', type: 'quantitative' },
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
normalizeSpec(spec, warnings);
|
|
152
|
+
expect(warnings.some((w) => w.includes('looks temporal but was declared as nominal'))).toBe(
|
|
153
|
+
true,
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('preserves explicit label config', () => {
|
|
158
|
+
const spec: ChartSpec = {
|
|
159
|
+
...lineSpec,
|
|
160
|
+
labels: { density: 'none', format: ',.0f' },
|
|
161
|
+
};
|
|
162
|
+
const result = normalizeSpec(spec) as NormalizedChartSpec;
|
|
163
|
+
expect(result.labels).toEqual({
|
|
164
|
+
density: 'none',
|
|
165
|
+
format: ',.0f',
|
|
166
|
+
prefix: '',
|
|
167
|
+
offsets: undefined,
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('fills partial label config with defaults', () => {
|
|
172
|
+
const spec: ChartSpec = {
|
|
173
|
+
...lineSpec,
|
|
174
|
+
labels: { density: 'endpoints' },
|
|
175
|
+
};
|
|
176
|
+
const result = normalizeSpec(spec) as NormalizedChartSpec;
|
|
177
|
+
expect(result.labels).toEqual({
|
|
178
|
+
density: 'endpoints',
|
|
179
|
+
format: '',
|
|
180
|
+
prefix: '',
|
|
181
|
+
offsets: undefined,
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('normalizes annotations with default styles', () => {
|
|
186
|
+
const spec: ChartSpec = {
|
|
187
|
+
...lineSpec,
|
|
188
|
+
annotations: [
|
|
189
|
+
{ type: 'refline', y: 15 },
|
|
190
|
+
{ type: 'text', x: '2020-01-01', y: 10, text: 'Start' },
|
|
191
|
+
{ type: 'range', y1: 10, y2: 20 },
|
|
192
|
+
],
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const result = normalizeSpec(spec) as NormalizedChartSpec;
|
|
196
|
+
const refline = result.annotations[0] as RefLineAnnotation;
|
|
197
|
+
expect(refline.style).toBe('dashed');
|
|
198
|
+
expect(refline.strokeWidth).toBe(1);
|
|
199
|
+
|
|
200
|
+
// Normalize must NOT stamp typography onto text annotations. The
|
|
201
|
+
// annotation layer owns fontSize (DEFAULT_ANNOTATION_FONT_SIZE) and
|
|
202
|
+
// fontWeight (the lede rule, which promotes a subtitle-bearing primary
|
|
203
|
+
// line to bold only when the author left the weight unset). Defaulting
|
|
204
|
+
// them here makes `fontWeight` always defined, so the lede rule can
|
|
205
|
+
// never fire and the size default can never apply.
|
|
206
|
+
const text = result.annotations[1] as TextAnnotation;
|
|
207
|
+
expect(text.fontSize).toBeUndefined();
|
|
208
|
+
expect(text.fontWeight).toBeUndefined();
|
|
209
|
+
expect(text.opacity).toBe(1);
|
|
210
|
+
|
|
211
|
+
const range = result.annotations[2] as RangeAnnotation;
|
|
212
|
+
expect(range.opacity).toBe(0.1);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('preserves author-set annotation typography through normalization', () => {
|
|
216
|
+
const spec: ChartSpec = {
|
|
217
|
+
...lineSpec,
|
|
218
|
+
annotations: [
|
|
219
|
+
{ type: 'text', x: '2020-01-01', y: 10, text: 'Start', fontSize: 18, fontWeight: 300 },
|
|
220
|
+
],
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const text = (normalizeSpec(spec) as NormalizedChartSpec).annotations[0] as TextAnnotation;
|
|
224
|
+
expect(text.fontSize).toBe(18);
|
|
225
|
+
expect(text.fontWeight).toBe(300);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
describe('table spec normalization', () => {
|
|
230
|
+
it('fills default values', () => {
|
|
231
|
+
const spec: TableSpec = {
|
|
232
|
+
type: 'table',
|
|
233
|
+
data: [{ name: 'Alice', age: 30 }],
|
|
234
|
+
columns: [{ key: 'name' }, { key: 'age' }],
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const result = normalizeSpec(spec) as NormalizedTableSpec;
|
|
238
|
+
expect(result.search).toBe(false);
|
|
239
|
+
expect(result.pagination).toBe(false);
|
|
240
|
+
expect(result.stickyFirstColumn).toBe(false);
|
|
241
|
+
expect(result.compact).toBe(false);
|
|
242
|
+
expect(result.responsive).toBe(true);
|
|
243
|
+
expect(result.darkMode).toBe('off');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('watermark defaults to true when not specified', () => {
|
|
247
|
+
const spec: TableSpec = {
|
|
248
|
+
type: 'table',
|
|
249
|
+
data: [{ name: 'Alice', age: 30 }],
|
|
250
|
+
columns: [{ key: 'name' }, { key: 'age' }],
|
|
251
|
+
};
|
|
252
|
+
const result = normalizeSpec(spec) as NormalizedTableSpec;
|
|
253
|
+
expect(result.watermark).toBe(true);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('watermark respects explicit false', () => {
|
|
257
|
+
const spec: TableSpec = {
|
|
258
|
+
type: 'table',
|
|
259
|
+
data: [{ name: 'Alice', age: 30 }],
|
|
260
|
+
columns: [{ key: 'name' }, { key: 'age' }],
|
|
261
|
+
watermark: false,
|
|
262
|
+
};
|
|
263
|
+
const result = normalizeSpec(spec) as NormalizedTableSpec;
|
|
264
|
+
expect(result.watermark).toBe(false);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
describe('graph spec normalization', () => {
|
|
269
|
+
it('fills default values', () => {
|
|
270
|
+
const spec: GraphSpec = {
|
|
271
|
+
type: 'graph',
|
|
272
|
+
nodes: [{ id: 'a' }, { id: 'b' }],
|
|
273
|
+
edges: [{ source: 'a', target: 'b' }],
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const result = normalizeSpec(spec) as NormalizedGraphSpec;
|
|
277
|
+
expect(result.encoding).toEqual({});
|
|
278
|
+
expect(result.layout).toEqual({ type: 'force', chargeStrength: -300, linkDistance: 30 });
|
|
279
|
+
expect(result.annotations).toEqual([]);
|
|
280
|
+
expect(result.darkMode).toBe('off');
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('watermark defaults to true when not specified', () => {
|
|
284
|
+
const spec: GraphSpec = {
|
|
285
|
+
type: 'graph',
|
|
286
|
+
nodes: [{ id: 'a' }, { id: 'b' }],
|
|
287
|
+
edges: [{ source: 'a', target: 'b' }],
|
|
288
|
+
};
|
|
289
|
+
const result = normalizeSpec(spec) as NormalizedGraphSpec;
|
|
290
|
+
expect(result.watermark).toBe(true);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('watermark respects explicit false', () => {
|
|
294
|
+
const spec: GraphSpec = {
|
|
295
|
+
type: 'graph',
|
|
296
|
+
nodes: [{ id: 'a' }, { id: 'b' }],
|
|
297
|
+
edges: [{ source: 'a', target: 'b' }],
|
|
298
|
+
watermark: false,
|
|
299
|
+
};
|
|
300
|
+
const result = normalizeSpec(spec) as NormalizedGraphSpec;
|
|
301
|
+
expect(result.watermark).toBe(false);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
describe('sankey spec normalization', () => {
|
|
306
|
+
const baseSankeySpec: SankeySpec = {
|
|
307
|
+
type: 'sankey',
|
|
308
|
+
data: [{ source: 'A', target: 'B', value: 10 }],
|
|
309
|
+
encoding: {
|
|
310
|
+
source: { field: 'source' },
|
|
311
|
+
target: { field: 'target' },
|
|
312
|
+
value: { field: 'value' },
|
|
313
|
+
},
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
it('watermark defaults to true when not specified', () => {
|
|
317
|
+
const result = normalizeSpec(baseSankeySpec) as NormalizedSankeySpec;
|
|
318
|
+
expect(result.watermark).toBe(true);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('watermark respects explicit false', () => {
|
|
322
|
+
const spec: SankeySpec = { ...baseSankeySpec, watermark: false };
|
|
323
|
+
const result = normalizeSpec(spec) as NormalizedSankeySpec;
|
|
324
|
+
expect(result.watermark).toBe(false);
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
describe('layer spec normalization', () => {
|
|
329
|
+
const baseLeaf: ChartSpec = {
|
|
330
|
+
mark: 'line',
|
|
331
|
+
data: [{ x: 1, y: 2 }],
|
|
332
|
+
encoding: {
|
|
333
|
+
x: { field: 'x', type: 'quantitative' },
|
|
334
|
+
y: { field: 'y', type: 'quantitative' },
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
it('watermark defaults to true for layer leaves', () => {
|
|
339
|
+
const layerSpec: LayerSpec = { layer: [baseLeaf] };
|
|
340
|
+
const result = normalizeSpec(layerSpec) as NormalizedChartSpec;
|
|
341
|
+
expect(result.watermark).toBe(true);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('layer-level watermark: false propagates to leaves', () => {
|
|
345
|
+
const layerSpec: LayerSpec = { layer: [baseLeaf], watermark: false };
|
|
346
|
+
const result = normalizeSpec(layerSpec) as NormalizedChartSpec;
|
|
347
|
+
expect(result.watermark).toBe(false);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('leaf-level watermark overrides layer-level', () => {
|
|
351
|
+
const leaf: ChartSpec = { ...baseLeaf, watermark: true };
|
|
352
|
+
const layerSpec: LayerSpec = { layer: [leaf], watermark: false };
|
|
353
|
+
const result = normalizeSpec(layerSpec) as NormalizedChartSpec;
|
|
354
|
+
// Leaf explicitly sets true, which should be preserved
|
|
355
|
+
expect(result.watermark).toBe(true);
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
describe('map spec normalization', () => {
|
|
360
|
+
const mapSpec: MapSpec = {
|
|
361
|
+
type: 'map',
|
|
362
|
+
geo: {
|
|
363
|
+
features: {
|
|
364
|
+
type: 'Topology',
|
|
365
|
+
objects: { x: { type: 'GeometryCollection', geometries: [] } },
|
|
366
|
+
arcs: [],
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
data: [],
|
|
370
|
+
encoding: {
|
|
371
|
+
key: { field: 'id', type: 'nominal' },
|
|
372
|
+
color: { field: 'value', type: 'quantitative' },
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
it('adds focus: null when not specified', () => {
|
|
377
|
+
const result = normalizeSpec(mapSpec) as NormalizedMapSpec;
|
|
378
|
+
expect(result.geo.focus).toBeNull();
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it('preserves explicit focus value', () => {
|
|
382
|
+
const specWithFocus: MapSpec = {
|
|
383
|
+
...mapSpec,
|
|
384
|
+
geo: { ...mapSpec.geo, focus: '36' },
|
|
385
|
+
};
|
|
386
|
+
const result = normalizeSpec(specWithFocus) as NormalizedMapSpec;
|
|
387
|
+
expect(result.geo.focus).toBe('36');
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
it('preserves focus: null (explicit clear)', () => {
|
|
391
|
+
const specWithNull: MapSpec = {
|
|
392
|
+
...mapSpec,
|
|
393
|
+
geo: { ...mapSpec.geo, focus: null },
|
|
394
|
+
};
|
|
395
|
+
const result = normalizeSpec(specWithNull) as NormalizedMapSpec;
|
|
396
|
+
expect(result.geo.focus).toBeNull();
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for sparkline default resolution.
|
|
3
|
+
*
|
|
4
|
+
* Focus: the trend signal must be honest about noisy series and short
|
|
5
|
+
* series. Naive `last - first` would mislabel `[100, 50, 200, 60, 101]`
|
|
6
|
+
* as up; a regression slope with deadband correctly reads it as neutral.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { MarkDef, ResolvedTheme } from '@opendata-ai/openchart-core';
|
|
10
|
+
import { describe, expect, it } from 'vitest';
|
|
11
|
+
import {
|
|
12
|
+
buildSparklineAreaGradient,
|
|
13
|
+
computeTrend,
|
|
14
|
+
computeTrendFromData,
|
|
15
|
+
hasExplicitColor,
|
|
16
|
+
trendColor,
|
|
17
|
+
} from '../sparkline-defaults';
|
|
18
|
+
|
|
19
|
+
const baseTheme = {
|
|
20
|
+
colors: {
|
|
21
|
+
categorical: ['#1b7fa3', '#aaa'],
|
|
22
|
+
positive: '#16a34a',
|
|
23
|
+
negative: '#dc2626',
|
|
24
|
+
},
|
|
25
|
+
} as unknown as ResolvedTheme;
|
|
26
|
+
|
|
27
|
+
describe('computeTrend', () => {
|
|
28
|
+
it('classifies clear up-trend as "up"', () => {
|
|
29
|
+
expect(computeTrend([1, 2, 3, 4, 5])).toBe('up');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('classifies clear down-trend as "down"', () => {
|
|
33
|
+
expect(computeTrend([5, 4, 3, 2, 1])).toBe('down');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('classifies flat series as "neutral"', () => {
|
|
37
|
+
expect(computeTrend([10, 10, 10, 10])).toBe('neutral');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('classifies noisy non-monotonic series with no meaningful net trend as "neutral"', () => {
|
|
41
|
+
// Symmetric oscillation around the mean — the regression slope works
|
|
42
|
+
// out to (almost) zero. Naive `last - first` heuristics would classify
|
|
43
|
+
// this incorrectly; the regression sees no net direction.
|
|
44
|
+
// Palindromic series — mirrors around the midpoint, so the regression
|
|
45
|
+
// slope is mathematically zero regardless of the noise amplitude.
|
|
46
|
+
expect(computeTrend([100, 105, 95, 102, 95, 105, 100])).toBe('neutral');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('classifies a real trend with noise as the net direction', () => {
|
|
50
|
+
// ~10% net rise over 20 steps with realistic chop — should still read up.
|
|
51
|
+
const series = [
|
|
52
|
+
100, 102, 99, 103, 101, 104, 103, 106, 104, 107, 105, 108, 107, 109, 108, 110, 109, 111, 110,
|
|
53
|
+
112,
|
|
54
|
+
];
|
|
55
|
+
expect(computeTrend(series)).toBe('up');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('returns "neutral" for empty series', () => {
|
|
59
|
+
expect(computeTrend([])).toBe('neutral');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('returns "neutral" for single-point series', () => {
|
|
63
|
+
expect(computeTrend([42])).toBe('neutral');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('returns "neutral" when all values are non-finite', () => {
|
|
67
|
+
expect(computeTrend([NaN, NaN, Infinity])).toBe('neutral');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('skips non-finite values and classifies remaining', () => {
|
|
71
|
+
expect(computeTrend([NaN, 1, 2, 3, 4, NaN])).toBe('up');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('treats very small relative slope as "neutral" (deadband)', () => {
|
|
75
|
+
// Slope is real but tiny relative to the mean.
|
|
76
|
+
expect(computeTrend([1000, 1000.5, 1001, 1001.2])).toBe('neutral');
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('computeTrendFromData', () => {
|
|
81
|
+
it('reads y values from data rows', () => {
|
|
82
|
+
const data = [
|
|
83
|
+
{ date: 'a', value: 1 },
|
|
84
|
+
{ date: 'b', value: 2 },
|
|
85
|
+
{ date: 'c', value: 3 },
|
|
86
|
+
];
|
|
87
|
+
expect(computeTrendFromData(data, 'value')).toBe('up');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('returns "neutral" when yField is missing', () => {
|
|
91
|
+
expect(computeTrendFromData([{ a: 1 }, { a: 2 }], undefined)).toBe('neutral');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('coerces string numbers and skips non-numeric values', () => {
|
|
95
|
+
const data = [{ v: '5' }, { v: 'not a number' }, { v: '4' }, { v: '3' }];
|
|
96
|
+
expect(computeTrendFromData(data, 'v')).toBe('down');
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('trendColor', () => {
|
|
101
|
+
it('returns positive for up', () => {
|
|
102
|
+
expect(trendColor('up', baseTheme)).toBe('#16a34a');
|
|
103
|
+
});
|
|
104
|
+
it('returns negative for down', () => {
|
|
105
|
+
expect(trendColor('down', baseTheme)).toBe('#dc2626');
|
|
106
|
+
});
|
|
107
|
+
it('returns palette[0] for neutral', () => {
|
|
108
|
+
expect(trendColor('neutral', baseTheme)).toBe('#1b7fa3');
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('hasExplicitColor', () => {
|
|
113
|
+
it('returns both false when markDef has no fill/stroke and no color encoding', () => {
|
|
114
|
+
expect(hasExplicitColor({ type: 'line' }, false)).toEqual({ fill: false, stroke: false });
|
|
115
|
+
});
|
|
116
|
+
it('returns fill=true when markDef.fill is set, leaves stroke false', () => {
|
|
117
|
+
expect(hasExplicitColor({ type: 'line', fill: '#ff00ff' } as MarkDef, false)).toEqual({
|
|
118
|
+
fill: true,
|
|
119
|
+
stroke: false,
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
it('returns stroke=true when markDef.stroke is set, leaves fill false', () => {
|
|
123
|
+
expect(hasExplicitColor({ type: 'line', stroke: '#ff00ff' } as MarkDef, false)).toEqual({
|
|
124
|
+
fill: false,
|
|
125
|
+
stroke: true,
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
it('returns both true when both markDef.fill and markDef.stroke are set', () => {
|
|
129
|
+
expect(
|
|
130
|
+
hasExplicitColor({ type: 'line', fill: '#ff00ff', stroke: '#00ffff' } as MarkDef, false),
|
|
131
|
+
).toEqual({ fill: true, stroke: true });
|
|
132
|
+
});
|
|
133
|
+
it('returns both true when encoding.color is present (color scale drives both)', () => {
|
|
134
|
+
expect(hasExplicitColor({ type: 'line' }, true)).toEqual({ fill: true, stroke: true });
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe('buildSparklineAreaGradient', () => {
|
|
139
|
+
it('builds a top-to-bottom gradient with 0.35 -> 0 opacity in the trend color', () => {
|
|
140
|
+
const grad = buildSparklineAreaGradient('#16a34a');
|
|
141
|
+
expect(grad).toEqual({
|
|
142
|
+
gradient: 'linear',
|
|
143
|
+
x1: 0,
|
|
144
|
+
y1: 0,
|
|
145
|
+
x2: 0,
|
|
146
|
+
y2: 1,
|
|
147
|
+
stops: [
|
|
148
|
+
{ offset: 0, color: '#16a34a', opacity: 0.35 },
|
|
149
|
+
{ offset: 1, color: '#16a34a', opacity: 0 },
|
|
150
|
+
],
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Levenshtein "did you mean" repair hints for misspelled field references.
|
|
3
|
+
*
|
|
4
|
+
* When a spec references a field that is not a column in the provided data,
|
|
5
|
+
* validation suggests the nearest actual column (by edit distance) so an LLM
|
|
6
|
+
* or author can self-correct a typo in one step. Short/unrelated names must
|
|
7
|
+
* NOT produce a spurious suggestion.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, expect, it } from 'vitest';
|
|
11
|
+
import { validateSpec } from '../validate';
|
|
12
|
+
|
|
13
|
+
const data = [
|
|
14
|
+
{ region: 'West', revenue: 100, quarter: '2024-Q1' },
|
|
15
|
+
{ region: 'East', revenue: 200, quarter: '2024-Q2' },
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
/** Pull the suggestion for the field-missing error on the given path. */
|
|
19
|
+
function suggestionFor(result: ReturnType<typeof validateSpec>, path: string): string {
|
|
20
|
+
const error = result.errors.find((e) => e.path === path);
|
|
21
|
+
return error?.suggestion ?? '';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('did-you-mean field suggestions', () => {
|
|
25
|
+
it('suggests the nearest column for a one-edit typo in an encoding field', () => {
|
|
26
|
+
const result = validateSpec({
|
|
27
|
+
mark: 'bar',
|
|
28
|
+
data,
|
|
29
|
+
encoding: {
|
|
30
|
+
x: { field: 'region', type: 'nominal' },
|
|
31
|
+
y: { field: 'reveune', type: 'quantitative' }, // typo of "revenue"
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
expect(result.valid).toBe(false);
|
|
35
|
+
const suggestion = suggestionFor(result, 'encoding.y.field');
|
|
36
|
+
expect(suggestion).toContain('Did you mean "revenue"?');
|
|
37
|
+
// The full available-column list is still present alongside the hint.
|
|
38
|
+
expect(suggestion).toContain('revenue');
|
|
39
|
+
expect(suggestion).toContain('region');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('does not invent a suggestion for a field with no close match', () => {
|
|
43
|
+
const result = validateSpec({
|
|
44
|
+
mark: 'bar',
|
|
45
|
+
data,
|
|
46
|
+
encoding: {
|
|
47
|
+
x: { field: 'region', type: 'nominal' },
|
|
48
|
+
y: { field: 'xyzqwerty', type: 'quantitative' },
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
expect(result.valid).toBe(false);
|
|
52
|
+
const suggestion = suggestionFor(result, 'encoding.y.field');
|
|
53
|
+
expect(suggestion).not.toContain('Did you mean');
|
|
54
|
+
// Still lists the real columns so the author can pick the right one.
|
|
55
|
+
expect(suggestion).toContain('revenue');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('suggests the nearest column for a misspelled table column key', () => {
|
|
59
|
+
const result = validateSpec({
|
|
60
|
+
type: 'table',
|
|
61
|
+
data,
|
|
62
|
+
columns: [{ key: 'quater' }], // typo of "quarter"
|
|
63
|
+
});
|
|
64
|
+
expect(result.valid).toBe(false);
|
|
65
|
+
const suggestion = suggestionFor(result, 'columns[0].key');
|
|
66
|
+
expect(suggestion).toContain('Did you mean "quarter"?');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('does not match on a short unrelated field where the edit distance exceeds the threshold', () => {
|
|
70
|
+
const shortData = [
|
|
71
|
+
{ a: 1, b: 2 },
|
|
72
|
+
{ a: 3, b: 4 },
|
|
73
|
+
];
|
|
74
|
+
const result = validateSpec({
|
|
75
|
+
mark: 'bar',
|
|
76
|
+
data: shortData,
|
|
77
|
+
encoding: {
|
|
78
|
+
x: { field: 'zzz', type: 'nominal' },
|
|
79
|
+
y: { field: 'a', type: 'quantitative' },
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
expect(result.valid).toBe(false);
|
|
83
|
+
const suggestion = suggestionFor(result, 'encoding.x.field');
|
|
84
|
+
expect(suggestion).not.toContain('Did you mean');
|
|
85
|
+
});
|
|
86
|
+
});
|