From 8e7cb406f935f0f1a45dfcf57f41ca7dfbb16bef Mon Sep 17 00:00:00 2001 From: Monica Kozbial <6621618+moniika@users.noreply.github.com> Date: Tue, 8 Jun 2021 12:28:59 -0700 Subject: [PATCH] Handle overriding components in ComponentManager (#4856) --- core/component_manager.js | 12 +++++++++++- tests/mocha/toolbox_test.js | 6 ++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/component_manager.js b/core/component_manager.js index 825473186..72828a4a4 100644 --- a/core/component_manager.js +++ b/core/component_manager.js @@ -54,9 +54,19 @@ Blockly.ComponentManager.ComponentDatum; * Adds a component. * @param {!Blockly.ComponentManager.ComponentDatum} componentInfo The data for * the component to register. + * @param {boolean=} opt_allowOverrides True to prevent an error when overriding + * an already registered item. * @template T */ -Blockly.ComponentManager.prototype.addComponent = function(componentInfo) { +Blockly.ComponentManager.prototype.addComponent = function( + componentInfo, opt_allowOverrides) { + // Don't throw an error if opt_allowOverrides is true. + if (!opt_allowOverrides && this.componentData_[componentInfo.id]) { + throw Error( + 'Plugin "' + componentInfo.id + '" with capabilities "' + + this.componentData_[componentInfo.id].capabilities + + '" already added.'); + } this.componentData_[componentInfo.id] = componentInfo; for (var i = 0, type; (type = componentInfo.capabilities[i]); i++) { var typeKey = String(type).toLowerCase(); diff --git a/tests/mocha/toolbox_test.js b/tests/mocha/toolbox_test.js index 19d88e4ab..ed49e7136 100644 --- a/tests/mocha/toolbox_test.js +++ b/tests/mocha/toolbox_test.js @@ -34,6 +34,8 @@ suite('Toolbox', function() { test('Init called -> Toolbox is subscribed to background and foreground colour', function() { var themeManager = this.toolbox.workspace_.getThemeManager(); var themeManagerSpy = sinon.spy(themeManager, 'subscribe'); + var componentManager = this.toolbox.workspace_.getComponentManager(); + sinon.stub(componentManager, 'addComponent'); this.toolbox.init(); sinon.assert.calledWith(themeManagerSpy, this.toolbox.HtmlDiv, 'toolboxBackgroundColour', 'background-color'); @@ -42,10 +44,14 @@ suite('Toolbox', function() { }); test('Init called -> Render is called', function() { var renderSpy = sinon.spy(this.toolbox, 'render'); + var componentManager = this.toolbox.workspace_.getComponentManager(); + sinon.stub(componentManager, 'addComponent'); this.toolbox.init(); sinon.assert.calledOnce(renderSpy); }); test('Init called -> Flyout is initialized', function() { + var componentManager = this.toolbox.workspace_.getComponentManager(); + sinon.stub(componentManager, 'addComponent'); this.toolbox.init(); chai.assert.isDefined(this.toolbox.flyout_); });