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