return scope;
}
- if (Object.keys(defaults.get(scope)).length) {
- throw new Error('Can not register "' + id + '", because "defaults.' + scope + '" would collide with existing defaults');
- }
-
items[id] = item;
registerDefaults(item, scope, parentScope);
}
function registerDefaults(item, scope, parentScope) {
- // Inherit the parent's defaults
- const itemDefaults = parentScope
- ? Object.assign({}, defaults.get(parentScope), item.defaults)
- : item.defaults;
+ // Inherit the parent's defaults and keep existing defaults
+ const itemDefaults = Object.assign(
+ Object.create(null),
+ parentScope && defaults.get(parentScope),
+ item.defaults,
+ defaults.get(scope)
+ );
defaults.set(scope, itemDefaults);
expect(afterUnregisterCount).withContext('afterUnregister').toBe(4);
});
- it('should not register anything that would collide with existing defaults', function() {
- Chart.defaults.controllers.test = {test: true};
+ it('should preserve existing defaults', function() {
+ Chart.defaults.controllers.test = {test1: true};
+
class testController extends Chart.DatasetController {}
testController.id = 'test';
- expect(function() {
- Chart.register(testController);
- }).toThrow(new Error('Can not register "test", because "defaults.controllers.test" would collide with existing defaults'));
- delete Chart.defaults.controllers.test;
+ testController.defaults = {test1: false, test2: true};
+
+ Chart.register(testController);
+ expect(Chart.defaults.controllers.test).toEqual({test1: true, test2: true});
+
+ Chart.unregister(testController);
+ expect(Chart.defaults.controllers.test).not.toBeDefined();
});
describe('should handle multiple items', function() {