Skip to content

Commit e81095c

Browse files
Jinjiangeddyerburgh
authored andcommitted
docs: [zh-cn] synced from #de89293 to #a5ad66a (#168)
* [zh-cn] synced from #de89293 to #a5ad66a * [zh-cn] translated guides/dom-events
1 parent d27becb commit e81095c

File tree

10 files changed

+250
-8
lines changed

10 files changed

+250
-8
lines changed

docs/zh-cn/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [教程](guides/README.md)
66
* [起步](guides/getting-started.md)
77
* [常用技巧](guides/common-tips.md)
8+
* [鼠标、键盘以及其它 DOM 事件](guides/dom-events.md)
89
* [选择一个测试运行器](guides/choosing-a-test-runner.md)
910
* [用 Jest 测试单文件组件](guides/testing-SFCs-with-jest.md)
1011
* [用 Mocha 和 webpack 测试单文件组件](guides/testing-SFCs-with-mocha-webpack.md)
@@ -44,6 +45,7 @@
4445
* [text](api/wrapper/text.md)
4546
* [trigger](api/wrapper/trigger.md)
4647
* [update](api/wrapper/update.md)
48+
* [destroy](api/wrapper/destroy.md)
4749
* [WrapperArray](api/wrapper-array/README.md)
4850
* [at](api/wrapper-array/at.md)
4951
* [contains](api/wrapper-array/contains.md)
@@ -59,5 +61,6 @@
5961
* [setProps](api/wrapper-array/setProps.md)
6062
* [trigger](api/wrapper-array/trigger.md)
6163
* [update](api/wrapper-array/update.md)
64+
* [destroy](api/wrapper-array/destroy.md)
6265
* [选择器](api/selectors.md)
6366
* [createLocalVue](api/createLocalVue.md)

docs/zh-cn/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [教程](guides/README.md)
44
* [起步](guides/getting-started.md)
55
* [常用技巧](guides/common-tips.md)
6+
* [鼠标、键盘以及其它 DOM 事件](guides/dom-events.md)
67
* [选择一个测试运行器](guides/choosing-a-test-runner.md)
78
* [用 Jest 测试单文件组件](guides/testing-SFCs-with-jest.md)
89
* [用 Mocha 和 webpack 测试单文件组件](guides/testing-SFCs-with-mocha-webpack.md)
@@ -42,6 +43,7 @@
4243
* [text](api/wrapper/text.md)
4344
* [trigger](api/wrapper/trigger.md)
4445
* [update](api/wrapper/update.md)
46+
* [destroy](api/wrapper/destroy.md)
4547
* [WrapperArray](api/wrapper-array/README.md)
4648
* [at](api/wrapper-array/at.md)
4749
* [contains](api/wrapper-array/contains.md)
@@ -57,5 +59,6 @@
5759
* [setProps](api/wrapper-array/setProps.md)
5860
* [trigger](api/wrapper-array/trigger.md)
5961
* [update](api/wrapper-array/update.md)
62+
* [destroy](api/wrapper-array/destroy.md)
6063
* [选择器](api/selectors.md)
6164
* [createLocalVue](api/createLocalVue.md)

docs/zh-cn/api/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* [text](./wrapper/text.md)
3434
* [trigger](./wrapper/trigger.md)
3535
* [update](./wrapper/update.md)
36+
* [destroy](./wrapper/destroy.md)
3637
* [WrapperArray](./wrapper-array/README.md)
3738
* [at](./wrapper-array/at.md)
3839
* [contains](./wrapper-array/contains.md)
@@ -48,5 +49,6 @@
4849
* [setProps](./wrapper-array/setProps.md)
4950
* [trigger](./wrapper-array/trigger.md)
5051
* [update](./wrapper-array/update.md)
52+
* [destroy](./wrapper-array/destroy.md)
5153
* [选择器](./selectors.md)
5254
* [createLocalVue](./createLocalVue.md)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# `destroy()`
2+
3+
销毁 `WrapperArray` 中的每个 Vue `Wrapper`
4+
5+
- **示例:**
6+
7+
```js
8+
import { mount } from 'vue-test-utils'
9+
import { expect } from 'chai'
10+
import Foo from './Foo.vue'
11+
12+
const wrapper = mount(Foo)
13+
const divArray = wrapper.findAll('div')
14+
expect(divArray.contains('p')).toBe(true)
15+
divArray.destroy()
16+
expect(divArray.contains('p')).toBe(false)
17+
```

