@@ -292,3 +292,144 @@ def test_element_without_collection(self):
292
292
result = pydom [f"#tests-terminal" ]
293
293
with pytest .raises (AttributeError ):
294
294
result .value = "some value"
295
+
296
+ def test_element_without_collection (self ):
297
+ result = pydom [f"#tests-terminal" ]
298
+ with pytest .raises (AttributeError ):
299
+ result .value = "some value"
300
+
301
+
302
+ class TestSelect :
303
+ def test_select_options_iter (self ):
304
+ select = pydom [f"#test_select_element_w_options" ][0 ]
305
+
306
+ for i , option in enumerate (select .options , 1 ):
307
+ assert option .value == f"{ i } "
308
+ assert option .html == f"Option { i } "
309
+
310
+ def test_select_options_len (self ):
311
+ select = pydom [f"#test_select_element_w_options" ][0 ]
312
+ assert len (select .options ) == 2
313
+
314
+ def test_select_options_clear (self ):
315
+ select = pydom [f"#test_select_element_to_clear" ][0 ]
316
+ assert len (select .options ) == 3
317
+
318
+ select .options .clear ()
319
+
320
+ assert len (select .options ) == 0
321
+
322
+ def test_select_element_add (self ):
323
+ # GIVEN the existing select element with no options
324
+ select = pydom [f"#test_select_element" ][0 ]
325
+
326
+ # EXPECT the select element to have no options
327
+ assert len (select .options ) == 0
328
+
329
+ # WHEN we add an option
330
+ select .options .add (value = "1" , html = "Option 1" )
331
+
332
+ # EXPECT the select element to have 1 option matching the attributes
333
+ # we passed in
334
+ assert len (select .options ) == 1
335
+ assert select .options [0 ].value == "1"
336
+ assert select .options [0 ].html == "Option 1"
337
+
338
+ # WHEN we add another option (blank this time)
339
+ select .options .add ()
340
+
341
+ # EXPECT the select element to have 2 options
342
+ assert len (select .options ) == 2
343
+
344
+ # EXPECT the last option to have an empty value and html
345
+ assert select .options [1 ].value == ""
346
+ assert select .options [1 ].html == ""
347
+
348
+ # WHEN we add another option (this time adding it in between the other 2
349
+ # options by using an integer index)
350
+ select .options .add (value = "2" , html = "Option 2" , before = 1 )
351
+
352
+ # EXPECT the select element to have 3 options
353
+ assert len (select .options ) == 3
354
+
355
+ # EXPECT the middle option to have the value and html we passed in
356
+ assert select .options [0 ].value == "1"
357
+ assert select .options [0 ].html == "Option 1"
358
+ assert select .options [1 ].value == "2"
359
+ assert select .options [1 ].html == "Option 2"
360
+ assert select .options [2 ].value == ""
361
+ assert select .options [2 ].html == ""
362
+
363
+ # WHEN we add another option (this time adding it in between the other 2
364
+ # options but using the option itself)
365
+ select .options .add (
366
+ value = "3" , html = "Option 3" , before = select .options [2 ], selected = True
367
+ )
368
+
369
+ # EXPECT the select element to have 3 options
370
+ assert len (select .options ) == 4
371
+
372
+ # EXPECT the middle option to have the value and html we passed in
373
+ assert select .options [0 ].value == "1"
374
+ assert select .options [0 ].html == "Option 1"
375
+ assert select .options [0 ].selected == select .options [0 ]._js .selected == False
376
+ assert select .options [1 ].value == "2"
377
+ assert select .options [1 ].html == "Option 2"
378
+ assert select .options [2 ].value == "3"
379
+ assert select .options [2 ].html == "Option 3"
380
+ assert select .options [2 ].selected == select .options [2 ]._js .selected == True
381
+ assert select .options [3 ].value == ""
382
+ assert select .options [3 ].html == ""
383
+
384
+ # WHEN we add another option (this time adding it in between the other 2
385
+ # options but using the JS element of the option itself)
386
+ select .options .add (value = "2a" , html = "Option 2a" , before = select .options [2 ]._js )
387
+
388
+ # EXPECT the select element to have 3 options
389
+ assert len (select .options ) == 5
390
+
391
+ # EXPECT the middle option to have the value and html we passed in
392
+ assert select .options [0 ].value == "1"
393
+ assert select .options [0 ].html == "Option 1"
394
+ assert select .options [1 ].value == "2"
395
+ assert select .options [1 ].html == "Option 2"
396
+ assert select .options [2 ].value == "2a"
397
+ assert select .options [2 ].html == "Option 2a"
398
+ assert select .options [3 ].value == "3"
399
+ assert select .options [3 ].html == "Option 3"
400
+ assert select .options [4 ].value == ""
401
+ assert select .options [4 ].html == ""
402
+
403
+ def test_select_options_remove (self ):
404
+ # GIVEN the existing select element with 3 options
405
+ select = pydom [f"#test_select_element_to_remove" ][0 ]
406
+
407
+ # EXPECT the select element to have 3 options
408
+ assert len (select .options ) == 4
409
+ # EXPECT the options to have the values originally set
410
+ assert select .options [0 ].value == "1"
411
+ assert select .options [1 ].value == "2"
412
+ assert select .options [2 ].value == "3"
413
+ assert select .options [3 ].value == "4"
414
+
415
+ # WHEN we remove the second option (index starts at 0)
416
+ select .options .remove (1 )
417
+
418
+ # EXPECT the select element to have 2 options
419
+ assert len (select .options ) == 3
420
+ # EXPECT the options to have the values originally set but the second
421
+ assert select .options [0 ].value == "1"
422
+ assert select .options [1 ].value == "3"
423
+ assert select .options [2 ].value == "4"
424
+
425
+ def test_select_get_selected_option (self ):
426
+ # GIVEN the existing select element with one selected option
427
+ select = pydom [f"#test_select_element_w_options" ][0 ]
428
+
429
+ # WHEN we get the selected option
430
+ selected_option = select .options .selected
431
+
432
+ # EXPECT the selected option to be correct
433
+ assert selected_option .value == "2"
434
+ assert selected_option .html == "Option 2"
435
+ assert selected_option .selected == selected_option ._js .selected == True
0 commit comments