简体   繁体   English

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

[英]Test click events on router-link of vuejs using jest

<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 (i am trying to trigger the click event and check the other component is loaded or not) Test.spec.js(我试图触发点击事件并检查其他组件是否已加载)

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

but the test is failing但测试失败

You dont need to do test about router-link, the behavior of this you dont care, becouse;您不需要对路由器链接进行测试,您不关心它的行为,因为; the behavior that you need is only know that the is rendered.您需要的行为只知道 已呈现。 No more.不再。 You can do test like this你可以做这样的测试

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

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

Other thing that you can doing is verify that the prop "to" recived exactly the string "/contact"您可以做的另一件事是验证道具“to”是否准确接收到字符串“/contact”

For me, is enough .toContain(...)对我来说,就足够了 .toContain(...)

PD: sorry for my english. PD:对不起我的英语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM