Skip to content

Commit 24d7abd

Browse files
authored
Merge pull request #53 from hweeTan/master
translate shallow renderer and test renderer
2 parents 596362e + 009e054 commit 24d7abd

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

content/docs/addons-shallow-renderer.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ var ShallowRenderer = require('react-test-renderer/shallow'); // ES5 with npm
1515

1616
## Overview {#overview}
1717

18-
When writing unit tests for React, shallow rendering can be helpful. Shallow rendering lets you render a component "one level deep" and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered. This does not require a DOM.
18+
Khi viết Unit Test cho React, render cạn có thể là cách hữu dụng. Việc render cạn cho phép bạn render một component "sâu một lớp" và xác nhận được sự trung thực của kết quả trả về từ hàm render, mà không cần quan tâm về cách hoạt động của những component con, khi mà chúng không được khởi tạo và render. Việc này không cần dùng DOM.
1919

20-
For example, if you have the following component:
20+
Ví dụ, nếu bạn cần render component sau:
2121

2222
```javascript
2323
function MyComponent() {
2424
return (
2525
<div>
26-
<span className="heading">Title</span>
26+
<span className="heading">Tiêu đề</span>
2727
<Subcomponent foo="bar" />
2828
</div>
2929
);
3030
}
3131
```
3232

33-
Then you can assert:
33+
Rồi bạn có thể xác nhận rằng:
3434

3535
```javascript
3636
import ShallowRenderer from 'react-test-renderer/shallow';
@@ -47,22 +47,22 @@ expect(result.props.children).toEqual([
4747
]);
4848
```
4949

50-
Shallow testing currently has some limitations, namely not supporting refs.
50+
Kiểm tra cạn hiện tại vẫn có một vài hạn chế, có thể kể tên là việc chưa hỗ trợ refs.
5151

