【发布时间】:2020-08-19 20:35:08
【问题描述】:
我正在尝试测试一个组件,该组件应在用户单击时隐藏。功能在浏览器中有效,但在使用 Jest 进行自动化测试期间测试失败。
这是测试:
it(`If the local variable is set to be clicked,
then the tip is hidden`, () => {
const wrapper = mount(Component, { props });
wrapper.setData({ was_clicked: true });
wrapper.vm.$forceUpdate();
expect(wrapper.classes()).toContain("hide"); // fails here
expect(wrapper.find(".toast").isVisible()).toBe(false);
});
这是组件:
<template>
<div @click="hide" class="toast" :class="{ hide: was_clicked }">
...
</div>
</template>
<script>
export default {
name: ...,
data() {
return {
was_clicked: false,
};
},
props: {
...
},
methods: {
hide(event) {
this.was_clicked = true;
},
},
};
</script>
我试图在测试中添加和删除wrapper.vm.$forceUpdate();,另外,我测试了wrapper.vm的nextTick
【问题讨论】:
标签: vue.js testing jestjs frontend vue-component