diff --git a/src/__tests__/components/Collapsible.vue b/src/__tests__/components/Collapsible.vue new file mode 100644 index 00000000..33766853 --- /dev/null +++ b/src/__tests__/components/Collapsible.vue @@ -0,0 +1,23 @@ + + + diff --git a/src/__tests__/visibility.js b/src/__tests__/visibility.js new file mode 100644 index 00000000..322c11eb --- /dev/null +++ b/src/__tests__/visibility.js @@ -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() +})