@@ -171,4 +171,60 @@ describe("userEvent.selectOptions", () => {
171
171
expect ( getByTestId ( "val2" ) . selected ) . toBe ( false ) ;
172
172
expect ( getByTestId ( "val3" ) . selected ) . toBe ( true ) ;
173
173
} ) ;
174
+
175
+ it ( "sets the selected prop on the selected OPTION using htmlFor" , ( ) => {
176
+ const onSubmit = jest . fn ( ) ;
177
+
178
+ const { getByTestId } = render (
179
+ < form onSubmit = { onSubmit } >
180
+ < label htmlFor = "select" > Example Select</ label >
181
+ < select id = "select" data-testid = "element" >
182
+ < option data-testid = "val1" value = "1" >
183
+ 1
184
+ </ option >
185
+ < option data-testid = "val2" value = "2" >
186
+ 2
187
+ </ option >
188
+ < option data-testid = "val3" value = "3" >
189
+ 3
190
+ </ option >
191
+ </ select >
192
+ </ form >
193
+ ) ;
194
+
195
+ userEvent . selectOptions ( getByTestId ( "element" ) , "2" ) ;
196
+
197
+ expect ( getByTestId ( "val1" ) . selected ) . toBe ( false ) ;
198
+ expect ( getByTestId ( "val2" ) . selected ) . toBe ( true ) ;
199
+ expect ( getByTestId ( "val3" ) . selected ) . toBe ( false ) ;
200
+ } ) ;
201
+
202
+ it ( "sets the selected prop on the selected OPTION using nested SELECT" , ( ) => {
203
+ const onSubmit = jest . fn ( ) ;
204
+
205
+ const { getByTestId } = render (
206
+ < form onSubmit = { onSubmit } >
207
+ < label >
208
+ Example Select
209
+ < select data-testid = "element" >
210
+ < option data-testid = "val1" value = "1" >
211
+ 1
212
+ </ option >
213
+ < option data-testid = "val2" value = "2" >
214
+ 2
215
+ </ option >
216
+ < option data-testid = "val3" value = "3" >
217
+ 3
218
+ </ option >
219
+ </ select >
220
+ </ label >
221
+ </ form >
222
+ ) ;
223
+
224
+ userEvent . selectOptions ( getByTestId ( "element" ) , "2" ) ;
225
+
226
+ expect ( getByTestId ( "val1" ) . selected ) . toBe ( false ) ;
227
+ expect ( getByTestId ( "val2" ) . selected ) . toBe ( true ) ;
228
+ expect ( getByTestId ( "val3" ) . selected ) . toBe ( false ) ;
229
+ } ) ;
174
230
} ) ;
0 commit comments