docs/zh-cn/api/wrapper-array/hasStyle.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
断言 `WrapperArray` 中的每一个 `Wrapper` 的 DOM 节点都有样式的匹配值。
44

5-
如果 `Wrapper` 的 DOM 节点有 `style` 样式值匹配 `string` 则返回 `true`
5+
如果 `Wrapper` 的 DOM 节点有 `style` 样式值匹配 `value` 则返回 `true`
66

77
**注意:当运行在 `jsdom` 中的时候只会匹配内联样式。**
88

docs/zh-cn/api/wrapper/destroy.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# `destroy()`
2+
3+
销毁一个 Vue 组件实例。
4+
5+
- **示例:**
6+
7+
```js
8+
import { mount } from 'vue-test-utils'
9+
import { expect } from 'chai'
10+
import sinon from 'sinon'
11+
12+
const spy = sinon.stub()
13+
mount({
14+
render: null,
15+
destroyed () {
16+
spy()
17+
}
18+
}).destroy()
19+
expect(spy.calledOnce).to.equal(true)
20+
```

docs/zh-cn/guides/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [起步](./getting-started.md)
44
* [常用技巧](./common-tips.md)
5+
* [鼠标、键盘以及其它 DOM 事件](./dom-events.md)
56
* [选择一个测试运行器](./choosing-a-test-runner.md)
67
* [用 Jest 测试单文件组件](./testing-SFCs-with-jest.md)
78
* [用 Mocha 和 webpack 测试单文件组件](./testing-SFCs-with-mocha-webpack.md)

docs/zh-cn/guides/choosing-a-test-runner.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
当然在我们选用测试运行器的时候也需要考虑一些事项:功能集合、性能和对单文件组件预编译的支持等。在仔细比对现有的库之后,我们推荐其中的两个测试运行器:
88

