Skip to content

Commit 73a869b

Browse files
authored
docs(zh): updated api docs 950763f...a0b40f0 (#1568)
* docs(zh): updated api docs 950763f...a0b40f0 * Update options.md * Update findAllComponents.md * Update config.md
1 parent 8ac6d44 commit 73a869b

30 files changed

+488
-69
lines changed

docs/zh/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Vue Test Utils 是 Vue.js 官方的单元测试实用工具库。
44

5+
<div class="vueschool"><a href="https://vueschool.io/courses/learn-how-to-test-vuejs-components?friend=vuejs" target="_blank" rel="sponsored noopener" title="Learn how to use Vue Test Utils to test Vue.js Components with Vue School">在 Vue School 学习如何测试 Vue.js 组件</a></div>
6+
57
- [教程](guides/)
68
- [起步](guides/getting-started.md)
79
- [常用技巧](guides/common-tips.md)
@@ -10,10 +12,12 @@ Vue Test Utils 是 Vue.js 官方的单元测试实用工具库。
1012
- [用 Jest 测试单文件组件](guides/testing-single-file-components-with-jest.md)
1113
- [用 Mocha 和 webpack 测试单文件组件](guides/testing-single-file-components-with-mocha-webpack.md)
1214
- [用 Karma 测试单文件组件](guides/testing-single-file-components-with-karma.md)
15+
- [无需构建在 Node.js 中测试](guides/usage-without-a-build-step-node.md)
1316
- [测试异步行为](guides/testing-async-components.md)
1417
- [配合 TypeScript 使用](guides/using-with-typescript.md)
1518
- [配合 Vue Router 使用](guides/using-with-vue-router.md)
1619
- [配合 Vuex 使用](guides/using-with-vuex.md)
20+
- [有用的测试库](guides/useful-libraries-for-testing.md)
1721
- [API](api/)
1822
- [mount](api/mount.md)
1923
- [shallowMount](api/shallowMount.md)
@@ -43,6 +47,7 @@ Vue Test Utils 是 Vue.js 官方的单元测试实用工具库。
4347
- [destroy](api/wrapper/destroy.md)
4448
- [find](api/wrapper/find.md)
4549
- [findAll](api/wrapper/findAll.md)
50+
- [get](api/wrapper/get.md)
4651
- [html](api/wrapper/html.md)
4752
- [is](api/wrapper/is.md)
4853
- [isEmpty](api/wrapper/isEmpty.md)

docs/zh/api/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
!!!include(docs/zh/api/createLocalVue.md)!!!
99
!!!include(docs/zh/api/createWrapper.md)!!!
1010
!!!include(docs/zh/api/config.md)!!!
11+
!!!include(docs/zh/api/enableAutoDestroy.md)!!!

docs/zh/api/config.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ Vue Test Utils 包含了一个定义其选项的配置对象。
44

55
### Vue Test Utils 配置选项
66

7+
### `showDeprecationWarnings`
8+
9+
- 类型:`Boolean`
10+
- 默认值:`true`
11+
12+
控制是否展示废弃警告。当设置为 `true` 时,所有的废弃警告都将会在控制台中打印出来。
13+
14+
示例:
15+
16+
```js
17+
import { config } from '@vue/test-utils'
18+
19+
config.showDeprecationWarnings = false
20+
```
21+
722
### `stubs`
823

924
- 类型:`{ [name: string]: Component | boolean | string }`
@@ -46,7 +61,7 @@ config.mocks['$store'] = {
4661
- 类型:`{ [name: string]: Function }`
4762
- 默认值:`{}`
4863

49-
你可以使用 `config` 对象配置默认的方法。它可以用于为组件注入方法的插件,例如 [VeeValidate](https://vee-validate.logaretm.com/)。你可以通过在挂载选项中传入 `methods` 来覆写 `config` 中的方法集合。
64+
你可以使用 `config` 对象配置默认的方法。它可以用于为组件注入方法的插件,例如 [VeeValidate](https://logaretm.github.io/vee-validate/)。你可以通过在挂载选项中传入 `methods` 来覆写 `config` 中的方法集合。
5065

5166
示例:
5267

docs/zh/api/createLocalVue.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
```js
1414
import { createLocalVue, shallowMount } from '@vue/test-utils'
15+
import MyPlugin from 'my-plugin'
1516
import Foo from './Foo.vue'
1617

1718
const localVue = createLocalVue()
19+
localVue.use(MyPlugin)
1820
const wrapper = shallowMount(Foo, {
1921
localVue,
2022
mocks: { foo: true }

docs/zh/api/enableAutoDestroy.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## enableAutoDestroy(hook)
2+
3+
- **参数:**
4+
5+
- `{Function} hook`
6+
7+
- **用法:**
8+
9+
`enableAutoDestroy` 将会使用被传入的该钩子函数 (例如 [`afterEach`](https://jestjs.io/docs/en/api#aftereachfn-timeout)) 销毁所有被创建的 `Wrapper` 实例。在调用这个方法之后,你可以通过调用 `resetAutoDestroyState` 方法恢复到默认行为。
10+
11+
```js
12+
import { enableAutoDestroy, mount } from '@vue/test-utils'
13+
import Foo from './Foo.vue'
14+
15+
// 将会在每个测试之后运行 `wrapper.destroy()`
16+
enableAutoDestroy(afterEach)
17+
18+
describe('Foo', () => {
19+
it('renders a div', () => {
20+
const wrapper = mount(Foo)
21+
expect(wrapper.contains('div')).toBe(true)
22+
// 不需要在此运行 `wrapper.destroy()`
23+
})
24+
})
25+
```

docs/zh/api/mount.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ import Foo from './Foo.vue'
5555

5656
describe('Foo', () => {
5757
it('renders a div', () => {
58+
const div = document.createElement('div')
59+
document.body.appendChild(div)
5860
const wrapper = mount(Foo, {
59-
attachToDocument: true
61+
attachTo: true
6062
})
6163
expect(wrapper.contains('div')).toBe(true)
64+
wrapper.destroy()
6265
})
6366
})
6467
```
@@ -116,9 +119,9 @@ describe('Foo', () => {
116119
it('renders a div', () => {
117120
const wrapper = mount(Foo, {
118121
stubs: {
119-
Bar: '<div class="stubbed" />',
120122
BarFoo: true,
121-
FooBar: Faz
123+
FooBar: Faz,
124+
Bar: { template: '<div class="stubbed" />' }
122125
}
123126
})
124127
expect(wrapper.contains('.stubbed')).toBe(true)
@@ -127,4 +130,8 @@ describe('Foo', () => {
127130
})
128131
```
129132

133+
**废弃通知:**
134+
135+
当对组件存根时,提供一个字符串的方式 (`ComponentToStub: '<div class="stubbed" />`) 已经不再被支持。
136+
130137
- **延伸阅读:**[`Wrapper`](wrapper/)

docs/zh/api/options.md

+151-17
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
:::
1111

1212
- [`context`](#context)
13+
- [`data`](#data)
1314
- [`slots`](#slots)
1415
- [`scopedSlots`](#scopedslots)
1516
- [`stubs`](#stubs)
1617
- [`mocks`](#mocks)
1718
- [`localVue`](#localvue)
19+
- [`attachTo`](#attachto)
1820
- [`attachToDocument`](#attachtodocument)
1921
- [`propsData`](#propsdata)
2022
- [`attrs`](#attrs)
@@ -44,6 +46,43 @@ const wrapper = mount(Component, {
4446
expect(wrapper.is(Component)).toBe(true)
4547
```
4648

49+
## data
50+
51+
- 类型:`Function`
52+
53+
向一个组件传入数据。这将会合并到现有的 `data` 函数中。
54+
55+
示例:
56+
57+
```js
58+
const Component = {
59+
template: `
60+
<div>
61+
<span id="foo">{{ foo }}</span>
62+
<span id="bar">{{ bar }}</span>
63+
</div>
64+
`,
65+
66+
data() {
67+
return {
68+
foo: 'foo',
69+
bar: 'bar'
70+
}
71+
}
72+
}
73+
74+
const wrapper = mount(Component, {
75+
data() {
76+
return {
77+
bar: 'my-override'
78+
}
79+
}
80+
})
81+
82+
wrapper.find('#foo').text() // 'foo'
83+
wrapper.find('#bar').text() // 'my-override'
84+
```
85+
4786
## slots
4887

4988
- 类型:`{ [name: string]: Array<Component>|Component|string }`
@@ -54,20 +93,43 @@ expect(wrapper.is(Component)).toBe(true)
5493

5594
```js
5695
import Foo from './Foo.vue'
96+
import MyComponent from './MyComponent.vue'
5797

5898
const bazComponent = {
5999
name: 'baz-component',
60100
template: '<p>baz</p>'
61101
}
62102

103+
const yourComponent = {
104+
props: {
105+
foo: {
106+
type: String,
107+
required: true
108+
}
109+
},
110+
render(h) {
111+
return h('p', this.foo)
112+
}
113+
}
114+
63115
const wrapper = shallowMount(Component, {
64116
slots: {
65117
default: [Foo, '<my-component />', 'text'],
66118
fooBar: Foo, // 将会匹配 `<slot name="FooBar" />`.
67119
foo: '<div />',
68120
bar: 'bar',
69121
baz: bazComponent,
70-
qux: '<my-component />'
122+
qux: '<my-component />',
123+
quux: '<your-component foo="lorem"/><your-component :foo="yourProperty"/>'
124+
},
125+
stubs: {
126+
// 用来注册自定义组件
127+
'my-component': MyComponent,
128+
'your-component': yourComponent
129+
},
130+
mocks: {
131+
// 用来向渲染上下文添加 property
132+
yourProperty: 'ipsum'
71133
}
72134
})
73135

@@ -124,12 +186,37 @@ shallowMount(Component, {
124186
})
125187
```
126188

189+
::: warning 必备根元素
190+
由于该特性内部实现的原因,这里的插槽内容必须返回一个根元素,即使一个作用域插槽是允许返回一个元素数组的。
191+
192+
如果你在测试中有这方面的需要,推荐的变通方式是把被测试的组件包裹在另一个组件里,然后挂载那个组件:
193+
:::
194+
195+
```javascript
196+
const WrapperComp = {
197+
template: `
198+
<ComponentUnderTest v-slot="props">
199+
<p>Using the {{props.a}}</p>
200+
<p>Using the {{props.a}}</p>
201+
</ComponentUnderTest>
202+
`,
203+
components: {
204+
ComponentUnderTest
205+
}
206+
}
207+
const wrapper = mount(WrapperComp).find(ComponentUnderTest)
208+
```
209+
127210
## stubs
128211

129-
- 类型:`{ [name: string]: Component | boolean } | Array<string>`
212+
- 类型:`{ [name: string]: Component | string | boolean } | Array<string>`
130213

131214
将子组件存根。可以是一个要存根的组件名的数组或对象。如果 `stubs` 是一个数组,则每个存根都是一个 `<${component name}-stub>`
132215

216+
**废弃通知:**
217+
218+
当对组件存根时,提供一个字符串的方式 (`ComponentToStub: '<div class="stubbed" />`) 已经不再被支持。
219+
133220
示例:
134221

135222
```js
@@ -169,6 +256,10 @@ const wrapper = shallowMount(Component, {
169256
expect(wrapper.vm.$route.path).toBe($route.path)
170257
```
171258

259+
::: tip
260+
如果想要伪造 `$root` 请换用[这里](https://github.com/vuejs/vue-test-utils/issues/481#issuecomment-423716430)描述的 `parentComponent` 选项。
261+
:::
262+
172263
## localVue
173264

174265
- 类型:`Vue`
@@ -198,12 +289,42 @@ const wrapper = mount(Component, {
198289
expect(wrapper.vm.$route).toBeInstanceOf(Object)
199290
```
200291

292+
## attachTo
293+
294+
- 类型:`HTMLElement | string`
295+
- 默认值:`null`
296+
297+
指定一个 `HTMLElement` 或定位到一个 HTML 元素的 CSS 选择器字符串,组件将会被完全挂载到文档中的这个元素。
298+
299+
当要挂载到 DOM 时,你需要在测试的结尾调用 `wrapper.destroy()` 以将该元素从文档中移除,并销毁该组件实例。
300+
301+
```js
302+
const Component = {
303+
template: '<div>ABC</div>'
304+
}
305+
let wrapper = mount(Component, {
306+
attachTo: '#root'
307+
})
308+
expect(wrapper.vm.$el.parentNode).to.not.be.null
309+
wrapper.destroy()
310+
311+
wrapper = mount(Component, {
312+
attachTo: document.getElementById('root')
313+
})
314+
expect(wrapper.vm.$el.parentNode).to.not.be.null
315+
wrapper.destroy()
316+
```
317+
201318
## attachToDocument
202319

203320
- 类型:`boolean`
204321
- 默认值:`false`
205322

206-
当设为 `true` 时,组件在渲染时将会挂载到 DOM 上。
323+
::: warning 废弃警告
324+
`attachToDocument` 已经被废弃并且将会在未来的发布中被移除。请换用 [`attachTo`](#attachto)
325+
:::
326+
327+
[`attachTo`](#attachto) 一样,不过会自动创建一个新的 `div` 元素并将其插入到 body 里。
207328

208329
如果添加到了 DOM 上,你应该在测试的最后调用 `wrapper.destroy()` 将元素从文档中移除并销毁组件实例。
209330

@@ -244,6 +365,23 @@ expect(wrapper.text()).toBe('aBC')
244365

245366
设置组件实例的 `$listeners` 对象。
246367

368+
示例:
369+
370+
```js
371+
const Component = {
372+
template: '<button v-on:click="$emit(\'click\')"></button>'
373+
}
374+
const onClick = jest.fn()
375+
const wrapper = mount(Component, {
376+
listeners: {
377+
click: onClick
378+
}
379+
})
380+
381+
wrapper.trigger('click')
382+
expect(onClick).toHaveBeenCalled()
383+
```
384+
247385
## parentComponent
248386

249387
- 类型:`Object`
@@ -292,26 +430,22 @@ expect(wrapper.text()).toBe('fooValue')
292430

293431
```js
294432
const Component = {
295-
template: '<div>{{ foo() }}{{ bar() }}{{ baz() }}</div>',
296-
methods: {
297-
foo() {
298-
return 'a'
299-
},
300-
bar() {
301-
return 'b'
433+
template: '<div>{{ foo }}</div>',
434+
data() {
435+
return {
436+
foo: 'fromComponent'
302437
}
303438
}
304439
}
305440
const options = {
306-
methods: {
307-
bar() {
308-
return 'B'
309-
},
310-
baz() {
311-
return 'C'
441+
data() {
442+
return {
443+
foo: 'fromOptions'
312444
}
313445
}
314446
}
447+
315448
const wrapper = mount(Component, options)
316-
expect(wrapper.text()).toBe('aBC')
449+
450+
expect(wrapper.text()).toBe('fromOptions')
317451
```

0 commit comments

Comments
 (0)