Skip to content

Commit

Permalink
fix: reset charts series objects when chart is detached (#8115) (#8123)
Browse files Browse the repository at this point in the history
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
  • Loading branch information
vaadin-bot and web-padawan authored Nov 8, 2024
1 parent 5274074 commit b337683
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/charts/src/vaadin-chart-series.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class ChartSeries extends PolymerElement {

/** @private */
__valueMinObserver(valueMin, series) {
if (valueMin === undefined || series === undefined) {
if (valueMin === undefined || series == null) {
return;
}

Expand All @@ -319,7 +319,7 @@ class ChartSeries extends PolymerElement {

/** @private */
__valueMaxObserver(valueMax, series) {
if (valueMax === undefined || series === undefined) {
if (valueMax === undefined || series == null) {
return;
}

Expand All @@ -335,23 +335,23 @@ class ChartSeries extends PolymerElement {

/** @private */
__titleObserver(title, series) {
if (title === undefined || series === undefined) {
if (title === undefined || series == null) {
return;
}
series.update({ name: title });
}

/** @private */
__stackObserver(stack, series) {
if (stack === undefined || series === undefined) {
if (stack === undefined || series == null) {
return;
}
series.update({ stack });
}

/** @private */
__neckPositionObserver(neckPosition, series) {
if (neckPosition === undefined || series === undefined) {
if (neckPosition === undefined || series == null) {
return;
}

Expand All @@ -360,7 +360,7 @@ class ChartSeries extends PolymerElement {

/** @private */
__neckWidthObserver(neckWidth, series) {
if (neckWidth === undefined || series === undefined) {
if (neckWidth === undefined || series == null) {
return;
}

Expand Down Expand Up @@ -404,7 +404,7 @@ class ChartSeries extends PolymerElement {

/** @private */
__markersObserver(markers, series) {
if (markers === undefined || series === undefined) {
if (markers === undefined || series == null) {
return;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/charts/src/vaadin-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,12 @@ class Chart extends ResizeMixin(ElementMixin(ThemableMixin(PolymerElement))) {
if (this.configuration) {
this.configuration.destroy();
this.configuration = undefined;

// Reset series objects to avoid errors while detached
const seriesNodes = Array.from(this.childNodes).filter(this.__filterSeriesNodes);
seriesNodes.forEach((series) => {
series.setSeries(null);
});
}

if (this._childObserver) {
Expand Down
126 changes: 126 additions & 0 deletions packages/charts/test/reattach.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,130 @@ describe('reattach', () => {
await oneEvent(chart, 'chart-load');
expect(chart.configuration.inverted).to.be.undefined;
});

describe('series', () => {
let series;

beforeEach(async () => {
await oneEvent(chart, 'chart-load');
series = chart.querySelector('vaadin-chart-series');
});

it('should apply the series title updated while detached after reattach', async () => {
wrapper.removeChild(chart);
await nextFrame();

series.title = 'Title';

wrapper.appendChild(chart);
await oneEvent(chart, 'chart-load');
await nextFrame();

expect(chart.configuration.series[0].name).to.be.equal('Title');
});

it('should apply the series type updated while detached after reattach', async () => {
wrapper.removeChild(chart);
await nextFrame();

series.type = 'area';

wrapper.appendChild(chart);
await oneEvent(chart, 'chart-load');
await nextFrame();

expect(chart.configuration.series[0].type).to.be.equal('area');
});

it('should apply the series unit updated while detached after reattach', async () => {
wrapper.removeChild(chart);
await nextFrame();

series.unit = 'unit';

wrapper.appendChild(chart);
await oneEvent(chart, 'chart-load');
await nextFrame();

expect(chart.configuration.yAxis[0].options.id).to.be.equal('unit');
});

it('should apply the series neck-width updated while detached after reattach', async () => {
wrapper.removeChild(chart);
await nextFrame();

series.neckWidth = 20;

wrapper.appendChild(chart);
await oneEvent(chart, 'chart-load');
await nextFrame();

expect(chart.configuration.series[0].options.neckWidth).to.be.equal(20);
});

it('should apply the series neck-position updated while detached after reattach', async () => {
wrapper.removeChild(chart);
await nextFrame();

series.neckPosition = 50;

wrapper.appendChild(chart);
await oneEvent(chart, 'chart-load');
await nextFrame();

expect(chart.configuration.series[0].options.neckHeight).to.be.equal(50);
});

it('should apply the series valueMin updated while detached after reattach', async () => {
wrapper.removeChild(chart);
await nextFrame();

series.valueMin = 5;

wrapper.appendChild(chart);
await oneEvent(chart, 'chart-load');
await nextFrame();

expect(chart.configuration.yAxis[0].options.min).to.be.equal(5);
});

it('should apply the series valueMax updated while detached after reattach', async () => {
wrapper.removeChild(chart);
await nextFrame();

series.valueMax = 10;

wrapper.appendChild(chart);
await oneEvent(chart, 'chart-load');
await nextFrame();

expect(chart.configuration.yAxis[0].options.max).to.be.equal(10);
});

it('should apply the series markers updated while detached after reattach', async () => {
wrapper.removeChild(chart);
await nextFrame();

series.markers = 'auto';

wrapper.appendChild(chart);
await oneEvent(chart, 'chart-load');
await nextFrame();

expect(chart.configuration.series[0].options.marker.enabled).to.be.equal(null);
});

it('should apply the series stack updated while detached after reattach', async () => {
wrapper.removeChild(chart);
await nextFrame();

series.stack = '1';

wrapper.appendChild(chart);
await oneEvent(chart, 'chart-load');
await nextFrame();

expect(chart.configuration.series[0].options.stack).to.be.equal('1');
});
});
});

0 comments on commit b337683

Please sign in to comment.