我有一个具有以下结构的Vue组件
// parent-component.vue
<main>
<component :is="my.component" @custom-event="callback"/>
</main>子组件始终具有以下mixin
// child-shared-mixin.js
export default {
mounted() {
this.$emit('custom-event')
},
}下面是子组件的示例
// child-component.vue
<script>
import { ChildSharedMixin } from 'mixins'
export default {
mixins: [
ChildSharedMixin
],
}
</script>因此,每当挂载child时,我都会触发一个事件到父进程,然后执行回调。
使用Jest和Vue Test Utils,如何测试mixin是否触发了custom-event
发布于 2018-11-09 10:03:58
返回包含包装器vm发出的自定义事件的对象。
https://vue-test-utils.vuejs.org/api/wrapper/#emitted
因此,要测试子组件,可以执行以下操作:
describe('myComponent',()={
it('should trigger custom-event on mounted hook',()=>{
let target=mount(myComponent);
expect(target.emitted()['custom-event']).toBeTruthy();
})
})要测试父组件,则需要进行相反的操作--通过模拟事件并期待回调被调用。看一看:
https://stackoverflow.com/questions/53222768
复制相似问题