Skip to content

Commit 993c87f

Browse files
authored
docs: improve wording and structure (#91)
* Fix broken links * Improve wording * Fix TOC placement * Consistency is key
1 parent 6d5aefb commit 993c87f

15 files changed

+134
-70
lines changed

README.md

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<div align="center">
22
<h1>Vue Testing Library</h1>
33

4+
<br />
5+
46
<a href="https://www.joypixels.com/emoji/1F98E">
57
<img
68
height="80"
@@ -16,7 +18,7 @@
1618

1719
<br />
1820

19-
[**Read the Docs**][docs] | [Edit the docs][docs-edit]
21+
[**Read the docs**][docs] | [Edit the docs][docs-edit]
2022

2123
<br />
2224

@@ -36,6 +38,9 @@
3638

3739
<h2>Table of Contents</h2>
3840

41+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
42+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
43+
3944
- [Installation](#installation)
4045
- [A simple example](#a-simple-example)
4146
- [More examples](#more-examples)
@@ -44,9 +49,11 @@
4449
- [License](#license)
4550
- [Contributors](#contributors)
4651

52+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
53+
4754
## Installation
4855

49-
This module is distributed via npm and should be installed as one of your
56+
This module is distributed via `npm` and should be installed as one of your
5057
project's `devDependencies`:
5158

5259
```
@@ -57,7 +64,7 @@ This library has `peerDependencies` listings for `Vue` and
5764
`vue-template-compiler`.
5865

5966
You may also be interested in installing `jest-dom` so you can use
60-
[the custom Jest matchers](https://github.com/gnapse/jest-dom#readme).
67+
[the custom Jest matchers](https://github.com/testing-library/jest-dom#readme).
6168

6269
## A simple example
6370

@@ -90,7 +97,7 @@ import {render, fireEvent} from '@testing-library/vue'
9097
import TestComponent from './TestComponent.vue'
9198

9299
test('increments value on click', async () => {
93-
// The render method returns a collection of utilities to query your component.
100+
// The render method returns a collection of utilities to query the component.
94101
const {getByText} = render(TestComponent)
95102

96103
// getByText returns the first matching node for the provided text, and
@@ -100,7 +107,7 @@ test('increments value on click', async () => {
100107
// `button` is the actual DOM element.
101108
const button = getByText('increment')
102109

103-
// Dispatch a native click event.
110+
// Dispatch a couple of native click events.
104111
await fireEvent.click(button)
105112
await fireEvent.click(button)
106113

@@ -124,12 +131,12 @@ Feel free to contribute with more examples!
124131

125132
## Docs
126133

127-
[**Read the Docs**][docs] | [Edit the docs][docs-edit]
134+
[**Read the docs**][docs] | [Edit the docs][docs-edit]
128135

129136
## Typings
130137

131138
The TypeScript type definitions are in the
132-
[DefinitelyTyped repo](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__vue)
139+
[DefinitelyTyped repo][types]
133140
and bundled with Vue Testing Library.
134141

135142
## License
@@ -166,13 +173,14 @@ and bundled with Vue Testing Library.
166173
[npm]: https://badge.fury.io/js/%40testing-library%2Fvue
167174
[license-badge]: https://img.shields.io/github/license/testing-library/vue-testing-library.svg
168175
[license]: https://github.com/testing-library/vue-testing-library/blob/master/LICENSE
176+
[types]: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__vue
169177

170178
[docs]: https://testing-library.com/vue
171179
[docs-edit]: https://github.com/testing-library/testing-library-docs
172180

173-
[test-directory]: https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__
174-
[vuex-example]: https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__/vuex.js
175-
[vue-router-example]: https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__/vue-router.js
176-
[vee-validate-example]: https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__/validate-plugin.js
181+
[test-directory]: https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__
182+
[vuex-example]: https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/vuex.js
183+
[vue-router-example]: https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/vue-router.js
184+
[vee-validate-example]: https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/validate-plugin.js
177185
[vue-i18n-example]: https://github.com/testing-library/vue-testing-library/blob/master/tests/__tests__/vueI18n.js
178186
<!-- prettier-ignore-end -->

src/__tests__/__snapshots__/axios-mock.js.snap

-9
This file was deleted.

src/__tests__/auto-cleanup-skip.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ beforeAll(async () => {
55
render = vtl.render
66
})
77

8-
// This one verifies that if VTL_SKIP_AUTO_CLEANUP is set
9-
// then we DON'T auto-wire up the afterEach for folks
8+
// This test verifies that if VTL_SKIP_AUTO_CLEANUP is set then we DON'T
9+
// auto-wire up the afterEach cleanup for folks.
1010
test('first test render a vue component', () => {
1111
render({
1212
template: `<h1>Hello World</h1>`,

src/__tests__/auto-cleanup.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {render} from '@testing-library/vue'
22
import '@testing-library/jest-dom/extend-expect'
33

4-
// This just verifies that by importing VTL in an
5-
// environment which supports afterEach (like jest)
6-
// we'll get automatic cleanup between tests.
4+
// This just verifies that by importing VTL in an environment which supports
5+
// afterEach (like jest) we'll get automatic cleanup between tests.
76
test('render the first component', () => {
87
render({
98
template: `<h1>Hello World</h1>`,

src/__tests__/axios-mock.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import '@testing-library/jest-dom/extend-expect'
12
import axiosMock from 'axios'
23
import {render, fireEvent} from '@testing-library/vue'
34
import Component from './components/Fetch.vue'
4-
import '@testing-library/jest-dom/extend-expect'
55

6-
test('makes an API call and displays the greeting when load-greeting is clicked', async () => {
6+
test('mocks an API call when load-greeting is clicked', async () => {
77
axiosMock.get.mockImplementationOnce(() =>
88
Promise.resolve({
99
data: {greeting: 'hello there'},
@@ -12,7 +12,6 @@ test('makes an API call and displays the greeting when load-greeting is clicked'
1212

1313
const {html, getByText} = render(Component, {props: {url: '/greeting'}})
1414

15-
// Act
1615
await fireEvent.click(getByText('Fetch'))
1716

1817
expect(axiosMock.get).toHaveBeenCalledTimes(1)
@@ -23,5 +22,11 @@ test('makes an API call and displays the greeting when load-greeting is clicked'
2322
// that Snapshot Testing should not be treated as a replacement for regular
2423
// tests.
2524
// More about the topic: https://twitter.com/searls/status/919594505938112512
26-
expect(html()).toMatchSnapshot()
25+
expect(html()).toMatchInlineSnapshot(`
26+
<div><button>
27+
Fetch
28+
</button> <span>
29+
hello there
30+
</span></div>
31+
`)
2732
})
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template>
2+
<div>
3+
<h2>{{ $t('Hello') }}</h2>
4+
<button @click="switchLocale('en')">English</button>
5+
<button @click="switchLocale('ja')">Japanese</button>
6+
</div>
7+
</template>
8+
9+
<script>
10+
export default {
11+
name: 'VueI18n',
12+
13+
methods: {
14+
switchLocale(locale) {
15+
this.$i18n.locale = locale
16+
},
17+
},
18+
}
19+
</script>

src/__tests__/debug.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test('debug pretty prints the provided parameter', () => {
3434
expect.stringContaining('Lorem ipsum dolor sit amet'),
3535
)
3636

37-
// Notice the 'not' particle
37+
// Notice the 'not' particle.
3838
expect(console.log).not.toHaveBeenCalledWith(
3939
expect.stringContaining('Hello World'),
4040
)

src/__tests__/disappearance.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ test('waits for the data to be loaded', async () => {
99
getByText('Loading...')
1010
expect(queryByText(/Loaded this message/)).not.toBeInTheDocument()
1111

12-
// Line reads as follows: "Wait until element with text 'Loading...' is gone."
12+
// Following line reads as follows:
13+
// "Wait until element with text 'Loading...' is gone."
1314
await waitForElementToBeRemoved(() => queryByText('Loading...'))
1415
// It is equivalent to:
1516
//
1617
// await wait(() => {
1718
// expect(queryByText('Loading...')).not.toBeInTheDocument()
1819
// })
1920
//
20-
// `wait()` waits until the callback function passes or times out.
21+
// because `wait()` waits until the callback function passes or times out.
2122

2223
// After 'Loading...' is gone, we can assert that fetched data is rendered.
2324
expect(queryByTestId('message')).toHaveTextContent(/Hello World/)

src/__tests__/form.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test('Review form submits', async () => {
2424

2525
const submitButton = getByText('Submit')
2626

27-
// Initially the submit button should be disabled
27+
// Initially the submit button should be disabled.
2828
expect(submitButton).toBeDisabled()
2929

3030
const titleInput = getByLabelText(/title of the movie/i)
@@ -33,7 +33,7 @@ test('Review form submits', async () => {
3333
const reviewTextarea = getByPlaceholderText('Write an awesome review')
3434
await fireEvent.update(reviewTextarea, fakeReview.review)
3535

36-
// Rating Radio buttons
36+
// Rating Radio buttons.
3737
const initiallySelectedInput = getByLabelText('Awful')
3838
const ratingSelect = getByLabelText('Wonderful')
3939

@@ -45,23 +45,20 @@ test('Review form submits', async () => {
4545
expect(ratingSelect.checked).toBe(true)
4646
expect(initiallySelectedInput.checked).toBe(false)
4747

48-
// Get the Input element by its implicit ARIA role
48+
// Get the Input element by its implicit ARIA role.
4949
const recommendInput = getByRole('checkbox')
5050

5151
expect(recommendInput.checked).toBe(false)
5252
await fireEvent.update(recommendInput)
5353
expect(recommendInput.checked).toBe(true)
5454

55-
// NOTE: in jsdom, it's not possible to trigger a form submission
56-
// by clicking on the submit button. This is really unfortunate.
57-
// So the next best thing is to fireEvent a submit on the form itself
58-
// then ensure that there's a submit button
55+
// Make sure the submit button is enabled.
5956
expect(submitButton).toBeEnabled()
6057
expect(submitButton).toHaveAttribute('type', 'submit')
6158

6259
await fireEvent.click(submitButton)
6360

64-
// Assert event has been emitted
61+
// Assert the right event has been emitted.
6562
expect(emitted()).toHaveProperty('submit')
6663
expect(emitted().submit[0][0]).toMatchObject(fakeReview)
6764
})

src/__tests__/functional.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ const Functional = {
88
},
99
}
1010

11-
test('renders functional comp', () => {
11+
test('renders functional component', () => {
1212
const {getByText} = render(Functional)
1313

1414
getByText('Hi!')
1515
})
1616

17-
test('renders functional SFC comp', () => {
17+
test('renders functional SFC component', () => {
1818
const {getByText} = render(FunctionalSFC)
1919

2020
getByText('Hi!')

src/__tests__/select.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ import {render, fireEvent} from '@testing-library/vue'
22
import '@testing-library/jest-dom/extend-expect'
33
import Select from './components/Select'
44

5-
// In this test file we showcase several ways to interact with a Select element
5+
// In this test file we showcase several ways to interact with a Select element.
66
test('Select component', async () => {
77
let optionElement
88
const {getByDisplayValue, getByText} = render(Select)
99

10-
// Get the Select element by using the initially displayed value
10+
// Get the Select element by using the initially displayed value.
1111
const select = getByDisplayValue('Tyrannosaurus')
1212
expect(select.value).toBe('dino1')
1313

14-
// Update it by manually sending a valid option value
14+
// Update it by manually sending a valid option value.
1515
await fireEvent.update(select, 'dino2')
1616
expect(select.value).toBe('dino2')
1717

18-
// We can trigger an update event by directly getting the <option> element
18+
// We can trigger an update event by directly getting the <option> element.
1919
optionElement = getByText('Deinonychus')
2020
await fireEvent.update(optionElement)
2121
expect(select.value).toBe('dino3')
2222

23-
// ...even if option is within an <optgroup>
23+
// ...even if option is within an <optgroup>.
2424
optionElement = getByText('Diplodocus')
2525
await fireEvent.update(optionElement)
2626
expect(select.value).toBe('dino4')

src/__tests__/simple-button.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ import '@testing-library/jest-dom/extend-expect'
55
test('renders button with text', () => {
66
const text = "Click me; I'm sick"
77

8-
// Set the prop value by using the second argument of `render()`
8+
// Set the prop value by using the second argument of `render()`.
99
const {getByRole} = render(Button, {
1010
props: {text},
1111
})
1212

13-
// Get the only element with a 'button' role
13+
// Get the only element with a 'button' role.
1414
const button = getByRole('button')
1515

1616
expect(button).toHaveTextContent(text)
1717
})
1818

19-
test('click event is emitted when button is clicked', async () => {
19+
test('emits click event when button is clicked', async () => {
2020
const text = 'Click me'
2121

2222
const {getByRole, emitted} = render(Button, {
2323
props: {text},
2424
})
2525

26-
// Send a click event
26+
// Send a click event.
2727
await fireEvent.click(getByRole('button'))
2828

2929
// Expect that the event emitted a "click" event. We should test for emitted
30-
// events has they are part of the public API of the component
30+
// events has they are part of the public API of the component.
3131
expect(emitted()).toHaveProperty('click')
3232
})

src/__tests__/stopwatch.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
1+
import '@testing-library/jest-dom/extend-expect'
12
import {render, wait, fireEvent} from '@testing-library/vue'
23
import StopWatch from './components/StopWatch.vue'
3-
import '@testing-library/jest-dom/extend-expect'
4-
5-
test('unmounts a component', async () => {
6-
jest.spyOn(console, 'error').mockImplementation(() => {})
7-
8-
const {unmount, isUnmounted, getByText} = render(StopWatch)
9-
await fireEvent.click(getByText('Start'))
10-
11-
// Destroys a Vue component instance.
12-
unmount()
13-
14-
expect(isUnmounted()).toBe(true)
15-
16-
await wait()
17-
18-
expect(console.error).not.toHaveBeenCalled()
19-
})
204

215
test('updates component state', async () => {
226
const {getByTestId, getByText} = render(StopWatch)
@@ -42,3 +26,19 @@ test('updates component state', async () => {
4226
// content has changed.
4327
expect(elapsedTime).not.toHaveTextContent('0ms')
4428
})
29+
30+
test('unmounts a component', async () => {
31+
jest.spyOn(console, 'error').mockImplementation(() => {})
32+
33+
const {unmount, isUnmounted, getByText} = render(StopWatch)
34+
await fireEvent.click(getByText('Start'))
35+
36+
// Destroys a Vue component instance.
37+
unmount()
38+
39+
expect(isUnmounted()).toBe(true)
40+
41+
await wait()
42+
43+
expect(console.error).not.toHaveBeenCalled()
44+
})

src/__tests__/update-props.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import '@testing-library/jest-dom/extend-expect'
12
import {render} from '@testing-library/vue'
23
import NumberDisplay from './components/NumberDisplay.vue'
3-
import '@testing-library/jest-dom/extend-expect'
44

5+
// It'd probably be better if you test the component that's doing the prop
6+
// updating to ensure that the props are being updated correctly.
7+
// That said, if you'd prefer to update the props of a rendered component, this
8+
// function can be used to update props of the rendered component.
59
test('calling render with the same component but different props does not remount', async () => {
610
const {getByTestId, updateProps} = render(NumberDisplay, {
711
props: {number: 1},

0 commit comments

Comments
 (0)