Skip to content

Commit

Permalink
fix(runtime-core): fix v-on object kebab-case event emit matching
Browse files Browse the repository at this point in the history
fix #3527
  • Loading branch information
yyx990803 committed Apr 1, 2021
1 parent 995d76b commit c1cd42e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
57 changes: 54 additions & 3 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Note: emits and listener fallthrough is tested in
// ./rendererAttrsFallthrough.spec.ts.

import { render, defineComponent, h, nodeOps } from '@vue/runtime-test'
import {
render,
defineComponent,
h,
nodeOps,
toHandlers
} from '@vue/runtime-test'
import { isEmitListener } from '../src/componentEmits'

describe('component: emit', () => {
Expand All @@ -28,7 +34,7 @@ describe('component: emit', () => {
expect(onBaz).toHaveBeenCalled()
})

test('trigger camelize event', () => {
test('trigger camelCase handler', () => {
const Foo = defineComponent({
render() {},
created() {
Expand All @@ -43,7 +49,52 @@ describe('component: emit', () => {
})
render(h(Comp), nodeOps.createElement('div'))

expect(fooSpy).toHaveBeenCalled()
expect(fooSpy).toHaveBeenCalledTimes(1)
})

test('trigger kebab-case handler', () => {
const Foo = defineComponent({
render() {},
created() {
this.$emit('test-event')
}
})

const fooSpy = jest.fn()
const Comp = () =>
h(Foo, {
'onTest-event': fooSpy
})
render(h(Comp), nodeOps.createElement('div'))

expect(fooSpy).toHaveBeenCalledTimes(1)
})

// #3527
test('trigger mixed case handlers', () => {
const Foo = defineComponent({
render() {},
created() {
this.$emit('test-event')
this.$emit('testEvent')
}
})

const fooSpy = jest.fn()
const barSpy = jest.fn()
const Comp = () =>
// simulate v-on="obj" usage
h(
Foo,
toHandlers({
'test-event': fooSpy,
testEvent: barSpy
})
)
render(h(Comp), nodeOps.createElement('div'))

expect(fooSpy).toHaveBeenCalledTimes(1)
expect(fooSpy).toHaveBeenCalledTimes(1)

This comment has been minimized.

Copy link
@danyadev

danyadev Apr 2, 2021

Maybe barSpy?

This comment has been minimized.

Copy link
@wellrus

wellrus Apr 7, 2021

@yyx990803
It looks like he's right

This comment has been minimized.

Copy link
@posva

posva Apr 7, 2021

Member

Thanks, created #3556

})

// for v-model:foo-bar usage in DOM templates
Expand Down
11 changes: 6 additions & 5 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,15 @@ export function emit(
}
}

// convert handler name to camelCase. See issue #2249
let handlerName = toHandlerKey(camelize(event))
let handler = props[handlerName]
let handlerName
let handler =
props[(handlerName = toHandlerKey(event))] ||
// also try camelCase event handler (#2249)
props[(handlerName = toHandlerKey(camelize(event)))]
// for v-model update:xxx events, also trigger kebab-case equivalent
// for props passed via kebab-case
if (!handler && isModelListener) {
handlerName = toHandlerKey(hyphenate(event))
handler = props[handlerName]
handler = props[(handlerName = toHandlerKey(hyphenate(event)))]
}

if (handler) {
Expand Down

0 comments on commit c1cd42e

Please sign in to comment.