Skip to content

Commit abc572b

Browse files
edufarreafontcu
authored andcommitted
docs: Add visibility example (#95)
* Create collapsible component * Create collapsed elements visibility test * Add why we can use getByText explanation
1 parent 66e5913 commit abc572b

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<template>
2+
<div>
3+
<button @click="handleClick">Click me</button>
4+
<div v-show="displayElement">
5+
<p>Text</p>
6+
</div>
7+
</div>
8+
</template>
9+
10+
<script>
11+
export default {
12+
data() {
13+
return {
14+
displayElement: false,
15+
}
16+
},
17+
methods: {
18+
handleClick(e) {
19+
this.displayElement = !this.displayElement
20+
},
21+
},
22+
}
23+
</script>

src/__tests__/visibility.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {render, fireEvent} from '@testing-library/vue'
2+
import '@testing-library/jest-dom/extend-expect'
3+
import Collapsible from './components/Collapsible'
4+
5+
// Using the query `getByText` here is completely right because
6+
// we use `v-show` in the component, which means that the element
7+
// will be rendered but not visible, whereas if we use `v-if` instead
8+
// we should use the `queryByText` and expect it to be `null` because
9+
// the element won't be rendered
10+
test('Collapsible component', async () => {
11+
const {getByText} = render(Collapsible)
12+
13+
// Check that text element is not initially visible.
14+
expect(getByText('Text')).not.toBeVisible()
15+
16+
// Click button in order to display the collapsed text element
17+
const button = getByText('Click me')
18+
await fireEvent.click(button)
19+
20+
// Check that text element is visible
21+
expect(getByText('Text')).toBeVisible()
22+
23+
// Click button to hide the visible text element
24+
await fireEvent.click(button)
25+
26+
// Check that text element is not visible again
27+
expect(getByText('Text')).not.toBeVisible()
28+
})

0 commit comments

Comments
 (0)