Use same merging logic for init and update (#8006)

* Use same merging logic for init and update
* Add test
This commit is contained in:
Jukka Kurkela 2020-11-04 22:52:20 +02:00 committed by GitHub
parent e277aa4c1f
commit aae53c835e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 24 deletions

View File

@ -101,25 +101,12 @@ function mergeConfig(...args/* config objects ... */) {
});
}
function includeDefaults(options, type) {
return mergeConfig(
function includeDefaults(config, options) {
const scaleConfig = mergeScaleConfig(config, options);
options = mergeConfig(
defaults,
defaults.controllers[type],
defaults.controllers[config.type],
options || {});
}
function initConfig(config) {
config = config || {};
// Do NOT use mergeConfig for the data object because this method merges arrays
// and so would change references to labels and datasets, preventing data updates.
const data = config.data = config.data || {datasets: [], labels: []};
data.datasets = data.datasets || [];
data.labels = data.labels || [];
const scaleConfig = mergeScaleConfig(config, config.options);
const options = config.options = includeDefaults(config.options, config.type);
options.hover = merge(Object.create(null), [
defaults.interaction,
@ -140,6 +127,19 @@ function initConfig(config) {
options.interaction,
options.tooltips
]);
return options;
}
function initConfig(config) {
config = config || {};
// Do NOT use mergeConfig for the data object because this method merges arrays
// and so would change references to labels and datasets, preventing data updates.
const data = config.data = config.data || {datasets: [], labels: []};
data.datasets = data.datasets || [];
data.labels = data.labels || [];
config.options = includeDefaults(config, config.options);
return config;
}
@ -171,11 +171,6 @@ export default class Config {
update(options) {
const config = this._config;
const scaleConfig = mergeScaleConfig(config, options);
options = includeDefaults(options, config.type);
options.scales = scaleConfig;
config.options = options;
config.options = includeDefaults(config, options);
}
}

View File

@ -355,6 +355,28 @@ describe('Chart', function() {
});
});
describe('Updating options', function() {
it('update should result to same set of options as construct', function() {
var chart = acquireChart({
type: 'line',
data: [],
options: {
animation: false,
locale: 'en-US',
responsive: false
}
});
const options = chart.options;
chart.options = {
animation: false,
locale: 'en-US',
responsive: false
};
chart.update();
expect(chart.options).toEqual(jasmine.objectContaining(options));
});
});
describe('config.options.responsive: true (maintainAspectRatio: false)', function() {
it('should fill parent width and height', function() {
var chart = acquireChart({

View File

@ -58,7 +58,6 @@ function acquireChart(config, options) {
config.options = config.options || {};
config.options.animation = config.options.animation === undefined ? false : config.options.animation;
config.options.responsive = config.options.responsive === undefined ? false : config.options.responsive;
config.options.fontFamily = config.options.fontFamily || 'Arial';
config.options.locale = config.options.locale || 'en-US';
wrapper.appendChild(canvas);