Skip to content

Commit 53c0b7b

Browse files
committed
Merge branch 'build/vite' into chore/storybook
2 parents 7384525 + 78455ef commit 53c0b7b

1 file changed

Lines changed: 54 additions & 7 deletions

File tree

‎src/typedCharts.spec.tsx‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { createRef } from 'react';
22
import { afterEach, describe, it, expect } from 'vitest';
33
import { render, cleanup } from '@testing-library/react';
4-
import { Chart as ChartJS, LinearScale } from 'chart.js';
4+
import {
5+
Chart as ChartJS,
6+
ArcElement,
7+
DoughnutController,
8+
LinearScale,
9+
PointElement,
10+
ScatterController,
11+
} from 'chart.js';
512
import type { Plugin } from 'chart.js';
613
import type { ChartJSOrUndefined } from './types.js';
714
import { Doughnut, Scatter } from './typedCharts.js';
815

916
// The components register their controllers themselves
10-
ChartJS.register(LinearScale);
17+
ChartJS.register(ArcElement, LinearScale, PointElement);
1118

1219
describe('typedCharts', () => {
1320
const data = {
@@ -19,13 +26,30 @@ describe('typedCharts', () => {
1926
});
2027

2128
describe('Scatter', () => {
29+
const scatterData = {
30+
datasets: [{ data: [{ x: 1, y: 1 }] }],
31+
};
32+
2233
it('should render a scatter chart', () => {
2334
const ref = createRef<ChartJSOrUndefined<'scatter'>>();
2435

25-
render(<Scatter data={data} options={{ responsive: false }} ref={ref} />);
36+
render(
37+
<Scatter ref={ref} data={scatterData} options={{ responsive: false }} />
38+
);
39+
40+
expect(ref.current?.getDatasetMeta(0).controller).toBeInstanceOf(
41+
ScatterController
42+
);
43+
});
44+
45+
it('should pass the chart instance to a ref', () => {
46+
const ref = createRef<ChartJSOrUndefined<'scatter'>>();
47+
48+
render(
49+
<Scatter ref={ref} data={scatterData} options={{ responsive: false }} />
50+
);
2651

2752
expect(ref.current).toBeInstanceOf(ChartJS);
28-
expect(ref.current?.config).toHaveProperty('type', 'scatter');
2953
});
3054

3155
it('should accept plugins of its chart type', () => {
@@ -52,15 +76,38 @@ describe('typedCharts', () => {
5276
});
5377

5478
describe('Doughnut', () => {
79+
const doughnutData = {
80+
datasets: [{ data: [1] }],
81+
};
82+
5583
it('should render a doughnut chart', () => {
5684
const ref = createRef<ChartJSOrUndefined<'doughnut'>>();
5785

5886
render(
59-
<Doughnut data={data} options={{ responsive: false }} ref={ref} />
87+
<Doughnut
88+
ref={ref}
89+
data={doughnutData}
90+
options={{ responsive: false }}
91+
/>
6092
);
6193

62-
expect(ref.current).toBeInstanceOf(ChartJS);
63-
expect(ref.current?.config).toHaveProperty('type', 'doughnut');
94+
expect(ref.current?.getDatasetMeta(0).controller).toBeInstanceOf(
95+
DoughnutController
96+
);
97+
});
98+
99+
it('should pass options to the chart', () => {
100+
const ref = createRef<ChartJSOrUndefined<'doughnut'>>();
101+
102+
render(
103+
<Doughnut
104+
ref={ref}
105+
data={doughnutData}
106+
options={{ responsive: false, cutout: '75%' }}
107+
/>
108+
);
109+
110+
expect(ref.current?.options).toHaveProperty('cutout', '75%');
64111
});
65112

66113
it('should accept options of its chart type', () => {

0 commit comments

Comments
 (0)