9-
- [Jest](https://facebook.github.io/jest/docs/en/getting-started.html#content) 是功能最全的测试运行器。它所需的配置是最少的,默认安装了 JSDOM,内置断言且命令行的用户体验非常好。不过你需要一个能够将单文件组件导入到测试中的预处理器。我们已经创建了 `jest-vue` 预处理器来处理最常见的单文件组件特性,但仍不是 `vue-loader` 100% 的功能。
9+
- [Jest](https://facebook.github.io/jest/docs/en/getting-started.html#content) 是功能最全的测试运行器。它所需的配置是最少的,默认安装了 JSDOM,内置断言且命令行的用户体验非常好。不过你需要一个能够将单文件组件导入到测试中的预处理器。我们已经创建了 `vue-jest` 预处理器来处理最常见的单文件组件特性,但仍不是 `vue-loader` 100% 的功能。
1010

1111
- [mocha-webpack](https://github.com/zinserjan/mocha-webpack) 是一个 webpack + Mocha 的包裹器,同时包含了更顺畅的接口和侦听模式。这些设置的好处在于我们能够通过 webpack + `vue-loader` 得到完整的单文件组件支持,但这本身是需要很多配置的。
1212

@@ -29,7 +29,7 @@ require('jsdom-global')()
2929

3030
Vue 的单文件组件在它们运行于 Node 或浏览器之前是需要预编译的。我们推荐两种方式完成编译:通过一个 Jest 预编译器,或直接使用 webpack。
3131

32-
`jest-vue` 预处理器支持基本的单文件组件功能,但是目前还不能处理样式块和自定义块,这些都只在 `vue-loader` 中支持。如果你依赖这些功能或其它 webpack 特有的配置项,那么你需要基于 webpack + `vue-loader` 进行设置。
32+
`vue-jest` 预处理器支持基本的单文件组件功能,但是目前还不能处理样式块和自定义块,这些都只在 `vue-loader` 中支持。如果你依赖这些功能或其它 webpack 特有的配置项,那么你需要基于 webpack + `vue-loader` 进行设置。
3333

3434
对于不同的设置方式请移步下面的教程:
3535

docs/zh-cn/guides/dom-events.md

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# 测试键盘、鼠标等其它 DOM 事件
2+
3+
## 触发事件
4+
5+
`Wrapper` 暴露了一个 `trigger` 方法。它可以用来触发 DOM 事件。
6+
7+
```js
8+
const wrapper = mount(MyButton)
9+
10+
wrapper.trigger('click')
11+
```
12+
13+
你应该注意到了,`find` 也会返回一个包裹器。假设 `MyComponent` 包含一个按钮,下面的代码会点击这个按钮。
14+
15+
```js
16+
const wrapper = mount(MyComponent)
17+
18+
wrapper.find('button').trigger('click')
19+
```
20+
21+
## 选项
22+
23+
该触发方法接受一个可选的 `options` 对象。这个 `options` 对象里的属性会被添加到事件中。
24+
25+
你可以通过在 `options` 里传入 `preventDefault: true` 来运行事件上的 `preventDefault`
26+
27+
```js
28+
const wrapper = mount(MyButton)
29+
30+
wrapper.trigger('click', { preventDefault: true })
31+
```
32+
33+
34+
## 鼠标点击示例
35+
36+
**待测试的组件**
37+
38+
```html
39+
<template>
40+
<div>
41+
<button class="yes" @click="callYes">Yes</button>
42+
<button class="no" @click="callNo">No</button>
43+
</div>
44+
</template>
45+
<script>
46+
export default {
47+
name: 'YesNoComponent',
48+
props: {
49+
callMe: {
50+
type: Function
51+
}
52+
},
53+
methods: {
54+
callYes() {
55+
this.callMe('yes')
56+
},
57+
callNo() {
58+
this.callMe('no')
59+
}
60+
}
61+
}
62+
</script>
63+
64+
```
65+
66+
**测试**
67+
68+
```js
69+
import YesNoComponent from '@/components/YesNoComponent'
70+
import { mount } from 'vue-test-utils'
71+
import sinon from 'sinon'
72+
73+
describe('点击事件', () => {
74+
it('在 yes 按钮上点击会调用我们的方法并附带参数 "yes"', () => {
75+
const spy = sinon.spy()
76+
const wrapper = mount(YesNoComponent, {
77+
propsData: {
78+
callMe: spy
79+
}
80+
})
81+
wrapper.find('button.yes').trigger('click')
82+
83+
spy.should.have.been.calledWith('yes')
84+
})
85+
})
86+
```
87+
88+
## 键盘示例
89+
90+
**待测试的组件**
91+
92+
这个组件允许使用不同的按键将数量递增/递减。
93+
94+
```html
95+
<template>
96+
<input type="text" @keydown.prevent="onKeydown" v-model="quantity" />
97+
</template>
98+
<script>
99+
const KEY_DOWN = 40
100+
const KEY_UP = 38
101+
const ESCAPE = 27
102+
const CHAR_A = 65
103+
104+
export default {
105+
data() {
106+
return {
107+
quantity: 0
108+
}
109+
},
110+
methods: {
111+
increment() {
112+
this.quantity += 1
113+
},
114+
decrement() {
115+
this.quantity -= 1
116+
},
117+
clear() {
118+
this.quantity = 0
119+
},
120+
onKeydown(e) {
121+
if (e.keyCode === ESCAPE) {
122+
this.clear()
123+
}
124+
if (e.keyCode === KEY_DOWN) {
125+
this.decrement()
126+
}
127+
if (e.keyCode === KEY_UP) {
128+
this.increment()
129+
}
130+
if (e.which === CHAR_A) {
131+
this.quantity = 13
132+
}
133+
}
134+
},
135+
watch: {
136+
quantity: function (newValue) {
137+
this.$emit('input', newValue)
138+
}
139+
}
140+
}
141+
</script>
142+
143+
```
144+
145+
**Test**
146+
147+
```js
148+
import QuantityComponent from '@/components/QuantityComponent'
149+
import { mount } from 'vue-test-utils'
150+
151+
describe('键盘事件测试', () => {
152+
it('默认的数量是零', () => {
153+
const wrapper = mount(QuantityComponent)
154+
expect(wrapper.vm.quantity).to.equal(0)
155+
})
156+
157+
it('上按键将数量设置为 1', () => {
158+
const wrapper = mount(QuantityComponent)
159+
wrapper.trigger('keydown.up')
160+
expect(wrapper.vm.quantity).to.equal(1)
161+
})
162+
163+
it('下按键将数量减 1', () => {
164+
const wrapper = mount(QuantityComponent)
165+
wrapper.vm.quantity = 5
166+
wrapper.trigger('keydown.down')
167+
expect(wrapper.vm.quantity).to.equal(4)
168+
})
169+
170+
it('ESC 键将数量设置为 0', () => {
171+
const wrapper = mount(QuantityComponent)
172+
wrapper.vm.quantity = 5
173+
wrapper.trigger('keydown.esc')
174+
expect(wrapper.vm.quantity).to.equal(0)
175+
})
176+
177+
it('魔术字符 "a" 键将数量设置为 13', () => {
178+
const wrapper = mount(QuantityComponent)
179+
wrapper.trigger('keydown', {
180+
which: 65
181+
})
182+
expect(wrapper.vm.quantity).to.equal(13)
183+
})
184+
})
185+
186+
```
187+
188+
**限制**
189+
190+
点后面的按键名 `keydown.up` 会被翻译成一个 `keyCode`。这些被支持的按键名有:
191+
192+
* `enter`, `tab`, `delete`, `esc`, `space`, `up`, `down`, `left`, `right`
193+
194+
## 重要事项
195+
196+
`vue-test-utils` 是同步触发事件。因此 `vue.nextTick` 不是必须的。

docs/zh-cn/guides/testing-SFCs-with-jest.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ $ npm install --save-dev jest vue-test-utils
2727

2828
## 在 Jest 中处理单文件组件
2929

30-
为了告诉 Jest 如何处理 `*.vue` 文件,我们需要安装和配置 `jest-vue` 预处理器:
30+
为了告诉 Jest 如何处理 `*.vue` 文件,我们需要安装和配置 `vue-jest` 预处理器:
3131

3232
``` bash
33-
npm install --save-dev jest-vue
33+
npm install --save-dev vue-jest
3434
```
3535

3636
接下来在 `package.json` 中创建一个 `jest` 块:
@@ -46,15 +46,15 @@ npm install --save-dev jest-vue
4646
"vue"
4747
],
4848
"transform": {
49-
// 用 `jest-vue` 处理 `*.vue` 文件
50-
".*\\.(vue)$": "<rootDir>/node_modules/jest-vue"
49+
// 用 `vue-jest` 处理 `*.vue` 文件
50+
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
5151
},
5252
"mapCoverage": true
5353
}
5454
}
5555
```
5656

57-
> **注意:**`jest-vue` 目前并不支持 `vue-loader` 所有的功能,比如自定义块和样式加载。额外的,诸如代码分隔等 webpack 特有的功能也是不支持的。如果要使用它们,请阅读教程里的[用 Mocha + webpack 测试单文件组件](./testing-SFCs-with-mocha-webpack.md)
57+
> **注意:**`vue-jest` 目前并不支持 `vue-loader` 所有的功能,比如自定义块和样式加载。额外的,诸如代码分隔等 webpack 特有的功能也是不支持的。如果要使用它们,请阅读教程里的[用 Mocha + webpack 测试单文件组件](./testing-SFCs-with-mocha-webpack.md)
5858
5959
## 处理 webpack 别名
6060

0 commit comments

Comments
 (0)