@opendata-ai/openchart-svelte 9.0.0-rc.2 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Chart.svelte CHANGED
@@ -14,6 +14,7 @@ import type {
14
14
  ElementRef,
15
15
  LayerSpec,
16
16
  MarkEvent,
17
+ MarkFocusEvent,
17
18
  ReaderChangeMeta,
18
19
  ThemeConfig,
19
20
  } from '@opendata-ai/openchart-core';
@@ -44,6 +45,7 @@ let {
44
45
  onedit,
45
46
  onmarkclick,
46
47
  onmarkhover,
48
+ onmarkfocus,
47
49
  onmarkleave,
48
50
  onlegendtoggle,
49
51
  onannotationclick,
@@ -71,6 +73,7 @@ let {
71
73
  onedit?: (tx: EditTransaction) => void;
72
74
  onmarkclick?: (event: MarkEvent) => void;
73
75
  onmarkhover?: (event: MarkEvent) => void;
76
+ onmarkfocus?: (event: MarkFocusEvent) => void;
74
77
  onmarkleave?: () => void;
75
78
  onlegendtoggle?: (series: string, visible: boolean) => void;
76
79
  onannotationclick?: (annotation: Annotation, event: MouseEvent, element: ElementRef) => void;
@@ -112,6 +115,7 @@ onMount(() => {
112
115
  const stableHandlers: MountOptions = {
113
116
  onMarkClick: (event: MarkEvent) => untrack(() => onmarkclick)?.(event),
114
117
  onMarkHover: (event: MarkEvent) => untrack(() => onmarkhover)?.(event),
118
+ onMarkFocus: (event: MarkFocusEvent) => untrack(() => onmarkfocus)?.(event),
115
119
  onMarkLeave: () => untrack(() => onmarkleave)?.(),
116
120
  onLegendToggle: (series: string, visible: boolean) =>
117
121
  untrack(() => onlegendtoggle)?.(series, visible),
@@ -1,4 +1,4 @@
1
- import type { Annotation, ChartSpec, DarkMode, EditTransaction, ElementRef, LayerSpec, MarkEvent, ReaderChangeMeta, ThemeConfig } from '@opendata-ai/openchart-core';
1
+ import type { Annotation, ChartSpec, DarkMode, EditTransaction, ElementRef, LayerSpec, MarkEvent, MarkFocusEvent, ReaderChangeMeta, ThemeConfig } from '@opendata-ai/openchart-core';
2
2
  import { type ChartInstance, type ChartReaderSelection, type InteractionMode, type ReaderPresentationOptions } from '@opendata-ai/openchart-vanilla';
3
3
  type $$ComponentProps = {
4
4
  reader?: false | ReaderPresentationOptions;
@@ -15,6 +15,7 @@ type $$ComponentProps = {
15
15
  onedit?: (tx: EditTransaction) => void;
16
16
  onmarkclick?: (event: MarkEvent) => void;
17
17
  onmarkhover?: (event: MarkEvent) => void;
18
+ onmarkfocus?: (event: MarkFocusEvent) => void;
18
19
  onmarkleave?: () => void;
19
20
  onlegendtoggle?: (series: string, visible: boolean) => void;
20
21
  onannotationclick?: (annotation: Annotation, event: MouseEvent, element: ElementRef) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"Chart.svelte.d.ts","sourceRoot":"","sources":["../src/Chart.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,UAAU,EACV,SAAS,EACT,QAAQ,EACR,eAAe,EACf,UAAU,EACV,SAAS,EACT,SAAS,EACT,gBAAgB,EAChB,WAAW,EACZ,MAAM,6BAA6B,CAAC;AAErC,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,oBAAoB,EAGzB,KAAK,eAAe,EAEpB,KAAK,yBAAyB,EAC/B,MAAM,gCAAgC,CAAC;AAIvC,KAAK,gBAAgB,GAAI;IACxB,MAAM,CAAC,EAAE,KAAK,GAAG,yBAAyB,CAAC;IAC3C,eAAe,CAAC,EAAE,oBAAoB,CAAC;IACvC,cAAc,CAAC,EAAE,CAAC,SAAS,EAAE,oBAAoB,EAAE,IAAI,EAAE,gBAAgB,KAAK,IAAI,CAAC;IACnF,IAAI,EAAE,SAAS,GAAG,SAAS,CAAC;IAC5B,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC;IACrC,uDAAuD;IACvD,QAAQ,CAAC,EAAE,OAAO,CAA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,cAAc,KAAK,IAAI,CAAC;IAC9C,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC;IACzB,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IAC5D,iBAAiB,CAAC,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,KAAK,IAAI,CAAC;IAC7F,iBAAiB,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;IAC/C,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC;QAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,KAAK,IAAI,CAAC;IACrE,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,UAAU,KAAK,IAAI,CAAC;IACzC,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,UAAU,KAAK,IAAI,CAAC;IAC3C,WAAW,CAAC,EAAE,eAAe,CAAC;IAC9B,eAAe,CAAC,EAAE,UAAU,CAAC;IAC7B,SAAS,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAkNF,QAAA,MAAM,KAAK;8BAvBqB,UAAU,GAAG,IAAI;kBAI3B,UAAU,KAAG,IAAI;oBAIjB,IAAI;uBAKD,aAAa,GAAG,IAAI;MAUM,CAAC;AACpD,KAAK,KAAK,GAAG,UAAU,CAAC,OAAO,KAAK,CAAC,CAAC;AACtC,eAAe,KAAK,CAAC"}
package/dist/Graph.svelte CHANGED
@@ -131,6 +131,9 @@ $effect(() => {
131
131
  const dims = dimensions;
132
132
  const currentSpec = untrack(() => spec);
133
133
 
134
+ // A recreation suppresses the entrance only when the previous instance's
135
+ // played: one still waiting to be seen replays on the new instance.
136
+ const prevEntrancePlayed = instance?.entrancePlayed ?? false;
134
137
  instance?.destroy();
135
138
 
136
139
  const tooltipOption: GraphMountOptions['tooltip'] = on
@@ -157,7 +160,7 @@ $effect(() => {
157
160
  // untrack so reading the flag doesn't add a dependency; the value is set
158
161
  // after the first mount and reset only on component teardown. A dimension
159
162
  // change is exempt: the incoming renderer should play its entrance.
160
- suppressEntrance: untrack(() => mountedOnce) && dims === prevDimensions,
163
+ suppressEntrance: untrack(() => mountedOnce) && dims === prevDimensions && prevEntrancePlayed,
161
164
  };
162
165
 
163
166
  instance = createGraph(containerEl, currentSpec, options);
@@ -1 +1 @@
1
- {"version":3,"file":"Graph.svelte.d.ts","sourceRoot":"","sources":["../src/Graph.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAEpF,OAAO,EACL,KAAK,mBAAmB,EAExB,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,oBAAoB,EAEzB,KAAK,eAAe,EAEpB,KAAK,qBAAqB,EAC3B,MAAM,gCAAgC,CAAC;AAKxC,gFAAgF;AAChF,KAAK,gBAAgB,GAAG,OAAO,GAAG;IAAE,SAAS,CAAC,EAAE,qBAAqB,CAAA;CAAE,CAAC;AACxE,4FAA4F;AAC5F,KAAK,eAAe,GAAG,OAAO,GAAG;IAAE,WAAW,CAAC,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAC5E,KAAK,gBAAgB,GAAI;IACxB,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,iNAAiN;IACjN,WAAW,C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AAI;yBAI/C,MAAM,SAAS,mBAAmB,GAAG;QAAE,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,KAAG,IAAI;oBAInE,cAAc,SAAS,mBAAmB,KAAG,IAAI;kBAInD,MAAM,KAAK,MAAM,SAAS,mBAAmB,KAAG,IAAI;qBAInD,WAAW;yBAIL,MAAM,SAAS;QAAE,GAAG,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,mBAAmB,KAAG,IAAI;4BAI5D,MAAM,EAAE;wBAIV,oBAAoB,SAAS;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,KAAG,IAAI;0BAI3D,IAAI;wBAIN,MAAM,EAAE,GAAG,IAAI;qBAIlB,eAAe,GAAG,IAAI;kCAIP,MAAM,EAAE,KAAG,IAAI;+BAIpB,MAAM,EAAE;uBAGd,SAAS,KAAG,IAAI;MAUQ,CAAC;AACpD,KAAK,KAAK,GAAG,UAAU,CAAC,OAAO,KAAK,CAAC,CAAC;AACtC,eAAe,KAAK,CAAC"}
@@ -0,0 +1,113 @@
1
+ <script module lang="ts">
2
+ import type { DarkMode, RaceFrame, RaceSpec, ThemeConfig } from '@opendata-ai/openchart-core';
3
+ import type { RaceMountOptions } from '@opendata-ai/openchart-vanilla';
4
+
5
+ /** Options and callbacks for a mounted race chart. */
6
+ export interface RaceProps {
7
+ spec: RaceSpec;
8
+ theme?: ThemeConfig;
9
+ darkMode?: DarkMode;
10
+ class?: string;
11
+ style?: string;
12
+ autoResize?: boolean;
13
+ watermark?: boolean;
14
+ onWarn?: RaceMountOptions['onWarn'];
15
+ onperiod?: (period: RaceFrame['period']) => void;
16
+ }
17
+ </script>
18
+
19
+ <script lang="ts">
20
+ import { mergeThemeConfig } from '@opendata-ai/openchart-core';
21
+ import { createRace, type RaceInstance } from '@opendata-ai/openchart-vanilla';
22
+ import { onMount, untrack } from 'svelte';
23
+ import { getVizDarkMode, getVizTheme } from './context.js';
24
+
25
+ let {
26
+ spec,
27
+ theme,
28
+ darkMode,
29
+ class: className,
30
+ style,
31
+ autoResize,
32
+ watermark,
33
+ onWarn,
34
+ onperiod,
35
+ }: RaceProps = $props();
36
+
37
+ let containerEl: HTMLDivElement;
38
+ let instance = $state.raw<RaceInstance | null>(null);
39
+ let mountedOptionsKey = '';
40
+ let mountedThemeKey = '';
41
+ let mountedSpecKey = '';
42
+ let periodSubscription:
43
+ | { instance: RaceInstance; callback: NonNullable<RaceProps['onperiod']>; unsubscribe: () => void }
44
+ | undefined;
45
+
46
+ const contextTheme = getVizTheme();
47
+ const contextDarkMode = getVizDarkMode();
48
+ const options = $derived<RaceMountOptions>({
49
+ theme: mergeThemeConfig(contextTheme?.(), theme),
50
+ darkMode: darkMode ?? contextDarkMode?.(),
51
+ autoResize,
52
+ watermark,
53
+ onWarn: (message) => (untrack(() => onWarn) ?? console.warn)(message),
54
+ });
55
+ const optionsKey = $derived(JSON.stringify({ autoResize, watermark }));
56
+ const themeKey = $derived(JSON.stringify({ theme: options.theme, darkMode: options.darkMode }));
57
+
58
+ /** Read the current race instance through bind:this. */
59
+ export function getInstance(): RaceInstance | null {
60
+ return instance;
61
+ }
62
+
63
+ onMount(() => () => {
64
+ periodSubscription?.unsubscribe();
65
+ periodSubscription = undefined;
66
+ instance?.destroy();
67
+ instance = null;
68
+ });
69
+
70
+ $effect(() => {
71
+ const container = containerEl;
72
+ const nextSpecKey = JSON.stringify(spec);
73
+ const nextOptionsKey = optionsKey;
74
+ const nextThemeKey = themeKey;
75
+
76
+ untrack(() => {
77
+ const previous = instance;
78
+ if (previous && nextOptionsKey === mountedOptionsKey) {
79
+ if (nextSpecKey !== mountedSpecKey) {
80
+ previous.update(spec);
81
+ mountedSpecKey = nextSpecKey;
82
+ }
83
+ if (nextThemeKey !== mountedThemeKey) {
84
+ previous.setTheme(options);
85
+ mountedThemeKey = nextThemeKey;
86
+ }
87
+ return;
88
+ }
89
+
90
+ const state = previous?.getState();
91
+ const current = createRace(container, spec, { ...options, initialState: state });
92
+ previous?.destroy();
93
+ if (state) current.resize();
94
+ instance = current;
95
+ mountedOptionsKey = nextOptionsKey;
96
+ mountedThemeKey = nextThemeKey;
97
+ mountedSpecKey = nextSpecKey;
98
+ });
99
+ });
100
+
101
+ $effect(() => {
102
+ const current = instance;
103
+ const callback = onperiod;
104
+ if (periodSubscription?.instance === current && periodSubscription.callback === callback) return;
105
+ periodSubscription?.unsubscribe();
106
+ periodSubscription =
107
+ current && callback
108
+ ? { instance: current, callback, unsubscribe: current.on('period', callback) }
109
+ : undefined;
110
+ });
111
+ </script>
112
+
113
+ <div bind:this={containerEl} class={className ? `oc-root ${className}` : 'oc-root'} {style}></div>
@@ -0,0 +1,21 @@
1
+ import type { DarkMode, RaceFrame, RaceSpec, ThemeConfig } from '@opendata-ai/openchart-core';
2
+ import type { RaceMountOptions } from '@opendata-ai/openchart-vanilla';
3
+ /** Options and callbacks for a mounted race chart. */
4
+ export interface RaceProps {
5
+ spec: RaceSpec;
6
+ theme?: ThemeConfig;
7
+ darkMode?: DarkMode;
8
+ class?: string;
9
+ style?: string;
10
+ autoResize?: boolean;
11
+ watermark?: boolean;
12
+ onWarn?: RaceMountOptions['onWarn'];
13
+ onperiod?: (period: RaceFrame['period']) => void;
14
+ }
15
+ import { type RaceInstance } from '@opendata-ai/openchart-vanilla';
16
+ declare const Race: import("svelte").Component<RaceProps, {
17
+ getInstance: () => RaceInstance | null;
18
+ }, "">;
19
+ type Race = ReturnType<typeof Race>;
20
+ export default Race;
21
+ //# sourceMappingURL=Race.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Race.svelte.d.ts","sourceRoot":"","sources":["../src/Race.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC9F,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAEvE,sDAAsD;AACtD,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,gBAAgB,CAAC,QAAQ,CAAC,CAAC;IACpC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC,QAAQ,CAAC,KAAK,IAAI,CAAC;CAClD;AAID,OAAO,EAAc,KAAK,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAwG/E,QAAA,MAAM,IAAI;uBA5De,YAAY,GAAG,IAAI;MA4DM,CAAC;AACnD,KAAK,IAAI,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC;AACpC,eAAe,IAAI,CAAC"}
@@ -10,6 +10,7 @@ import {
10
10
  isBarListSpec,
11
11
  isGeoMapSpec,
12
12
  isGraphSpec,
13
+ isRaceSpec,
13
14
  isSankeySpec,
14
15
  isTableSpec,
15
16
  isTileMapSpec,
@@ -19,6 +20,7 @@ import Chart from './Chart.svelte';
19
20
  import DataTable from './DataTable.svelte';
20
21
  import GeoMap from './GeoMap.svelte';
21
22
  import Graph from './Graph.svelte';
23
+ import Race from './Race.svelte';
22
24
  import Sankey from './Sankey.svelte';
23
25
  import TileMap from './TileMap.svelte';
24
26
 
@@ -49,6 +51,8 @@ let {
49
51
  <BarList {spec} {theme} {darkMode} class={className} {style} />
50
52
  {:else if isGeoMapSpec(spec)}
51
53
  <GeoMap {spec} {theme} {darkMode} class={className} {style} />
54
+ {:else if isRaceSpec(spec)}
55
+ <Race {spec} {theme} {darkMode} class={className} {style} />
52
56
  {:else}
53
57
  <Chart {spec} {theme} {darkMode} class={className} {style} />
54
58
  {/if}
@@ -1 +1 @@
1
- {"version":3,"file":"Visualization.svelte.d.ts","sourceRoot":"","sources":["../src/Visualization.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAiBjF,KAAK,gBAAgB,GAAI;IACxB,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAwCF,QAAA,MAAM,aAAa,sDAAwC,CAAC;AAC5D,KAAK,aAAa,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AACtD,eAAe,aAAa,CAAC"}
1
+ {"version":3,"file":"Visualization.svelte.d.ts","sourceRoot":"","sources":["../src/Visualization.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAmBjF,KAAK,gBAAgB,GAAI;IACxB,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AA2CF,QAAA,MAAM,aAAa,sDAAwC,CAAC;AAC5D,KAAK,aAAa,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AACtD,eAAe,aAAa,CAAC"}
@@ -59,7 +59,7 @@ describe('<Chart />', () => {
59
59
  const { container } = await renderChart({ spec: lineSpec });
60
60
  const svg = container.querySelector('svg');
61
61
  expect(svg).not.toBeNull();
62
- expect(svg?.getAttribute('class')).toBe('oc-chart');
62
+ expect(svg?.getAttribute('class')).toBe('oc-chart oc-animate'); // animation is on by default
63
63
  });
64
64
  it('renders chrome text elements', async () => {
65
65
  const { container } = await renderChart({ spec: lineSpec });
@@ -209,6 +209,25 @@ it.each([
209
209
  expect(container.querySelector('svg')).toBeNull();
210
210
  });
211
211
  describe('interaction mode and listener parity', () => {
212
+ it('rewires mark focus to the current handler without replacing the chart', async () => {
213
+ const before = vi.fn(), after = vi.fn();
214
+ const { container, rerender } = await renderChart({
215
+ spec: barSpec,
216
+ autoResize: false,
217
+ onmarkfocus: before,
218
+ });
219
+ const host = container.querySelector('.oc-chart-root');
220
+ const svg = host.querySelector('svg');
221
+ host.dispatchEvent(new FocusEvent('focus'));
222
+ expect(before).toHaveBeenCalledTimes(1);
223
+ expect(before.mock.calls[0][0].datum).toEqual(barSpec.data[0]);
224
+ await rerender({ spec: barSpec, autoResize: false, onmarkfocus: after });
225
+ host.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight' }));
226
+ expect(host.querySelector('svg')).toBe(svg);
227
+ expect(before).toHaveBeenCalledTimes(1);
228
+ expect(after).toHaveBeenCalledTimes(1);
229
+ expect(after.mock.calls[0][0].datum).toEqual(barSpec.data[1]);
230
+ });
212
231
  const clickTitle = (container) => container
213
232
  .querySelector('[data-chrome-key="title"]')
214
233
  .dispatchEvent(new MouseEvent('click', { bubbles: true }));
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Svelte Graph wrapper: a theme recreation suppresses the entrance only when
3
+ * the previous instance's entrance played.
4
+ *
5
+ * Renders the real vanilla graph. Whether the entrance played is read off the
6
+ * public camera (see `playsEntrance`); rAF and performance.now run on a manual
7
+ * frame clock.
8
+ */
9
+ import { cleanup, render, waitFor } from '@testing-library/svelte';
10
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
11
+ import { installFrameClock, playsEntrance, playsEntranceOnSeen, } from '../../../vanilla/src/__test-fixtures__/graph-frame-clock';
12
+ import { createFakeObserver } from '../../../vanilla/src/__test-fixtures__/visibility';
13
+ import { setObserverFactory } from '../../../vanilla/src/visibility';
14
+ import Graph from '../Graph.svelte';
15
+ // Warmed so the first delivered tick is near-settled and starts the entrance.
16
+ // The setup's global getBoundingClientRect stub gives happy-dom a layout, so a
17
+ // 'visible' entrance would wait on an IntersectionObserver that never fires.
18
+ const spec = {
19
+ type: 'graph',
20
+ nodes: [
21
+ { id: 'a', label: 'A' },
22
+ { id: 'b', label: 'B' },
23
+ { id: 'c', label: 'C' },
24
+ ],
25
+ edges: [
26
+ { source: 'a', target: 'b' },
27
+ { source: 'b', target: 'c' },
28
+ ],
29
+ layout: { type: 'force', warmup: 30 },
30
+ animation: { enter: { trigger: 'mount' } },
31
+ };
32
+ let clock;
33
+ beforeEach(() => {
34
+ clock = installFrameClock();
35
+ });
36
+ afterEach(() => {
37
+ cleanup();
38
+ clock.restore();
39
+ setObserverFactory(null);
40
+ });
41
+ async function renderGraph(props) {
42
+ const result = render(Graph, { props });
43
+ await waitFor(() => expect(result.container.querySelector('canvas')).not.toBeNull());
44
+ return result;
45
+ }
46
+ async function recreateDark(result) {
47
+ await result.rerender({ darkMode: 'force' });
48
+ await waitFor(() => expect(result.container.querySelector('.oc-graph-wrapper')?.classList.contains('oc-dark')).toBe(true));
49
+ }
50
+ describe('Graph suppressEntrance', () => {
51
+ it('a theme change after the entrance played recreates without replaying it', async () => {
52
+ const result = await renderGraph({ spec, darkMode: 'off' });
53
+ const api = result.component;
54
+ expect(await playsEntrance(clock, () => api.getCamera().k)).toBe(true);
55
+ await recreateDark(result);
56
+ expect(await playsEntrance(clock, () => api.getCamera().k, 2000)).toBe(false);
57
+ });
58
+ it('a theme change while the entrance still waits to be seen replays it on the new graph', async () => {
59
+ // Default trigger ('visible'), held by a fake observer the test drives.
60
+ const observer = createFakeObserver();
61
+ setObserverFactory(observer.factory);
62
+ const result = await renderGraph({ spec: { ...spec, animation: undefined }, darkMode: 'off' });
63
+ const api = result.component;
64
+ await Promise.resolve();
65
+ clock.pump(0);
66
+ // Armed and unseen.
67
+ expect(observer.observed.size).toBe(1);
68
+ await recreateDark(result);
69
+ expect(await playsEntranceOnSeen(clock, observer, () => api.getCamera().k, 100)).toBe(true);
70
+ });
71
+ });
@@ -0,0 +1,289 @@
1
+ import * as vanilla from '@opendata-ai/openchart-vanilla';
2
+ import { fireEvent, render, screen, within } from '@testing-library/svelte';
3
+ import { expect, it, onTestFinished, vi } from 'vitest';
4
+ import { installFrameClock } from '../../../vanilla/src/__test-fixtures__/graph-frame-clock';
5
+ import { createFakeObserver } from '../../../vanilla/src/__test-fixtures__/visibility';
6
+ import { setObserverFactory } from '../../../vanilla/src/visibility';
7
+ import { DARK_MODE_KEY, THEME_KEY } from '../context.js';
8
+ import { Race, Visualization } from '../index';
9
+ import RaceSpecHarness from './RaceSpecHarness.svelte';
10
+ const spec = {
11
+ type: 'race',
12
+ mark: 'bar',
13
+ data: [
14
+ { year: 2020, name: 'Alpha', value: 10 },
15
+ { year: 2021, name: 'Alpha', value: 20 },
16
+ { year: 2022, name: 'Alpha', value: 30 },
17
+ ],
18
+ encoding: {
19
+ time: { field: 'year', type: 'ordinal' },
20
+ key: { field: 'name', type: 'nominal' },
21
+ value: { field: 'value', type: 'quantitative' },
22
+ },
23
+ race: { top: 1, autoplay: false },
24
+ chrome: { title: 'Annual leaders' },
25
+ };
26
+ const updated = { ...spec, chrome: { title: 'Updated leaders' } };
27
+ const playbackSpec = { ...spec, race: { ...spec.race, duration: 400, holdEnd: 0 } };
28
+ it.each([
29
+ false,
30
+ true,
31
+ ])('replays from the start after a completed race changes theme with autoplay=%s', async (autoplay) => {
32
+ const clock = installFrameClock();
33
+ onTestFinished(clock.restore);
34
+ const replaySpec = { ...playbackSpec, race: { ...playbackSpec.race, autoplay } };
35
+ const { component, rerender, getByRole } = render(Race, { props: { spec: replaySpec } });
36
+ const previous = component.getInstance();
37
+ if (!previous)
38
+ throw new Error('Race did not mount');
39
+ previous.setSpeed(2);
40
+ if (!autoplay)
41
+ await fireEvent.click(getByRole('button', { name: 'Play' }));
42
+ clock.pump(0);
43
+ clock.pump(200);
44
+ expect(previous.getState()).toMatchObject({ t: 2, playing: false, ended: true });
45
+ await rerender({ theme: { fonts: { sizes: { title: 32 } } } });
46
+ const current = component.getInstance();
47
+ expect(current).toBe(previous);
48
+ expect(current?.getState()).toMatchObject({ t: 2, playing: false, ended: true, speed: 2 });
49
+ await fireEvent.click(getByRole('button', { name: 'Play' }));
50
+ expect(current?.getState()).toMatchObject({ t: 0, playing: true, speed: 2 });
51
+ clock.pump(300);
52
+ clock.pump(400);
53
+ expect(current?.getState().t).toBe(1);
54
+ });
55
+ it('replays from the start after a completed race remounts', async () => {
56
+ const clock = installFrameClock();
57
+ onTestFinished(clock.restore);
58
+ const replaySpec = { ...playbackSpec, race: { ...playbackSpec.race, autoplay: true } };
59
+ const { component, rerender, getByRole } = render(Race, { props: { spec: replaySpec } });
60
+ const previous = component.getInstance();
61
+ if (!previous)
62
+ throw new Error('Race did not mount');
63
+ previous.setSpeed(2);
64
+ clock.pump(0);
65
+ clock.pump(200);
66
+ expect(previous.getState()).toMatchObject({ t: 2, playing: false, ended: true });
67
+ await rerender({ watermark: false });
68
+ const current = component.getInstance();
69
+ expect(current).not.toBe(previous);
70
+ expect(current?.getState()).toMatchObject({ t: 2, playing: false, ended: true, speed: 2 });
71
+ await fireEvent.click(getByRole('button', { name: 'Play' }));
72
+ expect(current?.getState()).toMatchObject({ t: 0, playing: true, speed: 2 });
73
+ clock.pump(300);
74
+ clock.pump(400);
75
+ expect(current?.getState().t).toBe(1);
76
+ });
77
+ it('preserves the speed set before playback starts after a theme change', async () => {
78
+ const { component, rerender } = render(Race, { props: { spec } });
79
+ const previous = component.getInstance();
80
+ previous?.setSpeed(2);
81
+ await rerender({ theme: { fonts: { sizes: { title: 32 } } } });
82
+ expect(component.getInstance()).toBe(previous);
83
+ expect(component.getInstance()?.getState()).toMatchObject({ t: 0, playing: false, speed: 2 });
84
+ });
85
+ it('updates on in-place spec edits and reuses the chart for equal snapshots', async () => {
86
+ const { component, getByRole } = render(RaceSpecHarness, { props: { initialSpec: spec } });
87
+ const instance = component.getInstance();
88
+ if (!instance)
89
+ throw new Error('Race did not mount');
90
+ const update = vi.spyOn(instance, 'update');
91
+ const roundedMark = { type: 'bar', cornerRadius: 8 };
92
+ await fireEvent.click(getByRole('button', { name: 'Round bars' }));
93
+ expect(update).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ mark: roundedMark }));
94
+ const chart = getByRole('img');
95
+ await fireEvent.click(getByRole('button', { name: 'Render an equal spec' }));
96
+ expect(update).toHaveBeenCalledTimes(1);
97
+ expect(getByRole('img')).toBe(chart);
98
+ });
99
+ it('moves the initial period subscription to the new instance on remount', async () => {
100
+ const subscriptions = [];
101
+ const createRace = vanilla.createRace;
102
+ const mountSpy = vi.spyOn(vanilla, 'createRace').mockImplementation((...args) => {
103
+ const instance = createRace(...args);
104
+ const record = { instance, events: [], removed: 0 };
105
+ subscriptions.push(record);
106
+ const listen = instance.on;
107
+ vi.spyOn(instance, 'on').mockImplementation((event, callback) => {
108
+ record.events.push(event);
109
+ const unsubscribe = listen(event, callback);
110
+ return () => {
111
+ record.removed++;
112
+ unsubscribe();
113
+ };
114
+ });
115
+ return instance;
116
+ });
117
+ try {
118
+ const onPeriod = (_period) => { };
119
+ const { component, rerender } = render(Race, { props: { spec, onperiod: onPeriod } });
120
+ expect(component.getInstance()).toBe(subscriptions[0]?.instance);
121
+ await rerender({ watermark: false });
122
+ expect(component.getInstance()).toBe(subscriptions[1]?.instance);
123
+ expect(subscriptions.map(({ events, removed }) => ({ events, removed }))).toEqual([
124
+ { events: ['period'], removed: 1 },
125
+ { events: ['period'], removed: 0 },
126
+ ]);
127
+ }
128
+ finally {
129
+ mountSpy.mockRestore();
130
+ }
131
+ });
132
+ it('mounts a playable race and forwards the wrapper props', () => {
133
+ const { component, container } = render(Race, {
134
+ props: { spec, class: 'reader-race', style: 'width: 420px', watermark: false },
135
+ });
136
+ const chart = within(screen.getByRole('img'));
137
+ expect(chart.getByText('Annual leaders')).toBeInTheDocument();
138
+ expect(chart.getByText('Alpha')).toBeInTheDocument();
139
+ expect(screen.getByRole('slider', { name: 'Time' })).toHaveAttribute('aria-valuetext', '2020');
140
+ expect(container.firstElementChild).toHaveClass('reader-race');
141
+ expect(container.firstElementChild).toHaveStyle({ width: '420px' });
142
+ expect(screen.queryByText('OpenData')).not.toBeInTheDocument();
143
+ expect(component.getInstance()?.getState()).toMatchObject({ t: 0, playing: false });
144
+ });
145
+ it('updates the chart on the same instance and keeps the fractional playhead', async () => {
146
+ const { component, rerender } = render(Race, { props: { spec } });
147
+ const instance = component.getInstance();
148
+ expect(instance).toBeTruthy();
149
+ instance?.seek(1.25);
150
+ await rerender({ spec: updated });
151
+ expect(within(screen.getByRole('img')).getByText('Updated leaders')).toBeInTheDocument();
152
+ expect(component.getInstance()).toBe(instance);
153
+ expect(instance?.getState()).toMatchObject({ t: 1.25, playing: false });
154
+ });
155
+ it.each([true, false])('keeps t and playing=%s after a theme change', async (playing) => {
156
+ const raceSpec = { ...spec, race: { ...spec.race, autoplay: !playing } };
157
+ const { component, rerender } = render(Race, { props: { spec: raceSpec } });
158
+ const previous = component.getInstance();
159
+ expect(previous).toBeTruthy();
160
+ previous?.seek(1.25);
161
+ previous?.setSpeed(2);
162
+ if (playing)
163
+ previous?.play();
164
+ else
165
+ previous?.pause();
166
+ await rerender({ theme: { fonts: { sizes: { title: 32 } } } });
167
+ expect(component.getInstance()).toBe(previous);
168
+ expect(component.getInstance()?.getState()).toMatchObject({ t: 1.25, playing, speed: 2 });
169
+ expect(screen.getByText('Annual leaders').style.fontSize).toBe('32px');
170
+ });
171
+ it('keeps visibility autoplay pending after a dark mode change', async () => {
172
+ const observer = createFakeObserver();
173
+ setObserverFactory(observer.factory);
174
+ try {
175
+ const visible = { ...spec, race: { ...spec.race, autoplay: 'visible' } };
176
+ const { component, container, rerender } = render(Race, { props: { spec: visible } });
177
+ const host = container.firstElementChild;
178
+ if (!host)
179
+ throw new Error('Race did not mount');
180
+ const playing = () => component.getInstance()?.getState().playing;
181
+ expect(playing()).toBe(false);
182
+ await rerender({ darkMode: 'force' });
183
+ expect(playing()).toBe(false);
184
+ observer.fire(host, 0.5);
185
+ expect(playing()).toBe(true);
186
+ }
187
+ finally {
188
+ setObserverFactory(null);
189
+ }
190
+ });
191
+ it('merges context theme and redraws when the resolved dark mode changes', async () => {
192
+ const raceSpec = { ...spec, chrome: { title: 'Annual leaders', source: 'Annual survey' } };
193
+ const { component, rerender, container } = render(Race, {
194
+ props: { spec: raceSpec, theme: { fonts: { sizes: { title: 28 } } } },
195
+ context: new Map([
196
+ [THEME_KEY, () => ({ fonts: { sizes: { title: 34, small: 17 } } })],
197
+ [DARK_MODE_KEY, () => 'force'],
198
+ ]),
199
+ });
200
+ const previous = component.getInstance();
201
+ previous?.seek(1.25);
202
+ expect(container.querySelector('.oc-dark')).toBeInTheDocument();
203
+ expect(screen.getByText('Annual leaders').style.fontSize).toBe('28px');
204
+ expect(screen.getByText('Annual survey').style.fontSize).toBe('17px');
205
+ await rerender({ darkMode: 'off' });
206
+ expect(container.querySelector('.oc-dark')).not.toBeInTheDocument();
207
+ expect(component.getInstance()).toBe(previous);
208
+ expect(component.getInstance()?.getState().t).toBe(1.25);
209
+ });
210
+ it('delivers period crossings in order and stops notifying a removed callback', async () => {
211
+ const clock = installFrameClock();
212
+ onTestFinished(clock.restore);
213
+ const first = [];
214
+ const second = [];
215
+ const { component, rerender, unmount } = render(Race, {
216
+ props: { spec: playbackSpec, onperiod: (period) => first.push(period) },
217
+ });
218
+ const instance = component.getInstance();
219
+ if (!instance)
220
+ throw new Error('Race did not mount');
221
+ instance.play();
222
+ clock.pump(0);
223
+ clock.pump(100);
224
+ expect(first).toEqual([]);
225
+ clock.pump(200);
226
+ const period2021 = { index: 1, value: 2021, label: '2021' };
227
+ expect(first).toEqual([period2021]);
228
+ clock.pump(400);
229
+ const period2022 = { index: 2, value: 2022, label: '2022' };
230
+ expect(first).toEqual([period2021, period2022]);
231
+ await rerender({ onperiod: (period) => second.push(period) });
232
+ expect(component.getInstance()).toBe(instance);
233
+ instance.seek(0);
234
+ const period2020 = { index: 0, value: 2020, label: '2020' };
235
+ expect(second).toEqual([period2020]);
236
+ await rerender({ onperiod: undefined });
237
+ instance.seek(1);
238
+ expect(second).toEqual([period2020]);
239
+ await rerender({ onperiod: (period) => first.push(period) });
240
+ unmount();
241
+ instance.seek(2);
242
+ expect(first).toEqual([period2021, period2022]);
243
+ });
244
+ it('stops and destroys the real instance on unmount', () => {
245
+ const { component, container, unmount } = render(Race, { props: { spec } });
246
+ const host = container.firstElementChild;
247
+ const instance = component.getInstance();
248
+ expect(screen.getByRole('img')).toBeInTheDocument();
249
+ instance?.seek(1.25);
250
+ instance?.play();
251
+ unmount();
252
+ instance?.play();
253
+ instance?.seek(0);
254
+ expect(component.getInstance()).toBeNull();
255
+ expect(host?.querySelector('svg')).toBeNull();
256
+ expect(instance?.getState()).toMatchObject({ t: 1.25, playing: false });
257
+ });
258
+ it('forwards compile warnings to the current callback and propagates invalid specs', async () => {
259
+ const first = [];
260
+ const second = [];
261
+ const { component, rerender } = render(Race, {
262
+ props: { spec, onWarn: (message) => first.push(message) },
263
+ });
264
+ const instance = component.getInstance();
265
+ const warns = { ...spec, race: { ...spec.race, top: 2 } };
266
+ await rerender({ spec: warns, onWarn: (message) => second.push(message) });
267
+ expect(component.getInstance()).toBe(instance);
268
+ expect(first).toEqual([]);
269
+ expect(second).toEqual(['race.top is 2 but the data has 1 distinct keys. Showing at most 1.']);
270
+ const invalid = { ...spec, race: { top: 0 } };
271
+ await expect(rerender({ spec: invalid })).rejects.toThrow(/top/);
272
+ });
273
+ it('routes race specs through Visualization', () => {
274
+ render(Visualization, {
275
+ props: {
276
+ spec,
277
+ theme: { fonts: { sizes: { title: 32 } } },
278
+ darkMode: 'force',
279
+ class: 'routed-race',
280
+ style: 'width: 420px',
281
+ },
282
+ });
283
+ const chart = screen.getByRole('img');
284
+ expect(chart.closest('.routed-race')).toHaveStyle({ width: '420px' });
285
+ expect(chart.closest('.oc-dark')).toBeInTheDocument();
286
+ expect(within(chart).getByText('Annual leaders').style.fontSize).toBe('32px');
287
+ expect(within(chart).getByText('OpenData')).toBeInTheDocument();
288
+ expect(screen.getByRole('button', { name: 'Play' })).toBeInTheDocument();
289
+ });