Skip to content

Commit f46d9d1

Browse files
Yongmin CaiQC-L
Yongmin Cai
authored andcommitted
docs(cn): translate content/docs/addons-test-utils.md into Chinese (reactjs#10)
1 parent 3f67f58 commit f46d9d1

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

content/docs/addons-test-utils.md

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ layout: docs
66
category: Reference
77
---
88

9-
**Importing**
9+
**如何引入**
1010

1111
```javascript
1212
import ReactTestUtils from 'react-dom/test-utils'; // ES6
1313
var ReactTestUtils = require('react-dom/test-utils'); // ES5 with npm
1414
```
1515

16-
## Overview {#overview}
16+
## 概述 {#overview}
1717

18-
`ReactTestUtils` makes it easy to test React components in the testing framework of your choice. At Facebook we use [Jest](https://facebook.github.io/jest/) for painless JavaScript testing. Learn how to get started with Jest through the Jest website's [React Tutorial](http://facebook.github.io/jest/docs/en/tutorial-react.html#content).
18+
`ReactTestUtils` 可搭配你所选的测试框架,轻松实现 React 组件测试。在 Facebook 内部,我们使用 [Jest](https://facebook.github.io/jest/) 来轻松实现 JavaScript 测试。你可以从 Jest 官网的 [React Tutorial](http://facebook.github.io/jest/docs/en/tutorial-react.html#content) 中了解如何开始使用它。
1919

20-
> Note:
20+
> 注意:
2121
>
22-
> We recommend using [`react-testing-library`](https://git.io/react-testing-library) which is designed to enable and encourage writing tests that use your components as the end users do.
22+
> 我们推荐使用 [`react-testing-library`](https://git.io/react-testing-library),它使得对组件编写测试用例就像终端用户在使用它一样方便。
2323
>
24-
> Alternatively, Airbnb has released a testing utility called [Enzyme](http://airbnb.io/enzyme/), which makes it easy to assert, manipulate, and traverse your React Components' output.
24+
> 另外, Airbnb 发布了一款叫作 [Enzyme]([Enzyme](http://airbnb.io/enzyme/)) 的测试工具,通过它能够轻松对 React 组件的输出进行断言、操控和遍历。
2525
2626
- [`act()`](#act)
2727
- [`mockComponent()`](#mockcomponent)
@@ -40,17 +40,17 @@ var ReactTestUtils = require('react-dom/test-utils'); // ES5 with npm
4040
- [`renderIntoDocument()`](#renderintodocument)
4141
- [`Simulate`](#simulate)
4242

43-
## Reference {#reference}
43+
## 参考 {#reference}
4444

4545
### `act()` {#act}
4646

47-
To prepare a component for assertions, wrap the code rendering it and performing updates inside an `act()` call. This makes your test run closer to how React works in the browser.
47+
为断言准备一个组件,包裹要渲染的代码并在调用 `act()` 时执行更新。这会使得测试更接近 React 在浏览器中的工作方式。
4848

49-
>Note
49+
>注意
5050
>
51-
>If you use `react-test-renderer`, it also provides an `act` export that behaves the same way.
51+
>如果你使用了 `react-test-renderer`,它也提供了与 `act` 行为相同的函数。
5252
53-
For example, let's say we have this `Counter` component:
53+
例如,假设我们有个 `Counter` 组件:
5454

5555
```js
5656
class App extends React.Component {
@@ -83,7 +83,7 @@ class App extends React.Component {
8383
}
8484
```
8585

86-
Here is how we can test it:
86+
以下是其测试代码:
8787

8888
```js{3,20-22,29-31}
8989
import React from 'react';
@@ -104,7 +104,7 @@ afterEach(() => {
104104
});
105105
106106
it('can render and update a counter', () => {
107-
// Test first render and componentDidMount
107+
// 首先测试 render componentDidMount
108108
act(() => {
109109
ReactDOM.render(<Counter />, container);
110110
});
@@ -113,7 +113,7 @@ it('can render and update a counter', () => {
113113
expect(label.textContent).toBe('You clicked 0 times');
114114
expect(document.title).toBe('You clicked 0 times');
115115
116-
// Test second render and componentDidUpdate
116+
// 再测试 render componentDidUpdate
117117
act(() => {
118118
button.dispatchEvent(new MouseEvent('click', {bubbles: true}));
119119
});
@@ -122,7 +122,7 @@ it('can render and update a counter', () => {
122122
});
123123
```
124124

125-
Don't forget that dispatching DOM events only works when the DOM container is added to the `document`. You can use a helper like [`react-testing-library`](https://github.com/kentcdodds/react-testing-library) to reduce the boilerplate code.
125+
千万不要忘记,只有将 DOM 容器添加到 `document` 时,触发 DOM 事件才生效。你可以使用类似于 [`react-testing-library`](https://github.com/kentcdodds/react-testing-library) 这样的 helper 来减少样板代码(boilerplate code)。
126126

127127
* * *
128128

@@ -135,11 +135,11 @@ mockComponent(
135135
)
136136
```
137137

138-
Pass a mocked component module to this method to augment it with useful methods that allow it to be used as a dummy React component. Instead of rendering as usual, the component will become a simple `<div>` (or other tag if `mockTagName` is provided) containing any provided children.
138+
将模拟组件模块传入这个方法后,React 内部会使用有效的方法填充该模块,使其成为虚拟的 React 组件。与通常的渲染不同,组件将变成一个简单的 `<div>` (如果提供了 `mockTagName` 则是其他标签),包含任何提供的子级。
139139

140-
> Note:
140+
> 注意:
141141
>
142-
> `mockComponent()` is a legacy API. We recommend using [shallow rendering](/docs/test-utils.html#shallow-rendering) or [`jest.mock()`](https://facebook.github.io/jest/docs/en/tutorial-react-native.html#mock-native-modules-using-jestmock) instead.
142+
> `mockComponent()` 是一个过时的 API,我们推荐使用[浅层渲染](/docs/test-utils.html#shallow-rendering)或者 [`jest.mock()`](https://facebook.github.io/jest/docs/en/tutorial-react-native.html#mock-native-modules-using-jestmock) 来代替。
143143
144144
* * *
145145

@@ -149,7 +149,7 @@ Pass a mocked component module to this method to augment it with useful methods
149149
isElement(element)
150150
```
151151

152-
Returns `true` if `element` is any React element.
152+
`element` 是任何一种 React 元素时,返回 `true`
153153

154154
* * *
155155

@@ -162,7 +162,7 @@ isElementOfType(
162162
)
163163
```
164164

165-
Returns `true` if `element` is a React element whose type is of a React `componentClass`.
165+
`element` 是一种 React 元素,并且它的类型是参数 `componentClass` 的类型时,返回 `true`
166166

167167
* * *
168168

@@ -172,7 +172,7 @@ Returns `true` if `element` is a React element whose type is of a React `compone
172172
isDOMComponent(instance)
173173
```
174174

175-
Returns `true` if `instance` is a DOM component (such as a `<div>` or `<span>`).
175+
`instance` 是一个 DOM 组件(比如 `<div>` `<span>`)时,返回 `true`
176176

177177
* * *
178178

@@ -182,7 +182,7 @@ Returns `true` if `instance` is a DOM component (such as a `<div>` or `<span>`).
182182
isCompositeComponent(instance)
183183
```
184184

185-
Returns `true` if `instance` is a user-defined component, such as a class or a function.
185+
`instance` 是一个用户自定义的组件,比如一个类或者一个函数时,返回 `true`
186186

187187
* * *
188188

@@ -195,7 +195,7 @@ isCompositeComponentWithType(
195195
)
196196
```
197197

198-
Returns `true` if `instance` is a component whose type is of a React `componentClass`.
198+
`instance` 是一个组件,并且它的类型是参数 `componentClass` 的类型时,返回 `true`
199199

200200
* * *
201201

@@ -208,7 +208,7 @@ findAllInRenderedTree(
208208
)
209209
```
210210

211-
Traverse all components in `tree` and accumulate all components where `test(component)` is `true`. This is not that useful on its own, but it's used as a primitive for other test utils.
211+
遍历所有在参数 `tree` 中的组件,记录所有 `test(component)` `true` 的组件。单独调用此方法不是很有用,但是它常常被作为底层 API 被其他测试方法使用。
212212

213213
* * *
214214

@@ -221,7 +221,7 @@ scryRenderedDOMComponentsWithClass(
221221
)
222222
```
223223

224-
Finds all DOM elements of components in the rendered tree that are DOM components with the class name matching `className`.
224+
查找渲染树中组件的所有 DOM 元素,这些组件是 css 类名与参数 `className` 匹配的 DOM 组件。
225225

226226
* * *
227227

@@ -234,7 +234,7 @@ findRenderedDOMComponentWithClass(
234234
)
235235
```
236236

237-
Like [`scryRenderedDOMComponentsWithClass()`](#scryrendereddomcomponentswithclass) but expects there to be one result, and returns that one result, or throws exception if there is any other number of matches besides one.
237+
用法与 [`scryRenderedDOMComponentsWithClass()`](#scryrendereddomcomponentswithclass) 保持一致,但期望仅返回一个结果。不符合预期的情况下会抛出异常。
238238

239239
* * *
240240

@@ -247,7 +247,7 @@ scryRenderedDOMComponentsWithTag(
247247
)
248248
```
249249

250-
Finds all DOM elements of components in the rendered tree that are DOM components with the tag name matching `tagName`.
250+
查找渲染树中组件的所有的 DOM 元素,这些组件是标记名与参数 `tagName` 匹配的 DOM 组件。
251251

252252
* * *
253253

@@ -260,7 +260,7 @@ findRenderedDOMComponentWithTag(
260260
)
261261
```
262262

263-
Like [`scryRenderedDOMComponentsWithTag()`](#scryrendereddomcomponentswithtag) but expects there to be one result, and returns that one result, or throws exception if there is any other number of matches besides one.
263+
用法与 [`scryRenderedDOMComponentsWithTag()`](#scryrendereddomcomponentswithtag) 保持一致,但期望仅返回一个结果。不符合预期的情况下会抛出异常。
264264

265265
* * *
266266

@@ -273,7 +273,7 @@ scryRenderedComponentsWithType(
273273
)
274274
```
275275

276-
Finds all instances of components with type equal to `componentClass`.
276+
查找组件类型等于 `componentClass` 组件的所有实例。
277277

278278
* * *
279279

@@ -286,30 +286,30 @@ findRenderedComponentWithType(
286286
)
287287
```
288288

289-
Same as [`scryRenderedComponentsWithType()`](#scryrenderedcomponentswithtype) but expects there to be one result and returns that one result, or throws exception if there is any other number of matches besides one.
289+
用法与 [`scryRenderedComponentsWithType()`](#scryrenderedcomponentswithtype) 保持一致,但期望仅返回一个结果。不符合预期的情况下会抛出异常。
290290

291-
***
291+
* * *
292292

293293
### `renderIntoDocument()` {#renderintodocument}
294294

295295
```javascript
296296
renderIntoDocument(element)
297297
```
298298

299-
Render a React element into a detached DOM node in the document. **This function requires a DOM.** It is effectively equivalent to:
299+
渲染 React 元素到 document 中的某个单独的 DOM 节点上。**这个函数需要一个 DOM 对象。** 它实际相当于:
300300

301301
```js
302302
const domContainer = document.createElement('div');
303303
ReactDOM.render(element, domContainer);
304304
```
305305

306-
> Note:
306+
> 注意:
307307
>
308-
> You will need to have `window`, `window.document` and `window.document.createElement` globally available **before** you import `React`. Otherwise React will think it can't access the DOM and methods like `setState` won't work.
308+
> 你需要在引入 `React` **之前**确保 `window` 存在,`window.document` `window.document.createElement` 能在全局环境中获取到。不然 React 会认为它没有权限去操作 DOM,以及像 `setState` 这样的方法将不可用。
309309
310310
* * *
311311

312-
## Other Utilities {#other-utilities}
312+
## 其他工具方法 {#other-utilities}
313313

314314
### `Simulate` {#simulate}
315315

@@ -320,19 +320,19 @@ Simulate.{eventName}(
320320
)
321321
```
322322

323-
Simulate an event dispatch on a DOM node with optional `eventData` event data.
323+
使用可选的 `eventData` 事件数据来模拟在 DOM 节点上触发事件。
324324

325-
`Simulate` has a method for [every event that React understands](/docs/events.html#supported-events).
325+
[React 所支持的所有事件](/docs/events.html#supported-events)`Simulate` 中都有对应的方法。
326326

327-
**Clicking an element**
327+
**点击元素**
328328

329329
```javascript
330330
// <button ref={(node) => this.button = node}>...</button>
331331
const node = this.button;
332332
ReactTestUtils.Simulate.click(node);
333333
```
334334

335-
**Changing the value of an input field and then pressing ENTER.**
335+
**修改一个 input 输入框的值,然后按回车键。**
336336

337337
```javascript
338338
// <input ref={(node) => this.textInput = node} />
@@ -342,8 +342,8 @@ ReactTestUtils.Simulate.change(node);
342342
ReactTestUtils.Simulate.keyDown(node, {key: "Enter", keyCode: 13, which: 13});
343343
```
344344

345-
> Note
345+
> 注意:
346346
>
347-
> You will have to provide any event property that you're using in your component (e.g. keyCode, which, etc...) as React is not creating any of these for you.
347+
> 你必须提供一切需要在组件中用到的事件属性(比如:keyCodewhich 等等),因为 React 没有为你创建这些属性。
348348
349349
* * *

0 commit comments

Comments
 (0)