Skip to content

Commit

Permalink
Handle sealed objects & test more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg committed Feb 10, 2019
1 parent e9bd480 commit cadd326
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/core/core.datasetController.js
Expand Up @@ -225,7 +225,7 @@ helpers.extend(DatasetController.prototype, {
unlistenArrayEvents(me._data, me);
}

if (data && !Object.isFrozen(data)) {
if (data && Object.isExtensible(data)) {
listenArrayEvents(data, me);
}
me._data = data;
Expand Down
89 changes: 72 additions & 17 deletions test/specs/core.datasetController.tests.js
Expand Up @@ -38,23 +38,78 @@ describe('Chart.DatasetController', function() {
});
});

it('should handle a frozen data object', function() {
function createChart() {
var data = Object.freeze([0, 1, 2, 3, 4, 5]);
var chart = acquireChart({
type: 'line',
data: {
datasets: [{
data: data
}]
}
});

// Tests that the unlisten path also works for frozen objects
chart.destroy();
}

expect(createChart).not.toThrow();
describe('inextensible data', function() {
it('should handle a frozen data object', function() {
function createChart() {
var data = Object.freeze([0, 1, 2, 3, 4, 5]);
var chart = acquireChart({
type: 'line',
data: {
datasets: [{
data: data
}]
}
});

chart.data.datasets[0].data = Object.freeze([5, 4, 3, 2, 1, 0]);
chart.update();

// Tests that the unlisten path also works for frozen objects
chart.destroy();
}

expect(createChart).not.toThrow();
});

it('should handle a sealed data object', function() {
function createChart() {
var data = [0, 1, 2, 3, 4, 5];
Object.seal(data);
var chart = acquireChart({
type: 'line',
data: {
datasets: [{
data: data
}]
}
});

var data2 = [5, 4, 3, 2, 1, 0];
Object.seal(data2)
chart.data.datasets[0].data = data2;
chart.update();

// Tests that the unlisten path also works for frozen objects
chart.destroy();
}

expect(createChart).not.toThrow();
});

it('should handle an unextendable data object', function() {
function createChart() {
var data = [0, 1, 2, 3, 4, 5];
Object.preventExtensions(data);
var chart = acquireChart({
type: 'line',
data: {
datasets: [{
data: data
}]
}
});

var data2 = [5, 4, 3, 2, 1, 0];
Object.preventExtensions(data2)
chart.data.datasets[0].data = data2;
chart.update();

// Tests that the unlisten path also works for frozen objects
chart.destroy();
}

expect(createChart).not.toThrow();
});
});

it('should synchronize metadata when data are inserted or removed', function() {
Expand Down

0 comments on commit cadd326

Please sign in to comment.