@opendata-ai/openchart-vanilla 7.7.0 → 7.9.0
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/{chunk-PSMDJEXK.js → chunk-QRE4FQQM.js} +380 -14
- package/dist/chunk-QRE4FQQM.js.map +1 -0
- package/dist/index.d.ts +26 -19
- package/dist/index.js +1755 -378
- package/dist/index.js.map +1 -1
- package/dist/static.d.ts +2 -2
- package/dist/static.js +37 -27
- package/dist/static.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/animation.test.ts +1 -1
- package/src/__tests__/transition.test.ts +1592 -0
- package/src/barlist-mount.ts +1 -1
- package/src/barlist-renderer.ts +4 -4
- package/src/index.ts +2 -0
- package/src/mount.ts +64 -1
- package/src/renderers/axes.ts +18 -0
- package/src/renderers/marks.ts +21 -7
- package/src/sankey-mount.ts +2 -2
- package/src/sankey-renderer.ts +9 -8
- package/src/static.ts +42 -32
- package/src/svg-renderer.ts +8 -7
- package/src/table-mount.ts +1 -1
- package/src/table-renderer.ts +5 -5
- package/src/tilemap-mount.ts +1 -1
- package/src/tilemap-renderer.ts +8 -9
- package/src/transition.ts +2473 -0
- package/dist/chunk-PSMDJEXK.js.map +0 -1
|
@@ -0,0 +1,1592 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Data-update transition tests.
|
|
3
|
+
*
|
|
4
|
+
* Tests the canTransition gate (all ten checks), mark matching logic,
|
|
5
|
+
* the rAF-driven animation loop (with manual pump), round-trip invariant,
|
|
6
|
+
* ghost element lifecycle, and cancel semantics.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ChartLayout, ChartSpec } from '@opendata-ai/openchart-core';
|
|
10
|
+
import { compileChart } from '@opendata-ai/openchart-engine';
|
|
11
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
12
|
+
import { createContainer } from '../__test-fixtures__/dom';
|
|
13
|
+
import { createChart } from '../mount';
|
|
14
|
+
import { renderChartSVG } from '../svg-renderer';
|
|
15
|
+
import { canTransition, normalizePointArrays, runTransition } from '../transition';
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Helpers
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
/** Build a column chart spec with animation enabled. */
|
|
22
|
+
function columnSpec(data: Array<{ category: string; value: number }>): ChartSpec {
|
|
23
|
+
return {
|
|
24
|
+
animation: true,
|
|
25
|
+
mark: 'bar',
|
|
26
|
+
data,
|
|
27
|
+
encoding: {
|
|
28
|
+
x: { field: 'category', type: 'nominal' },
|
|
29
|
+
y: { field: 'value', type: 'quantitative' },
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Build a stacked column chart spec with cornerRadius. */
|
|
35
|
+
function stackedColumnSpec(
|
|
36
|
+
data: Array<{ category: string; value: number; group: string }>,
|
|
37
|
+
): ChartSpec {
|
|
38
|
+
return {
|
|
39
|
+
animation: true,
|
|
40
|
+
mark: { type: 'bar', cornerRadius: 4 },
|
|
41
|
+
data,
|
|
42
|
+
encoding: {
|
|
43
|
+
x: { field: 'category', type: 'nominal' },
|
|
44
|
+
y: { field: 'value', type: 'quantitative' },
|
|
45
|
+
color: { field: 'group', type: 'nominal' },
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Compile a spec to layout. */
|
|
51
|
+
function compile(spec: ChartSpec, width = 600, height = 400): ChartLayout {
|
|
52
|
+
return compileChart(spec, { width, height });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Compile and render to SVG, returning both. */
|
|
56
|
+
function compileAndRender(spec: ChartSpec, width = 600, height = 400) {
|
|
57
|
+
const container = createContainer(width, height);
|
|
58
|
+
const layout = compile(spec, width, height);
|
|
59
|
+
const svg = renderChartSVG(layout, container);
|
|
60
|
+
return { svg: svg as SVGSVGElement, container, layout };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Base gate args that pass all checks. */
|
|
64
|
+
function passingGateArgs(prevSpec: ChartSpec, nextSpec: ChartSpec, width = 600, height = 400) {
|
|
65
|
+
return {
|
|
66
|
+
prevLayout: compile(prevSpec, width, height),
|
|
67
|
+
nextLayout: compile(nextSpec, width, height),
|
|
68
|
+
prevSpec,
|
|
69
|
+
nextSpec,
|
|
70
|
+
isFirstRender: false,
|
|
71
|
+
entranceInFlight: false,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const DATA_A = [
|
|
76
|
+
{ category: 'Q1', value: 100 },
|
|
77
|
+
{ category: 'Q2', value: 200 },
|
|
78
|
+
{ category: 'Q3', value: 150 },
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
const DATA_B = [
|
|
82
|
+
{ category: 'Q1', value: 150 },
|
|
83
|
+
{ category: 'Q2', value: 180 },
|
|
84
|
+
{ category: 'Q3', value: 220 },
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
const DATA_C = [
|
|
88
|
+
{ category: 'Q1', value: 100 },
|
|
89
|
+
{ category: 'Q2', value: 200 },
|
|
90
|
+
{ category: 'Q3', value: 150 },
|
|
91
|
+
{ category: 'Q4', value: 250 },
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
const DATA_REMOVE = [
|
|
95
|
+
{ category: 'Q1', value: 100 },
|
|
96
|
+
{ category: 'Q2', value: 200 },
|
|
97
|
+
];
|
|
98
|
+
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
// rAF mock
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
|
|
103
|
+
let rafCallbacks: Map<number, FrameRequestCallback>;
|
|
104
|
+
let nextRafId: number;
|
|
105
|
+
|
|
106
|
+
function setupRafMock() {
|
|
107
|
+
rafCallbacks = new Map();
|
|
108
|
+
nextRafId = 1;
|
|
109
|
+
|
|
110
|
+
vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback): number => {
|
|
111
|
+
const id = nextRafId++;
|
|
112
|
+
rafCallbacks.set(id, cb);
|
|
113
|
+
return id;
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
vi.stubGlobal('cancelAnimationFrame', (id: number): void => {
|
|
117
|
+
rafCallbacks.delete(id);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Pump all pending rAF callbacks at the given timestamp. */
|
|
122
|
+
function pumpRaf(timestamp: number) {
|
|
123
|
+
const cbs = Array.from(rafCallbacks.entries());
|
|
124
|
+
rafCallbacks.clear();
|
|
125
|
+
for (const [, cb] of cbs) {
|
|
126
|
+
cb(timestamp);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Run rAF loop to completion by advancing past total duration. */
|
|
131
|
+
function runToCompletion(totalMs = 2000) {
|
|
132
|
+
// First pump at t=0 to set startTime
|
|
133
|
+
pumpRaf(0);
|
|
134
|
+
// Then pump way past the end
|
|
135
|
+
pumpRaf(totalMs);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
// Cleanup
|
|
140
|
+
// ---------------------------------------------------------------------------
|
|
141
|
+
|
|
142
|
+
beforeEach(() => {
|
|
143
|
+
setupRafMock();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
afterEach(() => {
|
|
147
|
+
document.body.innerHTML = '';
|
|
148
|
+
vi.restoreAllMocks();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// ---------------------------------------------------------------------------
|
|
152
|
+
// canTransition gate tests
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
|
|
155
|
+
describe('canTransition gate', () => {
|
|
156
|
+
it('passes when all conditions are met', () => {
|
|
157
|
+
const specA = columnSpec(DATA_A);
|
|
158
|
+
const specB = columnSpec(DATA_B);
|
|
159
|
+
expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('gate 1: fails when prevLayout is null', () => {
|
|
163
|
+
const specB = columnSpec(DATA_B);
|
|
164
|
+
expect(
|
|
165
|
+
canTransition({
|
|
166
|
+
prevLayout: null,
|
|
167
|
+
nextLayout: compile(specB),
|
|
168
|
+
prevSpec: columnSpec(DATA_A),
|
|
169
|
+
nextSpec: specB,
|
|
170
|
+
isFirstRender: false,
|
|
171
|
+
entranceInFlight: false,
|
|
172
|
+
}),
|
|
173
|
+
).toBe(false);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('gate 1: fails when prevSpec is null', () => {
|
|
177
|
+
const specA = columnSpec(DATA_A);
|
|
178
|
+
const specB = columnSpec(DATA_B);
|
|
179
|
+
expect(
|
|
180
|
+
canTransition({
|
|
181
|
+
prevLayout: compile(specA),
|
|
182
|
+
nextLayout: compile(specB),
|
|
183
|
+
prevSpec: null,
|
|
184
|
+
nextSpec: specB,
|
|
185
|
+
isFirstRender: false,
|
|
186
|
+
entranceInFlight: false,
|
|
187
|
+
}),
|
|
188
|
+
).toBe(false);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('gate 1: fails on first render', () => {
|
|
192
|
+
const specA = columnSpec(DATA_A);
|
|
193
|
+
const specB = columnSpec(DATA_B);
|
|
194
|
+
const args = passingGateArgs(specA, specB);
|
|
195
|
+
args.isFirstRender = true;
|
|
196
|
+
expect(canTransition(args)).toBe(false);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('gate 2: fails when animation.update is absent', () => {
|
|
200
|
+
const specA = columnSpec(DATA_A);
|
|
201
|
+
const specB: ChartSpec = { ...columnSpec(DATA_B), animation: { update: false } };
|
|
202
|
+
const args = passingGateArgs(specA, specB);
|
|
203
|
+
expect(canTransition(args)).toBe(false);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('gate 3: fails when mark type differs', () => {
|
|
207
|
+
// Don't compile - just test the gate logic with raw spec objects
|
|
208
|
+
const specA = columnSpec(DATA_A);
|
|
209
|
+
const specB = columnSpec(DATA_B);
|
|
210
|
+
const args = passingGateArgs(specA, specB);
|
|
211
|
+
// Override nextSpec to have a different mark type
|
|
212
|
+
args.nextSpec = { ...specB, mark: 'line' };
|
|
213
|
+
expect(canTransition(args)).toBe(false);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('gate 3: fails for unsupported mark type', () => {
|
|
217
|
+
const specA = columnSpec(DATA_A);
|
|
218
|
+
const specB = columnSpec(DATA_B);
|
|
219
|
+
const args = passingGateArgs(specA, specB);
|
|
220
|
+
// Override both specs to be arc (pie) charts which are not supported
|
|
221
|
+
args.prevSpec = { ...specA, mark: 'arc' };
|
|
222
|
+
args.nextSpec = { ...specB, mark: 'arc' };
|
|
223
|
+
expect(canTransition(args)).toBe(false);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('gate 3: passes for line mark type', () => {
|
|
227
|
+
const specA = columnSpec(DATA_A);
|
|
228
|
+
const specB = columnSpec(DATA_B);
|
|
229
|
+
const args = passingGateArgs(specA, specB);
|
|
230
|
+
args.prevSpec = { ...specA, mark: 'line' };
|
|
231
|
+
args.nextSpec = { ...specB, mark: 'line' };
|
|
232
|
+
expect(canTransition(args)).toBe(true);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('gate 3: passes for area mark type', () => {
|
|
236
|
+
const specA = columnSpec(DATA_A);
|
|
237
|
+
const specB = columnSpec(DATA_B);
|
|
238
|
+
const args = passingGateArgs(specA, specB);
|
|
239
|
+
args.prevSpec = { ...specA, mark: 'area' };
|
|
240
|
+
args.nextSpec = { ...specB, mark: 'area' };
|
|
241
|
+
expect(canTransition(args)).toBe(true);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('gate 4: fails when encoding field changes', () => {
|
|
245
|
+
const specA = columnSpec(DATA_A);
|
|
246
|
+
const specB = columnSpec(DATA_B);
|
|
247
|
+
const args = passingGateArgs(specA, specB);
|
|
248
|
+
// Override nextSpec encoding to change a field
|
|
249
|
+
args.nextSpec = {
|
|
250
|
+
...specB,
|
|
251
|
+
encoding: {
|
|
252
|
+
x: { field: 'other', type: 'nominal' },
|
|
253
|
+
y: { field: 'value', type: 'quantitative' },
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
expect(canTransition(args)).toBe(false);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('gate 5: fails for sparkline display', () => {
|
|
260
|
+
const specA = columnSpec(DATA_A);
|
|
261
|
+
const specB: ChartSpec = { ...columnSpec(DATA_B), display: 'sparkline' };
|
|
262
|
+
expect(canTransition(passingGateArgs(specA, specB))).toBe(false);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('gate 6: fails when entrance is in flight', () => {
|
|
266
|
+
const specA = columnSpec(DATA_A);
|
|
267
|
+
const specB = columnSpec(DATA_B);
|
|
268
|
+
const args = passingGateArgs(specA, specB);
|
|
269
|
+
args.entranceInFlight = true;
|
|
270
|
+
expect(canTransition(args)).toBe(false);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('gate 7: fails when dimensions change', () => {
|
|
274
|
+
const specA = columnSpec(DATA_A);
|
|
275
|
+
const specB = columnSpec(DATA_B);
|
|
276
|
+
expect(
|
|
277
|
+
canTransition({
|
|
278
|
+
prevLayout: compile(specA, 600, 400),
|
|
279
|
+
nextLayout: compile(specB, 800, 400),
|
|
280
|
+
prevSpec: specA,
|
|
281
|
+
nextSpec: specB,
|
|
282
|
+
isFirstRender: false,
|
|
283
|
+
entranceInFlight: false,
|
|
284
|
+
}),
|
|
285
|
+
).toBe(false);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('gate 8: fails when mark count exceeds 500', () => {
|
|
289
|
+
// Build a spec with > 500 data points
|
|
290
|
+
const bigData = Array.from({ length: 501 }, (_, i) => ({
|
|
291
|
+
category: `cat-${i}`,
|
|
292
|
+
value: i,
|
|
293
|
+
}));
|
|
294
|
+
const specA = columnSpec(DATA_A);
|
|
295
|
+
const specB = columnSpec(bigData);
|
|
296
|
+
const args = passingGateArgs(specA, specB);
|
|
297
|
+
expect(canTransition(args)).toBe(false);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('gate 9: fails when geometry is identical (zero-delta)', () => {
|
|
301
|
+
const specA = columnSpec(DATA_A);
|
|
302
|
+
// Same data = same geometry
|
|
303
|
+
expect(canTransition(passingGateArgs(specA, specA))).toBe(false);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('gate 10: fails when prefers-reduced-motion is active', () => {
|
|
307
|
+
const original = window.matchMedia;
|
|
308
|
+
vi.stubGlobal('matchMedia', (query: string) => ({
|
|
309
|
+
matches: query === '(prefers-reduced-motion: reduce)',
|
|
310
|
+
media: query,
|
|
311
|
+
addEventListener: () => {},
|
|
312
|
+
removeEventListener: () => {},
|
|
313
|
+
}));
|
|
314
|
+
|
|
315
|
+
const specA = columnSpec(DATA_A);
|
|
316
|
+
const specB = columnSpec(DATA_B);
|
|
317
|
+
expect(canTransition(passingGateArgs(specA, specB))).toBe(false);
|
|
318
|
+
|
|
319
|
+
vi.stubGlobal('matchMedia', original);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// ---------------------------------------------------------------------------
|
|
324
|
+
// Mark matching
|
|
325
|
+
// ---------------------------------------------------------------------------
|
|
326
|
+
|
|
327
|
+
describe('mark matching', () => {
|
|
328
|
+
it('correctly identifies entered marks when adding a category', () => {
|
|
329
|
+
const specA = columnSpec(DATA_A);
|
|
330
|
+
const specB = columnSpec(DATA_C); // adds Q4
|
|
331
|
+
const layoutA = compile(specA);
|
|
332
|
+
const layoutB = compile(specB);
|
|
333
|
+
|
|
334
|
+
const prevKeys = new Set(
|
|
335
|
+
layoutA.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
|
|
336
|
+
);
|
|
337
|
+
const nextKeys = new Set(
|
|
338
|
+
layoutB.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
const entered = [...nextKeys].filter((k) => !prevKeys.has(k));
|
|
342
|
+
expect(entered.length).toBeGreaterThan(0);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('correctly identifies exited marks when removing a category', () => {
|
|
346
|
+
const specA = columnSpec(DATA_A);
|
|
347
|
+
const specB = columnSpec(DATA_REMOVE); // removes Q3
|
|
348
|
+
const layoutA = compile(specA);
|
|
349
|
+
const layoutB = compile(specB);
|
|
350
|
+
|
|
351
|
+
const prevKeys = new Set(
|
|
352
|
+
layoutA.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
|
|
353
|
+
);
|
|
354
|
+
const nextKeys = new Set(
|
|
355
|
+
layoutB.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
|
|
356
|
+
);
|
|
357
|
+
|
|
358
|
+
const exited = [...prevKeys].filter((k) => !nextKeys.has(k));
|
|
359
|
+
expect(exited.length).toBeGreaterThan(0);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it('correctly identifies updated marks for value-only changes', () => {
|
|
363
|
+
const specA = columnSpec(DATA_A);
|
|
364
|
+
const specB = columnSpec(DATA_B); // same categories, different values
|
|
365
|
+
const layoutA = compile(specA);
|
|
366
|
+
const layoutB = compile(specB);
|
|
367
|
+
|
|
368
|
+
const prevKeys = new Set(
|
|
369
|
+
layoutA.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
|
|
370
|
+
);
|
|
371
|
+
const nextKeys = new Set(
|
|
372
|
+
layoutB.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
const updated = [...prevKeys].filter((k) => nextKeys.has(k));
|
|
376
|
+
expect(updated.length).toBe(3); // Q1, Q2, Q3 all present in both
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
// ---------------------------------------------------------------------------
|
|
381
|
+
// Round-trip invariant
|
|
382
|
+
// ---------------------------------------------------------------------------
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Extract rect geometry from all .oc-mark-rect elements in an SVG,
|
|
386
|
+
* keyed by data-key. Returns a map of key -> {x, y, width, height}.
|
|
387
|
+
* This tests that the transition snaps geometry to final values matching
|
|
388
|
+
* a fresh render from the same layout.
|
|
389
|
+
*/
|
|
390
|
+
function extractRectGeometry(
|
|
391
|
+
svg: SVGElement,
|
|
392
|
+
): Map<string, { x: string; y: string; w: string; h: string }> {
|
|
393
|
+
const result = new Map<string, { x: string; y: string; w: string; h: string }>();
|
|
394
|
+
const groups = svg.querySelectorAll('.oc-mark-rect[data-key]');
|
|
395
|
+
for (const g of groups) {
|
|
396
|
+
const key = g.getAttribute('data-key')!;
|
|
397
|
+
const rect = g.querySelector('rect');
|
|
398
|
+
const path = g.querySelector('path');
|
|
399
|
+
if (rect) {
|
|
400
|
+
result.set(key, {
|
|
401
|
+
x: rect.getAttribute('x') ?? '',
|
|
402
|
+
y: rect.getAttribute('y') ?? '',
|
|
403
|
+
w: rect.getAttribute('width') ?? '',
|
|
404
|
+
h: rect.getAttribute('height') ?? '',
|
|
405
|
+
});
|
|
406
|
+
} else if (path) {
|
|
407
|
+
// For path-based rects (cornerRadiusSides), store the d attribute
|
|
408
|
+
result.set(key, {
|
|
409
|
+
x: 'path',
|
|
410
|
+
y: 'path',
|
|
411
|
+
w: 'path',
|
|
412
|
+
h: path.getAttribute('d') ?? '',
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return result;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Run a round-trip test: render specB's layout, run transition from layoutA,
|
|
421
|
+
* pump to completion, then compare rect geometry against a fresh render of specB.
|
|
422
|
+
*
|
|
423
|
+
* The transition runs on an SVG already rendered from nextLayout (as mount.ts
|
|
424
|
+
* does), so after completion the geometry should match a fresh render exactly.
|
|
425
|
+
*/
|
|
426
|
+
function assertRoundTrip(specA: ChartSpec, specB: ChartSpec) {
|
|
427
|
+
const layoutA = compile(specA);
|
|
428
|
+
const layoutB = compile(specB);
|
|
429
|
+
|
|
430
|
+
// Render from specB's layout (as mount.ts render() does)
|
|
431
|
+
const container = createContainer();
|
|
432
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
433
|
+
|
|
434
|
+
runTransition({
|
|
435
|
+
svg,
|
|
436
|
+
prevLayout: layoutA,
|
|
437
|
+
nextLayout: layoutB,
|
|
438
|
+
animation: layoutB.animation!,
|
|
439
|
+
onComplete: () => {},
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
runToCompletion();
|
|
443
|
+
|
|
444
|
+
// Extract geometry after transition
|
|
445
|
+
const transitioned = extractRectGeometry(svg);
|
|
446
|
+
|
|
447
|
+
// Fresh render of specB for comparison
|
|
448
|
+
const { svg: freshSvg } = compileAndRender(specB);
|
|
449
|
+
const fresh = extractRectGeometry(freshSvg);
|
|
450
|
+
|
|
451
|
+
// Same set of keys
|
|
452
|
+
expect([...transitioned.keys()].sort()).toEqual([...fresh.keys()].sort());
|
|
453
|
+
|
|
454
|
+
// Same geometry per key
|
|
455
|
+
for (const [key, tGeom] of transitioned) {
|
|
456
|
+
const fGeom = fresh.get(key);
|
|
457
|
+
expect(fGeom).toBeDefined();
|
|
458
|
+
expect(tGeom).toEqual(fGeom);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// No ghost elements remain
|
|
462
|
+
expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
describe('round-trip invariant', () => {
|
|
466
|
+
it('value-only change: geometry matches fresh render', () => {
|
|
467
|
+
assertRoundTrip(columnSpec(DATA_A), columnSpec(DATA_B));
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('add category: geometry matches fresh render', () => {
|
|
471
|
+
assertRoundTrip(columnSpec(DATA_A), columnSpec(DATA_C));
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
it('remove category: geometry matches fresh render', () => {
|
|
475
|
+
assertRoundTrip(columnSpec(DATA_A), columnSpec(DATA_REMOVE));
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
it('stacked column with cornerRadius: geometry matches fresh render', () => {
|
|
479
|
+
assertRoundTrip(
|
|
480
|
+
stackedColumnSpec([
|
|
481
|
+
{ category: 'Q1', value: 100, group: 'A' },
|
|
482
|
+
{ category: 'Q1', value: 50, group: 'B' },
|
|
483
|
+
{ category: 'Q2', value: 200, group: 'A' },
|
|
484
|
+
{ category: 'Q2', value: 80, group: 'B' },
|
|
485
|
+
]),
|
|
486
|
+
stackedColumnSpec([
|
|
487
|
+
{ category: 'Q1', value: 150, group: 'A' },
|
|
488
|
+
{ category: 'Q1', value: 70, group: 'B' },
|
|
489
|
+
{ category: 'Q2', value: 180, group: 'A' },
|
|
490
|
+
{ category: 'Q2', value: 120, group: 'B' },
|
|
491
|
+
]),
|
|
492
|
+
);
|
|
493
|
+
});
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
// ---------------------------------------------------------------------------
|
|
497
|
+
// cancel() behavior
|
|
498
|
+
// ---------------------------------------------------------------------------
|
|
499
|
+
|
|
500
|
+
describe('cancel()', () => {
|
|
501
|
+
it('snaps to final values and removes ghosts', () => {
|
|
502
|
+
const specA = columnSpec(DATA_A);
|
|
503
|
+
const specB = columnSpec(DATA_REMOVE); // Q3 exits
|
|
504
|
+
|
|
505
|
+
const { svg, layout: layoutA } = compileAndRender(specA);
|
|
506
|
+
const layoutB = compile(specB);
|
|
507
|
+
|
|
508
|
+
let completed = false;
|
|
509
|
+
const handle = runTransition({
|
|
510
|
+
svg,
|
|
511
|
+
prevLayout: layoutA,
|
|
512
|
+
nextLayout: layoutB,
|
|
513
|
+
animation: layoutB.animation!,
|
|
514
|
+
onComplete: () => {
|
|
515
|
+
completed = true;
|
|
516
|
+
},
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
// Pump once to start
|
|
520
|
+
pumpRaf(0);
|
|
521
|
+
// Verify ghost exists mid-transition
|
|
522
|
+
expect(svg.querySelectorAll('.oc-ghost').length).toBeGreaterThan(0);
|
|
523
|
+
|
|
524
|
+
// Cancel
|
|
525
|
+
handle.cancel();
|
|
526
|
+
|
|
527
|
+
// Ghosts removed
|
|
528
|
+
expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
|
|
529
|
+
// onComplete NOT called
|
|
530
|
+
expect(completed).toBe(false);
|
|
531
|
+
// No longer running
|
|
532
|
+
expect(handle.running).toBe(false);
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
// ---------------------------------------------------------------------------
|
|
537
|
+
// Ghost element attributes
|
|
538
|
+
// ---------------------------------------------------------------------------
|
|
539
|
+
|
|
540
|
+
describe('ghost elements', () => {
|
|
541
|
+
it('ghosts have aria-hidden, pointer-events: none, and no data-key', () => {
|
|
542
|
+
const specA = columnSpec(DATA_A);
|
|
543
|
+
const specB = columnSpec(DATA_REMOVE); // Q3 exits
|
|
544
|
+
|
|
545
|
+
const { svg, layout: layoutA } = compileAndRender(specA);
|
|
546
|
+
const layoutB = compile(specB);
|
|
547
|
+
|
|
548
|
+
runTransition({
|
|
549
|
+
svg,
|
|
550
|
+
prevLayout: layoutA,
|
|
551
|
+
nextLayout: layoutB,
|
|
552
|
+
animation: layoutB.animation!,
|
|
553
|
+
onComplete: () => {},
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
// Pump once to start (ghosts are added before first rAF)
|
|
557
|
+
const ghosts = svg.querySelectorAll('.oc-ghost');
|
|
558
|
+
expect(ghosts.length).toBeGreaterThan(0);
|
|
559
|
+
for (const ghost of ghosts) {
|
|
560
|
+
expect(ghost.getAttribute('aria-hidden')).toBe('true');
|
|
561
|
+
expect(ghost.getAttribute('pointer-events')).toBe('none');
|
|
562
|
+
expect(ghost.hasAttribute('data-key')).toBe(false);
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
// ---------------------------------------------------------------------------
|
|
568
|
+
// Legend-toggle mid-transition
|
|
569
|
+
// ---------------------------------------------------------------------------
|
|
570
|
+
|
|
571
|
+
describe('legend toggle mid-transition', () => {
|
|
572
|
+
it('render() during transition cancels transition and stops attribute writes', () => {
|
|
573
|
+
const specA = columnSpec(DATA_A);
|
|
574
|
+
const specB = columnSpec(DATA_B);
|
|
575
|
+
|
|
576
|
+
const container = createContainer();
|
|
577
|
+
const chart = createChart(container, specA);
|
|
578
|
+
|
|
579
|
+
// Update to trigger transition
|
|
580
|
+
chart.update(specB);
|
|
581
|
+
|
|
582
|
+
// Pump one frame
|
|
583
|
+
pumpRaf(0);
|
|
584
|
+
|
|
585
|
+
// Force a re-render (simulating legend toggle) should cancel the transition
|
|
586
|
+
// by calling render() internally, which cancels transitionHandle
|
|
587
|
+
chart.resize();
|
|
588
|
+
|
|
589
|
+
// After resize, pumping more frames should have no effect
|
|
590
|
+
// (the transition was cancelled)
|
|
591
|
+
pumpRaf(100);
|
|
592
|
+
// No crash = success (the transition's rAF was cancelled)
|
|
593
|
+
|
|
594
|
+
chart.destroy();
|
|
595
|
+
});
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
// ---------------------------------------------------------------------------
|
|
599
|
+
// isDragging blocks transitions
|
|
600
|
+
// ---------------------------------------------------------------------------
|
|
601
|
+
|
|
602
|
+
describe('update during isDragging', () => {
|
|
603
|
+
it('does not start a transition when dragging is active', () => {
|
|
604
|
+
// We can't directly set isDragging from outside, but we can verify
|
|
605
|
+
// that calling render() with pendingRender produces no transition.
|
|
606
|
+
// This is implicitly tested by the mount lifecycle.
|
|
607
|
+
const specA = columnSpec(DATA_A);
|
|
608
|
+
const specB = columnSpec(DATA_B);
|
|
609
|
+
const container = createContainer();
|
|
610
|
+
const chart = createChart(container, specA);
|
|
611
|
+
|
|
612
|
+
// Normal update should work without error
|
|
613
|
+
chart.update(specB);
|
|
614
|
+
runToCompletion();
|
|
615
|
+
|
|
616
|
+
chart.destroy();
|
|
617
|
+
});
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
// ---------------------------------------------------------------------------
|
|
621
|
+
// data-key stamping
|
|
622
|
+
// ---------------------------------------------------------------------------
|
|
623
|
+
|
|
624
|
+
describe('data-key stamping', () => {
|
|
625
|
+
it('rect marks have data-key attributes when keys are present', () => {
|
|
626
|
+
const spec = columnSpec(DATA_A);
|
|
627
|
+
const { svg } = compileAndRender(spec);
|
|
628
|
+
|
|
629
|
+
const rectGroups = svg.querySelectorAll('.oc-mark-rect');
|
|
630
|
+
let hasKeys = false;
|
|
631
|
+
for (const g of rectGroups) {
|
|
632
|
+
if (g.hasAttribute('data-key')) {
|
|
633
|
+
hasKeys = true;
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
expect(hasKeys).toBe(true);
|
|
638
|
+
});
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
// ---------------------------------------------------------------------------
|
|
642
|
+
// Line/area morph: normalizePointArrays
|
|
643
|
+
// ---------------------------------------------------------------------------
|
|
644
|
+
|
|
645
|
+
describe('normalizePointArrays', () => {
|
|
646
|
+
it('append: inserted point from equals prev tail position', () => {
|
|
647
|
+
const prevPts = [
|
|
648
|
+
{ x: 0, y: 100 },
|
|
649
|
+
{ x: 50, y: 80 },
|
|
650
|
+
{ x: 100, y: 60 },
|
|
651
|
+
];
|
|
652
|
+
const nextPts = [
|
|
653
|
+
{ x: 0, y: 100 },
|
|
654
|
+
{ x: 50, y: 80 },
|
|
655
|
+
{ x: 100, y: 60 },
|
|
656
|
+
{ x: 150, y: 40 },
|
|
657
|
+
];
|
|
658
|
+
const prevKeys = ['a', 'b', 'c'];
|
|
659
|
+
const nextKeys = ['a', 'b', 'c', 'd'];
|
|
660
|
+
|
|
661
|
+
const [fromPts, toPts] = normalizePointArrays(prevPts, nextPts, prevKeys, nextKeys);
|
|
662
|
+
|
|
663
|
+
// Both arrays should have 4 points
|
|
664
|
+
expect(fromPts.length).toBe(4);
|
|
665
|
+
expect(toPts.length).toBe(4);
|
|
666
|
+
|
|
667
|
+
// The inserted point (d) should start from the prev tail (c's prev position)
|
|
668
|
+
// because it's at the tail end with only one neighbor before it
|
|
669
|
+
expect(fromPts[3].x).toBe(100);
|
|
670
|
+
expect(fromPts[3].y).toBe(60);
|
|
671
|
+
|
|
672
|
+
// The "to" for the inserted point should be its next position
|
|
673
|
+
expect(toPts[3].x).toBe(150);
|
|
674
|
+
expect(toPts[3].y).toBe(40);
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
it('remove middle: removed point to sits between surviving neighbors', () => {
|
|
678
|
+
const prevPts = [
|
|
679
|
+
{ x: 0, y: 100 },
|
|
680
|
+
{ x: 50, y: 80 },
|
|
681
|
+
{ x: 100, y: 60 },
|
|
682
|
+
];
|
|
683
|
+
const nextPts = [
|
|
684
|
+
{ x: 0, y: 90 },
|
|
685
|
+
{ x: 100, y: 50 },
|
|
686
|
+
];
|
|
687
|
+
const prevKeys = ['a', 'b', 'c'];
|
|
688
|
+
const nextKeys = ['a', 'c'];
|
|
689
|
+
|
|
690
|
+
const [fromPts, toPts] = normalizePointArrays(prevPts, nextPts, prevKeys, nextKeys);
|
|
691
|
+
|
|
692
|
+
expect(fromPts.length).toBe(3);
|
|
693
|
+
expect(toPts.length).toBe(3);
|
|
694
|
+
|
|
695
|
+
// Removed point 'b' (index 1 in merged) should collapse to midpoint
|
|
696
|
+
// between neighbors 'a' (next: {0,90}) and 'c' (next: {100,50})
|
|
697
|
+
// t = (50 - 0)/(100 - 0) = 0.5 in prev x, so in next space:
|
|
698
|
+
// lerp({0,90}, {100,50}, 0.5) = {50, 70}
|
|
699
|
+
expect(toPts[1].x).toBe(50);
|
|
700
|
+
expect(toPts[1].y).toBe(70);
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
it('full replacement (zero survivors): returns arrays as-is for crossfade', () => {
|
|
704
|
+
const prevPts = [
|
|
705
|
+
{ x: 0, y: 100 },
|
|
706
|
+
{ x: 50, y: 80 },
|
|
707
|
+
];
|
|
708
|
+
const nextPts = [
|
|
709
|
+
{ x: 10, y: 90 },
|
|
710
|
+
{ x: 60, y: 70 },
|
|
711
|
+
{ x: 110, y: 50 },
|
|
712
|
+
];
|
|
713
|
+
const prevKeys = ['a', 'b'];
|
|
714
|
+
const nextKeys = ['x', 'y', 'z'];
|
|
715
|
+
|
|
716
|
+
const [fromPts, toPts] = normalizePointArrays(prevPts, nextPts, prevKeys, nextKeys);
|
|
717
|
+
|
|
718
|
+
// Zero survivors: arrays returned as-is
|
|
719
|
+
expect(fromPts).toEqual(prevPts);
|
|
720
|
+
expect(toPts).toEqual(nextPts);
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
it('value-only change: same keys produce 1:1 mapping', () => {
|
|
724
|
+
const prevPts = [
|
|
725
|
+
{ x: 0, y: 100 },
|
|
726
|
+
{ x: 50, y: 80 },
|
|
727
|
+
{ x: 100, y: 60 },
|
|
728
|
+
];
|
|
729
|
+
const nextPts = [
|
|
730
|
+
{ x: 0, y: 90 },
|
|
731
|
+
{ x: 50, y: 70 },
|
|
732
|
+
{ x: 100, y: 50 },
|
|
733
|
+
];
|
|
734
|
+
const keys = ['a', 'b', 'c'];
|
|
735
|
+
|
|
736
|
+
const [fromPts, toPts] = normalizePointArrays(prevPts, nextPts, keys, keys);
|
|
737
|
+
|
|
738
|
+
expect(fromPts).toEqual(prevPts);
|
|
739
|
+
expect(toPts).toEqual(nextPts);
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
it('insert at head: from position equals nearest surviving endpoint', () => {
|
|
743
|
+
const prevPts = [
|
|
744
|
+
{ x: 50, y: 80 },
|
|
745
|
+
{ x: 100, y: 60 },
|
|
746
|
+
];
|
|
747
|
+
const nextPts = [
|
|
748
|
+
{ x: 0, y: 100 },
|
|
749
|
+
{ x: 50, y: 80 },
|
|
750
|
+
{ x: 100, y: 60 },
|
|
751
|
+
];
|
|
752
|
+
const prevKeys = ['b', 'c'];
|
|
753
|
+
const nextKeys = ['a', 'b', 'c'];
|
|
754
|
+
|
|
755
|
+
const [fromPts] = normalizePointArrays(prevPts, nextPts, prevKeys, nextKeys);
|
|
756
|
+
|
|
757
|
+
expect(fromPts.length).toBe(3);
|
|
758
|
+
// Inserted at head: from = nearest surviving start = b's prev position
|
|
759
|
+
expect(fromPts[0].x).toBe(50);
|
|
760
|
+
expect(fromPts[0].y).toBe(80);
|
|
761
|
+
});
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
// ---------------------------------------------------------------------------
|
|
765
|
+
// Line chart round-trip invariant
|
|
766
|
+
// ---------------------------------------------------------------------------
|
|
767
|
+
|
|
768
|
+
/** Build a line chart spec with animation enabled. */
|
|
769
|
+
function lineSpec(data: Array<{ month: string; value: number }>): ChartSpec {
|
|
770
|
+
return {
|
|
771
|
+
animation: true,
|
|
772
|
+
mark: 'line',
|
|
773
|
+
data,
|
|
774
|
+
encoding: {
|
|
775
|
+
x: { field: 'month', type: 'ordinal' },
|
|
776
|
+
y: { field: 'value', type: 'quantitative' },
|
|
777
|
+
},
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/** Build an area chart spec with animation enabled. */
|
|
782
|
+
function areaSpec(data: Array<{ month: string; value: number }>): ChartSpec {
|
|
783
|
+
return {
|
|
784
|
+
animation: true,
|
|
785
|
+
mark: 'area',
|
|
786
|
+
data,
|
|
787
|
+
encoding: {
|
|
788
|
+
x: { field: 'month', type: 'ordinal' },
|
|
789
|
+
y: { field: 'value', type: 'quantitative' },
|
|
790
|
+
},
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
const LINE_DATA_A = [
|
|
795
|
+
{ month: 'Jan', value: 100 },
|
|
796
|
+
{ month: 'Feb', value: 200 },
|
|
797
|
+
{ month: 'Mar', value: 150 },
|
|
798
|
+
];
|
|
799
|
+
|
|
800
|
+
const LINE_DATA_B = [
|
|
801
|
+
{ month: 'Jan', value: 150 },
|
|
802
|
+
{ month: 'Feb', value: 180 },
|
|
803
|
+
{ month: 'Mar', value: 220 },
|
|
804
|
+
];
|
|
805
|
+
|
|
806
|
+
const LINE_DATA_APPEND = [
|
|
807
|
+
{ month: 'Jan', value: 100 },
|
|
808
|
+
{ month: 'Feb', value: 200 },
|
|
809
|
+
{ month: 'Mar', value: 150 },
|
|
810
|
+
{ month: 'Apr', value: 250 },
|
|
811
|
+
];
|
|
812
|
+
|
|
813
|
+
const LINE_DATA_REMOVE = [
|
|
814
|
+
{ month: 'Jan', value: 100 },
|
|
815
|
+
{ month: 'Feb', value: 200 },
|
|
816
|
+
];
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* Extract path d attributes from line/area mark elements.
|
|
820
|
+
*/
|
|
821
|
+
function extractPathD(svg: SVGElement, markClass: string): Map<string, string> {
|
|
822
|
+
const result = new Map<string, string>();
|
|
823
|
+
const groups = svg.querySelectorAll(`.${markClass}[data-key]`);
|
|
824
|
+
for (const g of groups) {
|
|
825
|
+
const key = g.getAttribute('data-key')!;
|
|
826
|
+
const path = g.querySelector('path');
|
|
827
|
+
if (path) {
|
|
828
|
+
result.set(key, path.getAttribute('d') ?? '');
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
return result;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Run a round-trip test for line/area charts.
|
|
836
|
+
*/
|
|
837
|
+
function assertLineAreaRoundTrip(specA: ChartSpec, specB: ChartSpec, markClass: string) {
|
|
838
|
+
const layoutA = compile(specA);
|
|
839
|
+
const layoutB = compile(specB);
|
|
840
|
+
|
|
841
|
+
const container = createContainer();
|
|
842
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
843
|
+
|
|
844
|
+
runTransition({
|
|
845
|
+
svg,
|
|
846
|
+
prevLayout: layoutA,
|
|
847
|
+
nextLayout: layoutB,
|
|
848
|
+
animation: layoutB.animation!,
|
|
849
|
+
onComplete: () => {},
|
|
850
|
+
});
|
|
851
|
+
|
|
852
|
+
runToCompletion();
|
|
853
|
+
|
|
854
|
+
// Extract paths after transition
|
|
855
|
+
const transitioned = extractPathD(svg, markClass);
|
|
856
|
+
|
|
857
|
+
// Fresh render for comparison
|
|
858
|
+
const { svg: freshSvg } = compileAndRender(specB);
|
|
859
|
+
const fresh = extractPathD(freshSvg, markClass);
|
|
860
|
+
|
|
861
|
+
// Same set of keys
|
|
862
|
+
expect([...transitioned.keys()].sort()).toEqual([...fresh.keys()].sort());
|
|
863
|
+
|
|
864
|
+
// Same path d per key (round-trip invariant)
|
|
865
|
+
for (const [key, tPath] of transitioned) {
|
|
866
|
+
const fPath = fresh.get(key);
|
|
867
|
+
expect(fPath).toBeDefined();
|
|
868
|
+
expect(tPath).toBe(fPath);
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// No ghost elements remain
|
|
872
|
+
expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
describe('line chart round-trip invariant', () => {
|
|
876
|
+
it('value-only change: path matches fresh render', () => {
|
|
877
|
+
assertLineAreaRoundTrip(lineSpec(LINE_DATA_A), lineSpec(LINE_DATA_B), 'oc-mark-line');
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
it('append point: path matches fresh render', () => {
|
|
881
|
+
assertLineAreaRoundTrip(lineSpec(LINE_DATA_A), lineSpec(LINE_DATA_APPEND), 'oc-mark-line');
|
|
882
|
+
});
|
|
883
|
+
|
|
884
|
+
it('remove point: path matches fresh render', () => {
|
|
885
|
+
assertLineAreaRoundTrip(lineSpec(LINE_DATA_A), lineSpec(LINE_DATA_REMOVE), 'oc-mark-line');
|
|
886
|
+
});
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
describe('area chart round-trip invariant', () => {
|
|
890
|
+
it('value-only change: path matches fresh render', () => {
|
|
891
|
+
assertLineAreaRoundTrip(areaSpec(LINE_DATA_A), areaSpec(LINE_DATA_B), 'oc-mark-area');
|
|
892
|
+
});
|
|
893
|
+
|
|
894
|
+
it('append point: path matches fresh render', () => {
|
|
895
|
+
assertLineAreaRoundTrip(areaSpec(LINE_DATA_A), areaSpec(LINE_DATA_APPEND), 'oc-mark-area');
|
|
896
|
+
});
|
|
897
|
+
|
|
898
|
+
it('remove point: path matches fresh render', () => {
|
|
899
|
+
assertLineAreaRoundTrip(areaSpec(LINE_DATA_A), areaSpec(LINE_DATA_REMOVE), 'oc-mark-area');
|
|
900
|
+
});
|
|
901
|
+
});
|
|
902
|
+
|
|
903
|
+
// ---------------------------------------------------------------------------
|
|
904
|
+
// Line/area canTransition gate integration
|
|
905
|
+
// ---------------------------------------------------------------------------
|
|
906
|
+
|
|
907
|
+
describe('canTransition for line/area', () => {
|
|
908
|
+
it('passes for line chart with different values', () => {
|
|
909
|
+
const specA = lineSpec(LINE_DATA_A);
|
|
910
|
+
const specB = lineSpec(LINE_DATA_B);
|
|
911
|
+
expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
|
|
912
|
+
});
|
|
913
|
+
|
|
914
|
+
it('passes for area chart with different values', () => {
|
|
915
|
+
const specA = areaSpec(LINE_DATA_A);
|
|
916
|
+
const specB = areaSpec(LINE_DATA_B);
|
|
917
|
+
expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
it('fails for line to area mark type change', () => {
|
|
921
|
+
const specA = lineSpec(LINE_DATA_A);
|
|
922
|
+
const specB = areaSpec(LINE_DATA_A); // same data, different mark
|
|
923
|
+
// Can't call passingGateArgs because the mark types differ and compilation
|
|
924
|
+
// produces different layouts. Test the gate logic directly.
|
|
925
|
+
expect(
|
|
926
|
+
canTransition({
|
|
927
|
+
prevLayout: compile(specA),
|
|
928
|
+
nextLayout: compile(specB),
|
|
929
|
+
prevSpec: specA,
|
|
930
|
+
nextSpec: specB,
|
|
931
|
+
isFirstRender: false,
|
|
932
|
+
entranceInFlight: false,
|
|
933
|
+
}),
|
|
934
|
+
).toBe(false);
|
|
935
|
+
});
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
// ---------------------------------------------------------------------------
|
|
939
|
+
// Suppressed-point opacity
|
|
940
|
+
// ---------------------------------------------------------------------------
|
|
941
|
+
|
|
942
|
+
describe('suppressed-point opacity', () => {
|
|
943
|
+
it('point with opacity="0" retains opacity="0" after transition', () => {
|
|
944
|
+
// Create a line chart spec that produces point marks
|
|
945
|
+
const specWithPoints: ChartSpec = {
|
|
946
|
+
animation: true,
|
|
947
|
+
mark: { type: 'line', point: true },
|
|
948
|
+
data: LINE_DATA_A,
|
|
949
|
+
encoding: {
|
|
950
|
+
x: { field: 'month', type: 'ordinal' },
|
|
951
|
+
y: { field: 'value', type: 'quantitative' },
|
|
952
|
+
},
|
|
953
|
+
};
|
|
954
|
+
|
|
955
|
+
const layoutA = compile(specWithPoints);
|
|
956
|
+
const layoutB = compile({
|
|
957
|
+
...specWithPoints,
|
|
958
|
+
data: LINE_DATA_B,
|
|
959
|
+
});
|
|
960
|
+
|
|
961
|
+
const container = createContainer();
|
|
962
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
963
|
+
|
|
964
|
+
// Find all point marks and manually set one to opacity="0"
|
|
965
|
+
// (simulating endpoint-marker suppression)
|
|
966
|
+
const points = svg.querySelectorAll('circle.oc-mark-point[data-key]');
|
|
967
|
+
if (points.length > 0) {
|
|
968
|
+
points[0].setAttribute('opacity', '0');
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
runTransition({
|
|
972
|
+
svg,
|
|
973
|
+
prevLayout: layoutA,
|
|
974
|
+
nextLayout: layoutB,
|
|
975
|
+
animation: layoutB.animation!,
|
|
976
|
+
onComplete: () => {},
|
|
977
|
+
});
|
|
978
|
+
|
|
979
|
+
runToCompletion();
|
|
980
|
+
|
|
981
|
+
// The suppressed point should still have opacity="0"
|
|
982
|
+
if (points.length > 0) {
|
|
983
|
+
expect(points[0].getAttribute('opacity')).toBe('0');
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
// ---------------------------------------------------------------------------
|
|
989
|
+
// Scatter/dot (point) chart transitions
|
|
990
|
+
// ---------------------------------------------------------------------------
|
|
991
|
+
|
|
992
|
+
/** Build a scatter chart spec with animation enabled. */
|
|
993
|
+
function scatterSpec(
|
|
994
|
+
data: Array<{ x: number; y: number; size?: number }>,
|
|
995
|
+
hasSize = false,
|
|
996
|
+
): ChartSpec {
|
|
997
|
+
const encoding: ChartSpec['encoding'] = {
|
|
998
|
+
x: { field: 'x', type: 'quantitative' },
|
|
999
|
+
y: { field: 'y', type: 'quantitative' },
|
|
1000
|
+
};
|
|
1001
|
+
if (hasSize) {
|
|
1002
|
+
encoding.size = { field: 'size', type: 'quantitative' };
|
|
1003
|
+
}
|
|
1004
|
+
return {
|
|
1005
|
+
animation: true,
|
|
1006
|
+
mark: 'point',
|
|
1007
|
+
data,
|
|
1008
|
+
encoding,
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
describe('scatter chart transitions', () => {
|
|
1013
|
+
it('canTransition passes for point mark type', () => {
|
|
1014
|
+
// Need 3+ points with different values so geometry actually changes
|
|
1015
|
+
// (2 points always map to domain extremes producing identical pixel positions)
|
|
1016
|
+
const specA = scatterSpec([
|
|
1017
|
+
{ x: 10, y: 20 },
|
|
1018
|
+
{ x: 30, y: 40 },
|
|
1019
|
+
{ x: 50, y: 60 },
|
|
1020
|
+
]);
|
|
1021
|
+
const specB = scatterSpec([
|
|
1022
|
+
{ x: 10, y: 50 },
|
|
1023
|
+
{ x: 30, y: 30 },
|
|
1024
|
+
{ x: 50, y: 70 },
|
|
1025
|
+
]);
|
|
1026
|
+
expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
|
|
1027
|
+
});
|
|
1028
|
+
|
|
1029
|
+
it('y-swap between same-x points tweens without identity swap (encoding.key)', () => {
|
|
1030
|
+
// Two points at the same x, different y. After update, y values swap.
|
|
1031
|
+
// With proper keying, each point should tween to its new y, not swap identity.
|
|
1032
|
+
const specA: ChartSpec = {
|
|
1033
|
+
animation: true,
|
|
1034
|
+
mark: 'point',
|
|
1035
|
+
data: [
|
|
1036
|
+
{ id: 'a', x: 50, y: 20 },
|
|
1037
|
+
{ id: 'b', x: 50, y: 80 },
|
|
1038
|
+
],
|
|
1039
|
+
encoding: {
|
|
1040
|
+
x: { field: 'x', type: 'quantitative' },
|
|
1041
|
+
y: { field: 'y', type: 'quantitative' },
|
|
1042
|
+
key: { field: 'id' },
|
|
1043
|
+
},
|
|
1044
|
+
};
|
|
1045
|
+
const specB: ChartSpec = {
|
|
1046
|
+
...specA,
|
|
1047
|
+
data: [
|
|
1048
|
+
{ id: 'a', x: 50, y: 80 },
|
|
1049
|
+
{ id: 'b', x: 50, y: 20 },
|
|
1050
|
+
],
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
const layoutA = compile(specA);
|
|
1054
|
+
const layoutB = compile(specB);
|
|
1055
|
+
|
|
1056
|
+
// Verify marks have keys and they match across layouts
|
|
1057
|
+
const pointsA = layoutA.marks.filter((m) => m.type === 'point');
|
|
1058
|
+
const pointsB = layoutB.marks.filter((m) => m.type === 'point');
|
|
1059
|
+
expect(pointsA.length).toBe(2);
|
|
1060
|
+
expect(pointsB.length).toBe(2);
|
|
1061
|
+
|
|
1062
|
+
// Keys should match: point 'a' in both, point 'b' in both
|
|
1063
|
+
const keysA = new Set(pointsA.map((m) => m.key));
|
|
1064
|
+
const keysB = new Set(pointsB.map((m) => m.key));
|
|
1065
|
+
expect(keysA).toEqual(keysB);
|
|
1066
|
+
});
|
|
1067
|
+
|
|
1068
|
+
it('bubble r tween lands exactly on final value', () => {
|
|
1069
|
+
const specA = scatterSpec(
|
|
1070
|
+
[
|
|
1071
|
+
{ x: 10, y: 20, size: 5 },
|
|
1072
|
+
{ x: 30, y: 40, size: 10 },
|
|
1073
|
+
],
|
|
1074
|
+
true,
|
|
1075
|
+
);
|
|
1076
|
+
const specB = scatterSpec(
|
|
1077
|
+
[
|
|
1078
|
+
{ x: 10, y: 20, size: 15 },
|
|
1079
|
+
{ x: 30, y: 40, size: 20 },
|
|
1080
|
+
],
|
|
1081
|
+
true,
|
|
1082
|
+
);
|
|
1083
|
+
|
|
1084
|
+
const layoutA = compile(specA);
|
|
1085
|
+
const layoutB = compile(specB);
|
|
1086
|
+
|
|
1087
|
+
const container = createContainer();
|
|
1088
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
1089
|
+
|
|
1090
|
+
runTransition({
|
|
1091
|
+
svg,
|
|
1092
|
+
prevLayout: layoutA,
|
|
1093
|
+
nextLayout: layoutB,
|
|
1094
|
+
animation: layoutB.animation!,
|
|
1095
|
+
onComplete: () => {},
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
runToCompletion();
|
|
1099
|
+
|
|
1100
|
+
// After transition completes, check that the r values match the final layout
|
|
1101
|
+
const pointMarks = layoutB.marks.filter((m) => m.type === 'point');
|
|
1102
|
+
for (const mark of pointMarks) {
|
|
1103
|
+
if (!mark.key) continue;
|
|
1104
|
+
const el = svg.querySelector(
|
|
1105
|
+
`circle.oc-mark-point[data-key="${mark.key}"]`,
|
|
1106
|
+
) as SVGElement | null;
|
|
1107
|
+
if (!el) continue;
|
|
1108
|
+
const rAttr = el.getAttribute('r');
|
|
1109
|
+
expect(rAttr).toBe(String((mark as { r: number }).r));
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
|
|
1113
|
+
it('scatter round-trip: add/remove', () => {
|
|
1114
|
+
const specA = scatterSpec([
|
|
1115
|
+
{ x: 10, y: 20 },
|
|
1116
|
+
{ x: 30, y: 40 },
|
|
1117
|
+
{ x: 50, y: 60 },
|
|
1118
|
+
]);
|
|
1119
|
+
const specB = scatterSpec([
|
|
1120
|
+
{ x: 10, y: 50 },
|
|
1121
|
+
{ x: 30, y: 30 },
|
|
1122
|
+
{ x: 50, y: 70 },
|
|
1123
|
+
{ x: 70, y: 90 },
|
|
1124
|
+
]);
|
|
1125
|
+
|
|
1126
|
+
const layoutA = compile(specA);
|
|
1127
|
+
const layoutB = compile(specB);
|
|
1128
|
+
|
|
1129
|
+
const container = createContainer();
|
|
1130
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
1131
|
+
|
|
1132
|
+
runTransition({
|
|
1133
|
+
svg,
|
|
1134
|
+
prevLayout: layoutA,
|
|
1135
|
+
nextLayout: layoutB,
|
|
1136
|
+
animation: layoutB.animation!,
|
|
1137
|
+
onComplete: () => {},
|
|
1138
|
+
});
|
|
1139
|
+
|
|
1140
|
+
runToCompletion();
|
|
1141
|
+
|
|
1142
|
+
// No ghost elements remain
|
|
1143
|
+
expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
|
|
1144
|
+
|
|
1145
|
+
// All next layout point marks are present with correct positions
|
|
1146
|
+
const pointMarks = layoutB.marks.filter((m) => m.type === 'point');
|
|
1147
|
+
for (const mark of pointMarks) {
|
|
1148
|
+
if (!mark.key) continue;
|
|
1149
|
+
const el = svg.querySelector(
|
|
1150
|
+
`circle.oc-mark-point[data-key="${mark.key}"]`,
|
|
1151
|
+
) as SVGElement | null;
|
|
1152
|
+
expect(el).not.toBeNull();
|
|
1153
|
+
if (el) {
|
|
1154
|
+
expect(el.getAttribute('cx')).toBe(String((mark as { cx: number }).cx));
|
|
1155
|
+
expect(el.getAttribute('cy')).toBe(String((mark as { cy: number }).cy));
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
});
|
|
1159
|
+
});
|
|
1160
|
+
|
|
1161
|
+
// ---------------------------------------------------------------------------
|
|
1162
|
+
// Axis tick transitions
|
|
1163
|
+
// ---------------------------------------------------------------------------
|
|
1164
|
+
|
|
1165
|
+
describe('axis tick transitions', () => {
|
|
1166
|
+
it('data-tick-key is stamped on tick labels', () => {
|
|
1167
|
+
const spec = columnSpec(DATA_A);
|
|
1168
|
+
const { svg } = compileAndRender(spec);
|
|
1169
|
+
|
|
1170
|
+
const tickLabels = svg.querySelectorAll('.oc-axis-tick[data-tick-key]');
|
|
1171
|
+
expect(tickLabels.length).toBeGreaterThan(0);
|
|
1172
|
+
});
|
|
1173
|
+
|
|
1174
|
+
it('data-tick-key is stamped on gridlines', () => {
|
|
1175
|
+
const spec = columnSpec(DATA_A);
|
|
1176
|
+
const { svg } = compileAndRender(spec);
|
|
1177
|
+
|
|
1178
|
+
const gridlines = svg.querySelectorAll('.oc-gridline[data-tick-key]');
|
|
1179
|
+
expect(gridlines.length).toBeGreaterThan(0);
|
|
1180
|
+
});
|
|
1181
|
+
|
|
1182
|
+
it('adding a category: surviving tick labels from/to match prev/next layout tick positions', () => {
|
|
1183
|
+
const specA = columnSpec(DATA_A); // Q1, Q2, Q3
|
|
1184
|
+
const specB = columnSpec(DATA_C); // Q1, Q2, Q3, Q4
|
|
1185
|
+
|
|
1186
|
+
const layoutA = compile(specA);
|
|
1187
|
+
const layoutB = compile(specB);
|
|
1188
|
+
|
|
1189
|
+
// Surviving x-axis ticks (Q1, Q2, Q3) should have different positions
|
|
1190
|
+
// between layoutA and layoutB because adding Q4 changes the band scale
|
|
1191
|
+
const prevXTicks = layoutA.axes.x?.ticks ?? [];
|
|
1192
|
+
const nextXTicks = layoutB.axes.x?.ticks ?? [];
|
|
1193
|
+
|
|
1194
|
+
// Q1 should exist in both
|
|
1195
|
+
const prevQ1 = prevXTicks.find((t) => t.label === 'Q1');
|
|
1196
|
+
const nextQ1 = nextXTicks.find((t) => t.label === 'Q1');
|
|
1197
|
+
expect(prevQ1).toBeDefined();
|
|
1198
|
+
expect(nextQ1).toBeDefined();
|
|
1199
|
+
|
|
1200
|
+
// Now render and transition
|
|
1201
|
+
const container = createContainer();
|
|
1202
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
1203
|
+
|
|
1204
|
+
runTransition({
|
|
1205
|
+
svg,
|
|
1206
|
+
prevLayout: layoutA,
|
|
1207
|
+
nextLayout: layoutB,
|
|
1208
|
+
animation: layoutB.animation!,
|
|
1209
|
+
onComplete: () => {},
|
|
1210
|
+
});
|
|
1211
|
+
|
|
1212
|
+
runToCompletion();
|
|
1213
|
+
|
|
1214
|
+
// After transition, verify tick labels are at final positions
|
|
1215
|
+
const tickLabels = svg.querySelectorAll('.oc-axis-x .oc-axis-tick[data-tick-key]');
|
|
1216
|
+
expect(tickLabels.length).toBe(nextXTicks.length);
|
|
1217
|
+
|
|
1218
|
+
// No ghost elements remain
|
|
1219
|
+
expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
|
|
1220
|
+
});
|
|
1221
|
+
|
|
1222
|
+
it('removed tick ghost-fades', () => {
|
|
1223
|
+
const specA = columnSpec(DATA_C); // Q1, Q2, Q3, Q4
|
|
1224
|
+
const specB = columnSpec(DATA_A); // Q1, Q2, Q3
|
|
1225
|
+
|
|
1226
|
+
const layoutA = compile(specA);
|
|
1227
|
+
const layoutB = compile(specB);
|
|
1228
|
+
|
|
1229
|
+
const container = createContainer();
|
|
1230
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
1231
|
+
|
|
1232
|
+
runTransition({
|
|
1233
|
+
svg,
|
|
1234
|
+
prevLayout: layoutA,
|
|
1235
|
+
nextLayout: layoutB,
|
|
1236
|
+
animation: layoutB.animation!,
|
|
1237
|
+
onComplete: () => {},
|
|
1238
|
+
});
|
|
1239
|
+
|
|
1240
|
+
// After starting, there should be ghost tick labels for the removed Q4
|
|
1241
|
+
pumpRaf(0);
|
|
1242
|
+
const tickGhosts = svg.querySelectorAll('.oc-axis-tick.oc-ghost');
|
|
1243
|
+
// Q4 was removed, so there should be at least one ghost
|
|
1244
|
+
expect(tickGhosts.length).toBeGreaterThan(0);
|
|
1245
|
+
|
|
1246
|
+
// Run to completion
|
|
1247
|
+
pumpRaf(2000);
|
|
1248
|
+
|
|
1249
|
+
// Ghosts should be cleaned up
|
|
1250
|
+
expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
|
|
1251
|
+
});
|
|
1252
|
+
});
|
|
1253
|
+
|
|
1254
|
+
// ---------------------------------------------------------------------------
|
|
1255
|
+
// Gradient ghost test
|
|
1256
|
+
// ---------------------------------------------------------------------------
|
|
1257
|
+
|
|
1258
|
+
describe('gradient ghost', () => {
|
|
1259
|
+
it('exiting mark with gradient fill: new SVG defs contain the gradient, ghost fill references valid ID', () => {
|
|
1260
|
+
// Create an area chart spec with gradient fill (area marks use gradients by default)
|
|
1261
|
+
const specA: ChartSpec = {
|
|
1262
|
+
animation: true,
|
|
1263
|
+
mark: 'area',
|
|
1264
|
+
data: [
|
|
1265
|
+
{ month: 'Jan', sales: 100, group: 'A' },
|
|
1266
|
+
{ month: 'Feb', sales: 200, group: 'A' },
|
|
1267
|
+
{ month: 'Jan', sales: 80, group: 'B' },
|
|
1268
|
+
{ month: 'Feb', sales: 150, group: 'B' },
|
|
1269
|
+
],
|
|
1270
|
+
encoding: {
|
|
1271
|
+
x: { field: 'month', type: 'ordinal' },
|
|
1272
|
+
y: { field: 'sales', type: 'quantitative' },
|
|
1273
|
+
color: { field: 'group', type: 'nominal' },
|
|
1274
|
+
},
|
|
1275
|
+
};
|
|
1276
|
+
// Remove group B
|
|
1277
|
+
const specB: ChartSpec = {
|
|
1278
|
+
...specA,
|
|
1279
|
+
data: [
|
|
1280
|
+
{ month: 'Jan', sales: 120, group: 'A' },
|
|
1281
|
+
{ month: 'Feb', sales: 220, group: 'A' },
|
|
1282
|
+
],
|
|
1283
|
+
};
|
|
1284
|
+
|
|
1285
|
+
const layoutA = compile(specA);
|
|
1286
|
+
const layoutB = compile(specB);
|
|
1287
|
+
|
|
1288
|
+
// Check if any marks have gradient fills
|
|
1289
|
+
const hasGradients = layoutA.marks.some(
|
|
1290
|
+
(m) => 'fill' in m && typeof m.fill !== 'string' && m.fill !== undefined,
|
|
1291
|
+
);
|
|
1292
|
+
|
|
1293
|
+
// If the chart type produces gradients, verify ghost handling
|
|
1294
|
+
if (hasGradients) {
|
|
1295
|
+
const container = createContainer();
|
|
1296
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
1297
|
+
|
|
1298
|
+
runTransition({
|
|
1299
|
+
svg,
|
|
1300
|
+
prevLayout: layoutA,
|
|
1301
|
+
nextLayout: layoutB,
|
|
1302
|
+
animation: layoutB.animation!,
|
|
1303
|
+
onComplete: () => {},
|
|
1304
|
+
});
|
|
1305
|
+
|
|
1306
|
+
// After starting, check that ghost fill references a valid gradient
|
|
1307
|
+
const ghosts = svg.querySelectorAll('.oc-ghost path[fill]');
|
|
1308
|
+
for (const ghost of ghosts) {
|
|
1309
|
+
const fill = ghost.getAttribute('fill') ?? '';
|
|
1310
|
+
if (fill.startsWith('url(#')) {
|
|
1311
|
+
const id = fill.slice(5, -1);
|
|
1312
|
+
const gradientEl = svg.querySelector(`#${id}`);
|
|
1313
|
+
expect(gradientEl).not.toBeNull();
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
runToCompletion();
|
|
1318
|
+
expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
|
|
1319
|
+
}
|
|
1320
|
+
});
|
|
1321
|
+
});
|
|
1322
|
+
|
|
1323
|
+
// ---------------------------------------------------------------------------
|
|
1324
|
+
// Interruption / retargeting
|
|
1325
|
+
// ---------------------------------------------------------------------------
|
|
1326
|
+
|
|
1327
|
+
const DATA_D = [
|
|
1328
|
+
{ category: 'Q1', value: 300 },
|
|
1329
|
+
{ category: 'Q2', value: 100 },
|
|
1330
|
+
{ category: 'Q3', value: 250 },
|
|
1331
|
+
];
|
|
1332
|
+
|
|
1333
|
+
describe('interruption retargeting', () => {
|
|
1334
|
+
it('A -> B -> interrupt at ~50% -> C -> complete -> equals fresh render of C', () => {
|
|
1335
|
+
const specA = columnSpec(DATA_A);
|
|
1336
|
+
const specB = columnSpec(DATA_B);
|
|
1337
|
+
const specC = columnSpec(DATA_D);
|
|
1338
|
+
|
|
1339
|
+
// Use createChart so update() handles snapshot plumbing
|
|
1340
|
+
const container = createContainer();
|
|
1341
|
+
const chart = createChart(container, specA);
|
|
1342
|
+
|
|
1343
|
+
// Update A -> B, start transition
|
|
1344
|
+
chart.update(specB);
|
|
1345
|
+
|
|
1346
|
+
// Pump to ~50% of the transition (start at t=0, then advance)
|
|
1347
|
+
pumpRaf(0);
|
|
1348
|
+
pumpRaf(250); // 250ms into ~500ms transition
|
|
1349
|
+
|
|
1350
|
+
// Interrupt with C
|
|
1351
|
+
chart.update(specC);
|
|
1352
|
+
|
|
1353
|
+
// Run the C transition to completion
|
|
1354
|
+
pumpRaf(0);
|
|
1355
|
+
pumpRaf(2000);
|
|
1356
|
+
|
|
1357
|
+
// Extract geometry from the current SVG
|
|
1358
|
+
const svg = container.querySelector('svg') as SVGSVGElement;
|
|
1359
|
+
const transitioned = extractRectGeometry(svg);
|
|
1360
|
+
|
|
1361
|
+
// Fresh render of specC for comparison
|
|
1362
|
+
const { svg: freshSvg } = compileAndRender(specC);
|
|
1363
|
+
const fresh = extractRectGeometry(freshSvg);
|
|
1364
|
+
|
|
1365
|
+
// Same set of keys
|
|
1366
|
+
expect([...transitioned.keys()].sort()).toEqual([...fresh.keys()].sort());
|
|
1367
|
+
|
|
1368
|
+
// Same geometry per key (round-trip invariant holds through interruption)
|
|
1369
|
+
for (const [key, tGeom] of transitioned) {
|
|
1370
|
+
const fGeom = fresh.get(key);
|
|
1371
|
+
expect(fGeom).toBeDefined();
|
|
1372
|
+
expect(tGeom).toEqual(fGeom);
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
// No ghost elements remain
|
|
1376
|
+
expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
|
|
1377
|
+
|
|
1378
|
+
chart.destroy();
|
|
1379
|
+
});
|
|
1380
|
+
|
|
1381
|
+
it('snapshot captures intermediate rect geometry', () => {
|
|
1382
|
+
const specA = columnSpec(DATA_A);
|
|
1383
|
+
const specB = columnSpec(DATA_B);
|
|
1384
|
+
|
|
1385
|
+
const layoutA = compile(specA);
|
|
1386
|
+
const layoutB = compile(specB);
|
|
1387
|
+
|
|
1388
|
+
const container = createContainer();
|
|
1389
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
1390
|
+
|
|
1391
|
+
const handle = runTransition({
|
|
1392
|
+
svg,
|
|
1393
|
+
prevLayout: layoutA,
|
|
1394
|
+
nextLayout: layoutB,
|
|
1395
|
+
animation: layoutB.animation!,
|
|
1396
|
+
onComplete: () => {},
|
|
1397
|
+
});
|
|
1398
|
+
|
|
1399
|
+
// Pump to start, then mid-transition
|
|
1400
|
+
pumpRaf(0);
|
|
1401
|
+
pumpRaf(250);
|
|
1402
|
+
|
|
1403
|
+
const snap = handle.snapshot();
|
|
1404
|
+
|
|
1405
|
+
// Should have entries for updated marks
|
|
1406
|
+
expect(snap.size).toBeGreaterThan(0);
|
|
1407
|
+
|
|
1408
|
+
// Each entry should be a rect with intermediate values
|
|
1409
|
+
for (const [, geom] of snap) {
|
|
1410
|
+
expect(geom.type).toBe('rect');
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
handle.cancel();
|
|
1414
|
+
});
|
|
1415
|
+
});
|
|
1416
|
+
|
|
1417
|
+
// ---------------------------------------------------------------------------
|
|
1418
|
+
// Reduced motion
|
|
1419
|
+
// ---------------------------------------------------------------------------
|
|
1420
|
+
|
|
1421
|
+
describe('reduced motion', () => {
|
|
1422
|
+
it('canTransition returns false when prefers-reduced-motion matches', () => {
|
|
1423
|
+
const original = window.matchMedia;
|
|
1424
|
+
vi.stubGlobal('matchMedia', (query: string) => ({
|
|
1425
|
+
matches: query === '(prefers-reduced-motion: reduce)',
|
|
1426
|
+
media: query,
|
|
1427
|
+
addEventListener: () => {},
|
|
1428
|
+
removeEventListener: () => {},
|
|
1429
|
+
}));
|
|
1430
|
+
|
|
1431
|
+
const specA = columnSpec(DATA_A);
|
|
1432
|
+
const specB = columnSpec(DATA_B);
|
|
1433
|
+
expect(canTransition(passingGateArgs(specA, specB))).toBe(false);
|
|
1434
|
+
|
|
1435
|
+
vi.stubGlobal('matchMedia', original);
|
|
1436
|
+
});
|
|
1437
|
+
|
|
1438
|
+
it('canTransition returns true when reduced-motion is not active', () => {
|
|
1439
|
+
const original = window.matchMedia;
|
|
1440
|
+
vi.stubGlobal('matchMedia', (query: string) => ({
|
|
1441
|
+
matches: false,
|
|
1442
|
+
media: query,
|
|
1443
|
+
addEventListener: () => {},
|
|
1444
|
+
removeEventListener: () => {},
|
|
1445
|
+
}));
|
|
1446
|
+
|
|
1447
|
+
const specA = columnSpec(DATA_A);
|
|
1448
|
+
const specB = columnSpec(DATA_B);
|
|
1449
|
+
expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
|
|
1450
|
+
|
|
1451
|
+
vi.stubGlobal('matchMedia', original);
|
|
1452
|
+
});
|
|
1453
|
+
});
|
|
1454
|
+
|
|
1455
|
+
// ---------------------------------------------------------------------------
|
|
1456
|
+
// React StrictMode double-mount
|
|
1457
|
+
// ---------------------------------------------------------------------------
|
|
1458
|
+
|
|
1459
|
+
describe('React StrictMode double-mount', () => {
|
|
1460
|
+
it('destroy cancels rAF loop; fresh mount does not inherit stale state', () => {
|
|
1461
|
+
const specA = columnSpec(DATA_A);
|
|
1462
|
+
const specB = columnSpec(DATA_B);
|
|
1463
|
+
|
|
1464
|
+
const container = createContainer();
|
|
1465
|
+
|
|
1466
|
+
// First mount
|
|
1467
|
+
const chart1 = createChart(container, specA);
|
|
1468
|
+
chart1.update(specB);
|
|
1469
|
+
pumpRaf(0); // start transition
|
|
1470
|
+
|
|
1471
|
+
// Destroy mid-transition (simulates StrictMode unmount)
|
|
1472
|
+
chart1.destroy();
|
|
1473
|
+
|
|
1474
|
+
// Record rAF callback count after destroy
|
|
1475
|
+
const callbacksAfterDestroy = rafCallbacks.size;
|
|
1476
|
+
|
|
1477
|
+
// Second mount (fresh closure)
|
|
1478
|
+
const chart2 = createChart(container, specA);
|
|
1479
|
+
chart2.update(specB);
|
|
1480
|
+
pumpRaf(0);
|
|
1481
|
+
|
|
1482
|
+
// The old transition's rAF should not have re-registered
|
|
1483
|
+
// (only the new transition should be running)
|
|
1484
|
+
// Pump to completion - if old rAF leaked, it would crash or write to removed DOM
|
|
1485
|
+
pumpRaf(2000);
|
|
1486
|
+
|
|
1487
|
+
// Clean up
|
|
1488
|
+
chart2.destroy();
|
|
1489
|
+
|
|
1490
|
+
// No crash = success: the first transition did not leak rAF callbacks
|
|
1491
|
+
expect(callbacksAfterDestroy).toBe(0);
|
|
1492
|
+
});
|
|
1493
|
+
});
|
|
1494
|
+
|
|
1495
|
+
// ---------------------------------------------------------------------------
|
|
1496
|
+
// Secondary element crossfade
|
|
1497
|
+
// ---------------------------------------------------------------------------
|
|
1498
|
+
|
|
1499
|
+
describe('secondary element crossfade', () => {
|
|
1500
|
+
it('annotations start at opacity 0 during transition', () => {
|
|
1501
|
+
// Create a spec with annotations
|
|
1502
|
+
const specA: ChartSpec = {
|
|
1503
|
+
...columnSpec(DATA_A),
|
|
1504
|
+
annotations: [{ type: 'text', x: 'Q1', y: 100, text: 'Note' }],
|
|
1505
|
+
};
|
|
1506
|
+
const specB: ChartSpec = {
|
|
1507
|
+
...columnSpec(DATA_B),
|
|
1508
|
+
annotations: [{ type: 'text', x: 'Q1', y: 150, text: 'Updated' }],
|
|
1509
|
+
};
|
|
1510
|
+
|
|
1511
|
+
const layoutA = compile(specA);
|
|
1512
|
+
const layoutB = compile(specB);
|
|
1513
|
+
|
|
1514
|
+
const container = createContainer();
|
|
1515
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
1516
|
+
|
|
1517
|
+
runTransition({
|
|
1518
|
+
svg,
|
|
1519
|
+
prevLayout: layoutA,
|
|
1520
|
+
nextLayout: layoutB,
|
|
1521
|
+
animation: layoutB.animation!,
|
|
1522
|
+
onComplete: () => {},
|
|
1523
|
+
});
|
|
1524
|
+
|
|
1525
|
+
// After applying from-states, annotations should be at opacity 0
|
|
1526
|
+
const annotations = svg.querySelectorAll('.oc-annotation');
|
|
1527
|
+
for (const ann of annotations) {
|
|
1528
|
+
expect((ann as SVGElement).style.opacity).toBe('0');
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
// Run to completion
|
|
1532
|
+
runToCompletion();
|
|
1533
|
+
|
|
1534
|
+
// After completion, annotation opacity should be restored (empty = visible)
|
|
1535
|
+
for (const ann of annotations) {
|
|
1536
|
+
expect((ann as SVGElement).style.opacity).toBe('');
|
|
1537
|
+
}
|
|
1538
|
+
});
|
|
1539
|
+
|
|
1540
|
+
it('endpoint labels start at opacity 0 during transition', () => {
|
|
1541
|
+
// Use a line chart that produces endpoint labels
|
|
1542
|
+
const lineSpecWithLabels: ChartSpec = {
|
|
1543
|
+
animation: true,
|
|
1544
|
+
mark: 'line',
|
|
1545
|
+
data: [
|
|
1546
|
+
{ month: 'Jan', value: 100, group: 'A' },
|
|
1547
|
+
{ month: 'Feb', value: 200, group: 'A' },
|
|
1548
|
+
{ month: 'Jan', value: 80, group: 'B' },
|
|
1549
|
+
{ month: 'Feb', value: 150, group: 'B' },
|
|
1550
|
+
],
|
|
1551
|
+
encoding: {
|
|
1552
|
+
x: { field: 'month', type: 'ordinal' },
|
|
1553
|
+
y: { field: 'value', type: 'quantitative' },
|
|
1554
|
+
color: { field: 'group', type: 'nominal' },
|
|
1555
|
+
},
|
|
1556
|
+
};
|
|
1557
|
+
const lineSpecB: ChartSpec = {
|
|
1558
|
+
...lineSpecWithLabels,
|
|
1559
|
+
data: [
|
|
1560
|
+
{ month: 'Jan', value: 120, group: 'A' },
|
|
1561
|
+
{ month: 'Feb', value: 220, group: 'A' },
|
|
1562
|
+
{ month: 'Jan', value: 90, group: 'B' },
|
|
1563
|
+
{ month: 'Feb', value: 170, group: 'B' },
|
|
1564
|
+
],
|
|
1565
|
+
};
|
|
1566
|
+
|
|
1567
|
+
const layoutA = compile(lineSpecWithLabels);
|
|
1568
|
+
const layoutB = compile(lineSpecB);
|
|
1569
|
+
|
|
1570
|
+
const container = createContainer();
|
|
1571
|
+
const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
|
|
1572
|
+
|
|
1573
|
+
runTransition({
|
|
1574
|
+
svg,
|
|
1575
|
+
prevLayout: layoutA,
|
|
1576
|
+
nextLayout: layoutB,
|
|
1577
|
+
animation: layoutB.animation!,
|
|
1578
|
+
onComplete: () => {},
|
|
1579
|
+
});
|
|
1580
|
+
|
|
1581
|
+
// Check if endpoint labels exist
|
|
1582
|
+
const epLabels = svg.querySelector('.oc-endpoint-labels') as SVGElement | null;
|
|
1583
|
+
if (epLabels) {
|
|
1584
|
+
expect(epLabels.style.opacity).toBe('0');
|
|
1585
|
+
|
|
1586
|
+
runToCompletion();
|
|
1587
|
+
|
|
1588
|
+
// After completion, opacity restored
|
|
1589
|
+
expect(epLabels.style.opacity).toBe('');
|
|
1590
|
+
}
|
|
1591
|
+
});
|
|
1592
|
+
});
|