feat(runtime-core): type and attr fallthrough support for emits option

This commit is contained in:
Evan You
2020-04-03 12:05:52 -04:00
parent c409d4f297
commit bf473a64ea
9 changed files with 350 additions and 96 deletions

View File

@@ -8,7 +8,8 @@ import {
onUpdated,
defineComponent,
openBlock,
createBlock
createBlock,
FunctionalComponent
} from '@vue/runtime-dom'
import { mockWarn } from '@vue/shared'
@@ -428,4 +429,70 @@ describe('attribute fallthrough', () => {
await nextTick()
expect(root.innerHTML).toBe(`<div aria-hidden="false" class="barr"></div>`)
})
it('should not let listener fallthrough when declared in emits (stateful)', () => {
const Child = defineComponent({
emits: ['click'],
render() {
return h(
'button',
{
onClick: () => {
this.$emit('click', 'custom')
}
},
'hello'
)
}
})
const onClick = jest.fn()
const App = {
render() {
return h(Child, {
onClick
})
}
}
const root = document.createElement('div')
document.body.appendChild(root)
render(h(App), root)
const node = root.children[0] as HTMLElement
node.dispatchEvent(new CustomEvent('click'))
expect(onClick).toHaveBeenCalledTimes(1)
expect(onClick).toHaveBeenCalledWith('custom')
})
it('should not let listener fallthrough when declared in emits (functional)', () => {
const Child: FunctionalComponent<{}, { click: any }> = (_, { emit }) => {
// should not be in props
expect((_ as any).onClick).toBeUndefined()
return h('button', {
onClick: () => {
emit('click', 'custom')
}
})
}
Child.emits = ['click']
const onClick = jest.fn()
const App = {
render() {
return h(Child, {
onClick
})
}
}
const root = document.createElement('div')
document.body.appendChild(root)
render(h(App), root)
const node = root.children[0] as HTMLElement
node.dispatchEvent(new CustomEvent('click'))
expect(onClick).toHaveBeenCalledTimes(1)
expect(onClick).toHaveBeenCalledWith('custom')
})
})