@opendata-ai/openchart-vanilla 8.0.0-rc.2 → 8.0.0-rc.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/dist/{chunk-W34KN7FD.js → chunk-2NAQRP66.js} +2 -2
  2. package/dist/chunk-DCODCLWI.js +234 -0
  3. package/dist/chunk-DCODCLWI.js.map +1 -0
  4. package/dist/{chunk-5EOV5JQZ.js → chunk-EKAUQAAX.js} +50 -46
  5. package/dist/chunk-EKAUQAAX.js.map +1 -0
  6. package/dist/{chunk-ENXSUXC5.js → chunk-KNT5AZ2O.js} +7309 -5490
  7. package/dist/chunk-KNT5AZ2O.js.map +1 -0
  8. package/dist/chunk-RM5XS6NR.js +113 -0
  9. package/dist/chunk-RM5XS6NR.js.map +1 -0
  10. package/dist/chunk-T32UHMZA.js +282 -0
  11. package/dist/chunk-T32UHMZA.js.map +1 -0
  12. package/dist/export-gif.d.ts +113 -0
  13. package/dist/export-gif.js +21 -0
  14. package/dist/export-gif.js.map +1 -0
  15. package/dist/index.d.ts +283 -24
  16. package/dist/index.js +2012 -763
  17. package/dist/index.js.map +1 -1
  18. package/dist/{mount-CUKQLeqD.d.ts → mount-Dvv8NYIv.d.ts} +117 -3
  19. package/dist/simulation-worker.js +83 -2
  20. package/dist/static.js +7 -69
  21. package/dist/static.js.map +1 -1
  22. package/dist/story/index.d.ts +3 -2
  23. package/dist/story/index.js +5 -3
  24. package/dist/story/index.js.map +1 -1
  25. package/dist/styles.css +1 -1
  26. package/dist/styles.css.map +1 -1
  27. package/package.json +14 -6
  28. package/dist/chunk-5EOV5JQZ.js.map +0 -1
  29. package/dist/chunk-ENXSUXC5.js.map +0 -1
  30. package/src/__test-fixtures__/dom.ts +0 -70
  31. package/src/__test-fixtures__/specs.ts +0 -214
  32. package/src/__tests__/animation.test.ts +0 -358
  33. package/src/__tests__/annotations-render.test.ts +0 -201
  34. package/src/__tests__/compound-labels.test.ts +0 -122
  35. package/src/__tests__/continuous-legend.test.ts +0 -91
  36. package/src/__tests__/crosshair.test.ts +0 -130
  37. package/src/__tests__/edit-events.test.ts +0 -902
  38. package/src/__tests__/events.test.ts +0 -387
  39. package/src/__tests__/export.test.ts +0 -241
  40. package/src/__tests__/font-race.test.ts +0 -199
  41. package/src/__tests__/gradient-ids.test.ts +0 -159
  42. package/src/__tests__/gradient-utils.test.ts +0 -125
  43. package/src/__tests__/known-bugs-render.test.ts +0 -183
  44. package/src/__tests__/map.test.ts +0 -293
  45. package/src/__tests__/mount.test.ts +0 -357
  46. package/src/__tests__/pattern-fills.test.ts +0 -105
  47. package/src/__tests__/resize-timing.test.ts +0 -218
  48. package/src/__tests__/sankey.test.ts +0 -133
  49. package/src/__tests__/selection-events.test.ts +0 -869
  50. package/src/__tests__/series-search.test.ts +0 -311
  51. package/src/__tests__/static.test.ts +0 -141
  52. package/src/__tests__/svg-renderer.test.ts +0 -895
  53. package/src/__tests__/table-keyboard.test.ts +0 -361
  54. package/src/__tests__/table-mount.test.ts +0 -639
  55. package/src/__tests__/tilemap.test.ts +0 -158
  56. package/src/__tests__/tooltip.test.ts +0 -337
  57. package/src/__tests__/transition.test.ts +0 -1777
  58. package/src/__tests__/you-draw-it.test.ts +0 -273
  59. package/src/animation-vars.ts +0 -42
  60. package/src/animation.ts +0 -76
  61. package/src/barlist-mount.ts +0 -383
  62. package/src/barlist-renderer.ts +0 -267
  63. package/src/dom-helpers.ts +0 -17
  64. package/src/export.ts +0 -482
  65. package/src/gradient-utils.ts +0 -138
  66. package/src/graph/__tests__/canvas-renderer.test.ts +0 -713
  67. package/src/graph/__tests__/graph-mount.test.ts +0 -213
  68. package/src/graph/__tests__/interaction.test.ts +0 -205
  69. package/src/graph/__tests__/keyboard.test.ts +0 -653
  70. package/src/graph/__tests__/search.test.ts +0 -88
  71. package/src/graph/__tests__/simulation.test.ts +0 -233
  72. package/src/graph/__tests__/spatial-index.test.ts +0 -142
  73. package/src/graph/__tests__/zoom.test.ts +0 -203
  74. package/src/graph/canvas-renderer.ts +0 -747
  75. package/src/graph/interaction.ts +0 -367
  76. package/src/graph/keyboard.ts +0 -208
  77. package/src/graph/search.ts +0 -50
  78. package/src/graph/simulation-worker-url.ts +0 -12
  79. package/src/graph/simulation-worker.ts +0 -278
  80. package/src/graph/simulation.ts +0 -422
  81. package/src/graph/spatial-index.ts +0 -121
  82. package/src/graph/types.ts +0 -47
  83. package/src/graph/worker-protocol.ts +0 -70
  84. package/src/graph/zoom.ts +0 -134
  85. package/src/graph-mount.ts +0 -888
  86. package/src/index.ts +0 -76
  87. package/src/interactions/chart-events.ts +0 -139
  88. package/src/interactions/crosshair.ts +0 -233
  89. package/src/interactions/drag-handler.ts +0 -175
  90. package/src/interactions/editing-drags.ts +0 -515
  91. package/src/interactions/index.ts +0 -25
  92. package/src/interactions/keyboard-nav.ts +0 -111
  93. package/src/interactions/legend-interaction.ts +0 -46
  94. package/src/interactions/selection.ts +0 -280
  95. package/src/interactions/tooltip-events.ts +0 -72
  96. package/src/map-camera.ts +0 -88
  97. package/src/map-mount.ts +0 -701
  98. package/src/map-renderer.ts +0 -338
  99. package/src/map-transition.ts +0 -83
  100. package/src/measure-text.ts +0 -134
  101. package/src/mount.ts +0 -1382
  102. package/src/pattern-utils.ts +0 -110
  103. package/src/renderers/annotations.ts +0 -388
  104. package/src/renderers/axes.ts +0 -351
  105. package/src/renderers/brand.ts +0 -87
  106. package/src/renderers/chrome.ts +0 -196
  107. package/src/renderers/endpoint-labels.ts +0 -150
  108. package/src/renderers/legend.ts +0 -311
  109. package/src/renderers/marks.ts +0 -561
  110. package/src/renderers/metrics.ts +0 -63
  111. package/src/renderers/svg-dom.ts +0 -40
  112. package/src/renderers/table-cells.ts +0 -446
  113. package/src/resize-observer.ts +0 -46
  114. package/src/sankey-mount.ts +0 -622
  115. package/src/sankey-renderer.ts +0 -614
  116. package/src/series-search.ts +0 -392
  117. package/src/static.ts +0 -251
  118. package/src/story/__tests__/camera-math.test.ts +0 -145
  119. package/src/story/__tests__/create-chart-story.test.ts +0 -317
  120. package/src/story/__tests__/progress-math.test.ts +0 -95
  121. package/src/story/__tests__/scroll-driver.test.ts +0 -130
  122. package/src/story/__tests__/tween.test.ts +0 -159
  123. package/src/story/camera-math.ts +0 -125
  124. package/src/story/create-chart-story.ts +0 -353
  125. package/src/story/crossfade.ts +0 -70
  126. package/src/story/index.ts +0 -39
  127. package/src/story/progress-math.ts +0 -85
  128. package/src/story/resolve-camera-target.ts +0 -119
  129. package/src/story/scroll-driver.ts +0 -206
  130. package/src/story/story-camera.ts +0 -49
  131. package/src/story/tween.ts +0 -112
  132. package/src/story/types.ts +0 -76
  133. package/src/svg-ids.ts +0 -23
  134. package/src/svg-renderer.ts +0 -383
  135. package/src/table-keyboard.ts +0 -266
  136. package/src/table-mount.ts +0 -567
  137. package/src/table-renderer.ts +0 -414
  138. package/src/text-edit-overlay.ts +0 -255
  139. package/src/theme-tokens.ts +0 -51
  140. package/src/tilemap-mount.ts +0 -511
  141. package/src/tilemap-renderer.ts +0 -396
  142. package/src/tooltip.ts +0 -177
  143. package/src/transition.ts +0 -2816
  144. package/src/you-draw-it.ts +0 -480
  145. /package/dist/{chunk-W34KN7FD.js.map → chunk-2NAQRP66.js.map} +0 -0