52-
> Note:
52+
> Lưu ý:
5353
>
54-
> We also recommend checking out Enzyme's [Shallow Rendering API](https://airbnb.io/enzyme/docs/api/shallow.html). It provides a nicer higher-level API over the same functionality.
54+
> Chúng tôi cũng khuyến khích bạn xem thêm về [API Render cạn](https://airbnb.io/enzyme/docs/api/shallow.html) của Enzyme. Nó cung cấp API cấp cao hơn và tốt hơn cho cùng chức năng.
5555
56-
## Reference {#reference}
56+
## Tài liệu tham khảo {#reference}
5757

5858
### `shallowRenderer.render()` {#shallowrendererrender}
5959

60-
You can think of the shallowRenderer as a "place" to render the component you're testing, and from which you can extract the component's output.
60+
Bạn có thể tưởng tượng shallowRenderer như là một "nơi" để render component bạn muốn kiểm tra, và từ đó bạn có thể tách đầu ra của component đó.
6161

62-
`shallowRenderer.render()` is similar to [`ReactDOM.render()`](/docs/react-dom.html#render) but it doesn't require DOM and only renders a single level deep. This means you can test components isolated from how their children are implemented.
62+
`shallowRenderer.render()` gần giống với [`ReactDOM.render()`](/docs/react-dom.html#render), nhưng nó không cần dùng DOM và chỉ render sâu một lớp. Việc này nghĩa là bạn có thể kiểm tra những component mà không phụ thuộc vào cách những component con được triển khai.
6363

6464
### `shallowRenderer.getRenderOutput()` {#shallowrenderergetrenderoutput}
6565

66-
After `shallowRenderer.render()` has been called, you can use `shallowRenderer.getRenderOutput()` to get the shallowly rendered output.
66+
Sau khi gọi hàm `shallowRenderer.render()`, bạn có thể dùng `shallowRenderer.getRenderOutput()` để lấy đầu ra của việc render cạn.
6767

68-
You can then begin to assert facts about the output.
68+
Rồi bạn có thể bắt đầu việc xác nhận sự trung thực của đầu ra.

content/docs/reference-test-renderer.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ category: Reference
1010

1111
```javascript
1212
import TestRenderer from 'react-test-renderer'; // ES6
13-
const TestRenderer = require('react-test-renderer'); // ES5 with npm
13+
const TestRenderer = require('react-test-renderer'); // ES5 với npm
1414
```
1515

16-
## Overview {#overview}
16+
## Sơ lược {#overview}
1717

18-
This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment.
18+
Gói này cung cấp một React Renderer để sử dụng cho việc render những React component thành những Javascript object, mà không cần dựa trên DOM hay một môi trường native mobile nào.
1919

20-
Essentially, this package makes it easy to grab a snapshot of the platform view hierarchy (similar to a DOM tree) rendered by a React DOM or React Native component without using a browser or [jsdom](https://github.com/tmpvar/jsdom).
20+
Về bản chất, gói này giúp cho việc chụp một bức tranh toàn cảnh của cấu trúc view (tương tự như cây DOM) được render bởi một component React DOM hay React Native mà không cần tới một trình duyệt hay [jsdom](https://github.com/tmpvar/jsdom).
2121

22-
Example:
22+
Ví dụ:
2323

2424
```javascript
2525
import TestRenderer from 'react-test-renderer';
@@ -38,9 +38,9 @@ console.log(testRenderer.toJSON());
3838
// children: [ 'Facebook' ] }
3939
```
4040

41-
You can use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: [Learn more about it](https://jestjs.io/docs/en/snapshot-testing).
41+
Bạn có thể sử dụng chức năng chụp snapshot của Jest để tự động lưu một bản sao của cây JSON vào 1 tập tin và kiểm tra trong những bản thử của bạn rằng nó không thay đổi: [Xem thêm về nó](https://jestjs.io/docs/en/snapshot-testing).
4242

43-
You can also traverse the output to find specific nodes and make assertions about them.
43+
Bạn cũng có thể đi qua đầu ra để tìm những node nhất định và kiểm tra độ chính xác của chúng.
4444

4545
```javascript
4646
import TestRenderer from 'react-test-renderer';
@@ -95,15 +95,15 @@ expect(testInstance.findByProps({className: "sub"}).children).toEqual(['Sub']);
9595
* [`testInstance.parent`](#testinstanceparent)
9696
* [`testInstance.children`](#testinstancechildren)
9797

98-
## Reference {#reference}
98+
## Tài liệu tham khảo {#reference}
9999

100100
### `TestRenderer.create()` {#testrenderercreate}
101101

102102
```javascript
103103
TestRenderer.create(element, options);
104104
```
105105

106-
Create a `TestRenderer` instance with the passed React element. It doesn't use the real DOM, but it still fully renders the component tree into memory so you can make assertions about it. The returned instance has the following methods and properties.
106+
Tạo một instance `TestRenderer` với một element React trong tham số. No kh6ong sử dụng DOM thật, nhưng nó vẫn render cây component một cách đầy đủ vào trong bộ nhớ để bạn có thể kiểm định. Instance được trả về có những hàm và thuộc tính sau đây.
107107

108108
### `TestRenderer.act()` {#testrendereract}
109109

@@ -141,141 +141,141 @@ expect(root.toJSON()).toMatchSnapshot();
141141
testRenderer.toJSON()
142142
```
143143

144-
Return an object representing the rendered tree. This tree only contains the platform-specific nodes like `<div>` or `<View>` and their props, but doesn't contain any user-written components. This is handy for [snapshot testing](https://facebook.github.io/jest/docs/en/snapshot-testing.html#snapshot-testing-with-jest).
144+
Trả về một object thể hiện cậy được render. Cây này chỉ bao gồm các node riêng cho các nền tảng như `<div>` hay `<View>` và thuộc tính (prop) của chúng, nhưng không bao gồm bất kỳ component do người dùng viết. Việc này tiện cho [thử snapshot](https://facebook.github.io/jest/docs/en/snapshot-testing.html#snapshot-testing-with-jest).
145145

146146
### `testRenderer.toTree()` {#testrenderertotree}
147147

148148
```javascript
149149
testRenderer.toTree()
150150
```
151151

152-
Return an object representing the rendered tree. Unlike `toJSON()`, the representation is more detailed than the one provided by `toJSON()`, and includes the user-written components. You probably don't need this method unless you're writing your own assertion library on top of the test renderer.
152+
Trả về một object thể hiện cậy được render. Không như `toJSON()`, sự thể hiện này chi tiết hơn kết quả được cung cấp bởi `toJSON()`, và nó bao gồm cả những component do người dùng viết. Bạn cỏ thể không cần hàm này trừ khi bạn đang viết thư viện kiểm định trên test renderer.
153153

154154
### `testRenderer.update()` {#testrendererupdate}
155155

156156
```javascript
157157
testRenderer.update(element)
158158
```
159159

160-
Re-render the in-memory tree with a new root element. This simulates a React update at the root. If the new element has the same type and key as the previous element, the tree will be updated; otherwise, it will re-mount a new tree.
160+
Render lại cây trong bộ nhớ với một element gốc mới. Việc này giả lập một sự cập nhật của React tại gốc. Nếu element mới có cùng loại (type) và key (khoá) với element trước, cây sẽ được cập nhật; nếu không, nó sẽ mount lại một cây mới.
161161

162162
### `testRenderer.unmount()` {#testrendererunmount}
163163

164164
```javascript
165165
testRenderer.unmount()
166166
```
167167

168-
Unmount the in-memory tree, triggering the appropriate lifecycle events.
168+
Gỡ (unmount) cây trong bộ nhớ, phát ra các sự kiện vòng đời (lifecycle events) tương ứng.
169169

170170
### `testRenderer.getInstance()` {#testrenderergetinstance}
171171

172172
```javascript
173173
testRenderer.getInstance()
174174
```
175175

176-
Return the instance corresponding to the root element, if available. This will not work if the root element is a function component because they don't have instances.
176+
Trả về một instance tương ứng với element gốc, nếu có. Việc này sẽ không hoạt động nếu element gốc là một function component vì nó không có intance.
177177

178178
### `testRenderer.root` {#testrendererroot}
179179

180180
```javascript
181181
testRenderer.root
182182
```
183183

184-
Returns the root "test instance" object that is useful for making assertions about specific nodes in the tree. You can use it to find other "test instances" deeper below.
184+
Trả về object "test instance" gốc hữu dụng cho việc kiểm định những node nhất định trong cây. Bạn có thể dùng nó để tìm những test instance" khác sâu bên dưới.
185185

186186
### `testInstance.find()` {#testinstancefind}
187187

188188
```javascript
189189
testInstance.find(test)
190190
```
191191

192-
Find a single descendant test instance for which `test(testInstance)` returns `true`. If `test(testInstance)` does not return `true` for exactly one test instance, it will throw an error.
192+
Tìm một test instance ngay bên dưới khi `test(testInstance)` trả về `true`. Nếu `test(testInstance)` không trả về `true` cho đúng một test instance, một lỗi sẽ được ném ra.
193193

194194
### `testInstance.findByType()` {#testinstancefindbytype}
195195

196196
```javascript
197197
testInstance.findByType(type)
198198
```
199199

200-
Find a single descendant test instance with the provided `type`. If there is not exactly one test instance with the provided `type`, it will throw an error.
200+
Tìm một test instance ngay bên dưới với `type` được cung cấp. Nếu không có chính xác một test instance với `type` được cung cấp, nó sẽ ném ra một lỗi.
201201

202202
### `testInstance.findByProps()` {#testinstancefindbyprops}
203203

204204
```javascript
205205
testInstance.findByProps(props)
206206
```
207207

208-
Find a single descendant test instance with the provided `props`. If there is not exactly one test instance with the provided `props`, it will throw an error.
208+
Tìm một test instance ngay bên dưới với `props` được cung cấp. Nếu không có chính xác một test instance với `props` được cung cấp, nó sẽ ném ra một lỗi.
209209

210210
### `testInstance.findAll()` {#testinstancefindall}
211211

212212
```javascript
213213
testInstance.findAll(test)
214214
```
215215

216-
Find all descendant test instances for which `test(testInstance)` returns `true`.
216+
Tìm tất cả test instance ngay bên dưới khi `test(testInstance)` trả về `true`.
217217

218218
### `testInstance.findAllByType()` {#testinstancefindallbytype}
219219

220220
```javascript
221221
testInstance.findAllByType(type)
222222
```
223223

224-
Find all descendant test instances with the provided `type`.
224+
Tìm tất cả test instance với `type` được cung cấp.
225225

226226
### `testInstance.findAllByProps()` {#testinstancefindallbyprops}
227227

228228
```javascript
229229
testInstance.findAllByProps(props)
230230
```
231231

232-
Find all descendant test instances with the provided `props`.
232+
Tìm tất cả test instance với `props` được cung cấp.
233233

234234
### `testInstance.instance` {#testinstanceinstance}
235235

236236
```javascript
237237
testInstance.instance
238238
```
239239

240-
The component instance corresponding to this test instance. It is only available for class components, as function components don't have instances. It matches the `this` value inside the given component.
240+
Component instance tương ứng với test instance này. Nó chỉ khả dụng cho những class component, vì những function component không có instance. Nó tương đương với giá trị của `this` trong component.
241241

242242
### `testInstance.type` {#testinstancetype}
243243

244244
```javascript
245245
testInstance.type
246246
```
247247

248-
The component type corresponding to this test instance. For example, a `<Button />` component has a type of `Button`.
248+
Loại compoennt tương ứng với test instance này. Vi dụ, một component `<Button />` có loại là `Button`.
249249

250250
### `testInstance.props` {#testinstanceprops}
251251

252252
```javascript
253253
testInstance.props
254254
```
255255

256-
The props corresponding to this test instance. For example, a `<Button size="small" />` component has `{size: 'small'}` as props.
256+
Thuộc tính (props) tương ứng với test instance này. Ví dụ, một component `<Button size="small" />` thì có props là `{size: 'small'}`
257257

258258
### `testInstance.parent` {#testinstanceparent}
259259

260260
```javascript
261261
testInstance.parent
262262
```
263263

264-
The parent test instance of this test instance.
264+
Test instance cha của test instance này.
265265

266266
### `testInstance.children` {#testinstancechildren}
267267

268268
```javascript
269269
testInstance.children
270270
```
271271

272-
The children test instances of this test instance.
272+
Những test instance con của test instance này.
273273

274-
## Ideas {#ideas}
274+
## Ý tưởng {#ideas}
275275

276-
You can pass `createNodeMock` function to `TestRenderer.create` as the option, which allows for custom mock refs.
277-
`createNodeMock` accepts the current element and should return a mock ref object.
278-
This is useful when you test a component that relies on refs.
276+
Bạn có thể truyền hàm `createNodeMock` vào `TestRenderer.create` như một tuỳ chọn, nó cho phép tuỳ chỉnh mock refs.
277+
`createNodeMock` nhận vào element hiện tại và sẽ trả về mốt object mock ref.
278+
Việc này hữu dụng cho việc thử một component phụ thuộc vào refs.
279279

280280
```javascript
281281
import TestRenderer from 'react-test-renderer';

0 commit comments

Comments
 (0)