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
Copy file name to clipboardExpand all lines: src/content/reference/react/act.md
+44-42Lines changed: 44 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,36 +2,36 @@
2
2
title: act
3
3
---
4
4
5
-
{/* FIXME:L10N*/}
6
-
7
5
<Intro>
8
6
9
-
`act`is a test helper to apply pending React updates before making assertions.
7
+
`act`est un utilitaire de test pour appliquer les mises à jour React en attente avant d'exécuter vos assertions.
10
8
11
9
```js
12
10
awaitact(async actFn)
13
11
```
14
12
15
13
</Intro>
16
14
17
-
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
+
Pour préparer un composant à vos assertions, enrobez le code qui en fait le rendu et exécute des mises à jour par un appel `await act()`. Votre test aura ainsi un comportement plus proche du véritable fonctionnement de React au sein d'un navigateur.
18
16
19
17
<Note>
20
-
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
+
19
+
Vous estimerez peut-être que le recours direct à `act()` est un peu verbeux. Pour éviter en partie ce code générique, vous pourriez utiliser une bibliothèque telle que [React Testing Library](https://testing-library.com/docs/react-testing-library/intro), dont les utilitaires s'appuient déjà sur `act()`.
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.
32
+
Lors de l'écriture de tests pour votre interface utilisateur (UI), des tâches telles que le rendu, les événements utilisateurs ou le chargement de données sont considérées comme des « unités » d'interaction avec l'interface. React fournit un utilitaire appelé`act()`pour garantir que les mises à jour liées à ces « unités » ont bien été traitées et appliquées au DOM avant que vous n'exécutiez vos assertions.
33
33
34
-
The name`act`comes from the[Arrange-Act-Assert](https://wiki.c2.com/?ArrangeActAssert) pattern.
34
+
Le nom`act`vient de l'approche[Arrange-Act-Assert](https://wiki.c2.com/?ArrangeActAssert).
35
35
36
36
```js {2,4}
37
37
it ('renders with button disabled', async () => {
@@ -44,25 +44,25 @@ it ('renders with button disabled', async () => {
44
44
45
45
<Note>
46
46
47
-
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.
47
+
Nous conseillons d'utiliser`act`avec un `await`dans une fonction `async`. Même si la version synchrone fonctionne dans de nombreux cas, elle ne couvre pas tous les scénarios ; en raison de la façon dont React planifie les mises à jour en interne, il est difficile de prédire si vous pouvez utiliser la version synchrone sans risque.
48
48
49
-
We will deprecate and remove the sync version in the future.
49
+
Nous déprécierons puis retirerons la version synchrone à l'avenir.
50
50
51
51
</Note>
52
52
53
-
#### Parameters {/*parameters*/}
53
+
#### Paramètres {/*parameters*/}
54
54
55
-
*`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.
55
+
*`async actFn` : une fonction asynchrone qui enrobe les rendus et interactions avec le composant que vous allez tester. Toute mise à jour déclenchée au sein de `actFn` est ajoutée à une file interne dédiée, qui est déroulée d'un bloc pour traitement, et ses résultats appliqués au DOM. Dans la mesure où elle est asynchrone, React pourra exécuter tout code asynchrone et appliquer les mises à jour qui en résultent.
56
56
57
-
#### Returns {/*returns*/}
57
+
#### Valeur renvoyée {/*returns*/}
58
58
59
-
`act`does not return anything.
59
+
`act`ne renvoie rien.
60
60
61
-
## Usage {/*usage*/}
61
+
## Utilisation {/*usage*/}
62
62
63
-
When testing a component, you can use`act`to make assertions about its output.
63
+
Lors de tests d'un composant, vous pouvez utiliser`act`pour exprimer des assertions sur le résultat.
64
64
65
-
For example, let’s say we have this `Counter`component, the usage examples below show how to test it:
65
+
Par exemple, disons que vous avez le composant `Counter`suivant, les exemples d'utilisation ci-dessous illustreront comment le tester :
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.
114
+
Dans ce code, nous créons un conteneur, l'ajoutons au document, puis faisons le rendu du composant `Counter`au sein d'un appel `act()`. Ça garantit que le composant a bien été rendu et ses Effets appliqués avant de passer aux assertions.
115
115
116
-
Using `act` ensures that all updates have been applied before we make assertions.
116
+
Le recours à `act()` garantit que toute mise à jour a bien été appliquée avant d'exécuter nos assertions.
117
117
118
-
### Dispatching events in tests {/*dispatching-events-in-tests*/}
118
+
### Déclencher des événements dans nos tests {/*dispatching-events-in-tests*/}
119
119
120
-
To test events, wrap the event dispatch inside `act()`:
120
+
Pour tester les événements, enrobez leur déclenchement dans `act()`:
121
121
122
122
```js {14,16}
123
123
import {act} from'react';
@@ -132,48 +132,50 @@ it.only('can render and update a counter', async () => {
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.
147
+
Dans ce code, nous faisons le rendu dans`act()`, puis nous déclenchons un événement dans un autre `act()`. Ça garantit que toutes les mises à jour qu'aura entraînées l'événement ont bien été appliquées avant de passer aux assertions.
148
148
149
149
<Pitfall>
150
150
151
-
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.
151
+
N'oubliez pas que le déclenchement d'événements DOM ne fonctionne que si le conteneur DOM est bien ajouté au document. Vous pouvez utiliser une bibliothèque telle que [React Testing Library](https://testing-library.com/docs/react-testing-library/intro)pour alléger ce code.
152
152
153
153
</Pitfall>
154
154
155
-
## Troubleshooting {/*troubleshooting*/}
155
+
## Dépannage {/*troubleshooting*/}
156
156
157
-
### 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*/}
157
+
### J'ai une erreur : "The current testing environment is not configured to support act"(...)" {/*error-the-current-testing-environment-is-not-configured-to-support-act*/}
158
158
159
-
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.
159
+
Le recours à `act`nécessite un réglage `global.IS_REACT_ACT_ENVIRONMENT=true`dans votre environnement de test. Ça permet de garantir que `act`n'est utilisé que dans un environnement adapté.
160
160
161
-
If you don't set the global, you will see an error like this:
161
+
Si vous n'avez pas défini cette globale, vous obtiendrez l'erreur suivante :
162
162
163
163
<ConsoleBlocklevel="error">
164
164
165
165
Warning: The current testing environment is not configured to support act(...)
166
166
167
167
</ConsoleBlock>
168
168
169
-
To fix, add this to your global setup file for React tests:
169
+
_(« Avertissement : l'environnement de test actuel n'est pas configuré pour prendre en charge act(...) », NdT)_
170
+
171
+
Pour la corriger, ajouter le code suivant dans le fichier de mise en place globale de vos tests React :
170
172
171
173
```js
172
174
global.IS_REACT_ACT_ENVIRONMENT=true
173
175
```
174
176
175
177
<Note>
176
178
177
-
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.
179
+
Les bibliothèques telles que[React Testing Library](https://testing-library.com/docs/react-testing-library/intro) s'assurent que `IS_REACT_ACT_ENVIRONMENT`est déjà défini pour vous.
0 commit comments