@opendata-ai/openchart-react 7.11.0 → 8.0.0-rc.10

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.
@@ -0,0 +1,92 @@
1
+ import type { ChartSpec } from '@opendata-ai/openchart-core';
2
+ import { cleanup, render, waitFor } from '@testing-library/react';
3
+ import { createRef } from 'react';
4
+ import { afterEach, describe, expect, it } from 'vitest';
5
+ import { ChartStory, type ChartStoryHandle } from '../ChartStory';
6
+
7
+ const baseSpec: ChartSpec = {
8
+ mark: 'line',
9
+ data: [
10
+ { year: '2008', value: 10, country: 'US' },
11
+ { year: '2012', value: 40, country: 'US' },
12
+ { year: '2016', value: 25, country: 'US' },
13
+ ],
14
+ encoding: {
15
+ x: { field: 'year', type: 'ordinal' },
16
+ y: { field: 'value', type: 'quantitative' },
17
+ },
18
+ chrome: { title: 'Base' },
19
+ };
20
+
21
+ const steps = [
22
+ {},
23
+ { spec: { chrome: { title: 'One' } } },
24
+ { spec: { chrome: { subtitle: 'Two' } } },
25
+ ];
26
+ const narrative = [<p key="a">Intro</p>, <p key="b">Middle</p>, <p key="c">End</p>];
27
+
28
+ describe('ChartStory', () => {
29
+ afterEach(cleanup);
30
+
31
+ it('renders the sticky chart panel and one block per step', async () => {
32
+ const { container } = render(
33
+ <ChartStory spec={baseSpec} steps={steps} narrative={narrative} />,
34
+ );
35
+
36
+ await waitFor(() => {
37
+ expect(container.querySelector('svg')).not.toBeNull();
38
+ });
39
+
40
+ // The chart mounts and a title renders (self-driving scroll may advance
41
+ // the step in a headless layout; the block count is the stable assertion).
42
+ expect(container.querySelector('.oc-title')).not.toBeNull();
43
+ // One narrative block per step.
44
+ expect(container.querySelectorAll('[data-oc-story-step]')).toHaveLength(3);
45
+ });
46
+
47
+ it('drives the story from the controlled step prop', async () => {
48
+ const { container, rerender } = render(
49
+ <ChartStory spec={baseSpec} steps={steps} narrative={narrative} step={0} />,
50
+ );
51
+
52
+ await waitFor(() => {
53
+ expect(container.querySelector('svg')).not.toBeNull();
54
+ });
55
+
56
+ rerender(<ChartStory spec={baseSpec} steps={steps} narrative={narrative} step={2} />);
57
+
58
+ await waitFor(() => {
59
+ expect(container.querySelector('.oc-title')?.textContent).toBe('One');
60
+ expect(container.querySelector('.oc-subtitle')?.textContent).toBe('Two');
61
+ });
62
+ });
63
+
64
+ it('exposes goTo and the underlying instance through the ref', async () => {
65
+ const ref = createRef<ChartStoryHandle>();
66
+ const { container } = render(
67
+ <ChartStory ref={ref} spec={baseSpec} steps={steps} narrative={narrative} />,
68
+ );
69
+
70
+ await waitFor(() => {
71
+ expect(container.querySelector('svg')).not.toBeNull();
72
+ });
73
+
74
+ expect(ref.current?.instance).not.toBeNull();
75
+ ref.current?.goTo(1);
76
+ expect(container.querySelector('.oc-title')?.textContent).toBe('One');
77
+ });
78
+
79
+ it('tears down the story instance on unmount', async () => {
80
+ const ref = createRef<ChartStoryHandle>();
81
+ const { container, unmount } = render(
82
+ <ChartStory ref={ref} spec={baseSpec} steps={steps} narrative={narrative} />,
83
+ );
84
+
85
+ await waitFor(() => {
86
+ expect(container.querySelector('svg')).not.toBeNull();
87
+ });
88
+
89
+ unmount();
90
+ expect(container.querySelector('svg')).toBeNull();
91
+ });
92
+ });
package/src/index.ts CHANGED
@@ -45,6 +45,7 @@ export type {
45
45
  NormalizedChartSpec,
46
46
  NormalizedChrome,
47
47
  NormalizedGraphSpec,
48
+ NormalizedMapSpec,
48
49
  NormalizedSankeySpec,
49
50
  NormalizedSpec,
50
51
  NormalizedTableSpec,
@@ -60,6 +61,7 @@ export {
60
61
  compileBarList,
61
62
  compileChart,
62
63
  compileGraph,
64
+ compileMap,
63
65
  compileSankey,
64
66
  compileTable,
65
67
  compileTileMap,
@@ -73,6 +75,9 @@ export { BarList } from './BarList';
73
75
  export type { ChartHandle, ChartProps } from './Chart';
74
76
  // Components
75
77
  export { Chart } from './Chart';
78
+ export type { ChartStoryHandle, ChartStoryProps } from './ChartStory';
79
+ // Scrollytelling
80
+ export { ChartStory } from './ChartStory';
76
81
  export type { DataTableProps } from './DataTable';
77
82
  export { DataTable } from './DataTable';
78
83
  export type { GraphProps } from './Graph';
@@ -86,6 +91,8 @@ export type { UseTableReturn } from './hooks/useTable';
86
91
  export { useTable } from './hooks/useTable';
87
92
  export type { UseTableStateOptions, UseTableStateReturn } from './hooks/useTableState';
88
93
  export { useTableState } from './hooks/useTableState';
94
+ export type { MapHandle, MapProps } from './Map';
95
+ export { GeoMap } from './Map';
89
96
  export type { SankeyHandle, SankeyProps } from './Sankey';
90
97
  export { Sankey } from './Sankey';
91
98
  export type { VizThemeProviderProps } from './ThemeContext';