Oum*_*umy 4 javascript unit-testing vue.js jestjs
我对单元测试仍然很陌生,我想测试当isLinkPresent
计算属性返回图像的链接并且该组件不为空时是否显示链接按钮:
<template lang="pug">
.merchandising_image_title
.image(:style="`background-image: url('${imageTitle.image}');`" )
.panel
.top
h2 {{imageTitle.top_title}}
.line
.center
h1 {{imageTitle.center_title}}
.bottom(v-if="isLinkPresent")
a.btn.round( target='_blank' :href='imageTitle.link') Notify Me By Email
</template>
<script>
export default {
name: 'MerchandisingImageTitle',
props: {
imageTitle: Object
},
computed:{
isLinkPresent(){
return this.imageTitle.link && this.imageTitle.link !== ''
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
我尝试通过覆盖计算属性来测试它:
it("renders linked button when image title has a link and is not empty", () => {
const wrapper = mount(ImageTitle, {
propsData: {
imageTitle: Object
},
computed: {
isLinkPresent() {
return this.imageTitle.link && this.imageTitle.link !== "";
}
}
});
expect(wrapper.find("a").isVisible()).toBe(true);
});
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误:
[vue-test-utils]: find did not return a, cannot call isVisible() on empty Wrapper
13 | }
14 | });
> 15 | expect(wrapper.find("a").isVisible()).toBe(true);
| ^
16 | });
Run Code Online (Sandbox Code Playgroud)
我不确定我做错了什么,任何帮助将不胜感激
编辑:好的,所以我意识到我没有正确传递 propsData,所以我将其更改为isVisible()propsData: { imageTitle: { imageTitleData: { image: "", top_title: "", center_title: "", link: ""}}}
并这样做expect(wrapper.find(".bottom").exists()).toBe(true)
,因为 isVisible() 主要在 v-show 中使用,但仍然收到此错误:` 当图像标题有链接但没有链接时呈现链接按钮空的
expect(received).toBe(expected) // Object.is equality
Expected: true
Received: false
20 | }
21 | });
> 22 | expect(wrapper.find(".bottom").exists()).toBe(true);
| ^
23 | });
24 |`
Run Code Online (Sandbox Code Playgroud)
还可以选择直接在包装器提供的视图模型(vm
参见Vue2 视图实例)上测试属性。
expect(wrapper.vm.isLinkPresent).toBe(true)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17367 次 |
最近记录: |