Skip to content

Commit c2a1c9a

Browse files
committed
Improve example to use screen
1 parent 7074ba5 commit c2a1c9a

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

README.md

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
4242

4343
- [Installation](#installation)
44-
- [A simple example](#a-simple-example)
44+
- [A basic example](#a-basic-example)
4545
- [More examples](#more-examples)
46+
- [Guiding Principles](#guiding-principles)
4647
- [Docs](#docs)
4748
- [Typings](#typings)
4849
- [ESLint support](#eslint-support)
@@ -67,10 +68,10 @@ npm install --save-dev @testing-library/vue
6768
This library has `peerDependencies` listings for `Vue` and
6869
`vue-template-compiler`.
6970

70-
You may also be interested in installing `jest-dom` so you can use
71-
[the custom Jest matchers](https://github.com/testing-library/jest-dom#readme).
71+
You may also be interested in installing `jest-dom` so you can use [the custom
72+
Jest matchers][jest-dom].
7273

73-
## A simple example
74+
## A basic example
7475

7576
```html
7677
<!-- TestComponent.vue -->
@@ -83,6 +84,7 @@ You may also be interested in installing `jest-dom` so you can use
8384

8485
<script>
8586
export default {
87+
name: 'Button',
8688
data: () => ({
8789
count: 0,
8890
}),
@@ -96,29 +98,42 @@ You may also be interested in installing `jest-dom` so you can use
9698
```
9799

98100
```js
99-
// TestComponent.spec.js
100-
import {render, fireEvent} from '@testing-library/vue'
101-
import TestComponent from './TestComponent.vue'
101+
import {render, screen, fireEvent} from '@testing-library/vue'
102+
import Button from './Button'
102103

103104
test('increments value on click', async () => {
104-
// The render method returns a collection of utilities to query the component.
105-
const {getByText} = render(TestComponent)
105+
// The `render` method renders the component into the document.
106+
// It also binds to `screen` all the available queries to interact with
107+
// the component.
108+
render(Button)
106109

107-
// getByText returns the first matching node for the provided text, and
108-
// throws an error if no elements match or if more than one match is found.
109-
getByText('Times clicked: 0')
110+
// queryByText returns the first matching node for the provided text
111+
// or returns null.
112+
expect(screen.queryByText('Times clicked: 0')).toBeTruthy()
110113

111-
// `button` is the actual DOM element.
112-
const button = getByText('increment')
114+
// getByText returns the first matching node for the provided text
115+
// or throws an error.
116+
const button = screen.getByText('increment')
113117

114-
// Dispatch a couple of native click events.
118+
// Click a couple of times.
115119
await fireEvent.click(button)
116120
await fireEvent.click(button)
117121

118-
getByText('Times clicked: 2')
122+
expect(screen.queryByText('Times clicked: 2')).toBeTruthy()
119123
})
120124
```
121125

126+
> You might want to install jest-dom[jest-dom] to add handy assertions such as
127+
> `.toBeInTheDocument()`:
128+
> `expect(screen.queryByText('Times clicked: 0')).toBeInTheDocument()`.
129+
130+
> Using `byText` queries it's not the only nor the best way to query for
131+
> elements. Read
132+
> [Which query should I use?](https://testing-library.com/docs/guide-which-query)
133+
> to discover alternatives. In the example above,
134+
> `getByRole('button', {name: 'increment'})` is possibly the best option to get
135+
> the button element.
136+
122137
### More examples
123138

124139
You'll find examples of testing with different situations and popular libraries
@@ -134,6 +149,27 @@ Some included are:
134149

135150
Feel free to contribute with more examples!
136151

152+
## Guiding Principles
153+
154+
> [The more your tests resemble the way your software is used, the more
155+
> confidence they can give you.][guiding-principle]
156+
157+
We try to only expose methods and utilities that encourage you to write tests
158+
that closely resemble how your Vue components are used.
159+
160+
Utilities are included in this project based on the following guiding
161+
principles:
162+
163+
1. If it relates to rendering components, it deals with DOM nodes rather than
164+
component instances, nor should it encourage dealing with component
165+
instances.
166+
2. It should be generally useful for testing individual Vue components or
167+
full Vue applications.
168+
3. Utility implementations and APIs should be simple and flexible.
169+
170+
At the end of the day, what we want is for this library to be pretty
171+
light-weight, simple, and understandable.
172+
137173
## Docs
138174

139175
[**Read the docs**][docs] | [Edit the docs][docs-edit]
@@ -215,6 +251,8 @@ instead of filling an issue on GitHub.
215251
[license]: https://github.com/testing-library/vue-testing-library/blob/master/LICENSE
216252
[types]: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__vue
217253
[discord]: https://testing-library.com/discord
254+
[jest-dom]: https://github.com/testing-library/jest-dom
255+
[guiding-principle]: https://twitter.com/kentcdodds/status/977018512689455106
218256

219257
[docs]: https://testing-library.com/vue
220258
[docs-edit]: https://github.com/testing-library/testing-library-docs

0 commit comments

Comments
 (0)