Skip to content

docs(zh): keep update #972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/zh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Vue Test Utils 是 Vue.js 官方的单元测试实用工具库。
* [用 Mocha 和 webpack 测试单文件组件](guides/testing-single-file-components-with-mocha-webpack.md)
* [用 Karma 测试单文件组件](guides/testing-single-file-components-with-karma.md)
* [测试异步行为](guides/testing-async-components.md)
* [配合 TypeScript 使用](guides/using-with-typescript.md)
* [配合 Vue Router 使用](guides/using-with-vue-router.md)
* [配合 Vuex 实用](guides/using-with-vuex.md)
* [API](api/)
Expand All @@ -27,7 +28,9 @@ Vue Test Utils 是 Vue.js 官方的单元测试实用工具库。
- [localVue](api/options.md#localvue)
- [attachToDocument](api/options.md#attachtodocument)
- [attrs](api/options.md#attrs)
- [propsData](api/options.md#propsdata)
- [listeners](api/options.md#listeners)
- [parentComponent](api/options.md#parentComponent)
- [provide](api/options.md#provide)
- [sync](api/options.md#sync)
- [其它选项](api/options.md#other-options)
Expand Down Expand Up @@ -77,5 +80,6 @@ Vue Test Utils 是 Vue.js 官方的单元测试实用工具库。
* [TransitionGroupStub](api/components/TransitionGroupStub.md)
* [RouterLinkStub](api/components/RouterLinkStub.md)
* [选择器](api/selectors.md)
* [createWrapper](api/createWrapper.md)
* [createLocalVue](api/createLocalVue.md)
* [配置](api/config.md)
1 change: 1 addition & 0 deletions docs/zh/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
!!!include(docs/zh/api/renderToString.md)!!!
!!!include(docs/zh/api/selectors.md)!!!
!!!include(docs/zh/api/createLocalVue.md)!!!
!!!include(docs/zh/api/createWrapper.md)!!!
!!!include(docs/zh/api/config.md)!!!
25 changes: 25 additions & 0 deletions docs/zh/api/createWrapper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## createWrapper(node [, options])

- **参数:**

- `{vm|HTMLElement} node`
- `{Object} options`
- `{Boolean} sync`
- `{Boolean} attachedToDocument`

- **返回值:**
- `{Wrapper}`

- **用法:**

`createWrapper` 为一个被挂载的 Vue 实例或一个 HTML 元素创建一个 `Wrapper`。

```js
import { createWrapper } from '@vue/test-utils'
import Foo from './Foo.vue'

const Constructor = Vue.extend(Foo)
const vm = new Constructor().$mount()
const wrapper = createWrapper(vm)
expect(wrapper.vm.foo).toBe(true)
```
112 changes: 93 additions & 19 deletions docs/zh/api/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
- [`mocks`](#mocks)
- [`localVue`](#localvue)
- [`attachToDocument`](#attachtodocument)
- [`propsData`](#propsdata)
- [`attrs`](#attrs)
- [`listeners`](#listeners)
- [`parentComponent`](#parentComponent)
- [`parentComponent`](#parentcomponent)
- [`provide`](#provide)
- [`sync`](#sync)

## context

- 类型:`Object`

将上下文传递给函数式组件。该选项只能用于函数式组件
将上下文传递给函数式组件。该选项只能用于[函数式组件](https://cn.vuejs.org/v2/guide/render-function.html#函数式组件)

示例:

Expand Down Expand Up @@ -47,45 +48,74 @@ expect(wrapper.is(Component)).toBe(true)

```js
import Foo from './Foo.vue'
import Bar from './Bar.vue'

const bazComponent = {
name: 'baz-component',
template: '<p>baz</p>'
}

const wrapper = shallowMount(Component, {
slots: {
default: [Foo, Bar],
fooBar: Foo, // 将会匹配 `<slot name="FooBar" />`
default: [Foo, '<my-component />', 'text'],
fooBar: Foo, // 将会匹配 `<slot name="FooBar" />`.
foo: '<div />',
bar: 'bar'
bar: 'bar',
baz: bazComponent,
qux: '<my-component />'
}
})

expect(wrapper.find('div')).toBe(true)
```

## scopedSlots

- 类型:`{ [name: string]: string }`
- 类型:`{ [name: string]: string|Function }`

提供一个该组件所有作用域插槽内容的对象。每个键对应到插槽的名字,每个值可以是一个模板字符串
提供一个该组件所有作用域插槽的对象。每个键对应到插槽的名字。

这里有三处限制。
你可以使用 slot-scope 特性设置 prop 的名称:

* 该选项只支持 [email protected]+。
```js
shallowMount(Component, {
scopedSlots: {
foo: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>'
}
})
```

否则插槽被计算的时候可以通过 `props` 对象使用 prop:

```js
shallowMount(Component, {
scopedSlots: {
default: '<p>{{props.index}},{{props.text}}</p>'
}
})
```

* 你不能在 `scopedSlots` 选项中将 `<template>` 标签用作其根元素。
你也可以传递一个函数将 prop 作为参数带入:

* 我们不支持 PhantomJS。
你可以使用 [Puppeteer](https://github.com/karma-runner/karma-chrome-launcher#headless-chromium-with-puppeteer) 作为替代品。
```js
shallowMount(Component, {
scopedSlots: {
foo: function (props) {
return this.$createElement('div', props.index)
}
}
})
```

示例
或者你可以使用 JSX。如果你在一个方法里撰写 JSX,babel-plugin-transform-vue-jsx 会自动注入 `this.$createElement`

```js
const wrapper = shallowMount(Component, {
shallowMount(Component, {
scopedSlots: {
foo: '<p slot-scope="props">{{props.index}},{{props.text}}</p>'
foo (props) {
return <div>{ props.text }</div>
}
}
})
expect(wrapper.find('#fooWrapper').html()).toBe(
`<div id="fooWrapper"><p>0,text1</p><p>1,text2</p><p>2,text3</p></div>`
)
```

## stubs
Expand Down Expand Up @@ -177,6 +207,31 @@ expect(wrapper.vm.$route).toBeInstanceOf(Object)

设置组件实例的 `$attrs` 对象。

## propsData

- 类型:`Object`

在组件被挂载时设置组件实例的 prop。

示例:

```js
const Component = {
template: '<div>{{ msg }}</div>',
props: ['msg']
}
const wrapper = mount(Component, {
propsData: {
msg: 'aBC'
}
})
expect(wrapper.text()).toBe('aBC')
```

::: tip 提示
值得注意的是 `propsData` 实际上是一个 [Vue API](https://cn.vuejs.org/v2/api/#propsData),不是 Vue Test Utils 的挂载选项。它会被 [`extends`](https://cn.vuejs.org/v2/api/#extends) 处理。请查阅[其它选项](#其它选项)。
:::

## listeners

- 类型:`Object`
Expand Down Expand Up @@ -206,6 +261,25 @@ expect(wrapper.vm.$parent.$options.name).toBe('foo')

为组件传递用于注入的属性。可查阅 [provie/inject](https://cn.vuejs.org/v2/api/#provide-inject) 了解更多。

示例:

```js
const Component = {
inject: ['foo'],
template: '<div>{{this.foo()}}</div>'
}

const wrapper = shallowMount(Component, {
provide: {
foo () {
return 'fooValue'
}
}
})

expect(wrapper.text()).toBe('fooValue')
```

## sync

- 类型:`boolean`
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/api/shallowMount.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Foo', () => {
foo: '<div />'
}
})
expect(wrapper.find('div')).toBe(true)
expect(wrapper.contains('div')).toBe(true)
})
})
```
Expand Down
10 changes: 7 additions & 3 deletions docs/zh/api/wrapper/attributes.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## attributes()
## attributes([key])

返回 `Wrapper` DOM 节点的特性对象。
返回 `Wrapper` DOM 节点的特性对象。如果提供了 `key`,则返回这个 `key` 对应的值。

- **返回值:**`{[attribute: string]: any}`
- **参数:**
- `{string} key` **可选的**

- **返回值:**`{[attribute: string]: any} | string`

- **示例:**

Expand All @@ -12,4 +15,5 @@ import Foo from './Foo.vue'

const wrapper = mount(Foo)
expect(wrapper.attributes().id).toBe('foo')
expect(wrapper.attributes('id')).toBe('foo')
```
10 changes: 7 additions & 3 deletions docs/zh/api/wrapper/classes.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
## classes()
## classes([className])

返回 `Wrapper` DOM 节点的 class。

返回 class 名称的数组。
返回 class 名称的数组。或在提供 class 名的时候返回一个布尔值。

- **返回值:**`Array<{string}>`
- **参数:**
- `{string} className` **可选的**

- **返回值:**`Array<{string}> | boolean`

- **示例:**

Expand All @@ -14,4 +17,5 @@ import Foo from './Foo.vue'

const wrapper = mount(Foo)
expect(wrapper.classes()).toContain('bar')
expect(wrapper.classes('bar')).toBe(true)
```
10 changes: 7 additions & 3 deletions docs/zh/api/wrapper/props.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
## props()
## props([key])

返回 `Wrapper` `vm` 的 props 对象。
返回 `Wrapper` `vm` 的 props 对象。如果提供了 `key`,则返回这个 `key` 对应的值。

**注意:该包裹器必须包含一个 Vue 示例。**

- **返回值:**`{[prop: string]: any}`
- **参数:**
- `{string} key` **可选的**

- **返回值:**`{[prop: string]: any} | any`

- **示例:**

Expand All @@ -18,4 +21,5 @@ const wrapper = mount(Foo, {
}
})
expect(wrapper.props().bar).toBe('baz')
expect(wrapper.props('bar')).toBe('baz')
```
4 changes: 2 additions & 2 deletions docs/zh/api/wrapper/setChecked.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { mount } from '@vue/test-utils'
import Foo from './Foo.vue'

const wrapper = mount(Foo)
const option = wrapper.find('input[type="radio"]')
option.setChecked()
const radioInput = wrapper.find('input[type="radio"]')
radioInput.setChecked()
```

- **注意:**
Expand Down
4 changes: 2 additions & 2 deletions docs/zh/api/wrapper/setValue.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import Foo from './Foo.vue'

const wrapper = mount(Foo)

const input = wrapper.find('input[type="text"]')
input.setValue('some value')
const textInput = wrapper.find('input[type="text"]')
textInput.setValue('some value')

const select = wrapper.find('select')
select.setValue('option value')
Expand Down
3 changes: 2 additions & 1 deletion docs/zh/guides/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
!!!include(docs/zh/guides/testing-single-file-components-with-mocha-webpack.md)!!!
!!!include(docs/zh/guides/testing-single-file-components-with-karma.md)!!!
!!!include(docs/zh/guides/testing-async-components.md)!!!
!!!include(docs/zh/guides/using-with-typescript.md)!!!
!!!include(docs/zh/guides/using-with-vue-router.md)!!!
!!!include(docs/zh/guides/using-with-vuex.md)!!!
!!!include(docs/zh/guides/using-with-vuex.md)!!!
14 changes: 14 additions & 0 deletions docs/zh/guides/common-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ mount(Component, {
})
```

### 存根组件

你可以使用 `stubs` 选项覆写全局或局部注册的组件:

```js
import { mount } from '@vue/test-utils'

mount(Component, {
// 将会把 globally-registered-component 解析为
// 空的存根
stubs: ['globally-registered-component']
})
```

### 处理路由

因为路由需要在应用的全局结构中进行定义,且引入了很多组件,所以最好集成到 end-to-end 测试。对于依赖 `vue-router` 功能的独立的组件来说,你可以使用上面提到的技术仿造它们。
Expand Down
Loading