@@ -22,9 +22,11 @@ This API can be used to easily clear an editable element.
22
22
23
23
``` jsx
24
24
test (' clear' , async () => {
25
+ const user = userEvent .setup ()
26
+
25
27
render (< textarea defaultValue= " Hello, World!" / > )
26
28
27
- await userEvent .clear (screen .getByRole (' textbox' ))
29
+ await user .clear (screen .getByRole (' textbox' ))
28
30
29
31
expect (screen .getByRole (' textbox' )).toHaveValue (' ' )
30
32
})
@@ -37,12 +39,12 @@ be selected.
37
39
38
40
``` ts
39
41
selectOptions (
40
- element : Element ,
41
- values : HTMLElement | HTMLElement [] | string [] | string ,
42
+ element : Element ,
43
+ values : HTMLElement | HTMLElement [] | string [] | string ,
42
44
): Promise < void >
43
45
deselectOptions (
44
- element : Element ,
45
- values : HTMLElement | HTMLElement [] | string [] | string ,
46
+ element : Element ,
47
+ values : HTMLElement | HTMLElement [] | string [] | string ,
46
48
): Promise < void >
47
49
```
48
50
@@ -61,6 +63,8 @@ just provide the element. It also accepts an array of these.
61
63
62
64
``` jsx
63
65
test (' selectOptions' , async () => {
66
+ const user = userEvent .setup ()
67
+
64
68
render (
65
69
< select multiple>
66
70
< option value= " 1" > A < / option>
@@ -69,7 +73,7 @@ test('selectOptions', async () => {
69
73
< / select> ,
70
74
)
71
75
72
- await userEvent .selectOptions (screen .getByRole (' listbox' ), [' 1' , ' C' ])
76
+ await user .selectOptions (screen .getByRole (' listbox' ), [' 1' , ' C' ])
73
77
74
78
expect (screen .getByRole (' option' , {name: ' A' }).selected ).toBe (true )
75
79
expect (screen .getByRole (' option' , {name: ' B' }).selected ).toBe (false )
@@ -79,6 +83,8 @@ test('selectOptions', async () => {
79
83
80
84
``` jsx
81
85
test (' deselectOptions' , async () => {
86
+ const user = userEvent .setup ()
87
+
82
88
render (
83
89
< select multiple>
84
90
< option value= " 1" > A < / option>
@@ -89,7 +95,7 @@ test('deselectOptions', async () => {
89
95
< / select> ,
90
96
)
91
97
92
- await userEvent .deselectOptions (screen .getByRole (' listbox' ), ' 2' )
98
+ await user .deselectOptions (screen .getByRole (' listbox' ), ' 2' )
93
99
94
100
expect (screen .getByText (' B' ).selected ).toBe (false )
95
101
})
@@ -102,14 +108,14 @@ Note that this API triggers pointer events and is therefore subject to
102
108
103
109
``` ts
104
110
type (
105
- element : Element ,
106
- text : KeyboardInput ,
107
- options ?: {
108
- skipClick?: boolean
109
- skipAutoClose ?: boolean
110
- initialSelectionStart ?: number
111
- initialSelectionEnd ?: number
112
- }
111
+ element : Element ,
112
+ text : KeyboardInput ,
113
+ options ?: {
114
+ skipClick?: boolean
115
+ skipAutoClose ?: boolean
116
+ initialSelectionStart ?: number
117
+ initialSelectionEnd ?: number
118
+ }
113
119
): Promise < void >
114
120
```
115
121
@@ -129,10 +135,12 @@ Type into an input element.
129
135
130
136
``` jsx
131
137
test (' type into an input field' , async () => {
132
- render (< input defaultValue= " Hello," / > )
138
+ const user = userEvent .setup ()
139
+
140
+ render (< input defaultValue= " Hello," / > )
133
141
const input = screen .getByRole (' textbox' )
134
142
135
- await userEvent .type (input, ' World!' )
143
+ await user .type (input, ' World!' )
136
144
137
145
expect (input).toHaveValue (' Hello, World!' )
138
146
})
@@ -142,8 +150,8 @@ test('type into an input field', async () => {
142
150
143
151
``` ts
144
152
upload (
145
- element : HTMLElement ,
146
- fileOrFiles : File | File [],
153
+ element : HTMLElement ,
154
+ fileOrFiles : File | File [],
147
155
): Promise < void >
148
156
```
149
157
@@ -155,6 +163,8 @@ file upload dialog.
155
163
156
164
``` jsx
157
165
test (' upload file' , async () => {
166
+ const user = userEvent .setup ()
167
+
158
168
render (
159
169
< div>
160
170
< label htmlFor= " file-uploader" > Upload file: < / label>
@@ -164,14 +174,16 @@ test('upload file', async () => {
164
174
const file = new File ([' hello' ], ' hello.png' , {type: ' image/png' })
165
175
const input = screen .getByLabelText (/ upload file/ i )
166
176
167
- await userEvent .upload (input, file)
177
+ await user .upload (input, file)
168
178
169
179
expect (input .files [0 ]).toBe (file)
170
180
expect (input .files .item (0 )).toBe (file)
171
181
expect (input .files ).toHaveLength (1 )
172
182
})
173
183
174
184
test (' upload multiple files' , async () => {
185
+ const user = userEvent .setup ()
186
+
175
187
render (
176
188
< div>
177
189
< label htmlFor= " file-uploader" > Upload file: < / label>
@@ -184,7 +196,7 @@ test('upload multiple files', async () => {
184
196
]
185
197
const input = screen .getByLabelText (/ upload file/ i )
186
198
187
- await userEvent .upload (input, files)
199
+ await user .upload (input, files)
188
200
189
201
expect (input .files ).toHaveLength (2 )
190
202
expect (input .files [0 ]).toBe (files[0 ])
0 commit comments