Skip to content

Commit

Permalink
feat: assure redraw occurs when type changes (#1054) (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlenderDude authored Jul 12, 2022
1 parent 9ce951c commit bf0538f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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

0 comments on commit bf0538f

Please sign in to comment.