Vue Test Utils的简单使用2

161 阅读1分钟

这是我参与2022首次更文挑战的第21天,活动详情查看:2022首次更文挑战

情人节,情人不在,卷啊卷

toBe

使用该方法去做数据的对比,但是不能做浮点型的对比

expect(wrapper.get('h2').text()).toBe('1')

这里我们对比的是某个组件的h2的text的值,当前我们设置当前值为1

<h2>{{counts}}</h2>

setup () {
    const counts = ref(1)
    return {
      counts
    }
}

这里我们执行程序:

npm run test:unit

我们得到的结果是

image.png

trigger

我们在上面的基础上,再添加了点击事件,如下

const counts = ref(1)
  const handleClick = () => {
  counts.value++
}
return {
   counts,
   handleClick
}

trigger监听到click触发 DOM 事件,例如submitkeyup

那么我们可以去做点击事件的断言

如下:

it('should update the count when clicking the button', async () => {
    const msg = 'new message'
    const wrapper = shallowMount(HelloWorld, {
      props: { msg }
    })
    await wrapper.get('h2').trigger('click')
    expect(wrapper.get('h2').text()).toBe('2')
  })

toHaveProperty

用于.toHaveProperty检查keyPath对象是否存在提供的引用处的属性。要检查对象中深度嵌套的属性,您可以使用点表示法或包含 keyPath 的数组进行深度引用。

未完待续。。。