Skip to content

Commit 8503fb8

Browse files
author
Lindsay Gaines
authored
fix: don't record native events that are included in emits option (vuejs#444) (vuejs#521)
1 parent e1e4776 commit 8503fb8

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/vueWrapper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ export class VueWrapper<T extends ComponentPublicInstance>
5656
const vm = this.vm
5757
if (!vm) return
5858

59+
const emits = vm.$options.emits || []
5960
const element = this.element
6061
for (let eventName of Object.keys(eventTypes)) {
62+
// if a component includes events in 'emits' with the same name as native
63+
// events, the native events with that name should be ignored
64+
// @see https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md#fallthrough-control
65+
if (emits.includes(eventName)) continue
66+
6167
element.addEventListener(eventName, (...args) => {
6268
recordEvent(vm.$, eventName, args)
6369
})

tests/emit.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ describe('emitted', () => {
175175
expect(childWrapper.emitted().hi[1]).toEqual(['foo', 'bar'])
176176
})
177177

178+
it('should not propagate native events defined in emits', () => {
179+
const HiButton = defineComponent({
180+
name: 'HiButton',
181+
emits: ['hi', 'click'],
182+
setup(props, { emit }) {
183+
return () =>
184+
h('div', [h('button', { onClick: () => emit('hi', 'foo', 'bar') })])
185+
}
186+
})
187+
188+
const wrapper = mount(HiButton)
189+
190+
expect(wrapper.emitted()).toEqual({})
191+
192+
wrapper.find('button').trigger('click')
193+
194+
expect(wrapper.emitted().hi[0]).toEqual(['foo', 'bar'])
195+
expect(wrapper.emitted().click).toEqual(undefined)
196+
})
197+
178198
it('should allow passing the name of an event', () => {
179199
const Component = defineComponent({
180200
name: 'ContextEmit',

0 commit comments

Comments
 (0)