Question: How to use a real Transition component? #1913
-
Describe the bug To Reproduce Expected behavior Related information:
|
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
Thanks for the repro I don't get how that's supposed to work: watch(props, () => {
if (props.modelValue) {
showOverlay.value = true;
return;
}
showContent.value = !showContent.value;
}); when you call |
Beta Was this translation helpful? Give feedback.
-
hi @cexbrayat, thanks for your response :) Basically, the watch function does 2 things:
This is the step when
This is the step when
When modelValue is true it the first I can't remove |
Beta Was this translation helpful? Give feedback.
-
Ha OK. It's not a problem with the watcher then. It's because your test uses a transition mock (this is the case by default in VTU), and that mock won't emit events like
to use the real transition component, but then you'll have to trigger the next frame for the animation to finish. If I remember correctly, you can mock
So something like this works: it('should set width based on props', async () => {
vi.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb());
const wrapper = mount(Dialog, {
props: {
width: '400px',
},
global: {
stubs: {
transition: false
}
}
});
await wrapper.setProps({ modelValue: true });
console.log(wrapper.html());
expect(wrapper.get('.dialog').attributes('style')).toBe('width: 400px;');
}); |
Beta Was this translation helpful? Give feedback.
-
It works like a charm now! Thanks! But this code |
Beta Was this translation helpful? Give feedback.
-
I'm glad that helped 👍 You can just cast with TS or add a Let's close this as not an issue then |
Beta Was this translation helpful? Give feedback.
-
For anyone in the future with a similar problem, here's the complete code to use the real transition component in Vitest + VTU testing it('should set width based on props', async () => {
vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => {
callback(0)
return 0
})
const wrapper = mount(Dialog, {
props: {
width: '400px',
},
global: {
stubs: {
transition: false,
},
},
})
await wrapper.setProps({ modelValue: true })
expect(wrapper.get('.dialog').attributes('style')).toBe('width: 400px;')
}) References: |
Beta Was this translation helpful? Give feedback.
Ha OK. It's not a problem with the watcher then. It's because your test uses a transition mock (this is the case by default in VTU), and that mock won't emit events like
afterEnter
. You can try to use:to use the real transition component, but then you'll have to trigger the next frame for the animation to finish. If I remember correctly, you can mock
requestAnimationFrame
to do so with something like:So something like this works: