Skip to content

Commit 44b598a

Browse files
authored
Merge pull request #132 from afontcu/master
Add improved Vue docs
2 parents 0aa8069 + 29e895d commit 44b598a

File tree

5 files changed

+419
-113
lines changed

5 files changed

+419
-113
lines changed

docs/vue-testing-library/api.md

+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
---
2+
id: api
3+
title: API
4+
---
5+
6+
`Vue Testing Library` re-exports everything from `DOM Testing Library`.
7+
8+
It also exposes these methods:
9+
10+
- [`render(Component, options, callback)`](#rendercomponent-options-callback)
11+
- [Parameters](#parameters)
12+
- [Component](#component)
13+
- [Options](#options)
14+
- [Callback Function](#callback-function)
15+
- [`render` result](#render-result)
16+
- [`...queries`](#queries)
17+
- [`container`](#container)
18+
- [`baseElement`](#baseelement)
19+
- [`debug()`](#debug)
20+
- [`unmount()`](#unmount)
21+
- [`isUnmounted()`](#isunmounted)
22+
- [`html()`](#html)
23+
- [`emitted()`](#emitted)
24+
- [`updateProps(props)`](#updatepropsprops)
25+
- [`fireEvent`](#fireevent)
26+
- [`touch(elem)`](#touchelem)
27+
- [`update(elem, value)`](#updateelem-value)
28+
- [`cleanup`](#cleanup)
29+
30+
---
31+
32+
## `render(Component, options, callback)`
33+
34+
The `render` function is the only way of rendering components in Vue Testing
35+
Library.
36+
37+
It takes up to 3 parameters and returns an object with some helper methods.
38+
39+
```js
40+
function render(Component, options, callbackFunction) {
41+
return {
42+
...DOMTestingLibraryQueries,
43+
container,
44+
baseElement,
45+
debug,
46+
unmount,
47+
isUnmounted,
48+
html,
49+
emitted,
50+
updateProps,
51+
}
52+
}
53+
```
54+
55+
### Parameters
56+
57+
#### Component
58+
59+
The valid Vue Component to be tested.
60+
61+
#### Options
62+
63+
An object containing additional information to be passed to `@vue/test-utils`
64+
[mount](https://vue-test-utils.vuejs.org/api/options.html#context).
65+
66+
Additionally, the options object can also include the following three keys:
67+
68+
1. `store` - The object definition of a [Vuex](https://vuex.vuejs.org/) store.
69+
2. `routes` - A set of routes for [Vue Router](https://router.vuejs.org/).
70+
3. `props` - It will be merged with `propsData`.
71+
72+
If a `store` object is provided, `Vue Testing Library` will import and configure
73+
a Vuex store.
74+
75+
Similarly, if `routes` is provided, the library will import and configure Vue
76+
Router.
77+
78+
#### Callback Function
79+
80+
```js
81+
function callbackFunction(vueInstance, vuexStore, router) {}
82+
```
83+
84+
A callback function that receives the Vue instance as a parameter. This allows
85+
3rd party plugins to be installed prior to mount.
86+
87+
The function will also receive the Store or the Router object if the associated
88+
option was passed in during render.
89+
90+
### `render` result
91+
92+
The `render` method returns an object that has a few properties:
93+
94+
#### `...queries`
95+
96+
The most important feature of `render` is that the queries from
97+
[DOM Testing Library](dom-testing-library/api-queries.md) are automatically
98+
returned with their first argument bound to the [baseElement](#baseelement),
99+
which defaults to `document.body`.
100+
101+
See [Queries](dom-testing-library/api-queries.md) for a complete list of
102+
available methods.
103+
104+
```js
105+
const { getByLabelText, queryAllByTestId } = render(Component)
106+
```
107+
108+
#### `container`
109+
110+
The containing DOM node of your rendered Vue Component. It's a `div`. This is a
111+
regular DOM node, so you can call `container.querySelector` etc. to inspect the
112+
children.
113+
114+
> Tip: To get the root element of your rendered element, use
115+
> `container.firstChild`.
116+
117+
#### `baseElement`
118+
119+
Returns `document.body`, the DOM node where your Vue component is rendered.
120+
121+
#### `debug()`
122+
123+
This method is a shortcut for `console.log(prettyDOM(baseElement))`.
124+
125+
```jsx
126+
import { render } from '@testing-library/vue'
127+
128+
const HelloWorldComponent {
129+
template: `<h1>Hello World</h1>`
130+
}
131+
132+
const { debug } = render(HelloWorldComponent)
133+
debug()
134+
// <div>
135+
// <h1>Hello World</h1>
136+
// </div>
137+
138+
// you can also pass an element: debug(getByTestId('messages'))
139+
```
140+
141+
This is a simple wrapper around `prettyDOM` which is also exposed and comes from
142+
[`DOM Testing Library`](https://github.com/testing-library/dom-testing-library/blob/master/README.md#prettydom).
143+
144+
#### `unmount()`
145+
146+
An alias for `@vue/test-utils`
147+
[destroy](https://vue-test-utils.vuejs.org/api/wrapper/#destroy).
148+
149+
#### `isUnmounted()`
150+
151+
Returns whether if a Vue component has been destroyed.
152+
153+
#### `html()`
154+
155+
An alias for `@vue/test-utils`
156+
[html](https://vue-test-utils.vuejs.org/api/wrapper/#html).
157+
158+
#### `emitted()`
159+
160+
An alias for `@vue/test-utils`
161+
[emitted](https://vue-test-utils.vuejs.org/api/wrapper/#emitted).
162+
163+
#### `updateProps(props)`
164+
165+
An alias for `@vue/test-utils`
166+
[setProps](https://vue-test-utils.vuejs.org/api/wrapper/#setprops).
167+
168+
It returns a Promise through `wait()`, so you can `await updateProps(...)`.
169+
170+
---
171+
172+
## `fireEvent`
173+
174+
Vue Testing Library re-exports all DOM Testing Library
175+
[firing events](https://deploy-preview-132--testing-library.netlify.com/docs/dom-testing-library/api-events).
176+
However, it alters them so that all events are async.
177+
178+
```js
179+
await fireEvent.click(getByText('Click me'))
180+
```
181+
182+
Additionally, Vue Testing Library exposes two useful methods:
183+
184+
### `touch(elem)`
185+
186+
It triggers both `focus()` and `blur()` events.
187+
188+
```js
189+
await fireEvent.touch(getByLabelText('username'))
190+
191+
// Same as:
192+
await fireEvent.focus(getByLabelText('username'))
193+
await fireEvent.blur(getByLabelText('username'))
194+
```
195+
196+
### `update(elem, value)`
197+
198+
Properly handles inputs controlled by `v-model`. It updates the
199+
input/select/textarea inner value while emitting the appropiate native event.
200+
201+
See a working example of `update` in the
202+
[v-model example test](/docs/vue-testing-library/examples#example-using-v-model).
203+
204+
---
205+
206+
## `cleanup`
207+
208+
Unmounts Vue trees that were mounted with [render](#render).
209+
210+
```jsx
211+
import { cleanup, render } from '@testing-library/vue'
212+
import Component from './Component.vue'
213+
214+
afterEach(cleanup) // <-- add this
215+
216+
test('renders into document', () => {
217+
render(Component)
218+
// ...
219+
})
220+
221+
// ... more tests ...
222+
```
223+
224+
Failing to call `cleanup` when you've called `render` could result in a memory
225+
leak and tests which are not "idempotent" (which can lead to difficult to debug
226+
errors in your tests).
227+
228+
**If you don't want to add this to _every single test file_** then we recommend
229+
that you configure your test framework to run a file before your tests which
230+
does this automatically. See the [setup](./setup) section for guidance on how to
231+
set up your framework.

docs/vue-testing-library/examples.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
id: examples
3+
title: Examples
4+
---
5+
6+
## Basic example
7+
8+
```html
9+
<template>
10+
<div>
11+
<p>Times clicked: {{ count }}</p>
12+
<button @click="increment">increment</button>
13+
</div>
14+
</template>
15+
16+
<script>
17+
export default {
18+
data: () => ({
19+
count: 0,
20+
}),
21+
22+
methods: {
23+
increment() {
24+
this.count++
25+
},
26+
},
27+
}
28+
</script>
29+
```
30+
31+
```js
32+
import { render, fireEvent, cleanup } from '@testing-library/vue'
33+
import Component from './Component.vue'
34+
35+
// automatically unmount and cleanup DOM after the test is finished.
36+
afterEach(cleanup)
37+
38+
test('increments value on click', async () => {
39+
// The render method returns a collection of utilities to query your component.
40+
const { getByText } = render(Component)
41+
42+
// getByText returns the first matching node for the provided text, and
43+
// throws an error if no elements match or if more than one match is found.
44+
getByText('Times clicked: 0')
45+
46+
const button = getByText('increment')
47+
48+
// Dispatch a native click event to our button element.
49+
await fireEvent.click(button)
50+
await fireEvent.click(button)
51+
52+
getByText('Times clicked: 2')
53+
})
54+
```
55+
56+
## Example using `v-model`:
57+
58+
```html
59+
<template>
60+
<div>
61+
<p>Hi, my name is {{ user }}</p>
62+
63+
<label for="username">Username:</label>
64+
<input v-model="user" id="username" name="username" />
65+
</div>
66+
</template>
67+
68+
<script>
69+
export default {
70+
data: () => ({
71+
user: 'Alice',
72+
}),
73+
}
74+
</script>
75+
```
76+
77+
```js
78+
import { render, fireEvent, cleanup } from '@testing-library/vue'
79+
import Component from './Component.vue'
80+
81+
afterEach(cleanup)
82+
83+
test('properly handles v-model', async () => {
84+
const { getByLabelText, getByText } = render(Component)
85+
86+
// Asserts initial state.
87+
getByText('Hi, my name is Alice')
88+
89+
// Get the input DOM node by querying the associated label.
90+
const usernameInput = getByLabelText(/username/i)
91+
92+
// Updates the <input> value and triggers an `input` event.
93+
// fireEvent.input() would make the test fail.
94+
await fireEvent.update(usernameInput, 'Bob')
95+
96+
getByText('Hi, my name is Bob')
97+
})
98+
```
99+
100+
## More examples
101+
102+
You'll find examples of testing with different libraries in
103+
[the test directory](https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__).
104+
105+
Some included are:
106+
107+
- [`vuex`](https://github.com/testing-library/vue-testing-library/blob/master/tests/__tests__/vuex.js)
108+
- [`vue-router`](https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__/vue-router.js)
109+
- [`vee-validate`](https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__/validate-plugin.js)

0 commit comments

Comments
 (0)