我可以使用 vue test utils 通过 ref 选择一个元素吗 [英] Can I select an element by ref using vue test utils

查看:31
本文介绍了我可以使用 vue test utils 通过 ref 选择一个元素吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有图片

<img class="pineapple" ref="pineapple" src="pineapple.jpg"/>

我可以使用 $ref 吗

Can I use the $ref

expect(wrapper.find($refs.pineapple).exists()).toBe(true)

代替

expect(wrapper.find('.pineapple').exists()).toBe(true)

推荐答案

您可以使用 ref 属性将对象传递给 wrapper.find.

You can pass an object to the wrapper.find with the ref property.

expect(wrapper.find({ref: 'pineapple'}).exists()).toBe(true)

<小时>

来自 Vue 测试工具文档:


From the Vue Test util docs:

使用查找选项对象,Vue Test Utils 允许通过 $ref 在包装器组件上选择元素.

Using a find option object, Vue Test Utils allows for selecting elements by $ref on wrapper components.

const buttonWrapper = wrapper.find({ ref: 'myButton' })
buttonWrapper.trigger('click')

https://vue-test-utils.vuejs.org/api/选择器.html

这篇关于我可以使用 vue test utils 通过 ref 选择一个元素吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