vue.js - 如何测试在 jest 中安装方法内部的全局事件总线
问题描述
export default class Test extends Vue {
mounted() {
this.getData();
eventBus.$on("get", (id: string) => {
this.displayData(id);
});
}
getData(){
return "hello"
}
displayData(id){
}
我已经为 Test 组件编写了规范,如下所示。我使用了全局事件总线并尝试检查是否发出事件。
const EventBus = new Vue();
const GlobalPlugins = {
install(v:any) {
// Event bus
v.prototype.eventBus = EventBus;
}
};
const localVue = createLocalVue()
localVue.prototype.eventBus = createLocalVue();
localVue.use(GlobalPlugins);
describe('Test TestSuite', () => {
let wrapper: any
let TestObj: any;
beforeEach(() => {
const mocks = {
eventBus: {
$on: jest.fn(),
$emit: jest.fn()
}
};
wrapper = mount(Test, {
mocks,
localVue,
router
});
console.log(eventBus.$emit) ---> returns null
TestObj = wrapper.findComponent(Test).vm;
console.log(eventBus.$emit) ---> returns null
});
在这里,我尝试检查是否发出了 get 事件。但它只给出空对象。
解决方案
通过在安装前发出事件和模拟组件来解决问题。
推荐阅读
- python - 如何在 Google Cloud Talent Solution 中创建具有自定义属性的工作
- graphql - 解析器映射模板开发指南
- javascript - 为什么我在 ctx.fillRect() 上未定义?
- xpath - 如何将 Billboard 网站上的歌曲列表导入 Google 表格?
- javascript - 嵌套数组上的 Firebase arrayUnion
- android - 哪些布局元素用于以下类型的布局
- google-app-engine - 自部署以来,GAE 站点出现新异常:“无法读取未定义的属性 'parsePath'”
- rfid - 带 rfid 的伺服旋钮控制
- jquery - 更新另一个页面(相同域)中的 DIV 内容
- blockchain - 如何查看 Parity Substrate 中的宏生成的最终代码?