Skip to content

Commit

Permalink
feat(widget-element): add emit error
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Aug 19, 2024
1 parent 3a7a3da commit 0d34e33
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
},
{
"path": "packages/widget-element/dist/index.js",
"limit": "960 B"
"limit": "990 B"
}
]
33 changes: 33 additions & 0 deletions packages/widget-element/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,36 @@ test('show custom widget fallback', () => {

expect(widget.root.innerHTML).toBe('')
})

test('emit event', () => {
const widget = document.createElement('test-widget') as TestWidget
const onEvent = jest.fn()

document.body.append(widget)

widget.addEventListener('customevent', onEvent)
widget.emit('customevent', {detail: 'foo'})

const [[event]] = onEvent.mock.calls

expect(onEvent).toHaveBeenCalledTimes(1)
expect(event.detail).toBe('foo')
})

test('emit error', () => {
const widget = document.createElement('test-widget') as TestWidget
const onError = jest.fn()

document.body.append(widget)

const error = new Error('widget error')

widget.addEventListener('error', onError)
widget.emitError(error)

const [[event]] = onError.mock.calls

expect(onError).toHaveBeenCalledTimes(1)
expect(event.error).toBe(error)
expect(event.message).toBe(error.message)
})
5 changes: 5 additions & 0 deletions packages/widget-element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,9 @@ export class WidgetElement extends HTMLElement {
emit(eventType: string, options?: CustomEventInit) {
this.dispatchEvent(new CustomEvent(eventType, options))
}

/** Dispatch error */
emitError(error: Error) {
this.dispatchEvent(new ErrorEvent('error', {error, message: error.message}))
}
}

0 comments on commit 0d34e33

Please sign in to comment.