Skip to content

Collapsible element visibility example #95

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

Merged
merged 3 commits into from
Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/__tests__/components/Collapsible.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<template>
<div>
<button @click="handleClick">Click me</button>
<div v-show="displayElement">
<p>Text</p>
</div>
</div>
</template>

<script>
export default {
data() {
return {
displayElement: false,
}
},
methods: {
handleClick(e) {
this.displayElement = !this.displayElement
},
},
}
</script>
28 changes: 28 additions & 0 deletions src/__tests__/visibility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {render, fireEvent} from '@testing-library/vue'
import '@testing-library/jest-dom/extend-expect'
import Collapsible from './components/Collapsible'

// Using the query `getByText` here is completely right because
// we use `v-show` in the component, which means that the element
// will be rendered but not visible, whereas if we use `v-if` instead
// we should use the `queryByText` and expect it to be `null` because
// the element won't be rendered
test('Collapsible component', async () => {
const {getByText} = render(Collapsible)

// Check that text element is not initially visible.
expect(getByText('Text')).not.toBeVisible()

// Click button in order to display the collapsed text element
const button = getByText('Click me')
await fireEvent.click(button)

// Check that text element is visible
expect(getByText('Text')).toBeVisible()

// Click button to hide the visible text element
await fireEvent.click(button)

// Check that text element is not visible again
expect(getByText('Text')).not.toBeVisible()
})