Skip to content

Commit bcf2eb9

Browse files
committed
test: add an example to use Transition
1 parent d46ed8f commit bcf2eb9

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<template>
2+
<div>
3+
<button @click="toggle">toggle</button>
4+
<transition @after-enter="onAfterEnter">
5+
<div v-show="isShown">
6+
<p>{{ showingText }}</p>
7+
</div>
8+
</transition>
9+
</div>
10+
</template>
11+
12+
<script setup>
13+
import {ref} from 'vue'
14+
15+
const isShown = ref(false)
16+
const showingText = ref('')
17+
18+
const onAfterEnter = () => (showingText.value = 'Completed.')
19+
const toggle = () => {
20+
isShown.value = !isShown.value
21+
showingText.value = isShown.value ? 'Now fade in...' : 'Now fade out...'
22+
}
23+
</script>
24+
25+
<style scoped>
26+
.v-enter-active,
27+
.v-leave-active {
28+
transition: all 0.8s ease;
29+
}
30+
31+
.v-enter-from,
32+
.v-leave-to {
33+
transform: translateY(100%);
34+
opacity: 0;
35+
}
36+
</style>

src/__tests__/transition.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import '@testing-library/jest-dom'
2+
3+
import {fireEvent, render} from '..'
4+
5+
import TransitionSample from './components/TransitionSample'
6+
7+
test('shows the text', async () => {
8+
// In Vue Test Utils, the <Transition> component is stubbed
9+
// by default, but javascript hooks are not supported.
10+
// If you want to test user interactions using javascript hooks,
11+
// you can turn off the <Transition> component stubbing
12+
// by setting global stubs transition to false.
13+
const {getByRole, findByText} = render(TransitionSample, {
14+
global: {
15+
stubs: {
16+
transition: false,
17+
},
18+
},
19+
})
20+
21+
// Trigger fade in the text.
22+
await fireEvent.click(getByRole('button', {name: 'toggle'}))
23+
24+
// If setting transition stubs, this assertion is failed
25+
// because javascript hooks are not called and the text is not changed.
26+
expect(await findByText('Completed.')).toBeVisible()
27+
})
28+
29+
test('hides the text', async () => {
30+
// If there is no need to use JavaScript Hooks,
31+
// you can render with the default settings.
32+
const {getByRole, queryByText} = render(TransitionSample)
33+
34+
// Trigger fade in the text.
35+
const toggleButton = getByRole('button', {name: 'toggle'})
36+
await fireEvent.click(toggleButton)
37+
38+
// And trigger fade out the text.
39+
await fireEvent.click(toggleButton)
40+
41+
expect(queryByText('Now fade out...')).not.toBeVisible()
42+
})

0 commit comments

Comments
 (0)