Skip to content

Commit

Permalink
Fix: Initialize data object when replaced (#8918)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Apr 17, 2021
1 parent 8e535c5 commit 9e06f90
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/core/core.config.js
Expand Up @@ -85,12 +85,16 @@ function initOptions(config) {
options.scales = mergeScaleConfig(config, options);
}

function initConfig(config) {
config = config || {};

const data = config.data = config.data || {datasets: [], labels: []};
function initData(data) {
data = data || {};
data.datasets = data.datasets || [];
data.labels = data.labels || [];
return data;
}

function initConfig(config) {
config = config || {};
config.data = initData(config.data);

initOptions(config);

Expand Down Expand Up @@ -137,7 +141,7 @@ export default class Config {
}

set data(data) {
this._config.data = data;
this._config.data = initData(data);
}

get options() {
Expand Down
11 changes: 11 additions & 0 deletions test/specs/core.controller.tests.js
Expand Up @@ -230,6 +230,17 @@ describe('Chart', function() {
expect(createChart).toThrow(new Error('"area" is not a registered controller.'));
});

it('should initialize the data object', function() {
const chart = acquireChart({type: 'bar'});
expect(chart.data).toEqual(jasmine.objectContaining({labels: [], datasets: []}));
chart.data = {};
expect(chart.data).toEqual(jasmine.objectContaining({labels: [], datasets: []}));
chart.data = null;
expect(chart.data).toEqual(jasmine.objectContaining({labels: [], datasets: []}));
chart.data = undefined;
expect(chart.data).toEqual(jasmine.objectContaining({labels: [], datasets: []}));
});

describe('should disable hover', function() {
it('when options.hover=false', function() {
var chart = acquireChart({
Expand Down

0 comments on commit 9e06f90

Please sign in to comment.