Skip to content

Commit

Permalink
fix: don't record native events that are included in emits option (#444
Browse files Browse the repository at this point in the history
…) (#521)
  • Loading branch information
Lindsay Gaines authored Apr 6, 2021
1 parent e1e4776 commit 8503fb8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ export class VueWrapper<T extends ComponentPublicInstance>
const vm = this.vm
if (!vm) return

const emits = vm.$options.emits || []
const element = this.element
for (let eventName of Object.keys(eventTypes)) {
// if a component includes events in 'emits' with the same name as native
// events, the native events with that name should be ignored
// @see https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md#fallthrough-control
if (emits.includes(eventName)) continue

element.addEventListener(eventName, (...args) => {
recordEvent(vm.$, eventName, args)
})
Expand Down
20 changes: 20 additions & 0 deletions tests/emit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ describe('emitted', () => {
expect(childWrapper.emitted().hi[1]).toEqual(['foo', 'bar'])
})

it('should not propagate native events defined in emits', () => {
const HiButton = defineComponent({
name: 'HiButton',
emits: ['hi', 'click'],
setup(props, { emit }) {
return () =>
h('div', [h('button', { onClick: () => emit('hi', 'foo', 'bar') })])
}
})

const wrapper = mount(HiButton)

expect(wrapper.emitted()).toEqual({})

wrapper.find('button').trigger('click')

expect(wrapper.emitted().hi[0]).toEqual(['foo', 'bar'])
expect(wrapper.emitted().click).toEqual(undefined)
})

it('should allow passing the name of an event', () => {
const Component = defineComponent({
name: 'ContextEmit',
Expand Down

0 comments on commit 8503fb8

Please sign in to comment.