Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: assure redraw occurs when type changes (#1054) #1055

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ function ChartComponent<
}
}, [redraw, options, data.labels, data.datasets, updateMode]);

useEffect(() => {
if (!chartRef.current) return;

destroyChart();
setTimeout(renderChart);
}, [type]);

useEffect(() => {
renderChart();

Expand Down
14 changes: 14 additions & 0 deletions test/chart.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ describe('<Chart />', () => {
expect(chart.id).toEqual(id);
});

it('should properly update with a new chart type', () => {
const newType = 'line';

const { rerender } = render(
<Chart data={data} options={options} type='bar' ref={ref} />
);

const originalChartDestroy = Object.assign({}, destroy);

rerender(<Chart data={data} options={options} type={newType} ref={ref} />);

expect(originalChartDestroy).toHaveBeenCalled();
});

it('should properly maintain order with new data', () => {
const oldData = {
labels: ['red', 'blue'],
Expand Down