Skip to content

Commit fda8760

Browse files
committed
Add improved Vue docs
1 parent 0aa8069 commit fda8760

File tree

5 files changed

+414
-112
lines changed

5 files changed

+414
-112
lines changed

docs/vue-testing-library/api.md

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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, value)`](#touchelem-value)
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 alters the original `fireEvent` from DOM Testing Library so
175+
that all events are async (ie: `await fireEvent.click(button)`).
176+
177+
Vue Testing Library exposes two additional methods:
178+
179+
### `touch(elem, value)`
180+
181+
It triggers both `focus()` and `blur()` events.
182+
183+
### `update(elem, value)`
184+
185+
Properly handles inputs controlled by `v-model`. It updates the
186+
input/select/textarea inner value while emitting the appropiate native event.
187+
188+
See the [v-model test](/docs/vue-testing-library/examples#example-using-v-model)
189+
in the examples page.
190+
191+
---
192+
193+
## `cleanup`
194+
195+
Unmounts Vue trees that were mounted with [render](#render).
196+
197+
```jsx
198+
import { cleanup, render } from '@testing-library/vue'
199+
import Component from './Component.vue'
200+
201+
afterEach(cleanup) // <-- add this
202+
203+
test('renders into document', () => {
204+
render(Component)
205+
// ...
206+
})
207+
208+
// ... more tests ...
209+
```
210+
211+
Failing to call `cleanup` when you've called `render` could result in a memory
212+
leak and tests which are not "idempotent" (which can lead to difficult to debug
213+
errors in your tests).
214+
215+
**If you don't want to add this to _every single test file_** then we recommend
216+
that you configure your test framework to run a file before your tests which
217+
does this automatically. See the [setup](./setup) section for guidance on how to
218+
set up your framework.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
id: examples
3+
title: Examples
4+
---
5+
6+
## Basic example
7+
8+
```html
9+
<!-- src/TestComponent.vue -->
10+
<template>
11+
<div>
12+
<p>Times clicked: {{ count }}</p>
13+
<button @click="increment">increment</button>
14+
</div>
15+
</template>
16+
17+
<script>
18+
export default {
19+
data() {
20+
return {
21+
count: 0,
22+
}
23+
},
24+
25+
methods: {
26+
increment() {
27+
this.count++
28+
},
29+
},
30+
}
31+
</script>
32+
```
33+
34+
```js
35+
// src/TestComponent.spec.js
36+
import { render, fireEvent, cleanup } from '@testing-library/vue'
37+
import TestComponent from './TestComponent.vue'
38+
39+
// automatically unmount and cleanup DOM after the test is finished.
40+
afterEach(cleanup)
41+
42+
it('increments value on click', async () => {
43+
// The render method returns a collection of utilities to query your component.
44+
const { getByText } = render(TestComponent)
45+
46+
// getByText returns the first matching node for the provided text, and
47+
// throws an error if no elements match or if more than one match is found.
48+
getByText('Times clicked: 0')
49+
50+
const button = getByText('increment')
51+
52+
// Dispatch a native click event to our button element.
53+
await fireEvent.click(button)
54+
await fireEvent.click(button)
55+
56+
getByText('Times clicked: 2')
57+
})
58+
```
59+
60+
## Example using `v-model`:
61+
62+
```html
63+
<template>
64+
<div>
65+
<p>Hi, my name is {{ user }}</p>
66+
67+
<label for="username">Username:</label>
68+
<input v-model="user" id="username" name="username" />
69+
</div>
70+
</template>
71+
72+
<script>
73+
export default {
74+
data() {
75+
return {
76+
user: 'empty',
77+
}
78+
},
79+
}
80+
</script>
81+
```
82+
83+
```js
84+
import { render, fireEvent, cleanup } from '@testing-library/vue'
85+
import Component from './Component'
86+
87+
afterEach(cleanup)
88+
89+
test('properly handles v-model', async () => {
90+
const { getByLabelText, getByText } = render(Component)
91+
92+
// Asserts initial state.
93+
getByText('Hi, my name is empty')
94+
95+
const usernameInput = getByLabelText(/username/i)
96+
97+
// Updates the <input> value and triggers an `input` event.
98+
// fireEvent.input() would make the test fail.
99+
await fireEvent.update(usernameInput, 'Alice')
100+
101+
getByText('Hi, my name is Alice')
102+
})
103+
```
104+
105+
## More examples
106+
107+
You'll find examples of testing with different libraries in
108+
[the test directory](https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__).
109+
110+
Some included are:
111+
112+
- [`vuex`](https://github.com/testing-library/vue-testing-library/blob/master/tests/__tests__/vuex.js)
113+
- [`vue-router`](https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__/vue-router.js)
114+
- [`vee-validate`](https://github.com/testing-library/vue-testing-library/tree/master/tests/__tests__/validate-plugin.js)

0 commit comments

Comments
 (0)