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

时间:2020-10-07 10:01:21

标签: javascript vue.js jestjs

<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/>

Test.spec.js (我试图触发click事件并检查是否加载了其他组件)

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

但测试失败

1 个答案:

答案 0 :(得分:0)

您不需要对路由器链接进行测试,因为您不关心它的行为,因为;您需要的行为仅知道呈现了。不再。 您可以像这样进行测试

 const wrapper: any = shallowMount(YourComponent, {
    stubs: ['router-link', 'router-view']
  });

expect(wrapper.html()).toContain('to="/contact"');

您可以做的另一件事是验证道具“ to”正确接收字符串“ / contact”

对我来说.toContain(...)

PD:对不起,我的英语。

相关问题