如何在VUE 3测试效果中测试过渡功能?
我在带Jest和Vue测试UTLIS的Transiton功能方面遇到了问题。这是我的组件代码:
<div class="disclosure">
<button
class="label"
type="button"
@click="toggle"
>
<span class="label-text">
<slot name="disclosure-title" />
</span>
</button>
<Transition
name="disclosure"
@before-enter="transitionBaseState"
@enter="transitionEndState"
@before-leave="transitionEndState"
@leave="transitionBaseState"
>
<div
v-show="open"
class="panel"
>
<div class="panel-content">
<slot />
</div>
</div>
</Transition>
</div>
我在过渡功能上有问题:过渡性和过渡设备。当我为组件运行EJST规格时,覆盖范围标签显示这些功能未涵盖。您知道测试这些功能的最佳方法是什么?我显示元素的测试是:
it('can be changed to opened by clicking the panel', async () => {
await wrapper.find(buttonSelector).trigger('click');
expect(wrapper.find(panelSelector).isVisible()).toBe(true);
});
函数:
function transitionBaseState(el: HTMLElement): void {
el.style.height = '0';
}
function transitionEndState(el: HTMLElement): void {
el.style.height = `${el.scrollHeight}px`;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的。这是艰难的。 vue测试用 false 值在全球范围内模拟它。
如您所见,我添加了使用假计时器的解决方法来处理过渡持续时间。
实际上,在大多数情况下,您只是不需要测试过渡的作用。 Vue Core团队已经对此进行了测试,但是也许在某些情况下应该对其进行测试。如您所见,测试中的行数增加只是为了避免默认行为。它还使用无证件模型,您应该在每次更新VUE/VUE检验效果之后保持工作,并进行一些破坏性的更改。
无论如何,我已经在上面测试了此代码。这是我的尝试
Ok. This is the tough one. Vue test utils stubs transitions by default. And this behavior is not covered by docs. You need to mock it globally by hardcoded
false
value.As you can see, I added workaround with fake timers to deal with transition duration.
In fact, in most cases you just don't need to test what transition does. This is already tested by vue core team, but maybe there are some cases where it should be tested. As you can see, the number of line in tests grows just to avoid default behavior. It also uses undocumented mocks and you should to keep that working after every update of Vue/vue-test-utils with some breaking changes.
Anyway I've tested this code above. Here is my attempt
这是我的解决方案:
This is my solution: