@@ -71,19 +71,19 @@ effects.
71
71
72
72
``` jsx
73
73
import React from " react" ;
74
- import { render } from " @testing-library/react" ;
74
+ import { render , screen } from " @testing-library/react" ;
75
75
import userEvent from " @testing-library/user-event" ;
76
76
77
77
test (" click" , () => {
78
- const { getByText , getByTestId } = render (
78
+ render (
79
79
< div>
80
80
< label htmlFor= " checkbox" > Check< / label>
81
81
< input id= " checkbox" data- testid= " checkbox" type= " checkbox" / >
82
82
< / div>
83
83
);
84
84
85
- userEvent .click (getByText (" Check" ));
86
- expect (getByTestId (" checkbox" )).toHaveAttribute (" checked" , true );
85
+ userEvent .click (screen . getByText (" Check" ));
86
+ expect (screen . getByTestId (" checkbox" )).toHaveAttribute (" checked" , true );
87
87
});
88
88
```
89
89
@@ -94,15 +94,13 @@ side effects.
94
94
95
95
``` jsx
96
96
import React from " react" ;
97
- import { render } from " @testing-library/react" ;
97
+ import { render , screen } from " @testing-library/react" ;
98
98
import userEvent from " @testing-library/user-event" ;
99
99
100
100
test (" double click" , () => {
101
101
const onChange = jest .fn ();
102
- const { getByTestId } = render (
103
- < input type= " checkbox" id= " checkbox" onChange= {onChange} / >
104
- );
105
- const checkbox = getByTestId (" checkbox" );
102
+ render (< input type= " checkbox" id= " checkbox" onChange= {onChange} / > );
103
+ const checkbox = screen .getByTestId (" checkbox" );
106
104
userEvent .dblClick (checkbox);
107
105
expect (onChange).toHaveBeenCalledTimes (2 );
108
106
expect (checkbox).toHaveProperty (" checked" , false );
@@ -115,15 +113,15 @@ Writes `text` inside an `<input>` or a `<textarea>`.
115
113
116
114
``` jsx
117
115
import React from " react" ;
118
- import { render } from " @testing-library/react" ;
116
+ import { render , screen } from " @testing-library/react" ;
119
117
import userEvent from " @testing-library/user-event" ;
120
118
121
- const { getByTestId } = test (" click " , () => {
119
+ test (" type " , async () => {
122
120
render (< textarea data- testid= " email" / > );
123
- });
124
121
125
- await userEvent .type (getByTestId (" email" ), " Hello, World!" );
126
- expect (getByTestId (" email" )).toHaveAttribute (" value" , " Hello, World!" );
122
+ await userEvent .type (screen .getByTestId (" email" ), " Hello, World!" );
123
+ expect (screen .getByTestId (" email" )).toHaveAttribute (" value" , " Hello, World!" );
124
+ });
127
125
```
128
126
129
127
If ` options.allAtOnce ` is ` true ` , ` type ` will write ` text ` at once rather than
@@ -133,35 +131,54 @@ one character at the time. `false` is the default value.
133
131
are typed. By default it's 0. You can use this option if your component has a
134
132
different behavior for fast or slow users.
135
133
134
+ ### clear(element)
135
+
136
+ Selects the text inside an ` <input> ` or ` <textarea> ` and deletes it.
137
+
138
+ ``` jsx
139
+ import React from " react" ;
140
+ import { render , screen } from " @testing-library/react" ;
141
+ import userEvent from " @testing-library/user-event" ;
142
+
143
+ test (" clear" , () => {
144
+ render (< textarea data- testid= " email" value= " Hello, World!" / > );
145
+
146
+ userEvent .clear (screen .getByTestId (" email" ));
147
+ expect (screen .getByTestId (" email" )).toHaveAttribute (" value" , " " );
148
+ });
149
+ ```
150
+
136
151
### ` selectOptions(element, values) `
137
152
138
153
Selects the specified option(s) of a ` <select> ` or a ` <select multiple> `
139
154
element.
140
155
141
156
``` jsx
142
157
import React from " react" ;
143
- import { render } from " @testing-library/react" ;
158
+ import { render , screen } from " @testing-library/react" ;
144
159
import userEvent from " @testing-library/user-event" ;
145
160
146
- const { getByTestId } = render (
147
- < select multiple data- testid= " select-multiple" >
148
- < option data- testid= " val1" value= " 1" >
149
- A
150
- < / option>
151
- < option data- testid= " val2" value= " 2" >
152
- B
153
- < / option>
154
- < option data- testid= " val3" value= " 3" >
155
- C
156
- < / option>
157
- < / select>
158
- );
159
-
160
- userEvent .selectOptions (getByTestId (" select-multiple" ), [" 1" , " 3" ]);
161
-
162
- expect (getByTestId (" val1" ).selected ).toBe (true );
163
- expect (getByTestId (" val2" ).selected ).toBe (false );
164
- expect (getByTestId (" val3" ).selected ).toBe (true );
161
+ test (" selectOptions" , () => {
162
+ render (
163
+ < select multiple data- testid= " select-multiple" >
164
+ < option data- testid= " val1" value= " 1" >
165
+ A
166
+ < / option>
167
+ < option data- testid= " val2" value= " 2" >
168
+ B
169
+ < / option>
170
+ < option data- testid= " val3" value= " 3" >
171
+ C
172
+ < / option>
173
+ < / select>
174
+ );
175
+
176
+ userEvent .selectOptions (screen .getByTestId (" select-multiple" ), [" 1" , " 3" ]);
177
+
178
+ expect (screen .getByTestId (" val1" ).selected ).toBe (true );
179
+ expect (screen .getByTestId (" val2" ).selected ).toBe (false );
180
+ expect (screen .getByTestId (" val3" ).selected ).toBe (true );
181
+ });
165
182
```
166
183
167
184
The ` values ` parameter can be either an array of values or a singular scalar
@@ -188,20 +205,20 @@ Options:
188
205
189
206
``` jsx
190
207
import React from " react" ;
191
- import { render } from " @testing-library/react" ;
208
+ import { render , screen } from " @testing-library/react" ;
192
209
import " @testing-library/jest-dom/extend-expect" ;
193
210
import userEvent from " @testing-library/user-event" ;
194
211
195
212
it (" should cycle elements in document tab order" , () => {
196
- const { getAllByTestId } = render (
213
+ render (
197
214
< div>
198
215
< input data- testid= " element" type= " checkbox" / >
199
216
< input data- testid= " element" type= " radio" / >
200
217
< input data- testid= " element" type= " number" / >
201
218
< / div>
202
219
);
203
220
204
- const [checkbox , radio , number ] = getAllByTestId (" element" );
221
+ const [checkbox , radio , number ] = screen . getAllByTestId (" element" );
205
222
206
223
expect (document .body ).toHaveFocus ();
207
224
0 commit comments