diff --git a/packages/common/test/unit.js b/packages/common/test/unit.js index 171b402c8d224a788f33480b89190264c407642d..e444d46dde9214299a00ea3b8abe7023054e207a 100644 --- a/packages/common/test/unit.js +++ b/packages/common/test/unit.js @@ -27,6 +27,15 @@ suite('utils', () => { expect(node.foo).to.equal(42); }); + test('defineCustomElement multiple times', () => { + class SomeElement2 extends HTMLElement { + } + let res = utils.defineCustomElement("test-some-element-2", SomeElement2); + assert.isTrue(res); + res = utils.defineCustomElement("test-some-element-2", SomeElement2); + assert.isTrue(res); + }); + test('getAPiUrl', () => { assert(utils.getAPiUrl().startsWith("http")); }); diff --git a/packages/common/utils.js b/packages/common/utils.js index d6e8753c59109d2b35c95719b4bd0a9f88dbf63f..a0d3c469d23cb7815b8c0abece7b8f7f9277fba1 100644 --- a/packages/common/utils.js +++ b/packages/common/utils.js @@ -103,6 +103,10 @@ export const base64EncodeUnicode = (str) => { * @param {object} options */ export const defineCustomElement = (name, constructor, options) => { + // In case the constructor is already defined just do nothing + if (customElements.get(name) === constructor) { + return true; + } // Checks taken from https://github.com/webcomponents/webcomponentsjs/blob/master/webcomponents-loader.js if (!('attachShadow' in Element.prototype && 'getRootNode' in Element.prototype && window.customElements)) { var elements = document.getElementsByTagName(name);