使用jest测试vuejs的路由器链接上的点击事件


<ul>
<router-link to="/" tag="li" active-class="active" exact><a >Home</a></router-link>
<router-link to="/contact" tag="li" exact><a>contact</a></router-link>
<router-link to="/other" tag="li" exact><a>other</a></router-link>
<ul/>

测试.spec.js(我正在尝试触发点击事件,并检查是否加载了其他组件(

await wrapper.findAll('a').at(2).trigger('click')
await wrapper.vm.$nextTick()
expect(wrapper.contains('This is other Page')).toBe(true)

但是测试没有通过

你不需要对路由器链接进行测试,因为你不在乎它的行为;您需要的行为只是知道已渲染。不再。你可以做这样的测试

const wrapper: any = shallowMount(YourComponent, {
stubs: ['router-link', 'router-view']
});
expect(wrapper.html()).toContain('to="/contact"');

你可以做的另一件事是验证道具";至";收到的正是字符串"/"联系人";

对我来说,已经足够了。包含(…(

PD:对不起我的英语。

最新更新