-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidate-react-page-props.test.ts
More file actions
242 lines (204 loc) · 11.3 KB
/
Copy pathvalidate-react-page-props.test.ts
File metadata and controls
242 lines (204 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect } from 'vitest';
import {
validateReactPageProps,
REACT_CHART_FIELD_UNKNOWN,
REACT_CHART_AGGREGATE_INVALID,
REACT_CHART_AXIS_UNKNOWN,
} from './validate-react-page-props.js';
const page = (source: string) => ({ pages: [{ name: 'p', kind: 'react', source }] });
describe('validateReactPageProps (ADR-0081 Phase 2)', () => {
it('passes a correct ObjectForm usage', () => {
const f = validateReactPageProps(page('function Page(){ return <ObjectForm objectName="account" mode="edit" onSuccess={()=>{}} />; }'));
expect(f).toEqual([]);
});
it('flags a missing required binding (objectName)', () => {
const f = validateReactPageProps(page('function Page(){ return <ObjectForm mode="edit" />; }'));
expect(f.some((x) => x.rule === 'react-prop-missing-required' && /objectName/.test(x.message))).toBe(true);
});
it('flags a typo of a contract prop (onSucces → onSuccess)', () => {
const f = validateReactPageProps(page('function Page(){ return <ObjectForm objectName="a" onSucces={()=>{}} />; }'));
expect(f.some((x) => x.rule === 'react-prop-typo' && /onSuccess/.test(x.message))).toBe(true);
});
it('flags ListView onRowClik typo', () => {
const f = validateReactPageProps(page('function Page(){ return <ListView objectName="a" onRowClik={()=>{}} />; }'));
expect(f.some((x) => x.rule === 'react-prop-typo' && /onRowClick/.test(x.message))).toBe(true);
});
it('does NOT flag a spread (props may come from it)', () => {
const f = validateReactPageProps(page('function Page(){ const p={objectName:"a"}; return <ObjectForm {...p} />; }'));
expect(f).toEqual([]);
});
it('ignores unknown components (author HTML / own components)', () => {
const f = validateReactPageProps(page('function Page(){ return <div className="x"><MyThing foo="bar" /></div>; }'));
expect(f).toEqual([]);
});
it('does NOT false-flag a valid non-contract prop (no near match)', () => {
const f = validateReactPageProps(page('function Page(){ return <ListView objectName="a" striped={true} />; }'));
expect(f).toEqual([]);
});
});
// ─────────────────────────────────────────────────────────────────────────
// <ObjectChart> binding integrity (#3701)
// ─────────────────────────────────────────────────────────────────────────
const invoice = {
name: 'invoice',
fields: [{ name: 'total' }, { name: 'status' }, { name: 'closed_at' }],
};
const chartPage = (source: string, objects: unknown[] = [invoice]) => ({
objects,
pages: [{ name: 'p', kind: 'react', source }],
});
const chart = (attrs: string) => `function Page(){ return <ObjectChart ${attrs} />; }`;
describe('validateReactPageProps — <ObjectChart> bindings (#3701)', () => {
it('passes an aggregate whose field and groupBy exist on the object', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }}`)),
);
expect(f).toEqual([]);
});
it('flags an aggregate.field the object does not declare', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'amount', function: 'sum', groupBy: 'status' }}`)),
);
expect(f.some((x) => x.rule === REACT_CHART_FIELD_UNKNOWN && /aggregate\.field "amount"/.test(x.message))).toBe(true);
});
it('flags an aggregate.groupBy the object does not declare', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'stage' }}`)),
);
expect(f.some((x) => x.rule === REACT_CHART_FIELD_UNKNOWN && /aggregate\.groupBy "stage"/.test(x.message))).toBe(true);
});
it('reads the field out of a structured, date-bucketed groupBy', () => {
const ok = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: { field: 'closed_at', dateGranularity: 'quarter' } }}`)),
);
expect(ok).toEqual([]);
const bad = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: { field: 'signed_at' } }}`)),
);
expect(bad.some((x) => x.rule === REACT_CHART_FIELD_UNKNOWN && /signed_at/.test(x.message))).toBe(true);
});
it('flags an aggregation function no chart renderer implements', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'median', groupBy: 'status' }}`)),
);
expect(f.some((x) => x.rule === REACT_CHART_AGGREGATE_INVALID && /median/.test(x.message))).toBe(true);
});
it('flags a non-count function with nothing to aggregate', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ function: 'sum', groupBy: 'status' }}`)),
);
expect(f.some((x) => x.rule === REACT_CHART_AGGREGATE_INVALID && /no "field"/.test(x.message))).toBe(true);
});
it('accepts a fieldless count', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ function: 'count', groupBy: 'status' }} series={[{ dataKey: 'count' }]}`)),
);
expect(f).toEqual([]);
});
it('accepts axes bound to the aggregate result columns', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} xAxisKey="status" series={[{ dataKey: 'total' }]}`)),
);
expect(f).toEqual([]);
});
it('flags a series bound to the DATASET-style measure name', () => {
// The exact bug #3684 catches on dataset charts, on the object-bound path:
// an object aggregate keys its rows `total`, never `sum_total`.
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} series={[{ dataKey: 'sum_total' }]}`)),
);
expect(f.some((x) => x.rule === REACT_CHART_AXIS_UNKNOWN && /sum_total/.test(x.message))).toBe(true);
});
it('flags an xAxisKey that is neither result column', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} xAxisKey="closed_at"`)),
);
expect(f.some((x) => x.rule === REACT_CHART_AXIS_UNKNOWN && /closed_at/.test(x.message))).toBe(true);
});
it('flags an xAxisKey bound to the VALUE column (a real column, wrong axis)', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} xAxisKey="total"`)),
);
expect(f.some((x) => x.rule === REACT_CHART_AXIS_UNKNOWN && /VALUE column/.test(x.message))).toBe(true);
});
it('accepts the comparison-overlay column', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} series={[{ dataKey: 'total' }, { dataKey: 'total__comparison' }]}`)),
);
expect(f).toEqual([]);
});
// ── the spec ChartConfig shape is the contract again (#3729) ────────────
it('accepts the spec axis shape', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" type="bar" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} xAxis={{ field: 'status' }} yAxis={[{ field: 'total' }]}`)),
);
expect(f).toEqual([]);
});
it('flags a spec yAxis[].field that is not a result column', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} yAxis={[{ field: 'sum_total' }]}`)),
);
expect(f.some((x) => x.rule === REACT_CHART_AXIS_UNKNOWN && /sum_total/.test(x.message))).toBe(true);
});
it('flags a spec series[].name that is not a result column', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} series={[{ name: 'sum_total' }]}`)),
);
const hit = f.find((x) => x.rule === REACT_CHART_AXIS_UNKNOWN);
expect(hit?.hint).toMatch(/series\[\]\.name/);
});
it('flags a spec xAxis.field bound to the VALUE column', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} xAxis={{ field: 'total' }}`)),
);
expect(f.some((x) => x.rule === REACT_CHART_AXIS_UNKNOWN && /VALUE column/.test(x.message))).toBe(true);
});
it('still accepts the internal spelling — dashboards emit it', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} xAxisKey="status" series={[{ dataKey: 'total' }]}`)),
);
expect(f).toEqual([]);
});
// ── false-positive guards ───────────────────────────────────────────────
it('skips an aggregate built from variables (not knowable at build time)', () => {
const f = validateReactPageProps(
chartPage('function Page(){ const agg = { field: f, function: "sum", groupBy: g }; return <ObjectChart objectName="invoice" aggregate={agg} />; }'),
);
expect(f).toEqual([]);
});
it('skips a shorthand property (its value is hidden)', () => {
const f = validateReactPageProps(
chartPage('function Page(){ const field = "nope"; return <ObjectChart objectName="invoice" aggregate={{ field, function: "sum", groupBy: "status" }} />; }'),
);
expect(f).toEqual([]);
});
it('skips an object declared in another package', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="sys_user" aggregate={{ field: 'nope', function: 'sum', groupBy: 'also_nope' }}`)),
);
expect(f.some((x) => x.rule === REACT_CHART_FIELD_UNKNOWN)).toBe(false);
});
it('does not flag system fields the registry injects', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" aggregate={{ function: 'count', groupBy: 'owner_id' }}`)),
);
expect(f).toEqual([]);
});
it('skips the axis check when static `data` supplies the rows', () => {
const f = validateReactPageProps(
chartPage(chart(`objectName="invoice" data={[{ x: 1 }]} aggregate={{ field: 'total', function: 'sum', groupBy: 'status' }} xAxisKey="x"`)),
);
expect(f).toEqual([]);
});
it('skips everything behind a spread', () => {
const f = validateReactPageProps(
chartPage('function Page(){ const p = {}; return <ObjectChart {...p} aggregate={{ field: "nope", function: "sum", groupBy: "status" }} />; }'),
);
expect(f).toEqual([]);
});
it('leaves a chart with no aggregate alone (dataset-bound or data-bound)', () => {
const f = validateReactPageProps(chartPage(chart(`objectName="invoice"`)));
expect(f).toEqual([]);
});
});