You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var ReactTestUtils =require('react-dom/test-utils'); // ES5 with npm
14
14
```
15
15
16
-
## Overview {#overview}
16
+
## 概述 {#overview}
17
17
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).
> 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.
> 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.
@@ -40,17 +40,17 @@ var ReactTestUtils = require('react-dom/test-utils'); // ES5 with npm
40
40
-[`renderIntoDocument()`](#renderintodocument)
41
41
-[`Simulate`](#simulate)
42
42
43
-
## Reference {#reference}
43
+
## 参考 {#reference}
44
44
45
45
### `act()` {#act}
46
46
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.
@@ -122,7 +122,7 @@ it('can render and update a counter', () => {
122
122
});
123
123
```
124
124
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)。
126
126
127
127
* * *
128
128
@@ -135,11 +135,11 @@ mockComponent(
135
135
)
136
136
```
137
137
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.
> `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.
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 被其他测试方法使用。
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.
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.
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.
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 对象。**它实际相当于:
300
300
301
301
```js
302
302
constdomContainer=document.createElement('div');
303
303
ReactDOM.render(element, domContainer);
304
304
```
305
305
306
-
> Note:
306
+
> 注意:
307
307
>
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.
> 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.
0 commit comments