Skip to content

Commit 85e9765

Browse files
Jinjiangeddyerburgh
authored andcommitted
docs: synced recent updates (#441)
1 parent ca4d89b commit 85e9765

File tree

5 files changed

+114
-12
lines changed

5 files changed

+114
-12
lines changed

docs/zh-cn/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [API](api/README.md)
1919
* [mount](api/mount.md)
2020
* [shallow](api/shallow.md)
21+
* [renderToString](api/renderToString.md)
2122
* [挂载选项](api/options.md)
2223
- [context](api/options.md#context)
2324
- [slots](api/options.md#slots)

docs/zh-cn/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [API](api/README.md)
1515
* [mount](api/mount.md)
1616
* [shallow](api/shallow.md)
17+
* [renderToString](api/renderToString.md)
1718
* [挂载选项](api/options.md)
1819
- [context](api/options.md#context)
1920
- [slots](api/options.md#slots)

docs/zh-cn/api/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [mount](./mount.md)
44
* [shallow](./shallow.md)
5+
* [renderToString](./renderToString.md)
56
* [挂载选项](./options.md)
67
- [context](./options.md#context)
78
- [slots](./options.md#slots)

docs/zh-cn/api/renderToString.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# `renderToString(component {, options}])`
2+
3+
- **参数:**
4+
5+
- `{Component} component`
6+
- `{Object} options`
7+
- `{Object} context`
8+
- `{Array<Component|Object>|Component} children`
9+
- `{Object} slots`
10+
- `{Array<Componet|Object>|Component|String} default`
11+
- `{Array<Componet|Object>|Component|String} named`
12+
- `{Object} mocks`
13+
- `{Object|Array<string>} stubs`
14+
- `{Vue} localVue`
15+
16+
- **返回值:** `{string}`
17+
18+
- **选项:**
19+
20+
查阅[挂载选项](./options.md)
21+
22+
- **使用:**
23+
24+
将一个组件渲染为 HTML。
25+
26+
`renderToString` 在底层使用了 [`vue-server-renderer`](https://ssr.vuejs.org/en/basic.html) 来将一个组件渲染为 HTML。
27+
28+
**不带选项:**
29+
30+
```js
31+
import { renderToString } from '@vue/test-utils'
32+
import Foo from './Foo.vue'
33+
34+
describe('Foo', () => {
35+
it('renders a div', () => {
36+
const renderedString = renderToString(Foo)
37+
expect(renderedString).toContain('<div></div>')
38+
})
39+
})
40+
```
41+
42+
**带 Vue 选项:**
43+
44+
```js
45+
import { renderToString } from '@vue/test-utils'
46+
import Foo from './Foo.vue'
47+
48+
describe('Foo', () => {
49+
it('renders a div', () => {
50+
const renderedString = renderToString(Foo, {
51+
propsData: {
52+
color: 'red'
53+
}
54+
})
55+
expect(renderedString).toContain('red')
56+
})
57+
})
58+
```
59+
60+
**默认插槽和具名插槽:**
61+
62+
```js
63+
import { renderToString } from '@vue/test-utils'
64+
import Foo from './Foo.vue'
65+
import Bar from './Bar.vue'
66+
import FooBar from './FooBar.vue'
67+
68+
describe('Foo', () => {
69+
it('renders a div', () => {
70+
const renderedString = renderToString(Foo, {
71+
slots: {
72+
default: [Bar, FooBar],
73+
fooBar: FooBar, // Will match <slot name="FooBar" />,
74+
foo: '<div />'
75+
}
76+
})
77+
expect(renderedString).toContain('<div></div>')
78+
})
79+
})
80+
```
81+
82+
**全局属性存根:**
83+
84+
```js
85+
import { renderToString } from '@vue/test-utils'
86+
import Foo from './Foo.vue'
87+
88+
describe('Foo', () => {
89+
it('renders a div', () => {
90+
const $route = { path: 'http://www.example-path.com' }
91+
const renderedString = renderToString(Foo, {
92+
mocks: {
93+
$route
94+
}
95+
})
96+
expect(renderedString).toContain($route.path)
97+
})
98+
})
99+
```

docs/zh-cn/guides/testing-async-components.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Testing Asynchronous Behavior
1+
# 测试异步行为
22

3-
To simplify testing, `vue-test-utils` applies DOM updates _synchronously_. However, there are some techniques you need to be aware of when testing a component with asynchronous behavior such as callbacks or promises.
3+
为了让测试变得简单,`vue-test-utils` _同步_应用 DOM 更新。不过当测试一个带有回调或 Promise 等异步行为的组件时,你需要留意一些技巧。
44

5-
One of the most common asynchronous behaviors is API calls and Vuex actions. The following examples shows how to test a method that makes an API call. This example uses Jest to run the test and to mock the HTTP library `axios`. More about Jest manual mocks can be found [here](https://facebook.github.io/jest/docs/en/manual-mocks.html#content).
5+
API 调用和 Vuex action 都是最常见的异步行为之一。下列例子展示了如何测试一个会调用到 API 的方法。这个例子使用 Jest 运行测试用例同时模拟了 HTTP `axios`。更多关于 Jest 的手动模拟的介绍可移步[这里](https://facebook.github.io/jest/docs/en/manual-mocks.html#content)
66

7-
The implementation of the `axios` mock looks like this:
7+
`axios` 的模拟实现大概是这个样子的:
88

99
``` js
1010
export default {
@@ -14,7 +14,7 @@ export default {
1414
}
1515
```
1616

17-
The below component makes an API call when a button is clicked, then assigns the response to `value`.
17+
下面的组件在按钮被点击的时候会调用一个 API,然后将响应的值赋给 `value`
1818

1919
``` html
2020
<template>
@@ -41,10 +41,10 @@ The below component makes an API call when a button is clicked, then assigns the
4141
</script>
4242
```
4343

44-
A test can be written like this:
44+
测试用例可以写成像这样:
4545

4646
``` js
47-
import { shallow } from '@vue/test-utils'
47+
import { shallow } from 'vue-test-utils'
4848
import Foo from './Foo'
4949
jest.mock('axios')
5050

@@ -57,7 +57,7 @@ test('Foo', () => {
5757
})
5858
```
5959

60-
This test currently fails because the assertion is called before the promise in `fetchResults` resolves. Most unit test libraries provide a callback to let the runner know when the test is complete. Jest and Mocha both use `done`. We can use `done` in combination with `$nextTick` or `setTimeout` to ensure any promises resolve before the assertion is made.
60+
现在这则测试用例会失败,因为断言在 `fetchResults` 中的 Promise 完成之前就被调用了。大多数单元测试库都提供一个回调来使得运行期知道测试用例的完成时机。Jest Mocha 都是用了 `done`。我们可以和 `$nectTick``setTimeout` 结合使用 `done` 来确保任何 Promise 都会在断言之前完成。
6161

6262
``` js
6363
test('Foo', () => {
@@ -72,14 +72,14 @@ test('Foo', () => {
7272
})
7373
```
7474

75-
The reason `$nextTick` or `setTimeout` allow the test to pass is because the microtask queue where promise callbacks are processed run before the task queue, where `$nextTick` and `setTimeout` are processed. This means by the time the `$nexTick` and `setTimeout` run, any promise callbacks on the microtask queue will have been executed. See [here](https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/) for a more detailed explanation.
75+
`$nextTick` `setTimeout` 允许测试通过的原因是 Promise 回调的 microtask 队列会在处理 `$nextTick` `setTimeout` 的任务队列之前先被处理。也就是说在 `$nextTick` `setTimeout` 运行的时候,任何 microtask 队列上的 Promise 回调都已经执行过了。更多的解释请移步[这里](https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/)
7676

77-
Another solution is to use an `async` function and the npm package `flush-promises`. `flush-promises` flushes all pending resolved promise handlers. You can `await` the call of `flushPromises` to flush pending promises and improve the readability of your test.
77+
另一个解决方案时使用一个 `async` 函数配合 npm `flush-promises``flush-promises` 会清除所有等待完成的 Promise 具柄。你可以 `await` `flushPromiese` 调用,以此清除等待中的 Promise 并改进你的测试用例的可读性。
7878

7979
The updated test looks like this:
8080

8181
``` js
82-
import { shallow } from '@vue/test-utils'
82+
import { shallow } from 'vue-test-utils'
8383
import flushPromises from 'flush-promises'
8484
import Foo from './Foo'
8585
jest.mock('axios')
@@ -94,4 +94,4 @@ test('Foo', () => {
9494
})
9595
```
9696

97-
This same technique can be applied to Vuex actions, which return a promise by default.
97+
相同的技巧可以被运用在同样默认返回一个 Promise 的 Vuex action 中。

0 commit comments

Comments
 (0)