Skip to content

Commit d583bb1

Browse files
authored
Merge pull request #63 from vuejs/add-missing-api-docs
Add missing docs from latest releases
2 parents 0b143e8 + c4d4032 commit d583bb1

File tree

2 files changed

+155
-2
lines changed

2 files changed

+155
-2
lines changed

src/.vuepress/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports = {
6767
locales: {
6868
'/': {
6969
lang: 'en-US',
70-
title: 'Vue Test Utils (2.0.0-beta.7)'
70+
title: 'Vue Test Utils (2.0.0-beta.10)'
7171
}
7272
},
7373
themeConfig: {

src/api/README.md

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,45 @@ test('mounts on a specific element', () => {
6666
})
6767
```
6868

69+
### `attrs`
70+
71+
Assigns attributes to component.
72+
73+
```js
74+
test('assigns extra attributes on components', () => {
75+
const wrapper = mount(Component, {
76+
attrs: {
77+
id: 'hello',
78+
disabled: true
79+
}
80+
})
81+
82+
expect(wrapper.attributes()).toEqual({
83+
disabled: 'true',
84+
id: 'hello'
85+
})
86+
})
87+
```
88+
89+
Notice that setting a prop will always trump an attribute:
90+
91+
```js
92+
test('attribute is overridden by a prop with the same name', () => {
93+
const wrapper = mount(Component, {
94+
props: {
95+
message: 'Hello World'
96+
},
97+
attrs: {
98+
message: 'this will get overridden'
99+
}
100+
})
101+
102+
expect(wrapper.props()).toEqual({ message: 'Hello World' })
103+
104+
expect(wrapper.attributes()).toEqual({})
105+
})
106+
```
107+
69108
### `data`
70109

71110
Overrides a component's default `data`. Must be a function.
@@ -463,6 +502,74 @@ test('stubs a component using a custom component', () => {
463502
})
464503
```
465504

505+
### `global.config`
506+
507+
Configures [Vue's application global configuration](https://v3.vuejs.org/api/application-config.html#application-config).
508+
509+
### `global.renderStubDefaultSlot`
510+
511+
Renders the `default` slot content, even when using `shallow` or `shallowMount`.
512+
513+
Due to technical limitations, this behavior cannot be extended to slots other than the default one.
514+
515+
```js
516+
import { config, mount } from '@vue/test-utils'
517+
518+
beforeAll(() => {
519+
config.renderStubDefaultSlot = true
520+
})
521+
522+
afterAll(() => {
523+
config.renderStubDefaultSlot = false
524+
})
525+
526+
test('shallow with stubs', () => {
527+
const Component = {
528+
template: `<div><slot /></div>`
529+
}
530+
531+
const wrapper = mount(Component, {
532+
shallow: true
533+
})
534+
535+
expect(wrapper.html()).toContain('Content from the default slot')
536+
})
537+
```
538+
539+
::: tip
540+
This behavior is global, not on a mount by mount basis. Remember to enable/disable it before and after each test.
541+
:::
542+
543+
### `shallow`
544+
545+
Stubs out out all child components from the components under testing.
546+
547+
```js
548+
test('stubs all components automatically using { shallow: true }', () => {
549+
const Component = {
550+
template: `
551+
<custom-component />
552+
<another-component />
553+
`,
554+
components: {
555+
CustomComponent,
556+
AnotherComponent
557+
}
558+
}
559+
560+
const wrapper = mount(Component, { shallow: true })
561+
562+
expect(wrapper.html()).toEqual(
563+
`<custom-component-stub></custom-component-stub><another-component></another-component>`
564+
)
565+
}
566+
```
567+
568+
::: tip
569+
`shallowMount` is an alias to mounting a component with `shallow: true`.
570+
:::
571+
572+
466573
## Wrapper methods
467574
468575
When you use `mount`, a `VueWrapper` is returned with a number of useful methods for testing. A `VueWrapper` is a thin wrapper around your component instance. Methods like `find` return a `DOMWrapper`, which is a thin wrapper around the DOM nodes in your component and it's children. Both implement a similar same API.
@@ -859,6 +966,21 @@ test('html', () => {
859966
})
860967
```
861968
969+
### `isVisible`
970+
971+
Verify whether or not a found element is visible or not.
972+
973+
```js
974+
test('isVisible', () => {
975+
const Comp = {
976+
template: `<div v-show="false"><span /></div>`
977+
}
978+
const wrapper = mount(Comp)
979+
980+
expect(wrapper.find('span').isVisible()).toBe(false)
981+
})
982+
```
983+
862984
### `props`
863985
864986
Returns props applied on a Vue Component. This should be used mostly to assert props applied to a stubbed component.
@@ -912,6 +1034,37 @@ test('props', () => {
9121034
})
9131035
```
9141036
1037+
### `setData`
1038+
1039+
Updates component data.
1040+
1041+
::: tip
1042+
You should use `await` when you call `setData` to ensure that Vue updates the DOM before you make an assertion.
1043+
:::
1044+
1045+
`Component.vue`:
1046+
1047+
```js
1048+
test('updates component data', async () => {
1049+
const Component = {
1050+
template: '<div>Count: {{ count }}</div>',
1051+
data: () => ({ count: 0 })
1052+
}
1053+
1054+
const wrapper = mount(Component)
1055+
expect(wrapper.html()).toContain('Count: 0')
1056+
1057+
await wrapper.setData({ count: 1 })
1058+
1059+
expect(wrapper.html()).toContain('Count: 1')
1060+
})
1061+
```
1062+
1063+
Notice that `setData` does not allow setting new properties that are not
1064+
defined in the component.
1065+
1066+
Also, notice that `setData` does not modify composition API `setup()` data.
1067+
9151068
### `setProps`
9161069
9171070
Updates component props.
@@ -1093,4 +1246,4 @@ test('unmount', () => {
10931246
10941247
### `vm`
10951248
1096-
This is the ```Vue``` instance. You can access all of the [instance methods and properties of a vm](https://v3.vuejs.org/api/instance-properties.html) with ```wrapper.vm```. This only exists on ```VueWrapper```.
1249+
This is the ```Vue``` instance. You can access all of the [instance methods and properties of a vm](https://v3.vuejs.org/api/instance-properties.html) with ```wrapper.vm```. This only exists on ```VueWrapper```.

0 commit comments

Comments
 (0)