Skip to content

Commit 7e84f91

Browse files
committed
Translate "act" API
1 parent 1dd60f2 commit 7e84f91

File tree

1 file changed

+29
-29
lines changed
  • src/content/reference/react

1 file changed

+29
-29
lines changed

src/content/reference/react/act.md

Lines changed: 29 additions & 29 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 テストを書く際、レンダー、ユーザイベント、データフェッチなどのタスクは、ユーザインターフェースにおける「操作単位 (unit of interaction)」と捉えることができます。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';
@@ -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+
ここではコンテナを作成し、それをドキュメントに追加し、`Counter` コンポーネントを `act()` 内でレンダーします。これにより、コンポーネントのレンダーとエフェクトの適用が終わってからアサーションが行えることが保証されます。
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';
@@ -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)