Skip to content

Commit b82e221

Browse files
authored
Merge pull request #211 from danglequocbao001/feat/translate-addons-test-utils
feat: translate addons test utils
2 parents fe6d624 + d48f937 commit b82e221

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

content/docs/addons-test-utils.md

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

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

16-
## Overview {#overview}
16+
## Tổng quan {#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](https://jestjs.io/docs/tutorial-react).
18+
`ReactTestUtils` giúp cho việc test các component trong React dễ dàng hơn trong một test framework mà bạn muốn. Ở Facebook chúng tôi dùng [Jest](https://facebook.github.io/jest/) để test JavaScript một cách dễ dàng. Giờ bạn có thể tìm hiểu cách bắt đầu với Jest thông qua website này [React Tutorial](https://jestjs.io/docs/tutorial-react).
1919

20-
> Note:
20+
> Lưu ý:
2121
>
22-
> We recommend using [React Testing Library](https://testing-library.com/react) which is designed to enable and encourage writing tests that use your components as the end users do.
22+
> Chúng tôi khuyên dùng [React Testing Library](https://testing-library.com/react) được thiết kế để hỗ trợ viết test mà dùng các component của bạn như là những người dùng cuối cùng(có thể hiểu như là người dùng thực tế).
2323
>
24-
> For React versions <= 16, the [Enzyme](https://airbnb.io/enzyme/) library makes it easy to assert, manipulate, and traverse your React Components' output.
24+
> Đối với phiên bản React <= 16, thư viện [Enzyme](https://airbnb.io/enzyme/) giúp bạn dễ dàng assert(là một xác nhận - assert - là một vị từ được kết nối với một điểm trong chương trình, luôn được đánh giá là true tại thời điểm đó trong quá trình thực thi mã), sử dụng và kiểm tra output của các React Component.
2525
2626

2727

@@ -42,17 +42,17 @@ var ReactTestUtils = require('react-dom/test-utils'); // ES5 with npm
4242
- [`renderIntoDocument()`](#renderintodocument)
4343
- [`Simulate`](#simulate)
4444

45-
## Reference {#reference}
45+
## Chức vụ {#reference}
4646

4747
### `act()` {#act}
4848

49-
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.
49+
Để chuẩn bị một component cho các assertion(assertion chính là những method dùng để kiểm tra kết quả của đơn vị cần test có đúng với mong đợi không), render component đó và thực hiện cập nhật bên trong hàm `act()`. Điều này giúp cho test của bạn chạy gần giống như với cách React chạy trên browser thực tế.
5050

51-
>Note
51+
>Lưu ý
5252
>
53-
>If you use `react-test-renderer`, it also provides an `act` export that behaves the same way.
53+
>Nếu bạn dùng `react-test-renderer`, nó cũng cung cấp một `act` chạy tương tự.
5454
55-
For example, let's say we have this `Counter` component:
55+
Ví dụ, chúng ta có một `Counter` component:
5656

5757
```js
5858
class Counter extends React.Component {
@@ -62,10 +62,10 @@ class Counter extends React.Component {
6262
this.handleClick = this.handleClick.bind(this);
6363
}
6464
componentDidMount() {
65-
document.title = `You clicked ${this.state.count} times`;
65+
document.title = `Bạn click ${this.state.count} lần`;
6666
}
6767
componentDidUpdate() {
68-
document.title = `You clicked ${this.state.count} times`;
68+
document.title = `Bạn click ${this.state.count} lần`;
6969
}
7070
handleClick() {
7171
this.setState(state => ({
@@ -75,17 +75,17 @@ class Counter extends React.Component {
7575
render() {
7676
return (
7777
<div>
78-
<p>You clicked {this.state.count} times</p>
78+
<p>Bạn đã click {this.state.count} lần</p>
7979
<button onClick={this.handleClick}>
80-
Click me
80+
Click vào đây
8181
</button>
8282
</div>
8383
);
8484
}
8585
}
8686
```
8787

88-
Here is how we can test it:
88+
Đây là cách mà chúng ta test :
8989

9090
```js{3,20-22,29-31}
9191
import React from 'react';
@@ -105,28 +105,28 @@ afterEach(() => {
105105
container = null;
106106
});
107107
108-
it('can render and update a counter', () => {
108+
it('có thể render và cập nhật một counter', () => {
109109
// Test first render and componentDidMount
110110
act(() => {
111111
ReactDOM.render(<Counter />, container);
112112
});
113113
const button = container.querySelector('button');
114114
const label = container.querySelector('p');
115-
expect(label.textContent).toBe('You clicked 0 times');
116-
expect(document.title).toBe('You clicked 0 times');
115+
expect(label.textContent).toBe('Bạn click 0 lần');
116+
expect(document.title).toBe('Bạn click 0 lần');
117117
118118
// Test second render and componentDidUpdate
119119
act(() => {
120120
button.dispatchEvent(new MouseEvent('click', {bubbles: true}));
121121
});
122-
expect(label.textContent).toBe('You clicked 1 times');
123-
expect(document.title).toBe('You clicked 1 times');
122+
expect(label.textContent).toBe('Bạn click 1 lần');
123+
expect(document.title).toBe('Bạn click 1 lần');
124124
});
125125
```
126126

127-
- Don't forget that dispatching DOM events only works when the DOM container is added to the `document`. You can use a library like [React Testing Library](https://testing-library.com/react) to reduce the boilerplate code.
127+
- Nhớ rằng việc điều phối các sự kiện DOM chỉ hoạt động khi vùng chứa DOM được thêm vào `document`. Bạn có thể dùng thư viện như [React Testing Library](https://testing-library.com/react) để dùng code đã có sẵn (boilerplate code).
128128

129-
- The [`recipes`](/docs/testing-recipes.html) document contains more details on how `act()` behaves, with examples and usage.
129+
- Document này [`recipes`](/docs/testing-recipes.html) cho biết thông tin chi tiết cách `act()` hoạt động, gồm cả ví dụ lẫn cách sử dụng.
130130

131131
* * *
132132

@@ -139,11 +139,11 @@ mockComponent(
139139
)
140140
```
141141

142-
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.
142+
Sơ qua một mocked module component, có một phương pháp để kết hợp nó với các method hữu ích mà cho phép nó được sử dụng như một component giả lập trong React. Thay vì render nó ra như bình thường, component chỉ ngắn gọn như một thẻ `<div>` (hoặc là một thẻ khác nếu `mockTagName` được đặt) chứa bất kỳ children nào trong nó.
143143

144-
> Note:
144+
> Lưu ý:
145145
>
146-
> `mockComponent()` is a legacy API. We recommend using [`jest.mock()`](https://jestjs.io/docs/tutorial-react-native#mock-native-modules-using-jestmock) instead.
146+
> `mockComponent()` là một API kế thừa. Chúng tôi khuyên nên dùng [`jest.mock()`](https://facebook.github.io/jest/docs/en/tutorial-react-native.html#mock-native-modules-using-jestmock) thay cho nó.
147147
148148
* * *
149149

@@ -153,7 +153,7 @@ Pass a mocked component module to this method to augment it with useful methods
153153
isElement(element)
154154
```
155155

156-
Returns `true` if `element` is any React element.
156+
Trả về `true` nếu `element` thuộc bất kỳ element trong React.
157157

158158
* * *
159159

@@ -166,7 +166,7 @@ isElementOfType(
166166
)
167167
```
168168

169-
Returns `true` if `element` is a React element whose type is of a React `componentClass`.
169+
Trả về `true` nếu `element` là một element trong React mà có kiểu thuộc `componentClass` trong React.
170170

171171
* * *
172172

@@ -176,7 +176,7 @@ Returns `true` if `element` is a React element whose type is of a React `compone
176176
isDOMComponent(instance)
177177
```
178178

179-
Returns `true` if `instance` is a DOM component (such as a `<div>` or `<span>`).
179+
Trả về `true` nếu `instance` là một component trong DOM (ví dụ như là `<div>` hoặc `<span>`).
180180

181181
* * *
182182

@@ -186,7 +186,7 @@ Returns `true` if `instance` is a DOM component (such as a `<div>` or `<span>`).
186186
isCompositeComponent(instance)
187187
```
188188

189-
Returns `true` if `instance` is a user-defined component, such as a class or a function.
189+
Trả về `true` nếu `instance` là một component do người dùng tự định nghĩa, như là class component hoặc function component.
190190

191191
* * *
192192

@@ -199,7 +199,7 @@ isCompositeComponentWithType(
199199
)
200200
```
201201

202-
Returns `true` if `instance` is a component whose type is of a React `componentClass`.
202+
Trả về `true` nếu `instance` là một component mà có kiểu thuộc `componentClass` trong React.
203203

204204
* * *
205205

@@ -212,7 +212,7 @@ findAllInRenderedTree(
212212
)
213213
```
214214

215-
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.
215+
Duyệt tất cả các component trong `tree` và gom lại tất cả các component mà `test(component)` trả về `true`. Điều này tuy không có ích lắm, nhưng được sử dụng làm nền tảng cho các test-util khác.
216216

217217
* * *
218218

@@ -225,7 +225,7 @@ scryRenderedDOMComponentsWithClass(
225225
)
226226
```
227227

228-
Finds all DOM elements of components in the rendered tree that are DOM components with the class name matching `className`.
228+
Tìm tất cả các element trong DOM thuộc các component trong tree đã render mà component DOM có tên của class giống với `className`.
229229

230230
* * *
231231

@@ -238,7 +238,7 @@ findRenderedDOMComponentWithClass(
238238
)
239239
```
240240

241-
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.
241+
Giống với [`scryRenderedDOMComponentsWithClass()`](#scryrendereddomcomponentswithclass) nhưng sẽ chỉ có một và trả về một kết quả duy nhất, hoặc throw ra exception nếu có bất kỳ các kết quả trùng nhau.
242242

243243
* * *
244244

@@ -251,7 +251,7 @@ scryRenderedDOMComponentsWithTag(
251251
)
252252
```
253253

254-
Finds all DOM elements of components in the rendered tree that are DOM components with the tag name matching `tagName`.
254+
Tìm tất cả các element trong DOM thuộc các component trong tree đã render mà component DOM có tên của thẻ giống với `tagName`.
255255

256256
* * *
257257

@@ -264,7 +264,7 @@ findRenderedDOMComponentWithTag(
264264
)
265265
```
266266

267-
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.
267+
Giống [`scryRenderedDOMComponentsWithTag()`](#scryrendereddomcomponentswithtag) nhưng sẽ chỉ có một và trả về một kết quả duy nhất, hoặc throw ra exception nếu có bất kỳ các kết quả trùng nhau.
268268

269269
* * *
270270

@@ -277,7 +277,7 @@ scryRenderedComponentsWithType(
277277
)
278278
```
279279

280-
Finds all instances of components with type equal to `componentClass`.
280+
Tìm tất cả các instance của các component có kiểu giống với `componentClass`.
281281

282282
* * *
283283

@@ -290,7 +290,7 @@ findRenderedComponentWithType(
290290
)
291291
```
292292

293-
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.
293+
Giống với [`scryRenderedComponentsWithType()`](#scryrenderedcomponentswithtype) nhưng sẽ chỉ có một và trả về một kết quả duy nhất, hoặc throw ra exception nếu có bất kỳ các kết quả trùng nhau.
294294

295295
***
296296

@@ -300,20 +300,20 @@ Same as [`scryRenderedComponentsWithType()`](#scryrenderedcomponentswithtype) bu
300300
renderIntoDocument(element)
301301
```
302302

303-
Render a React element into a detached DOM node in the document. **This function requires a DOM.** It is effectively equivalent to:
303+
Render một element của React vào một node riêng của DOM trong một document. **Function này yêu cầu DOM.** Nó tương tự với:
304304

305305
```js
306306
const domContainer = document.createElement('div');
307307
ReactDOM.render(element, domContainer);
308308
```
309309

310-
> Note:
310+
> Lưu ý:
311311
>
312-
> 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.
312+
> Bạn cần có `window`, `window.document` `window.document.createElement` có sẵn ở toàn cục **trước khi** bạn import `React`. Nếu không, React sẽ nghĩ rằng nó không thể truy cập DOM và các phương thức như `setState` không hoạt động.
313313
314314
* * *
315315

316-
## Other Utilities {#other-utilities}
316+
## Các tiện ích khác {#other-utilities}
317317

318318
### `Simulate` {#simulate}
319319

@@ -324,19 +324,19 @@ Simulate.{eventName}(
324324
)
325325
```
326326

327-
Simulate an event dispatch on a DOM node with optional `eventData` event data.
327+
Giả lập một sự kiện được gửi trên một node DOM với tùy chọn dữ liệu của sự kiện `eventData`.
328328

329-
`Simulate` has a method for [every event that React understands](/docs/events.html#supported-events).
329+
`Simulate` có một method cho [tất cả sự kiện mà React hỗ trợ](/docs/events.html#supported-events).
330330

331-
**Clicking an element**
331+
**Click vào một element**
332332

333333
```javascript
334334
// <button ref={(node) => this.button = node}>...</button>
335335
const node = this.button;
336336
ReactTestUtils.Simulate.click(node);
337337
```
338338

339-
**Changing the value of an input field and then pressing ENTER.**
339+
**Thay đổi giá trị của trường đầu vào rồi ENTER.**
340340

341341
```javascript
342342
// <input ref={(node) => this.textInput = node} />
@@ -346,8 +346,8 @@ ReactTestUtils.Simulate.change(node);
346346
ReactTestUtils.Simulate.keyDown(node, {key: "Enter", keyCode: 13, which: 13});
347347
```
348348

349-
> Note
349+
> Lưu ý
350350
>
351-
> 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.
351+
> React sẽ không tạo ra mà bạn phải cung cấp tất cả event property đang dùng trong component của bạn (v.d. keyCode, which, etc...).
352352
353353
* * *

0 commit comments

Comments
 (0)