我想使用笑话来测试我的代码,但是我使用window.getApp。$ emit。反正有测试这个案子吗?
已经使用: const spy = jest.spyOn(wrapper.vm,'$ emit')
和 const glowSpy = jest.fn(); global.getApp。$ emit = glowSpy;
我的代码如下:
successUnpackPackage (item) {
this.loading = false
window.getApp.$emit('SHOW_NOTIFICATION', `Package ${item.id} has been unpacked`, 'green')
this.searchPackages()
}
使用笑话的单元测试:
test('successUnpackPackage', () => {
const router = new VueRouter({
mode: 'history',
routes: [
{
path: '/management/package',
meta: { breadcrumb: true },
name: 'management/package',
component: Component
}
]
})
const wrapper = shallowMount(Component, {
store,
localVue,
router
})
var item = {
id: 'id'
}
wrapper.vm.loading = true
const spy = jest.spyOn(wrapper.window.getApp, '$emit')
wrapper.vm.successUnpackPackage(item)
expect(wrapper.vm.loading).toBe(false)
expect(spy).toHaveBeenCalledTimes(1)
spy.mockRestore()
})
TypeError:无法读取未定义的属性'getApp'
411 |
412 | wrapper.vm.loading = true
> 413 | const spy = jest.spyOn(wrapper.window.getApp, '$emit')
414 | wrapper.vm.successUnpackPackage(item)
415 | expect(wrapper.vm.loading).toBe(false)
416 | expect(spy).toHaveBeenCalledTimes(1)
at Object.getApp (test/unit/specs/views/management/PackageManagement.spec.js:413:43)