Skip to content

Commit

Permalink
fix: do not reset configuration if chart is immediately re-attached (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Nov 8, 2024
1 parent e706b38 commit c6cfcbe
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
20 changes: 13 additions & 7 deletions packages/charts/src/vaadin-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,14 +1078,20 @@ class Chart extends ResizeMixin(ElementMixin(ThemableMixin(PolymerElement))) {
disconnectedCallback() {
super.disconnectedCallback();

if (this.configuration) {
this.configuration.destroy();
this.configuration = undefined;
}
queueMicrotask(() => {
if (this.isConnected) {
return;
}

if (this._childObserver) {
this._childObserver.disconnect();
}
if (this.configuration) {
this.configuration.destroy();
this.configuration = undefined;
}

if (this._childObserver) {
this._childObserver.disconnect();
}
});
}

/** @private */
Expand Down
43 changes: 42 additions & 1 deletion packages/charts/test/reattach.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { fixtureSync, oneEvent } from '@vaadin/testing-helpers';
import { fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers';
import sinon from 'sinon';
import '../vaadin-chart.js';

Expand All @@ -25,24 +25,42 @@ describe('reattach', () => {

const spy = sinon.spy(chart.configuration, 'destroy');
wrapper.removeChild(chart);
await nextFrame();

expect(spy).to.be.calledOnce;
expect(chart.configuration).to.be.undefined;
});

it('should re-create chart configuration when attached to a new parent', async () => {
await oneEvent(chart, 'chart-load');
wrapper.removeChild(chart);
await nextFrame();

inner.appendChild(chart);
await oneEvent(chart, 'chart-load');
expect(chart.configuration.series.length).to.be.equal(chart.childElementCount);
});

it('should not re-create chart configuration when moved to a new parent', async () => {
await oneEvent(chart, 'chart-load');

const configuration = chart.configuration;

const spy = sinon.spy(chart.configuration, 'destroy');
inner.appendChild(chart);
await nextFrame();

expect(spy).to.not.be.called;
expect(chart.configuration).to.be.equal(configuration);
});

it('should apply title updated while detached after reattach', async () => {
chart.title = 'Title';
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ title: { text: 'New title' } });

inner.appendChild(chart);
Expand All @@ -55,6 +73,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ subtitle: { text: 'New subtitle' } });

inner.appendChild(chart);
Expand All @@ -67,6 +87,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ chart: { type: 'line' } });

inner.appendChild(chart);
Expand All @@ -79,6 +101,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ tooltip: { enabled: false } });

inner.appendChild(chart);
Expand All @@ -91,6 +115,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ legend: { enabled: true } });

inner.appendChild(chart);
Expand All @@ -103,6 +129,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ chart: { options3d: { enabled: false } } });

inner.appendChild(chart);
Expand All @@ -115,6 +143,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ chart: { polar: false } });

inner.appendChild(chart);
Expand All @@ -128,6 +158,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ lang: { noData: 'Nothing' } });

inner.appendChild(chart);
Expand All @@ -147,6 +179,7 @@ describe('reattach', () => {
series: { stacking: 'percent' },
},
});
await nextFrame();

inner.appendChild(chart);
await oneEvent(chart, 'chart-load');
Expand All @@ -158,6 +191,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ xAxis: { categories: ['Jun', 'Jul', 'Aug'] } });

inner.appendChild(chart);
Expand All @@ -173,6 +208,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ xAxis: { min: 1 } });

inner.appendChild(chart);
Expand All @@ -185,6 +222,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ xAxis: { max: 3 } });

inner.appendChild(chart);
Expand All @@ -197,6 +236,8 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');

wrapper.removeChild(chart);
await nextFrame();

chart.updateConfiguration({ chart: { inverted: false } });

inner.appendChild(chart);
Expand Down

0 comments on commit c6cfcbe

Please sign in to comment.