diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 5a17d1afb64..d73bb4e4934 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -35,8 +35,13 @@ defaults._set('global', { responsiveAnimationDuration: 0 }); -function mergeScaleConfig(/* objects ... */) { - return helpers.merge(helpers.clone(arguments[0]), [].slice.call(arguments, 1), { +/** + * Recursively merge the given config objects as the `scales` options by + * incorporating scale defaults in `xAxes` and `yAxes` array items, then + * returns a deep copy of the result, thus doesn't alter inputs. + */ +function mergeScaleConfig(/* config objects ... */) { + return helpers.merge({}, Array.prototype.slice.call(arguments), { merger: function(key, target, source, options) { if (key === 'xAxes' || key === 'yAxes') { var slen = source[key].length; @@ -70,8 +75,13 @@ function mergeScaleConfig(/* objects ... */) { }); } -function mergeConfig(/* objects ... */) { - return helpers.merge(helpers.clone(arguments[0]), [].slice.call(arguments, 1), { +/** + * Recursively merge the given config objects as the root options by handling + * default scale options for the `scales` and `scale` properties, then returns + * a deep copy of the result, thus doesn't alter inputs. + */ +function mergeConfig(/* config objects ... */) { + return helpers.merge({}, Array.prototype.slice.call(arguments), { merger: function(key, target, source, options) { var tval = target[key] || {}; var sval = source[key]; diff --git a/test/specs/core.controller.tests.js b/test/specs/core.controller.tests.js index 62c54b75ce1..819074393b1 100644 --- a/test/specs/core.controller.tests.js +++ b/test/specs/core.controller.tests.js @@ -271,6 +271,36 @@ describe('Chart', function() { _jasmineCheckE: 'e2' })); }); + + it('should not alter defaults when merging config', function() { + var chart = acquireChart({ + type: 'line', + options: { + _jasmineCheck: 42, + scales: { + xAxes: [{ + id: 'foo', + type: 'linear', + _jasmineCheck: 42, + }], + yAxes: [{ + id: 'bar', + type: 'category', + _jasmineCheck: 42, + }] + } + } + }); + + expect(chart.options._jasmineCheck).toBeDefined(); + expect(chart.scales.foo.options._jasmineCheck).toBeDefined(); + expect(chart.scales.bar.options._jasmineCheck).toBeDefined(); + + expect(Chart.defaults.line._jasmineCheck).not.toBeDefined(); + expect(Chart.defaults.global._jasmineCheck).not.toBeDefined(); + expect(Chart.scaleService.defaults.linear._jasmineCheck).not.toBeDefined(); + expect(Chart.scaleService.defaults.category._jasmineCheck).not.toBeDefined(); + }); }); describe('config.options.responsive: false', function() {