Skip to content

Commit 504df7c

Browse files
authored
docs: translate act (#1680)
2 parents 7829ce6 + d0eb522 commit 504df7c

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

src/content/learn/creating-a-react-app.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ title: 创建一个 React 应用
1616

1717
<Note>
1818

19-
#### Full-stack frameworks do not require a server. {/*react-frameworks-do-not-require-a-server*/}
19+
#### 全栈框架不需要服务器 {/*react-frameworks-do-not-require-a-server*/}
2020

21-
All the frameworks on this page support client-side rendering ([CSR](https://developer.mozilla.org/en-US/docs/Glossary/CSR)), single-page apps ([SPA](https://developer.mozilla.org/en-US/docs/Glossary/SPA)), and static-site generation ([SSG](https://developer.mozilla.org/en-US/docs/Glossary/SSG)). These apps can be deployed to a [CDN](https://developer.mozilla.org/en-US/docs/Glossary/CDN) or static hosting service without a server. Additionally, these frameworks allow you to add server-side rendering on a per-route basis, when it makes sense for your use case.
21+
本页列出的所有框架都支持客户端渲染([CSR](https://developer.mozilla.org/en-US/docs/Glossary/CSR))、单页应用([SPA](https://developer.mozilla.org/en-US/docs/Glossary/SPA))和静态站点生成([SSG](https://developer.mozilla.org/en-US/docs/Glossary/SSG))。这些应用可以部署到 [CDN](https://developer.mozilla.org/en-US/docs/Glossary/CDN) 或静态托管服务(无需服务器)。此外,这些框架允许你根据实际需求,针对特定路由单独启用服务端渲染。
2222

23-
This allows you to start with a client-only app, and if your needs change later, you can opt-in to using server features on individual routes without rewriting your app. See your framework's documentation for configuring the rendering strategy.
23+
这意味着你可以从纯客户端应用开始构建,后续需求变更时,无需重写整个应用,即可在特定路由上启用服务端功能。具体渲染策略的配置方法,请参考各框架的官方文档。
2424

2525
</Note>
2626

src/content/reference/react/act.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ title: act
44

55
<Intro>
66

7-
`act` is a test helper to apply pending React updates before making assertions.
7+
`act` 是个测试辅助工具,用于在断言前应用待处理的 React 更新。
88

99
```js
1010
await act(async actFn)
1111
```
1212

1313
</Intro>
1414

15-
To prepare a component for assertions, wrap the code rendering it and performing updates inside an `await act()` call. This makes your test run closer to how React works in the browser.
15+
要将组件准备好进行断言,请将渲染代码和执行更新的操作包裹在 `await act()` 中。这能让你的测试更接近 React 在浏览器中的实际工作方式。
1616

1717
<Note>
18-
You might find using `act()` directly a bit too verbose. To avoid some of the boilerplate, you could use a library like [React Testing Library](https://testing-library.com/docs/react-testing-library/intro), whose helpers are wrapped with `act()`.
18+
你可能会发现直接使用 `act()` 略显繁琐。为了减少样板代码,可以使用像 [React Testing Library](https://testing-library.com/docs/react-testing-library/intro)这样的库,其辅助方法已经内置了对 `act()` 的封装。
1919
</Note>
2020

2121

2222
<InlineToc />
2323

2424
---
2525

26-
## Reference {/*reference*/}
26+
## 参考 {/*reference*/}
2727

2828
### `await act(async actFn)` {/*await-act-async-actfn*/}
2929

30-
When writing UI tests, tasks like rendering, user events, or data fetching can be considered as “units” of interaction with a user interface. React provides a helper called `act()` that makes sure all updates related to these “units” have been processed and applied to the DOM before you make any assertions.
30+
在编写 UI 测试时,诸如渲染、用户事件或数据获取等操作都可以视为与用户界面的“交互单元”。React 提供了 act() 辅助工具,确保在进行任何断言之前,所有与这些“交互单元”相关的更新都已处理并应用到 DOM
3131

32-
The name `act` comes from the [Arrange-Act-Assert](https://wiki.c2.com/?ArrangeActAssert) pattern.
32+
名称 `act` 来源于 [Arrange-Act-Assert](https://wiki.c2.com/?ArrangeActAssert) 模式。
3333

3434
```js {2,4}
3535
it ('renders with button disabled', async () => {
@@ -42,25 +42,25 @@ it ('renders with button disabled', async () => {
4242

4343
<Note>
4444

45-
We recommend using `act` with `await` and an `async` function. Although the sync version works in many cases, it doesn't work in all cases and due to the way React schedules updates internally, it's difficult to predict when you can use the sync version.
45+
我们推荐将 `act` `await` `async` 函数配合使用。虽然同步版本在许多情况下有效,但由于 React 内部更新调度机制的原因,同步版本无法覆盖所有情况,且难以预测何时可以使用同步版本。
4646

47-
We will deprecate and remove the sync version in the future.
47+
我们将在未来弃用并移除同步版本。
4848

4949
</Note>
5050

51-
#### Parameters {/*parameters*/}
51+
#### 参数 {/*parameters*/}
5252

53-
* `async actFn`: An async function wrapping renders or interactions for components being tested. Any updates triggered within the `actFn`, are added to an internal act queue, which are then flushed together to process and apply any changes to the DOM. Since it is async, React will also run any code that crosses an async boundary, and flush any updates scheduled.
53+
* `async actFn`: 包裹被测组件渲染或交互的异步函数。在 `actFn` 内触发的所有更新都会被加入内部 act 队列,随后统一执行以处理变更并应用到 DOM。由于是异步操作,React 还会执行跨越异步边界的代码,并刷新所有计划中的更新。
5454

55-
#### Returns {/*returns*/}
55+
#### 返回值 {/*returns*/}
5656

57-
`act` does not return anything.
57+
`act` 不返回任何值。
5858

59-
## Usage {/*usage*/}
59+
## 用法 {/*usage*/}
6060

61-
When testing a component, you can use `act` to make assertions about its output.
61+
测试组件时,可以使用 `act` 对其输出进行断言。
6262

63-
For example, let’s say we have this `Counter` component, the usage examples below show how to test it:
63+
例如,假设我们有这个 `Counter` 组件,以下示例演示如何测试它:
6464

6565
```js
6666
function Counter() {
@@ -84,9 +84,9 @@ function Counter() {
8484
}
8585
```
8686

87-
### Rendering components in tests {/*rendering-components-in-tests*/}
87+
### 在测试中渲染组件 {/*rendering-components-in-tests*/}
8888

89-
To test the render output of a component, wrap the render inside `act()`:
89+
要测试组件的渲染输出,请将渲染操作包裹在 `act()` 中:
9090

9191
```js {10,12}
9292
import {act} from 'react';
@@ -97,7 +97,7 @@ it('can render and update a counter', async () => {
9797
container = document.createElement('div');
9898
document.body.appendChild(container);
9999

100-
//Render the component inside act().
100+
//act() 内渲染组件
101101
await act(() => {
102102
ReactDOMClient.createRoot(container).render(<Counter />);
103103
});
@@ -109,13 +109,13 @@ it('can render and update a counter', async () => {
109109
});
110110
```
111111

112-
Here, we create a container, append it to the document, and render the `Counter` component inside `act()`. This ensures that the component is rendered and its effects are applied before making assertions.
112+
这里我们创建容器元素并添加到文档中,然后在 `act()` 内渲染 `Counter`组件。这确保了在断言前组件已完成渲染且副作用已应用。
113113

114-
Using `act` ensures that all updates have been applied before we make assertions.
114+
使用 `act` 能保证所有更新在断言前已完成处理。
115115

116-
### Dispatching events in tests {/*dispatching-events-in-tests*/}
116+
### 在测试中触发事件 {/*dispatching-events-in-tests*/}
117117

118-
To test events, wrap the event dispatch inside `act()`:
118+
要测试事件,请将事件触发操作包裹在 `act()` 中:
119119

120120
```js {14,16}
121121
import {act} from 'react';
@@ -130,7 +130,7 @@ it.only('can render and update a counter', async () => {
130130
ReactDOMClient.createRoot(container).render(<Counter />);
131131
});
132132

133-
//Dispatch the event inside act().
133+
//act() 内触发事件
134134
await act(async () => {
135135
button.dispatchEvent(new MouseEvent('click', { bubbles: true }));
136136
});
@@ -142,36 +142,36 @@ it.only('can render and update a counter', async () => {
142142
});
143143
```
144144

145-
Here, we render the component with `act`, and then dispatch the event inside another `act()`. This ensures that all updates from the event are applied before making assertions.
145+
这里我们先用 `act`渲染组件,然后在另一个 `act()` 内触发事件。这确保了事件引起的所有更新在断言前已完成处理。
146146

147147
<Pitfall>
148148

149-
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/docs/react-testing-library/intro) to reduce the boilerplate code.
149+
只有将 DOM 容器添加到文档后,触发 DOM 事件才会生效。可以使用 [React Testing Library](https://testing-library.com/docs/react-testing-library/intro) 等库来减少样板代码。
150150

151151
</Pitfall>
152152

153-
## Troubleshooting {/*troubleshooting*/}
153+
## 疑难解答 {/*troubleshooting*/}
154154

155-
### I'm getting an error: "The current testing environment is not configured to support act"(...)" {/*error-the-current-testing-environment-is-not-configured-to-support-act*/}
155+
### 出现错误 "The current testing environment is not configured to support act"(...)" {/*error-the-current-testing-environment-is-not-configured-to-support-act*/}
156156

157-
Using `act` requires setting `global.IS_REACT_ACT_ENVIRONMENT=true` in your test environment. This is to ensure that `act` is only used in the correct environment.
157+
使用 `act` 需要在测试环境中设置 `global.IS_REACT_ACT_ENVIRONMENT=true` 。这是为了确保 `act` 仅在正确的环境中使用。
158158

159-
If you don't set the global, you will see an error like this:
159+
如果未设置该全局变量,将看到如下错误:
160160

161161
<ConsoleBlock level="error">
162162

163163
Warning: The current testing environment is not configured to support act(...)
164164

165165
</ConsoleBlock>
166166

167-
To fix, add this to your global setup file for React tests:
167+
解决方法:在 React 测试的全局设置文件中添加:
168168

169169
```js
170170
global.IS_REACT_ACT_ENVIRONMENT=true
171171
```
172172

173173
<Note>
174174

175-
In testing frameworks like [React Testing Library](https://testing-library.com/docs/react-testing-library/intro), `IS_REACT_ACT_ENVIRONMENT` is already set for you.
175+
[React Testing Library](https://testing-library.com/docs/react-testing-library/intro) 等测试框架中,`IS_REACT_ACT_ENVIRONMENT` 已自动配置。
176176

177177
</Note>

0 commit comments

Comments
 (0)