Skip to content

Commit 4c34f57

Browse files
authored
Merge pull request #767 from codfish/vtl-missing-docs
docs(vue): improve container and baseElement docs on VTL api page
2 parents 6f41cb9 + 99f094d commit 4c34f57

File tree

1 file changed

+62
-30
lines changed

1 file changed

+62
-30
lines changed

docs/vue-testing-library/api.mdx

+62-30
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,58 @@ The valid Vue Component to be tested.
6363
An object containing additional information to be passed to `@vue/test-utils`
6464
[mount](https://vue-test-utils.vuejs.org/api/options.html#mounting-options).
6565

66-
Additionally, the options object can also include the following three keys:
66+
Additionally, the following options can also be provided:
6767

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`.
68+
##### `store` (`Object`)
7169

72-
If a `store` object is provided, `Vue Testing Library` will import and configure
73-
a Vuex store.
70+
The object definition of a [Vuex](https://vuex.vuejs.org/) store. If a `store`
71+
object is provided, `Vue Testing Library` will import and configure a Vuex
72+
store.
7473

75-
Similarly, if `routes` is provided, the library will import and configure Vue
76-
Router.
74+
##### `routes` (`Array`)
75+
76+
A set of routes for [Vue Router](https://router.vuejs.org/). If `routes` is
77+
provided, the library will import and configure Vue Router.
78+
79+
##### `props` (`Object`)
80+
81+
It will be merged with `propsData`.
82+
83+
##### `container` (`HTMLElement`)
84+
85+
By default, `Vue Testing Library` will create a `div` and append it to the
86+
`baseElement`. This is where your component will be rendered. If you provide
87+
your own `HTMLElement` container via this option, it will not be appended to the
88+
`baseElement` automatically.
89+
90+
For example: If you are unit testing a `tablebody` element, it cannot be a child
91+
of a `div`. In this case, you can specify a `table` as the render `container`.
92+
93+
```js
94+
const table = document.createElement('table')
95+
96+
const { container } = render(TableBody, {
97+
props,
98+
container: document.body.appendChild(table),
99+
})
100+
```
101+
102+
##### `baseElement` (`HTMLElement`)
103+
104+
If the `container` is specified, then this defaults to that, otherwise this
105+
defaults to `document.body`. `baseElement` is used as the base element for the
106+
queries as well as what is printed when you use `debug()`.
77107

78108
#### Callback Function
79109

80110
```js
81111
function callbackFunction(vueInstance, vuexStore, router) {}
82112
```
83113

84-
A callback function that receives the Vue instance as a parameter. Its return value is merged with [options](#options), allowing
85-
3rd party plugins to be installed prior to mount. To see how this works, see the example using [`vue-i18n`](https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/vue-i18n.js).
114+
A callback function that receives the Vue instance as a parameter. Its return
115+
value is merged with [options](#options), allowing 3rd party plugins to be
116+
installed prior to mount. To see how this works, see the example using
117+
[`vue-i18n`](https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/vue-i18n.js).
86118

87119
The function will also receive the Store or the Router object if the associated
88120
option was passed in during render.
@@ -94,9 +126,9 @@ The `render` method returns an object that has a few properties:
94126
#### `...queries`
95127

96128
The most important feature of `render` is that the queries from
97-
[DOM Testing Library](queries/about.mdx) are automatically
98-
returned with their first argument bound to the [baseElement](#baseelement),
99-
which defaults to `document.body`.
129+
[DOM Testing Library](queries/about.mdx) are automatically returned with their
130+
first argument bound to the [baseElement](#baseelement), which defaults to
131+
`document.body`.
100132

101133
See [Queries](queries/about.mdx) for a complete list.
102134

@@ -106,30 +138,30 @@ const { getByLabelText, queryAllByTestId } = render(Component)
106138

107139
#### `container`
108140

109-
By default, `Vue Testing Library` will create a `div` and append it to the
110-
`baseElement`. This is where your component will be rendered. If you provide
111-
your own HTMLElement container via this option, it will not be appended to the
112-
`baseElement` automatically.
113-
114-
```js
115-
const table = document.createElement('table')
116-
117-
const { container } = render(TableBody, {
118-
container: document.body.appendChild(table),
119-
})
120-
```
141+
The containing DOM node of your rendered Vue Component. By default it's a `div`.
142+
This is a regular DOM node, so you can call `container.querySelector` etc. to
143+
inspect the children.
121144

122145
> Tip: To get the root element of your rendered element, use
123146
> `container.firstChild`.
124147
148+
> 🚨 If you find yourself using `container` to query for rendered elements then
149+
> you should reconsider! The other queries are designed to be more resilient to
150+
> changes that will be made to the component you're testing. Avoid using
151+
> `container` to query for elements!
152+
125153
#### `baseElement`
126154

127-
`baseElement` is used as the base element for the queries as well as what is
128-
printed when you use `debug()`.
155+
The containing DOM node where your Vue Component is rendered in the `container`.
156+
If you don't specify the `baseElement` in the options of `render`, it will
157+
default to `document.body`.
129158

130-
It matches `container` if no custom `baseElement` is provided. If neither
131-
`baseElement` or `container` options are provided, `baseElement` defaults to
132-
`document.body`.
159+
This is useful when the component you want to test renders something outside the
160+
container `div`, e.g. when you want to snapshot test your portal component which
161+
renders its HTML directly in the body.
162+
163+
> Note: the queries returned by the `render` looks into `baseElement`, so you
164+
> can use queries to test your portal component without the `baseElement`.
133165
134166
#### `debug(element)`
135167

0 commit comments

Comments
 (0)