@@ -5,13 +5,14 @@ async function setup(page: Page, selector: string) {
5
5
6
6
const driver = createTestDriver ( page . getByTestId ( selector ) ) ;
7
7
8
- const { getListbox, getTrigger, getOptions } = driver ;
8
+ const { getListbox, getTrigger, getOptions, getValue } = driver ;
9
9
10
10
return {
11
11
driver,
12
12
getListbox,
13
13
getTrigger,
14
14
getOptions,
15
+ getValue,
15
16
} ;
16
17
}
17
18
@@ -40,6 +41,56 @@ test.describe('Mouse Behavior', () => {
40
41
await expect ( getListbox ( ) ) . toBeHidden ( ) ;
41
42
await expect ( getTrigger ( ) ) . toHaveAttribute ( 'aria-expanded' , 'false' ) ;
42
43
} ) ;
44
+
45
+ test ( `GIVEN a hero select with an open listbox
46
+ WHEN an option is clicked
47
+ THEN close the listbox AND aria-expanded should be false` , async ( { page } ) => {
48
+ const { getTrigger, getListbox, getOptions } = await setup ( page , 'select-hero-test' ) ;
49
+
50
+ await getTrigger ( ) . click ( ) ;
51
+ // should be open initially
52
+ await expect ( getListbox ( ) ) . toBeVisible ( ) ;
53
+
54
+ const options = await getOptions ( ) ;
55
+ options [ 0 ] . click ( ) ;
56
+
57
+ await expect ( getListbox ( ) ) . toBeHidden ( ) ;
58
+ await expect ( getTrigger ( ) ) . toHaveAttribute ( 'aria-expanded' , 'false' ) ;
59
+ } ) ;
60
+
61
+ test ( `GIVEN a hero select with an open listbox
62
+ WHEN the 2nd option is clicked
63
+ THEN the 2nd option should have aria-selected` , async ( { page } ) => {
64
+ const { getTrigger, getListbox, getOptions } = await setup ( page , 'select-hero-test' ) ;
65
+
66
+ await getTrigger ( ) . click ( ) ;
67
+ // should be open initially
68
+ await expect ( getListbox ( ) ) . toBeVisible ( ) ;
69
+
70
+ const options = await getOptions ( ) ;
71
+ options [ 1 ] . click ( ) ;
72
+
73
+ await expect ( options [ 1 ] ) . toHaveAttribute ( 'aria-selected' , 'true' ) ;
74
+ } ) ;
75
+
76
+ test ( `GIVEN a hero select with an open listbox
77
+ WHEN the 3rd option is clicked
78
+ THEN the 3rd option should be the selected value` , async ( { page } ) => {
79
+ const { getTrigger, getListbox, getOptions, getValue } = await setup (
80
+ page ,
81
+ 'select-hero-test' ,
82
+ ) ;
83
+
84
+ await getTrigger ( ) . click ( ) ;
85
+ // should be open initially
86
+ await expect ( getListbox ( ) ) . toBeVisible ( ) ;
87
+
88
+ const options = await getOptions ( ) ;
89
+ await options [ 2 ] . click ( ) ;
90
+
91
+ await expect ( options [ 2 ] ) . toHaveAttribute ( 'aria-selected' , 'true' ) ;
92
+ expect ( options [ 2 ] . textContent ( ) ) . toEqual ( getValue ( ) ) ;
93
+ } ) ;
43
94
} ) ;
44
95
45
96
test . describe ( 'Keyboard Behavior' , ( ) => {
@@ -274,7 +325,6 @@ test.describe('Keyboard Behavior', () => {
274
325
// should be open initially
275
326
await expect ( getListbox ( ) ) . toBeVisible ( ) ;
276
327
277
- // third option highlighted
278
328
const options = await getOptions ( ) ;
279
329
await expect ( options [ 0 ] ) . toHaveAttribute ( 'data-highlighted' ) ;
280
330
await getTrigger ( ) . press ( 'ArrowDown' ) ;
@@ -284,10 +334,9 @@ test.describe('Keyboard Behavior', () => {
284
334
await expect ( options [ 1 ] ) . toHaveAttribute ( 'data-highlighted' ) ;
285
335
} ) ;
286
336
287
- test ( `GIVEN an open hero select
288
- WHEN the listbox is closed with a chosen option
289
- AND the down arrow key is pressed
290
- THEN the data-highlighted option should not change on re-open` , async ( {
337
+ test ( `GIVEN a hero select with a chosen option
338
+ AND the down arrow key is pressed
339
+ THEN the data-highlighted option should not change on re-open` , async ( {
291
340
page,
292
341
} ) => {
293
342
const { getTrigger, getListbox, getOptions } = await setup (
@@ -311,9 +360,83 @@ test.describe('Keyboard Behavior', () => {
311
360
await expect ( options [ 1 ] ) . toHaveAttribute ( 'data-highlighted' ) ;
312
361
} ) ;
313
362
} ) ;
363
+
364
+ test . describe ( 'selecting options' , ( ) => {
365
+ test ( `GIVEN an open hero select
366
+ WHEN an option has data-highlighted
367
+ AND the Enter key is pressed
368
+ THEN the listbox should be closed and aria-expanded false` , async ( {
369
+ page,
370
+ } ) => {
371
+ const { getTrigger, getListbox, getOptions } = await setup (
372
+ page ,
373
+ 'select-hero-test' ,
374
+ ) ;
375
+
376
+ await getTrigger ( ) . focus ( ) ;
377
+ await getTrigger ( ) . press ( 'Enter' ) ;
378
+ // should be open initially
379
+ await expect ( getListbox ( ) ) . toBeVisible ( ) ;
380
+
381
+ const options = await getOptions ( ) ;
382
+ await expect ( options [ 0 ] ) . toHaveAttribute ( 'data-highlighted' ) ;
383
+ await getTrigger ( ) . press ( 'Enter' ) ;
384
+ await expect ( getListbox ( ) ) . toBeHidden ( ) ;
385
+ await expect ( getTrigger ( ) ) . toHaveAttribute ( 'aria-expanded' , 'false' ) ;
386
+ } ) ;
387
+
388
+ test ( `GIVEN an open hero select
389
+ WHEN an option has data-highlighted
390
+ AND the Enter key is pressed
391
+ THEN option value should be the selected value
392
+ AND should have an aria-selected of true` , async ( { page } ) => {
393
+ const { getTrigger, getListbox, getOptions, getValue } = await setup (
394
+ page ,
395
+ 'select-hero-test' ,
396
+ ) ;
397
+
398
+ await getTrigger ( ) . focus ( ) ;
399
+ await getTrigger ( ) . press ( 'Enter' ) ;
400
+ // should be open initially
401
+ await expect ( getListbox ( ) ) . toBeVisible ( ) ;
402
+
403
+ const options = await getOptions ( ) ;
404
+ await expect ( options [ 0 ] ) . toHaveAttribute ( 'data-highlighted' ) ;
405
+ const optStr = await options [ 0 ] . textContent ( ) ;
406
+ await getTrigger ( ) . press ( 'Enter' ) ;
407
+
408
+ console . log ( optStr ) ;
409
+ console . log ( await getValue ( ) ) ;
410
+ expect ( optStr ) . toEqual ( await getValue ( ) ) ;
411
+ } ) ;
412
+
413
+ test ( `GIVEN an open hero select
414
+ WHEN an option has data-highlighted
415
+ AND the Space key is pressed
416
+ THEN the listbox should be closed and aria-expanded false` , async ( {
417
+ page,
418
+ } ) => {
419
+ const { getTrigger, getListbox, getOptions } = await setup (
420
+ page ,
421
+ 'select-hero-test' ,
422
+ ) ;
423
+
424
+ await getTrigger ( ) . focus ( ) ;
425
+ await getTrigger ( ) . press ( 'Space' ) ;
426
+ // should be open initially
427
+ await expect ( getListbox ( ) ) . toBeVisible ( ) ;
428
+
429
+ // second option highlighted
430
+ const options = await getOptions ( ) ;
431
+ await expect ( options [ 0 ] ) . toHaveAttribute ( 'data-highlighted' ) ;
432
+ await getTrigger ( ) . press ( 'Space' ) ;
433
+ await expect ( getListbox ( ) ) . toBeHidden ( ) ;
434
+ await expect ( getTrigger ( ) ) . toHaveAttribute ( 'aria-expanded' , 'false' ) ;
435
+ } ) ;
436
+ } ) ;
314
437
} ) ;
315
438
316
- test . describe ( 'disabled ' , ( ) => {
439
+ test . describe ( 'Disabled ' , ( ) => {
317
440
test ( `GIVEN an open disabled select with the first option disabled
318
441
WHEN clicking the disabled option
319
442
It should have aria-disabled` , async ( { page } ) => {
0 commit comments