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

Fix cleaning up metasets #9656

Merged
merged 4 commits into from Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/core/core.controller.js
Expand Up @@ -800,12 +800,11 @@ class Chart {
* @private
*/
_destroyDatasetMeta(datasetIndex) {
const meta = this._metasets && this._metasets[datasetIndex];

const meta = this._metasets[datasetIndex];
if (meta && meta.controller) {
meta.controller._destroy();
delete this._metasets[datasetIndex];
}
delete this._metasets[datasetIndex];
}

_stop() {
Expand Down Expand Up @@ -917,7 +916,7 @@ class Chart {

_remove('resize', listener);

// Stop animating and remove metasets, so when re-attached, the animations start from begining.
// Stop animating and remove metasets, so when re-attached, the animations start from beginning.
this._stop();
this._resize(0, 0);

Expand Down
24 changes: 22 additions & 2 deletions test/specs/core.controller.tests.js
Expand Up @@ -1121,7 +1121,7 @@ describe('Chart', function() {
});
});

it('should resize the canvas if attached to the DOM after construction with mutiple parents', function(done) {
it('should resize the canvas if attached to the DOM after construction with multiple parents', function(done) {
var canvas = document.createElement('canvas');
var wrapper = document.createElement('div');
var wrapper2 = document.createElement('div');
Expand Down Expand Up @@ -1848,7 +1848,7 @@ describe('Chart', function() {
expect(metasets[2].order).toEqual(3);
expect(metasets[3].order).toEqual(4);
});
it('should be moved when datasets are removed from begining', function() {
it('should be moved when datasets are removed from beginning', function() {
this.chart.data.datasets.splice(0, 2);
this.chart.update();
const metasets = this.chart._metasets;
Expand Down Expand Up @@ -1910,6 +1910,26 @@ describe('Chart', function() {
});
});

describe('_destroyDatasetMeta', function() {
beforeEach(function() {
this.chart = acquireChart({
type: 'line',
data: {
datasets: [
{label: '1', order: 2},
{label: '2', order: 1},
{label: '3', order: 4},
{label: '4', order: 3},
]
}
});
});
it('cleans up metasets when the chart is destroyed', function() {
this.chart.destroy();
expect(this.chart._metasets.length).toHaveSize(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(this.chart._metasets.length).toHaveSize(0);
expect(this.chart._metasets).toHaveSize(0);

toHaveSize(expected)
expect the actual size to be equal to the expected, using array-like length or object keys size.

array = [1,2];
expect(array).toHaveSize(2);

https://jasmine.github.io/api/edge/matchers.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this. After taking another look, .toEqual([]) seems more explicit than .toHaveSize(0), so I went with that. (I'm too used to Jest and TypeScript; if there's a better way to write these tests, please let me know.)

});
});

describe('data visibility', function() {
it('should hide a dataset', function() {
var chart = acquireChart({
Expand Down