-
Notifications
You must be signed in to change notification settings - Fork 113
Add test code examples for Vue component including Transition
#327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
src/__tests__/components/TransitionSample.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<template> | ||
<div> | ||
<button @click="toggle">toggle</button> | ||
<transition @after-enter="onAfterEnter"> | ||
<div v-show="isShown"> | ||
<p>{{ showingText }}</p> | ||
</div> | ||
</transition> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
import {ref} from 'vue' | ||
|
||
const isShown = ref(false) | ||
const showingText = ref('') | ||
|
||
const onAfterEnter = () => (showingText.value = 'Completed.') | ||
const toggle = () => { | ||
isShown.value = !isShown.value | ||
showingText.value = isShown.value ? 'Now fade in...' : 'Now fade out...' | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
.v-enter-active, | ||
.v-leave-active { | ||
transition: all 0.8s ease; | ||
} | ||
|
||
.v-enter-from, | ||
.v-leave-to { | ||
transform: translateY(100%); | ||
opacity: 0; | ||
} | ||
</style> |
42 changes: 42 additions & 0 deletions
src/__tests__/transition.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import '@testing-library/jest-dom' | ||
|
||
import {fireEvent, render} from '..' | ||
|
||
import TransitionSample from './components/TransitionSample' | ||
|
||
test('shows the text', async () => { | ||
// In Vue Test Utils, the <Transition> component is stubbed | ||
// by default, but javascript hooks are not supported. | ||
// If you want to test user interactions using javascript hooks, | ||
// you can turn off the <Transition> component stubbing | ||
// by setting global stubs transition to false. | ||
const {getByRole, findByText} = render(TransitionSample, { | ||
global: { | ||
stubs: { | ||
transition: false, | ||
}, | ||
}, | ||
}) | ||
|
||
// Trigger fade in the text. | ||
await fireEvent.click(getByRole('button', {name: 'toggle'})) | ||
|
||
// If setting transition stubs, this assertion is failed | ||
// because javascript hooks are not called and the text is not changed. | ||
expect(await findByText('Completed.')).toBeVisible() | ||
}) | ||
|
||
test('hides the text', async () => { | ||
// If there is no need to use JavaScript Hooks, | ||
// you can render with the default settings. | ||
const {getByRole, queryByText} = render(TransitionSample) | ||
|
||
// Trigger fade in the text. | ||
const toggleButton = getByRole('button', {name: 'toggle'}) | ||
await fireEvent.click(toggleButton) | ||
|
||
// And trigger fade out the text. | ||
await fireEvent.click(toggleButton) | ||
|
||
expect(queryByText('Now fade out...')).not.toBeVisible() | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.