Skip to content

Commit

Permalink
fix(widget-element): fix multiple definition of a custom element
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Aug 15, 2024
1 parent e3c6e7d commit 3ae2ce5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/widget-element/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class TestWidgetWithFallback extends TestWidget {
TestWidget.register('test-widget')
TestWidgetWithFallback.register('test-widget-fallback')

test('register custom element once', () => {
expect(customElements.get('test-widget')).toBe(TestWidget)

// NOTE: try register multiple times
TestWidget.register('test-widget')

expect(customElements.get('test-widget')).toBe(TestWidget)
})

test('widget is ready', async () => {
const widget = document.createElement('test-widget') as TestWidget
const onReady = jest.fn()
Expand Down
5 changes: 4 additions & 1 deletion packages/widget-element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export class WidgetElement extends HTMLElement {

/** Register a widget custom element */
static register(tagName: string) {
customElements.define(tagName, this)
// NOTE: exclude multiple definition of a custom element
if (!customElements.get(tagName)) {
customElements.define(tagName, this)
}
}

/** Widget params (attributes map) */
Expand Down

0 comments on commit 3ae2ce5

Please sign in to comment.