过渡
通常,您可能希望测试过渡后的最终 DOM,这就是 Vue 测试工具默认情况下模拟 <transition>
和 <transition-group>
的原因。
以下是一个简单的组件,它切换一个包裹在淡入淡出过渡中的内容
vue
<template>
<button @click="show = !show">Toggle</button>
<transition name="fade">
<p v-if="show">hello</p>
</transition>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
const show = ref(false)
return {
show
}
}
}
</script>
<style lang="css">
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s ease;
}
.fade-enter-from, .fade-leave-to {
opacity: 0;
}
</style>
由于 Vue 测试工具对内置过渡进行了存根处理,因此您可以像测试任何其他组件一样测试上面的组件
js
import Component from './Component.vue'
import { mount } from '@vue/test-utils'
test('works with transitions', async () => {
const wrapper = mount(Component)
expect(wrapper.find('hello').exists()).toBe(false)
await wrapper.find('button').trigger('click')
// After clicking the button, the <p> element exists and is visible
expect(wrapper.get('p').text()).toEqual('hello')
})
部分支持
Vue 测试工具内置的过渡存根很简单,它没有涵盖 Vue 的所有 过渡功能。例如,JavaScript 钩子 不受支持。此限制可能会导致 Vue 警告。