Skip to content

Commit

Permalink
fix: emits can be an object (#542)
Browse files Browse the repository at this point in the history
The fix introduced in #521 only account for emits define as an array, but it can also be define as an object.
The latest rc.5 release breaks on components that use an object to define emits with:

    TypeError: emits.includes is not a function

This commit fixes the unit test to have both cases check, and the relevant code to properly handle the object case.
  • Loading branch information
cexbrayat authored Apr 19, 2021
1 parent 9ffd586 commit fb92c07
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ export class VueWrapper<T extends ComponentPublicInstance>
const vm = this.vm
if (!vm) return

const emits = vm.$options.emits || []
const emits = vm.$options.emits
? // if emits is declared as an array
Array.isArray(vm.$options.emits)
? // use it
vm.$options.emits
: // otherwise it's declared as an object
// and we only need the keys
Object.keys(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
Expand Down
4 changes: 3 additions & 1 deletion tests/emit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ describe('emitted', () => {
it('should not propagate child custom events', () => {
const Child = defineComponent({
name: 'Child',
emits: ['hi'],
emits: {
hi: (foo: 'foo', bar: 'bar') => true
},
setup(props, { emit }) {
return () =>
h('div', [h('button', { onClick: () => emit('hi', 'foo', 'bar') })])
Expand Down

0 comments on commit fb92c07

Please sign in to comment.