@@ -1,1777 +0,0 @@
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 9: passes when only an annotation was added', () => {
307
- // Regression: gate 9 used to inspect layout.marks only. Annotations are not
308
- // marks, so an annotate-only step reported "nothing changed", the whole
309
- // transition was skipped, and the annotation popped in on render()'s
310
- // instant swap -- with the fade code sitting unreachable further down.
311
- const specA = columnSpec(DATA_A);
312
- const specB: ChartSpec = {
313
- ...columnSpec(DATA_A),
314
- annotations: [{ type: 'text', x: 'Q1', y: 100, text: 'Note' }],
315
- };
316
- expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
317
- });
318
-
319
- it('gate 9: passes when only the highlight changed', () => {
320
- // Regression: a highlight mute recolors marks without moving them, so this
321
- // also read as a zero-delta and snapped.
322
- const base: ChartSpec = {
323
- animation: true,
324
- mark: 'line',
325
- data: [
326
- { month: 'Jan', value: 10, series: 'A' },
327
- { month: 'Feb', value: 20, series: 'A' },
328
- { month: 'Jan', value: 30, series: 'B' },
329
- { month: 'Feb', value: 40, series: 'B' },
330
- ],
331
- encoding: {
332
- x: { field: 'month', type: 'ordinal' },
333
- y: { field: 'value', type: 'quantitative' },
334
- color: { field: 'series', type: 'nominal' },
335
- },
336
- };
337
- const highlighted: ChartSpec = {
338
- ...base,
339
- encoding: { ...base.encoding, color: { field: 'series', type: 'nominal', highlight: ['A'] } },
340
- };
341
- expect(canTransition(passingGateArgs(base, highlighted))).toBe(true);
342
- });
343
-
344
- it('gate 10: fails when prefers-reduced-motion is active', () => {
345
- const original = window.matchMedia;
346
- vi.stubGlobal('matchMedia', (query: string) => ({
347
- matches: query === '(prefers-reduced-motion: reduce)',
348
- media: query,
349
- addEventListener: () => {},
350
- removeEventListener: () => {},
351
- }));
352
-
353
- const specA = columnSpec(DATA_A);
354
- const specB = columnSpec(DATA_B);
355
- expect(canTransition(passingGateArgs(specA, specB))).toBe(false);
356
-
357
- vi.stubGlobal('matchMedia', original);
358
- });
359
- });
360
-
361
- // ---------------------------------------------------------------------------
362
- // Mark matching
363
- // ---------------------------------------------------------------------------
364
-
365
- describe('mark matching', () => {
366
- it('correctly identifies entered marks when adding a category', () => {
367
- const specA = columnSpec(DATA_A);
368
- const specB = columnSpec(DATA_C); // adds Q4
369
- const layoutA = compile(specA);
370
- const layoutB = compile(specB);
371
-
372
- const prevKeys = new Set(
373
- layoutA.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
374
- );
375
- const nextKeys = new Set(
376
- layoutB.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
377
- );
378
-
379
- const entered = [...nextKeys].filter((k) => !prevKeys.has(k));
380
- expect(entered.length).toBeGreaterThan(0);
381
- });
382
-
383
- it('correctly identifies exited marks when removing a category', () => {
384
- const specA = columnSpec(DATA_A);
385
- const specB = columnSpec(DATA_REMOVE); // removes Q3
386
- const layoutA = compile(specA);
387
- const layoutB = compile(specB);
388
-
389
- const prevKeys = new Set(
390
- layoutA.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
391
- );
392
- const nextKeys = new Set(
393
- layoutB.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
394
- );
395
-
396
- const exited = [...prevKeys].filter((k) => !nextKeys.has(k));
397
- expect(exited.length).toBeGreaterThan(0);
398
- });
399
-
400
- it('correctly identifies updated marks for value-only changes', () => {
401
- const specA = columnSpec(DATA_A);
402
- const specB = columnSpec(DATA_B); // same categories, different values
403
- const layoutA = compile(specA);
404
- const layoutB = compile(specB);
405
-
406
- const prevKeys = new Set(
407
- layoutA.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
408
- );
409
- const nextKeys = new Set(
410
- layoutB.marks.filter((m) => m.type === 'rect' && m.key).map((m) => m.key),
411
- );
412
-
413
- const updated = [...prevKeys].filter((k) => nextKeys.has(k));
414
- expect(updated.length).toBe(3); // Q1, Q2, Q3 all present in both
415
- });
416
- });
417
-
418
- // ---------------------------------------------------------------------------
419
- // Round-trip invariant
420
- // ---------------------------------------------------------------------------
421
-
422
- /**
423
- * Extract rect geometry from all .oc-mark-rect elements in an SVG,
424
- * keyed by data-key. Returns a map of key -> {x, y, width, height}.
425
- * This tests that the transition snaps geometry to final values matching
426
- * a fresh render from the same layout.
427
- */
428
- function extractRectGeometry(
429
- svg: SVGElement,
430
- ): Map<string, { x: string; y: string; w: string; h: string }> {
431
- const result = new Map<string, { x: string; y: string; w: string; h: string }>();
432
- const groups = svg.querySelectorAll('.oc-mark-rect[data-key]');
433
- for (const g of groups) {
434
- const key = g.getAttribute('data-key')!;
435
- const rect = g.querySelector('rect');
436
- const path = g.querySelector('path');
437
- if (rect) {
438
- result.set(key, {
439
- x: rect.getAttribute('x') ?? '',
440
- y: rect.getAttribute('y') ?? '',
441
- w: rect.getAttribute('width') ?? '',
442
- h: rect.getAttribute('height') ?? '',
443
- });
444
- } else if (path) {
445
- // For path-based rects (cornerRadiusSides), store the d attribute
446
- result.set(key, {
447
- x: 'path',
448
- y: 'path',
449
- w: 'path',
450
- h: path.getAttribute('d') ?? '',
451
- });
452
- }
453
- }
454
- return result;
455
- }
456
-
457
- /**
458
- * Run a round-trip test: render specB's layout, run transition from layoutA,
459
- * pump to completion, then compare rect geometry against a fresh render of specB.
460
- *
461
- * The transition runs on an SVG already rendered from nextLayout (as mount.ts
462
- * does), so after completion the geometry should match a fresh render exactly.
463
- */
464
- function assertRoundTrip(specA: ChartSpec, specB: ChartSpec) {
465
- const layoutA = compile(specA);
466
- const layoutB = compile(specB);
467
-
468
- // Render from specB's layout (as mount.ts render() does)
469
- const container = createContainer();
470
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
471
-
472
- runTransition({
473
- svg,
474
- prevLayout: layoutA,
475
- nextLayout: layoutB,
476
- animation: layoutB.animation!,
477
- onComplete: () => {},
478
- });
479
-
480
- runToCompletion();
481
-
482
- // Extract geometry after transition
483
- const transitioned = extractRectGeometry(svg);
484
-
485
- // Fresh render of specB for comparison
486
- const { svg: freshSvg } = compileAndRender(specB);
487
- const fresh = extractRectGeometry(freshSvg);
488
-
489
- // Same set of keys
490
- expect([...transitioned.keys()].sort()).toEqual([...fresh.keys()].sort());
491
-
492
- // Same geometry per key
493
- for (const [key, tGeom] of transitioned) {
494
- const fGeom = fresh.get(key);
495
- expect(fGeom).toBeDefined();
496
- expect(tGeom).toEqual(fGeom);
497
- }
498
-
499
- // No ghost elements remain
500
- expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
501
- }
502
-
503
- describe('round-trip invariant', () => {
504
- it('value-only change: geometry matches fresh render', () => {
505
- assertRoundTrip(columnSpec(DATA_A), columnSpec(DATA_B));
506
- });
507
-
508
- it('add category: geometry matches fresh render', () => {
509
- assertRoundTrip(columnSpec(DATA_A), columnSpec(DATA_C));
510
- });
511
-
512
- it('remove category: geometry matches fresh render', () => {
513
- assertRoundTrip(columnSpec(DATA_A), columnSpec(DATA_REMOVE));
514
- });
515
-
516
- it('stacked column with cornerRadius: geometry matches fresh render', () => {
517
- assertRoundTrip(
518
- stackedColumnSpec([
519
- { category: 'Q1', value: 100, group: 'A' },
520
- { category: 'Q1', value: 50, group: 'B' },
521
- { category: 'Q2', value: 200, group: 'A' },
522
- { category: 'Q2', value: 80, group: 'B' },
523
- ]),
524
- stackedColumnSpec([
525
- { category: 'Q1', value: 150, group: 'A' },
526
- { category: 'Q1', value: 70, group: 'B' },
527
- { category: 'Q2', value: 180, group: 'A' },
528
- { category: 'Q2', value: 120, group: 'B' },
529
- ]),
530
- );
531
- });
532
- });
533
-
534
- // ---------------------------------------------------------------------------
535
- // cancel() behavior
536
- // ---------------------------------------------------------------------------
537
-
538
- describe('cancel()', () => {
539
- it('snaps to final values and removes ghosts', () => {
540
- const specA = columnSpec(DATA_A);
541
- const specB = columnSpec(DATA_REMOVE); // Q3 exits
542
-
543
- const { svg, layout: layoutA } = compileAndRender(specA);
544
- const layoutB = compile(specB);
545
-
546
- let completed = false;
547
- const handle = runTransition({
548
- svg,
549
- prevLayout: layoutA,
550
- nextLayout: layoutB,
551
- animation: layoutB.animation!,
552
- onComplete: () => {
553
- completed = true;
554
- },
555
- });
556
-
557
- // Pump once to start
558
- pumpRaf(0);
559
- // Verify ghost exists mid-transition
560
- expect(svg.querySelectorAll('.oc-ghost').length).toBeGreaterThan(0);
561
-
562
- // Cancel
563
- handle.cancel();
564
-
565
- // Ghosts removed
566
- expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
567
- // onComplete NOT called
568
- expect(completed).toBe(false);
569
- // No longer running
570
- expect(handle.running).toBe(false);
571
- });
572
- });
573
-
574
- // ---------------------------------------------------------------------------
575
- // Ghost element attributes
576
- // ---------------------------------------------------------------------------
577
-
578
- describe('ghost elements', () => {
579
- it('ghosts have aria-hidden, pointer-events: none, and no data-key', () => {
580
- const specA = columnSpec(DATA_A);
581
- const specB = columnSpec(DATA_REMOVE); // Q3 exits
582
-
583
- const { svg, layout: layoutA } = compileAndRender(specA);
584
- const layoutB = compile(specB);
585
-
586
- runTransition({
587
- svg,
588
- prevLayout: layoutA,
589
- nextLayout: layoutB,
590
- animation: layoutB.animation!,
591
- onComplete: () => {},
592
- });
593
-
594
- // Pump once to start (ghosts are added before first rAF)
595
- const ghosts = svg.querySelectorAll('.oc-ghost');
596
- expect(ghosts.length).toBeGreaterThan(0);
597
- for (const ghost of ghosts) {
598
- expect(ghost.getAttribute('aria-hidden')).toBe('true');
599
- expect(ghost.getAttribute('pointer-events')).toBe('none');
600
- expect(ghost.hasAttribute('data-key')).toBe(false);
601
- }
602
- });
603
- });
604
-
605
- // ---------------------------------------------------------------------------
606
- // Legend-toggle mid-transition
607
- // ---------------------------------------------------------------------------
608
-
609
- describe('legend toggle mid-transition', () => {
610
- it('render() during transition cancels transition and stops attribute writes', () => {
611
- const specA = columnSpec(DATA_A);
612
- const specB = columnSpec(DATA_B);
613
-
614
- const container = createContainer();
615
- const chart = createChart(container, specA);
616
-
617
- // Update to trigger transition
618
- chart.update(specB);
619
-
620
- // Pump one frame
621
- pumpRaf(0);
622
-
623
- // Force a re-render (simulating legend toggle) should cancel the transition
624
- // by calling render() internally, which cancels transitionHandle
625
- chart.resize();
626
-
627
- // After resize, pumping more frames should have no effect
628
- // (the transition was cancelled)
629
- pumpRaf(100);
630
- // No crash = success (the transition's rAF was cancelled)
631
-
632
- chart.destroy();
633
- });
634
- });
635
-
636
- // ---------------------------------------------------------------------------
637
- // isDragging blocks transitions
638
- // ---------------------------------------------------------------------------
639
-
640
- describe('update during isDragging', () => {
641
- it('does not start a transition when dragging is active', () => {
642
- // We can't directly set isDragging from outside, but we can verify
643
- // that calling render() with pendingRender produces no transition.
644
- // This is implicitly tested by the mount lifecycle.
645
- const specA = columnSpec(DATA_A);
646
- const specB = columnSpec(DATA_B);
647
- const container = createContainer();
648
- const chart = createChart(container, specA);
649
-
650
- // Normal update should work without error
651
- chart.update(specB);
652
- runToCompletion();
653
-
654
- chart.destroy();
655
- });
656
- });
657
-
658
- // ---------------------------------------------------------------------------
659
- // data-key stamping
660
- // ---------------------------------------------------------------------------
661
-
662
- describe('data-key stamping', () => {
663
- it('rect marks have data-key attributes when keys are present', () => {
664
- const spec = columnSpec(DATA_A);
665
- const { svg } = compileAndRender(spec);
666
-
667
- const rectGroups = svg.querySelectorAll('.oc-mark-rect');
668
- let hasKeys = false;
669
- for (const g of rectGroups) {
670
- if (g.hasAttribute('data-key')) {
671
- hasKeys = true;
672
- break;
673
- }
674
- }
675
- expect(hasKeys).toBe(true);
676
- });
677
- });
678
-
679
- // ---------------------------------------------------------------------------
680
- // Line/area morph: normalizePointArrays
681
- // ---------------------------------------------------------------------------
682
-
683
- describe('normalizePointArrays', () => {
684
- it('append: inserted point from equals prev tail position', () => {
685
- const prevPts = [
686
- { x: 0, y: 100 },
687
- { x: 50, y: 80 },
688
- { x: 100, y: 60 },
689
- ];
690
- const nextPts = [
691
- { x: 0, y: 100 },
692
- { x: 50, y: 80 },
693
- { x: 100, y: 60 },
694
- { x: 150, y: 40 },
695
- ];
696
- const prevKeys = ['a', 'b', 'c'];
697
- const nextKeys = ['a', 'b', 'c', 'd'];
698
-
699
- const [fromPts, toPts] = normalizePointArrays(prevPts, nextPts, prevKeys, nextKeys);
700
-
701
- // Both arrays should have 4 points
702
- expect(fromPts.length).toBe(4);
703
- expect(toPts.length).toBe(4);
704
-
705
- // The inserted point (d) should start from the prev tail (c's prev position)
706
- // because it's at the tail end with only one neighbor before it
707
- expect(fromPts[3].x).toBe(100);
708
- expect(fromPts[3].y).toBe(60);
709
-
710
- // The "to" for the inserted point should be its next position
711
- expect(toPts[3].x).toBe(150);
712
- expect(toPts[3].y).toBe(40);
713
- });
714
-
715
- it('remove middle: removed point to sits between surviving neighbors', () => {
716
- const prevPts = [
717
- { x: 0, y: 100 },
718
- { x: 50, y: 80 },
719
- { x: 100, y: 60 },
720
- ];
721
- const nextPts = [
722
- { x: 0, y: 90 },
723
- { x: 100, y: 50 },
724
- ];
725
- const prevKeys = ['a', 'b', 'c'];
726
- const nextKeys = ['a', 'c'];
727
-
728
- const [fromPts, toPts] = normalizePointArrays(prevPts, nextPts, prevKeys, nextKeys);
729
-
730
- expect(fromPts.length).toBe(3);
731
- expect(toPts.length).toBe(3);
732
-
733
- // Removed point 'b' (index 1 in merged) should collapse to midpoint
734
- // between neighbors 'a' (next: {0,90}) and 'c' (next: {100,50})
735
- // t = (50 - 0)/(100 - 0) = 0.5 in prev x, so in next space:
736
- // lerp({0,90}, {100,50}, 0.5) = {50, 70}
737
- expect(toPts[1].x).toBe(50);
738
- expect(toPts[1].y).toBe(70);
739
- });
740
-
741
- it('full replacement (zero survivors): returns arrays as-is for crossfade', () => {
742
- const prevPts = [
743
- { x: 0, y: 100 },
744
- { x: 50, y: 80 },
745
- ];
746
- const nextPts = [
747
- { x: 10, y: 90 },
748
- { x: 60, y: 70 },
749
- { x: 110, y: 50 },
750
- ];
751
- const prevKeys = ['a', 'b'];
752
- const nextKeys = ['x', 'y', 'z'];
753
-
754
- const [fromPts, toPts] = normalizePointArrays(prevPts, nextPts, prevKeys, nextKeys);
755
-
756
- // Zero survivors: arrays returned as-is
757
- expect(fromPts).toEqual(prevPts);
758
- expect(toPts).toEqual(nextPts);
759
- });
760
-
761
- it('value-only change: same keys produce 1:1 mapping', () => {
762
- const prevPts = [
763
- { x: 0, y: 100 },
764
- { x: 50, y: 80 },
765
- { x: 100, y: 60 },
766
- ];
767
- const nextPts = [
768
- { x: 0, y: 90 },
769
- { x: 50, y: 70 },
770
- { x: 100, y: 50 },
771
- ];
772
- const keys = ['a', 'b', 'c'];
773
-
774
- const [fromPts, toPts] = normalizePointArrays(prevPts, nextPts, keys, keys);
775
-
776
- expect(fromPts).toEqual(prevPts);
777
- expect(toPts).toEqual(nextPts);
778
- });
779
-
780
- it('insert at head: from position equals nearest surviving endpoint', () => {
781
- const prevPts = [
782
- { x: 50, y: 80 },
783
- { x: 100, y: 60 },
784
- ];
785
- const nextPts = [
786
- { x: 0, y: 100 },
787
- { x: 50, y: 80 },
788
- { x: 100, y: 60 },
789
- ];
790
- const prevKeys = ['b', 'c'];
791
- const nextKeys = ['a', 'b', 'c'];
792
-
793
- const [fromPts] = normalizePointArrays(prevPts, nextPts, prevKeys, nextKeys);
794
-
795
- expect(fromPts.length).toBe(3);
796
- // Inserted at head: from = nearest surviving start = b's prev position
797
- expect(fromPts[0].x).toBe(50);
798
- expect(fromPts[0].y).toBe(80);
799
- });
800
- });
801
-
802
- // ---------------------------------------------------------------------------
803
- // Line chart round-trip invariant
804
- // ---------------------------------------------------------------------------
805
-
806
- /** Build a line chart spec with animation enabled. */
807
- function lineSpec(data: Array<{ month: string; value: number }>): ChartSpec {
808
- return {
809
- animation: true,
810
- mark: 'line',
811
- data,
812
- encoding: {
813
- x: { field: 'month', type: 'ordinal' },
814
- y: { field: 'value', type: 'quantitative' },
815
- },
816
- };
817
- }
818
-
819
- /** Build an area chart spec with animation enabled. */
820
- function areaSpec(data: Array<{ month: string; value: number }>): ChartSpec {
821
- return {
822
- animation: true,
823
- mark: 'area',
824
- data,
825
- encoding: {
826
- x: { field: 'month', type: 'ordinal' },
827
- y: { field: 'value', type: 'quantitative' },
828
- },
829
- };
830
- }
831
-
832
- const LINE_DATA_A = [
833
- { month: 'Jan', value: 100 },
834
- { month: 'Feb', value: 200 },
835
- { month: 'Mar', value: 150 },
836
- ];
837
-
838
- const LINE_DATA_B = [
839
- { month: 'Jan', value: 150 },
840
- { month: 'Feb', value: 180 },
841
- { month: 'Mar', value: 220 },
842
- ];
843
-
844
- const LINE_DATA_APPEND = [
845
- { month: 'Jan', value: 100 },
846
- { month: 'Feb', value: 200 },
847
- { month: 'Mar', value: 150 },
848
- { month: 'Apr', value: 250 },
849
- ];
850
-
851
- const LINE_DATA_REMOVE = [
852
- { month: 'Jan', value: 100 },
853
- { month: 'Feb', value: 200 },
854
- ];
855
-
856
- /**
857
- * Extract path d attributes from line/area mark elements.
858
- */
859
- function extractPathD(svg: SVGElement, markClass: string): Map<string, string> {
860
- const result = new Map<string, string>();
861
- const groups = svg.querySelectorAll(`.${markClass}[data-key]`);
862
- for (const g of groups) {
863
- const key = g.getAttribute('data-key')!;
864
- const path = g.querySelector('path');
865
- if (path) {
866
- result.set(key, path.getAttribute('d') ?? '');
867
- }
868
- }
869
- return result;
870
- }
871
-
872
- /**
873
- * Run a round-trip test for line/area charts.
874
- */
875
- function assertLineAreaRoundTrip(specA: ChartSpec, specB: ChartSpec, markClass: string) {
876
- const layoutA = compile(specA);
877
- const layoutB = compile(specB);
878
-
879
- const container = createContainer();
880
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
881
-
882
- runTransition({
883
- svg,
884
- prevLayout: layoutA,
885
- nextLayout: layoutB,
886
- animation: layoutB.animation!,
887
- onComplete: () => {},
888
- });
889
-
890
- runToCompletion();
891
-
892
- // Extract paths after transition
893
- const transitioned = extractPathD(svg, markClass);
894
-
895
- // Fresh render for comparison
896
- const { svg: freshSvg } = compileAndRender(specB);
897
- const fresh = extractPathD(freshSvg, markClass);
898
-
899
- // Same set of keys
900
- expect([...transitioned.keys()].sort()).toEqual([...fresh.keys()].sort());
901
-
902
- // Same path d per key (round-trip invariant)
903
- for (const [key, tPath] of transitioned) {
904
- const fPath = fresh.get(key);
905
- expect(fPath).toBeDefined();
906
- expect(tPath).toBe(fPath);
907
- }
908
-
909
- // No ghost elements remain
910
- expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
911
- }
912
-
913
- describe('line chart round-trip invariant', () => {
914
- it('value-only change: path matches fresh render', () => {
915
- assertLineAreaRoundTrip(lineSpec(LINE_DATA_A), lineSpec(LINE_DATA_B), 'oc-mark-line');
916
- });
917
-
918
- it('append point: path matches fresh render', () => {
919
- assertLineAreaRoundTrip(lineSpec(LINE_DATA_A), lineSpec(LINE_DATA_APPEND), 'oc-mark-line');
920
- });
921
-
922
- it('remove point: path matches fresh render', () => {
923
- assertLineAreaRoundTrip(lineSpec(LINE_DATA_A), lineSpec(LINE_DATA_REMOVE), 'oc-mark-line');
924
- });
925
- });
926
-
927
- describe('area chart round-trip invariant', () => {
928
- it('value-only change: path matches fresh render', () => {
929
- assertLineAreaRoundTrip(areaSpec(LINE_DATA_A), areaSpec(LINE_DATA_B), 'oc-mark-area');
930
- });
931
-
932
- it('append point: path matches fresh render', () => {
933
- assertLineAreaRoundTrip(areaSpec(LINE_DATA_A), areaSpec(LINE_DATA_APPEND), 'oc-mark-area');
934
- });
935
-
936
- it('remove point: path matches fresh render', () => {
937
- assertLineAreaRoundTrip(areaSpec(LINE_DATA_A), areaSpec(LINE_DATA_REMOVE), 'oc-mark-area');
938
- });
939
- });
940
-
941
- // ---------------------------------------------------------------------------
942
- // Line/area canTransition gate integration
943
- // ---------------------------------------------------------------------------
944
-
945
- describe('canTransition for line/area', () => {
946
- it('passes for line chart with different values', () => {
947
- const specA = lineSpec(LINE_DATA_A);
948
- const specB = lineSpec(LINE_DATA_B);
949
- expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
950
- });
951
-
952
- it('passes for area chart with different values', () => {
953
- const specA = areaSpec(LINE_DATA_A);
954
- const specB = areaSpec(LINE_DATA_B);
955
- expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
956
- });
957
-
958
- it('fails for line to area mark type change', () => {
959
- const specA = lineSpec(LINE_DATA_A);
960
- const specB = areaSpec(LINE_DATA_A); // same data, different mark
961
- // Can't call passingGateArgs because the mark types differ and compilation
962
- // produces different layouts. Test the gate logic directly.
963
- expect(
964
- canTransition({
965
- prevLayout: compile(specA),
966
- nextLayout: compile(specB),
967
- prevSpec: specA,
968
- nextSpec: specB,
969
- isFirstRender: false,
970
- entranceInFlight: false,
971
- }),
972
- ).toBe(false);
973
- });
974
- });
975
-
976
- // ---------------------------------------------------------------------------
977
- // Suppressed-point opacity
978
- // ---------------------------------------------------------------------------
979
-
980
- describe('suppressed-point opacity', () => {
981
- it('point with opacity="0" retains opacity="0" after transition', () => {
982
- // Create a line chart spec that produces point marks
983
- const specWithPoints: ChartSpec = {
984
- animation: true,
985
- mark: { type: 'line', point: true },
986
- data: LINE_DATA_A,
987
- encoding: {
988
- x: { field: 'month', type: 'ordinal' },
989
- y: { field: 'value', type: 'quantitative' },
990
- },
991
- };
992
-
993
- const layoutA = compile(specWithPoints);
994
- const layoutB = compile({
995
- ...specWithPoints,
996
- data: LINE_DATA_B,
997
- });
998
-
999
- const container = createContainer();
1000
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1001
-
1002
- // Find all point marks and manually set one to opacity="0"
1003
- // (simulating endpoint-marker suppression)
1004
- const points = svg.querySelectorAll('circle.oc-mark-point[data-key]');
1005
- if (points.length > 0) {
1006
- points[0].setAttribute('opacity', '0');
1007
- }
1008
-
1009
- runTransition({
1010
- svg,
1011
- prevLayout: layoutA,
1012
- nextLayout: layoutB,
1013
- animation: layoutB.animation!,
1014
- onComplete: () => {},
1015
- });
1016
-
1017
- runToCompletion();
1018
-
1019
- // The suppressed point should still have opacity="0"
1020
- if (points.length > 0) {
1021
- expect(points[0].getAttribute('opacity')).toBe('0');
1022
- }
1023
- });
1024
- });
1025
-
1026
- // ---------------------------------------------------------------------------
1027
- // Scatter/dot (point) chart transitions
1028
- // ---------------------------------------------------------------------------
1029
-
1030
- /** Build a scatter chart spec with animation enabled. */
1031
- function scatterSpec(
1032
- data: Array<{ x: number; y: number; size?: number }>,
1033
- hasSize = false,
1034
- ): ChartSpec {
1035
- const encoding: ChartSpec['encoding'] = {
1036
- x: { field: 'x', type: 'quantitative' },
1037
- y: { field: 'y', type: 'quantitative' },
1038
- };
1039
- if (hasSize) {
1040
- encoding.size = { field: 'size', type: 'quantitative' };
1041
- }
1042
- return {
1043
- animation: true,
1044
- mark: 'point',
1045
- data,
1046
- encoding,
1047
- };
1048
- }
1049
-
1050
- describe('scatter chart transitions', () => {
1051
- it('canTransition passes for point mark type', () => {
1052
- // Need 3+ points with different values so geometry actually changes
1053
- // (2 points always map to domain extremes producing identical pixel positions)
1054
- const specA = scatterSpec([
1055
- { x: 10, y: 20 },
1056
- { x: 30, y: 40 },
1057
- { x: 50, y: 60 },
1058
- ]);
1059
- const specB = scatterSpec([
1060
- { x: 10, y: 50 },
1061
- { x: 30, y: 30 },
1062
- { x: 50, y: 70 },
1063
- ]);
1064
- expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
1065
- });
1066
-
1067
- it('y-swap between same-x points tweens without identity swap (encoding.key)', () => {
1068
- // Two points at the same x, different y. After update, y values swap.
1069
- // With proper keying, each point should tween to its new y, not swap identity.
1070
- const specA: ChartSpec = {
1071
- animation: true,
1072
- mark: 'point',
1073
- data: [
1074
- { id: 'a', x: 50, y: 20 },
1075
- { id: 'b', x: 50, y: 80 },
1076
- ],
1077
- encoding: {
1078
- x: { field: 'x', type: 'quantitative' },
1079
- y: { field: 'y', type: 'quantitative' },
1080
- key: { field: 'id' },
1081
- },
1082
- };
1083
- const specB: ChartSpec = {
1084
- ...specA,
1085
- data: [
1086
- { id: 'a', x: 50, y: 80 },
1087
- { id: 'b', x: 50, y: 20 },
1088
- ],
1089
- };
1090
-
1091
- const layoutA = compile(specA);
1092
- const layoutB = compile(specB);
1093
-
1094
- // Verify marks have keys and they match across layouts
1095
- const pointsA = layoutA.marks.filter((m) => m.type === 'point');
1096
- const pointsB = layoutB.marks.filter((m) => m.type === 'point');
1097
- expect(pointsA.length).toBe(2);
1098
- expect(pointsB.length).toBe(2);
1099
-
1100
- // Keys should match: point 'a' in both, point 'b' in both
1101
- const keysA = new Set(pointsA.map((m) => m.key));
1102
- const keysB = new Set(pointsB.map((m) => m.key));
1103
- expect(keysA).toEqual(keysB);
1104
- });
1105
-
1106
- it('bubble r tween lands exactly on final value', () => {
1107
- const specA = scatterSpec(
1108
- [
1109
- { x: 10, y: 20, size: 5 },
1110
- { x: 30, y: 40, size: 10 },
1111
- ],
1112
- true,
1113
- );
1114
- const specB = scatterSpec(
1115
- [
1116
- { x: 10, y: 20, size: 15 },
1117
- { x: 30, y: 40, size: 20 },
1118
- ],
1119
- true,
1120
- );
1121
-
1122
- const layoutA = compile(specA);
1123
- const layoutB = compile(specB);
1124
-
1125
- const container = createContainer();
1126
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1127
-
1128
- runTransition({
1129
- svg,
1130
- prevLayout: layoutA,
1131
- nextLayout: layoutB,
1132
- animation: layoutB.animation!,
1133
- onComplete: () => {},
1134
- });
1135
-
1136
- runToCompletion();
1137
-
1138
- // After transition completes, check that the r values match the final layout
1139
- const pointMarks = layoutB.marks.filter((m) => m.type === 'point');
1140
- for (const mark of pointMarks) {
1141
- if (!mark.key) continue;
1142
- const el = svg.querySelector(
1143
- `circle.oc-mark-point[data-key="${mark.key}"]`,
1144
- ) as SVGElement | null;
1145
- if (!el) continue;
1146
- const rAttr = el.getAttribute('r');
1147
- expect(rAttr).toBe(String((mark as { r: number }).r));
1148
- }
1149
- });
1150
-
1151
- it('scatter round-trip: add/remove', () => {
1152
- const specA = scatterSpec([
1153
- { x: 10, y: 20 },
1154
- { x: 30, y: 40 },
1155
- { x: 50, y: 60 },
1156
- ]);
1157
- const specB = scatterSpec([
1158
- { x: 10, y: 50 },
1159
- { x: 30, y: 30 },
1160
- { x: 50, y: 70 },
1161
- { x: 70, y: 90 },
1162
- ]);
1163
-
1164
- const layoutA = compile(specA);
1165
- const layoutB = compile(specB);
1166
-
1167
- const container = createContainer();
1168
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1169
-
1170
- runTransition({
1171
- svg,
1172
- prevLayout: layoutA,
1173
- nextLayout: layoutB,
1174
- animation: layoutB.animation!,
1175
- onComplete: () => {},
1176
- });
1177
-
1178
- runToCompletion();
1179
-
1180
- // No ghost elements remain
1181
- expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
1182
-
1183
- // All next layout point marks are present with correct positions
1184
- const pointMarks = layoutB.marks.filter((m) => m.type === 'point');
1185
- for (const mark of pointMarks) {
1186
- if (!mark.key) continue;
1187
- const el = svg.querySelector(
1188
- `circle.oc-mark-point[data-key="${mark.key}"]`,
1189
- ) as SVGElement | null;
1190
- expect(el).not.toBeNull();
1191
- if (el) {
1192
- expect(el.getAttribute('cx')).toBe(String((mark as { cx: number }).cx));
1193
- expect(el.getAttribute('cy')).toBe(String((mark as { cy: number }).cy));
1194
- }
1195
- }
1196
- });
1197
- });
1198
-
1199
- // ---------------------------------------------------------------------------
1200
- // Axis tick transitions
1201
- // ---------------------------------------------------------------------------
1202
-
1203
- describe('axis tick transitions', () => {
1204
- it('data-tick-key is stamped on tick labels', () => {
1205
- const spec = columnSpec(DATA_A);
1206
- const { svg } = compileAndRender(spec);
1207
-
1208
- const tickLabels = svg.querySelectorAll('.oc-axis-tick[data-tick-key]');
1209
- expect(tickLabels.length).toBeGreaterThan(0);
1210
- });
1211
-
1212
- it('data-tick-key is stamped on gridlines', () => {
1213
- const spec = columnSpec(DATA_A);
1214
- const { svg } = compileAndRender(spec);
1215
-
1216
- const gridlines = svg.querySelectorAll('.oc-gridline[data-tick-key]');
1217
- expect(gridlines.length).toBeGreaterThan(0);
1218
- });
1219
-
1220
- it('adding a category: surviving tick labels from/to match prev/next layout tick positions', () => {
1221
- const specA = columnSpec(DATA_A); // Q1, Q2, Q3
1222
- const specB = columnSpec(DATA_C); // Q1, Q2, Q3, Q4
1223
-
1224
- const layoutA = compile(specA);
1225
- const layoutB = compile(specB);
1226
-
1227
- // Surviving x-axis ticks (Q1, Q2, Q3) should have different positions
1228
- // between layoutA and layoutB because adding Q4 changes the band scale
1229
- const prevXTicks = layoutA.axes.x?.ticks ?? [];
1230
- const nextXTicks = layoutB.axes.x?.ticks ?? [];
1231
-
1232
- // Q1 should exist in both
1233
- const prevQ1 = prevXTicks.find((t) => t.label === 'Q1');
1234
- const nextQ1 = nextXTicks.find((t) => t.label === 'Q1');
1235
- expect(prevQ1).toBeDefined();
1236
- expect(nextQ1).toBeDefined();
1237
-
1238
- // Now render and transition
1239
- const container = createContainer();
1240
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1241
-
1242
- runTransition({
1243
- svg,
1244
- prevLayout: layoutA,
1245
- nextLayout: layoutB,
1246
- animation: layoutB.animation!,
1247
- onComplete: () => {},
1248
- });
1249
-
1250
- runToCompletion();
1251
-
1252
- // After transition, verify tick labels are at final positions
1253
- const tickLabels = svg.querySelectorAll('.oc-axis-x .oc-axis-tick[data-tick-key]');
1254
- expect(tickLabels.length).toBe(nextXTicks.length);
1255
-
1256
- // No ghost elements remain
1257
- expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
1258
- });
1259
-
1260
- it('removed tick ghost-fades', () => {
1261
- const specA = columnSpec(DATA_C); // Q1, Q2, Q3, Q4
1262
- const specB = columnSpec(DATA_A); // Q1, Q2, Q3
1263
-
1264
- const layoutA = compile(specA);
1265
- const layoutB = compile(specB);
1266
-
1267
- const container = createContainer();
1268
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1269
-
1270
- runTransition({
1271
- svg,
1272
- prevLayout: layoutA,
1273
- nextLayout: layoutB,
1274
- animation: layoutB.animation!,
1275
- onComplete: () => {},
1276
- });
1277
-
1278
- // After starting, there should be ghost tick labels for the removed Q4
1279
- pumpRaf(0);
1280
- const tickGhosts = svg.querySelectorAll('.oc-axis-tick.oc-ghost');
1281
- // Q4 was removed, so there should be at least one ghost
1282
- expect(tickGhosts.length).toBeGreaterThan(0);
1283
-
1284
- // Run to completion
1285
- pumpRaf(2000);
1286
-
1287
- // Ghosts should be cleaned up
1288
- expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
1289
- });
1290
- });
1291
-
1292
- // ---------------------------------------------------------------------------
1293
- // Gradient ghost test
1294
- // ---------------------------------------------------------------------------
1295
-
1296
- describe('gradient ghost', () => {
1297
- it('exiting mark with gradient fill: new SVG defs contain the gradient, ghost fill references valid ID', () => {
1298
- // Create an area chart spec with gradient fill (area marks use gradients by default)
1299
- const specA: ChartSpec = {
1300
- animation: true,
1301
- mark: 'area',
1302
- data: [
1303
- { month: 'Jan', sales: 100, group: 'A' },
1304
- { month: 'Feb', sales: 200, group: 'A' },
1305
- { month: 'Jan', sales: 80, group: 'B' },
1306
- { month: 'Feb', sales: 150, group: 'B' },
1307
- ],
1308
- encoding: {
1309
- x: { field: 'month', type: 'ordinal' },
1310
- y: { field: 'sales', type: 'quantitative' },
1311
- color: { field: 'group', type: 'nominal' },
1312
- },
1313
- };
1314
- // Remove group B
1315
- const specB: ChartSpec = {
1316
- ...specA,
1317
- data: [
1318
- { month: 'Jan', sales: 120, group: 'A' },
1319
- { month: 'Feb', sales: 220, group: 'A' },
1320
- ],
1321
- };
1322
-
1323
- const layoutA = compile(specA);
1324
- const layoutB = compile(specB);
1325
-
1326
- // Check if any marks have gradient fills
1327
- const hasGradients = layoutA.marks.some(
1328
- (m) => 'fill' in m && typeof m.fill !== 'string' && m.fill !== undefined,
1329
- );
1330
-
1331
- // If the chart type produces gradients, verify ghost handling
1332
- if (hasGradients) {
1333
- const container = createContainer();
1334
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1335
-
1336
- runTransition({
1337
- svg,
1338
- prevLayout: layoutA,
1339
- nextLayout: layoutB,
1340
- animation: layoutB.animation!,
1341
- onComplete: () => {},
1342
- });
1343
-
1344
- // After starting, check that ghost fill references a valid gradient
1345
- const ghosts = svg.querySelectorAll('.oc-ghost path[fill]');
1346
- for (const ghost of ghosts) {
1347
- const fill = ghost.getAttribute('fill') ?? '';
1348
- if (fill.startsWith('url(#')) {
1349
- const id = fill.slice(5, -1);
1350
- const gradientEl = svg.querySelector(`#${id}`);
1351
- expect(gradientEl).not.toBeNull();
1352
- }
1353
- }
1354
-
1355
- runToCompletion();
1356
- expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
1357
- }
1358
- });
1359
- });
1360
-
1361
- // ---------------------------------------------------------------------------
1362
- // Interruption / retargeting
1363
- // ---------------------------------------------------------------------------
1364
-
1365
- const DATA_D = [
1366
- { category: 'Q1', value: 300 },
1367
- { category: 'Q2', value: 100 },
1368
- { category: 'Q3', value: 250 },
1369
- ];
1370
-
1371
- describe('interruption retargeting', () => {
1372
- it('A -> B -> interrupt at ~50% -> C -> complete -> equals fresh render of C', () => {
1373
- const specA = columnSpec(DATA_A);
1374
- const specB = columnSpec(DATA_B);
1375
- const specC = columnSpec(DATA_D);
1376
-
1377
- // Use createChart so update() handles snapshot plumbing
1378
- const container = createContainer();
1379
- const chart = createChart(container, specA);
1380
-
1381
- // Update A -> B, start transition
1382
- chart.update(specB);
1383
-
1384
- // Pump to ~50% of the transition (start at t=0, then advance)
1385
- pumpRaf(0);
1386
- pumpRaf(250); // 250ms into ~500ms transition
1387
-
1388
- // Interrupt with C
1389
- chart.update(specC);
1390
-
1391
- // Run the C transition to completion
1392
- pumpRaf(0);
1393
- pumpRaf(2000);
1394
-
1395
- // Extract geometry from the current SVG
1396
- const svg = container.querySelector('svg') as SVGSVGElement;
1397
- const transitioned = extractRectGeometry(svg);
1398
-
1399
- // Fresh render of specC for comparison
1400
- const { svg: freshSvg } = compileAndRender(specC);
1401
- const fresh = extractRectGeometry(freshSvg);
1402
-
1403
- // Same set of keys
1404
- expect([...transitioned.keys()].sort()).toEqual([...fresh.keys()].sort());
1405
-
1406
- // Same geometry per key (round-trip invariant holds through interruption)
1407
- for (const [key, tGeom] of transitioned) {
1408
- const fGeom = fresh.get(key);
1409
- expect(fGeom).toBeDefined();
1410
- expect(tGeom).toEqual(fGeom);
1411
- }
1412
-
1413
- // No ghost elements remain
1414
- expect(svg.querySelectorAll('.oc-ghost').length).toBe(0);
1415
-
1416
- chart.destroy();
1417
- });
1418
-
1419
- it('snapshot captures intermediate rect geometry', () => {
1420
- const specA = columnSpec(DATA_A);
1421
- const specB = columnSpec(DATA_B);
1422
-
1423
- const layoutA = compile(specA);
1424
- const layoutB = compile(specB);
1425
-
1426
- const container = createContainer();
1427
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1428
-
1429
- const handle = runTransition({
1430
- svg,
1431
- prevLayout: layoutA,
1432
- nextLayout: layoutB,
1433
- animation: layoutB.animation!,
1434
- onComplete: () => {},
1435
- });
1436
-
1437
- // Pump to start, then mid-transition
1438
- pumpRaf(0);
1439
- pumpRaf(250);
1440
-
1441
- const snap = handle.snapshot();
1442
-
1443
- // Should have entries for updated marks
1444
- expect(snap.size).toBeGreaterThan(0);
1445
-
1446
- // Each entry should be a rect with intermediate values
1447
- for (const [, geom] of snap) {
1448
- expect(geom.type).toBe('rect');
1449
- }
1450
-
1451
- handle.cancel();
1452
- });
1453
- });
1454
-
1455
- // ---------------------------------------------------------------------------
1456
- // Reduced motion
1457
- // ---------------------------------------------------------------------------
1458
-
1459
- describe('reduced motion', () => {
1460
- it('canTransition returns false when prefers-reduced-motion matches', () => {
1461
- const original = window.matchMedia;
1462
- vi.stubGlobal('matchMedia', (query: string) => ({
1463
- matches: query === '(prefers-reduced-motion: reduce)',
1464
- media: query,
1465
- addEventListener: () => {},
1466
- removeEventListener: () => {},
1467
- }));
1468
-
1469
- const specA = columnSpec(DATA_A);
1470
- const specB = columnSpec(DATA_B);
1471
- expect(canTransition(passingGateArgs(specA, specB))).toBe(false);
1472
-
1473
- vi.stubGlobal('matchMedia', original);
1474
- });
1475
-
1476
- it('canTransition returns true when reduced-motion is not active', () => {
1477
- const original = window.matchMedia;
1478
- vi.stubGlobal('matchMedia', (query: string) => ({
1479
- matches: false,
1480
- media: query,
1481
- addEventListener: () => {},
1482
- removeEventListener: () => {},
1483
- }));
1484
-
1485
- const specA = columnSpec(DATA_A);
1486
- const specB = columnSpec(DATA_B);
1487
- expect(canTransition(passingGateArgs(specA, specB))).toBe(true);
1488
-
1489
- vi.stubGlobal('matchMedia', original);
1490
- });
1491
- });
1492
-
1493
- // ---------------------------------------------------------------------------
1494
- // React StrictMode double-mount
1495
- // ---------------------------------------------------------------------------
1496
-
1497
- describe('React StrictMode double-mount', () => {
1498
- it('destroy cancels rAF loop; fresh mount does not inherit stale state', () => {
1499
- const specA = columnSpec(DATA_A);
1500
- const specB = columnSpec(DATA_B);
1501
-
1502
- const container = createContainer();
1503
-
1504
- // First mount
1505
- const chart1 = createChart(container, specA);
1506
- chart1.update(specB);
1507
- pumpRaf(0); // start transition
1508
-
1509
- // Destroy mid-transition (simulates StrictMode unmount)
1510
- chart1.destroy();
1511
-
1512
- // Record rAF callback count after destroy
1513
- const callbacksAfterDestroy = rafCallbacks.size;
1514
-
1515
- // Second mount (fresh closure)
1516
- const chart2 = createChart(container, specA);
1517
- chart2.update(specB);
1518
- pumpRaf(0);
1519
-
1520
- // The old transition's rAF should not have re-registered
1521
- // (only the new transition should be running)
1522
- // Pump to completion - if old rAF leaked, it would crash or write to removed DOM
1523
- pumpRaf(2000);
1524
-
1525
- // Clean up
1526
- chart2.destroy();
1527
-
1528
- // No crash = success: the first transition did not leak rAF callbacks
1529
- expect(callbacksAfterDestroy).toBe(0);
1530
- });
1531
- });
1532
-
1533
- // ---------------------------------------------------------------------------
1534
- // Secondary element crossfade
1535
- // ---------------------------------------------------------------------------
1536
-
1537
- describe('secondary element crossfade', () => {
1538
- it('fades in an annotation that did not exist before', () => {
1539
- const specA: ChartSpec = columnSpec(DATA_A);
1540
- const specB: ChartSpec = {
1541
- ...columnSpec(DATA_B),
1542
- annotations: [{ type: 'text', x: 'Q1', y: 150, text: 'Note' }],
1543
- };
1544
-
1545
- const layoutA = compile(specA);
1546
- const layoutB = compile(specB);
1547
-
1548
- const container = createContainer();
1549
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1550
-
1551
- runTransition({
1552
- svg,
1553
- prevLayout: layoutA,
1554
- nextLayout: layoutB,
1555
- animation: layoutB.animation!,
1556
- onComplete: () => {},
1557
- });
1558
-
1559
- const annotations = svg.querySelectorAll('.oc-annotation');
1560
- expect(annotations.length).toBeGreaterThan(0);
1561
- for (const ann of annotations) {
1562
- expect((ann as SVGElement).style.opacity).toBe('0');
1563
- }
1564
-
1565
- runToCompletion();
1566
-
1567
- for (const ann of annotations) {
1568
- expect((ann as SVGElement).style.opacity).toBe('1');
1569
- }
1570
- });
1571
-
1572
- it('does not re-fade an annotation that was already on screen', () => {
1573
- // The blink bug: the old blanket crossfade drove EVERY annotation from
1574
- // opacity 0 on every update, so an unchanged annotation flickered out and
1575
- // back in whenever any other part of the chart moved.
1576
- const annotation = { type: 'text' as const, x: 'Q1', y: 100, text: 'Note' };
1577
- const layoutA = compile({ ...columnSpec(DATA_A), annotations: [annotation] });
1578
- const layoutB = compile({ ...columnSpec(DATA_B), annotations: [annotation] });
1579
-
1580
- const container = createContainer();
1581
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1582
-
1583
- runTransition({
1584
- svg,
1585
- prevLayout: layoutA,
1586
- nextLayout: layoutB,
1587
- animation: layoutB.animation!,
1588
- onComplete: () => {},
1589
- });
1590
-
1591
- const annotations = svg.querySelectorAll('.oc-annotation');
1592
- expect(annotations.length).toBeGreaterThan(0);
1593
- for (const ann of annotations) {
1594
- expect((ann as SVGElement).style.opacity).not.toBe('0');
1595
- }
1596
- });
1597
-
1598
- it('endpoint labels start at opacity 0 during transition', () => {
1599
- // Use a line chart that produces endpoint labels
1600
- const lineSpecWithLabels: ChartSpec = {
1601
- animation: true,
1602
- mark: 'line',
1603
- data: [
1604
- { month: 'Jan', value: 100, group: 'A' },
1605
- { month: 'Feb', value: 200, group: 'A' },
1606
- { month: 'Jan', value: 80, group: 'B' },
1607
- { month: 'Feb', value: 150, group: 'B' },
1608
- ],
1609
- encoding: {
1610
- x: { field: 'month', type: 'ordinal' },
1611
- y: { field: 'value', type: 'quantitative' },
1612
- color: { field: 'group', type: 'nominal' },
1613
- },
1614
- };
1615
- const lineSpecB: ChartSpec = {
1616
- ...lineSpecWithLabels,
1617
- data: [
1618
- { month: 'Jan', value: 120, group: 'A' },
1619
- { month: 'Feb', value: 220, group: 'A' },
1620
- { month: 'Jan', value: 90, group: 'B' },
1621
- { month: 'Feb', value: 170, group: 'B' },
1622
- ],
1623
- };
1624
-
1625
- const layoutA = compile(lineSpecWithLabels);
1626
- const layoutB = compile(lineSpecB);
1627
-
1628
- const container = createContainer();
1629
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1630
-
1631
- runTransition({
1632
- svg,
1633
- prevLayout: layoutA,
1634
- nextLayout: layoutB,
1635
- animation: layoutB.animation!,
1636
- onComplete: () => {},
1637
- });
1638
-
1639
- // Check if endpoint labels exist
1640
- const epLabels = svg.querySelector('.oc-endpoint-labels') as SVGElement | null;
1641
- if (epLabels) {
1642
- expect(epLabels.style.opacity).toBe('0');
1643
-
1644
- runToCompletion();
1645
-
1646
- // After completion, opacity restored
1647
- expect(epLabels.style.opacity).toBe('');
1648
- }
1649
- });
1650
- });
1651
-
1652
- // ---------------------------------------------------------------------------
1653
- // Mark color interpolation
1654
- // ---------------------------------------------------------------------------
1655
-
1656
- describe('mark color interpolation', () => {
1657
- const COLORED: ChartSpec = {
1658
- animation: true,
1659
- mark: 'line',
1660
- data: [
1661
- { month: 'Jan', value: 10, series: 'A' },
1662
- { month: 'Feb', value: 20, series: 'A' },
1663
- { month: 'Jan', value: 30, series: 'B' },
1664
- { month: 'Feb', value: 40, series: 'B' },
1665
- ],
1666
- encoding: {
1667
- x: { field: 'month', type: 'ordinal' },
1668
- y: { field: 'value', type: 'quantitative' },
1669
- color: { field: 'series', type: 'nominal' },
1670
- },
1671
- };
1672
-
1673
- const HIGHLIGHTED: ChartSpec = {
1674
- ...COLORED,
1675
- encoding: {
1676
- ...COLORED.encoding,
1677
- color: { field: 'series', type: 'nominal', highlight: ['A'] },
1678
- },
1679
- };
1680
-
1681
- it('tweens stroke through an intermediate color instead of snapping', () => {
1682
- const layoutA = compile(COLORED);
1683
- const layoutB = compile(HIGHLIGHTED);
1684
-
1685
- // The highlight must actually recolor something, else this proves nothing.
1686
- const strokesA = layoutA.marks.map((m) => (m as { stroke?: string }).stroke);
1687
- const strokesB = layoutB.marks.map((m) => (m as { stroke?: string }).stroke);
1688
- expect(strokesA).not.toEqual(strokesB);
1689
-
1690
- const container = createContainer();
1691
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1692
-
1693
- runTransition({
1694
- svg,
1695
- prevLayout: layoutA,
1696
- nextLayout: layoutB,
1697
- animation: layoutB.animation!,
1698
- onComplete: () => {},
1699
- });
1700
-
1701
- // At t=0 the muted line must be back at its ORIGINAL color, not the
1702
- // already-rendered muted gray -- that is what makes it a fade and not a snap.
1703
- const muted = layoutB.marks.findIndex(
1704
- (m, i) =>
1705
- (m as { stroke?: string }).stroke !== (layoutA.marks[i] as { stroke?: string }).stroke,
1706
- );
1707
- expect(muted).toBeGreaterThanOrEqual(0);
1708
- const key = (layoutB.marks[muted] as { key?: string }).key;
1709
- const el = svg.querySelector(`[data-key="${key}"]`) as SVGElement;
1710
- const shape = (el.querySelector('path, line, rect, circle') as SVGElement) ?? el;
1711
-
1712
- const from = (layoutA.marks[muted] as { stroke?: string }).stroke;
1713
- const to = (layoutB.marks[muted] as { stroke?: string }).stroke;
1714
-
1715
- pumpRaf(0);
1716
- const atStart = shape.getAttribute('stroke');
1717
- expect(atStart).not.toBe(to);
1718
-
1719
- runToCompletion();
1720
- // And it lands exactly on the final color.
1721
- const atEnd = shape.getAttribute('stroke');
1722
- expect(atEnd).toBe(to);
1723
- expect(atEnd).not.toBe(from);
1724
- });
1725
-
1726
- it('strokes the area top-line, never the closed fill path', () => {
1727
- // An area mark is two stacked paths: the closed fill shape (stroke: none)
1728
- // and `.oc-area-top`, which traces the data points alone. A blanket
1729
- // `querySelector` grabs the *first* one, so a stroke tween would outline
1730
- // the whole closed shape -- baseline included -- and snapTweenToFinal
1731
- // would leave that outline on screen permanently.
1732
- const AREA: ChartSpec = { ...COLORED, mark: 'area' };
1733
- const AREA_HL: ChartSpec = {
1734
- ...AREA,
1735
- encoding: {
1736
- ...AREA.encoding,
1737
- color: { field: 'series', type: 'nominal', highlight: ['A'] },
1738
- },
1739
- };
1740
-
1741
- const layoutA = compile(AREA);
1742
- const layoutB = compile(AREA_HL);
1743
-
1744
- const muted = layoutB.marks.findIndex(
1745
- (m, i) =>
1746
- (m as { stroke?: string }).stroke !== (layoutA.marks[i] as { stroke?: string }).stroke,
1747
- );
1748
- expect(muted).toBeGreaterThanOrEqual(0);
1749
-
1750
- const container = createContainer();
1751
- const svg = renderChartSVG(layoutB, container) as SVGSVGElement;
1752
-
1753
- runTransition({
1754
- svg,
1755
- prevLayout: layoutA,
1756
- nextLayout: layoutB,
1757
- animation: layoutB.animation!,
1758
- onComplete: () => {},
1759
- });
1760
- runToCompletion();
1761
-
1762
- const key = (layoutB.marks[muted] as { key?: string }).key;
1763
- const group = svg.querySelector(`[data-key="${key}"]`) as SVGElement;
1764
- const areaTop = group.querySelector('.oc-area-top') as SVGElement;
1765
- const fillPath = group.querySelector('path:not(.oc-area-top)') as SVGElement;
1766
- expect(areaTop).toBeTruthy();
1767
- expect(fillPath).toBeTruthy();
1768
-
1769
- // The stroke landed on the top-line...
1770
- const to = (layoutB.marks[muted] as { stroke?: string }).stroke;
1771
- expect(areaTop.getAttribute('stroke')).toBe(to);
1772
-
1773
- // ...and the fill path is still unstroked. Anything else is an outline
1774
- // traced around the entire filled region.
1775
- expect(fillPath.getAttribute('stroke')).toBe('none');
1776
- });
1777
- });