@@ -410,4 +410,71 @@ describe('combobox-nav', function () {
410410 } )
411411 } )
412412 } )
413+
414+ describe ( 'with missing IDs on options' , function ( ) {
415+ let input
416+ let list
417+ beforeEach ( function ( ) {
418+ document . body . innerHTML = `
419+ <input type="text">
420+ <ul role="listbox" id="list-id">
421+ <li role="option">Baymax</li>
422+ <li id="hubot" role="option">Hubot</li>
423+ <li role="option">R2-D2</li>
424+ </ul>
425+ `
426+ input = document . querySelector ( 'input' )
427+ list = document . querySelector ( 'ul' )
428+ } )
429+
430+ afterEach ( function ( ) {
431+ document . body . innerHTML = ''
432+ } )
433+
434+ it ( 'automatically adds and removes option IDs when needed for aria-activedescendant' , function ( ) {
435+ const combobox = new Combobox ( input , list )
436+ combobox . start ( )
437+ assert . equal ( input . getAttribute ( 'aria-expanded' ) , 'true' )
438+
439+ press ( input , 'ArrowDown' )
440+ assert . equal ( input . getAttribute ( 'aria-activedescendant' ) , 'list-id-selected' )
441+ assert . equal ( list . children [ 0 ] . getAttribute ( 'id' ) , 'list-id-selected' )
442+ assert . equal ( list . children [ 1 ] . getAttribute ( 'id' ) , 'hubot' )
443+ assert . equal ( list . children [ 2 ] . getAttribute ( 'id' ) , null )
444+
445+ press ( input , 'ArrowDown' )
446+ assert . equal ( input . getAttribute ( 'aria-activedescendant' ) , 'hubot' )
447+ assert . equal ( list . children [ 0 ] . getAttribute ( 'id' ) , null )
448+ assert . equal ( list . children [ 1 ] . getAttribute ( 'id' ) , 'hubot' )
449+ assert . equal ( list . children [ 2 ] . getAttribute ( 'id' ) , null )
450+
451+ press ( input , 'ArrowDown' )
452+ assert . equal ( input . getAttribute ( 'aria-activedescendant' ) , 'list-id-selected' )
453+ assert . equal ( list . children [ 0 ] . getAttribute ( 'id' ) , null )
454+ assert . equal ( list . children [ 1 ] . getAttribute ( 'id' ) , 'hubot' )
455+ assert . equal ( list . children [ 2 ] . getAttribute ( 'id' ) , 'list-id-selected' )
456+
457+ press ( input , 'Escape' )
458+ assert . equal ( input . getAttribute ( 'aria-activedescendant' ) , null )
459+ assert . equal ( list . children [ 0 ] . getAttribute ( 'id' ) , null )
460+ assert . equal ( list . children [ 1 ] . getAttribute ( 'id' ) , 'hubot' )
461+ assert . equal ( list . children [ 2 ] . getAttribute ( 'id' ) , null )
462+ } )
463+
464+ it ( 'avoids collisions with existing IDs when automatically adding option IDs' , function ( ) {
465+ const div = document . createElement ( 'div' )
466+ div . setAttribute ( 'id' , 'list-id-selected' )
467+ document . body . appendChild ( div )
468+
469+ const combobox = new Combobox ( input , list )
470+ combobox . start ( )
471+ assert . equal ( input . getAttribute ( 'aria-expanded' ) , 'true' )
472+
473+ press ( input , 'ArrowDown' )
474+ assert . equal ( input . getAttribute ( 'aria-activedescendant' ) , null )
475+ assert . equal ( list . children [ 0 ] . getAttribute ( 'id' ) , null )
476+ assert . equal ( list . children [ 1 ] . getAttribute ( 'id' ) , 'hubot' )
477+ assert . equal ( list . children [ 2 ] . getAttribute ( 'id' ) , null )
478+ } )
479+ } )
413480} )
0 commit comments