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: Initialize data object when replaced #8918

Merged
merged 1 commit into from Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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