@@ -468,4 +468,186 @@ describe('dropdownToggle', function() {
468468 expect ( elm1 . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
469469 } ) ;
470470 } ) ;
471+
472+ describe ( '`keyboard-nav` option' , function ( ) {
473+ function dropdown ( ) {
474+ return $compile ( '<li dropdown keyboard-nav><a href dropdown-toggle></a><ul><li><a href>Hello</a></li><li><a href>Hello Again</a></li></ul></li>' ) ( $rootScope ) ;
475+ }
476+ beforeEach ( function ( ) {
477+ element = dropdown ( ) ;
478+ } ) ;
479+
480+ it ( 'should focus first list element when down arrow pressed' , function ( ) {
481+ $document . find ( 'body' ) . append ( element ) ;
482+ clickDropdownToggle ( ) ;
483+ triggerKeyDown ( $document , 40 ) ;
484+
485+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
486+ var optionEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 0 ) ;
487+ expect ( isFocused ( optionEl ) ) . toBe ( true ) ;
488+ } ) ;
489+
490+ it ( 'should not focus first list element when down arrow pressed if closed' , function ( ) {
491+ $document . find ( 'body' ) . append ( element ) ;
492+ triggerKeyDown ( $document , 40 ) ;
493+
494+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( false ) ;
495+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 0 ) ;
496+ expect ( isFocused ( focusEl ) ) . toBe ( false ) ;
497+ } ) ;
498+
499+ it ( 'should focus second list element when down arrow pressed twice' , function ( ) {
500+ $document . find ( 'body' ) . append ( element ) ;
501+ clickDropdownToggle ( ) ;
502+ triggerKeyDown ( $document , 40 ) ;
503+ triggerKeyDown ( $document , 40 ) ;
504+
505+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
506+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 1 ) ;
507+ expect ( isFocused ( focusEl ) ) . toBe ( true ) ;
508+ } ) ;
509+ } ) ;
510+
511+ describe ( '`keyboard-nav` option' , function ( ) {
512+ function dropdown ( ) {
513+ return $compile ( '<li dropdown keyboard-nav><a href dropdown-toggle></a><ul><li><a href>Hello</a></li><li><a href>Hello Again</a></li></ul></li>' ) ( $rootScope ) ;
514+ }
515+ beforeEach ( function ( ) {
516+ element = dropdown ( ) ;
517+ } ) ;
518+
519+ it ( 'should focus first list element when down arrow pressed' , function ( ) {
520+ $document . find ( 'body' ) . append ( element ) ;
521+ clickDropdownToggle ( ) ;
522+ triggerKeyDown ( $document , 40 ) ;
523+
524+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
525+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 0 ) ;
526+ expect ( isFocused ( focusEl ) ) . toBe ( true ) ;
527+ } ) ;
528+
529+ it ( 'should not focus first list element when up arrow pressed after dropdown toggled' , function ( ) {
530+ $document . find ( 'body' ) . append ( element ) ;
531+ clickDropdownToggle ( ) ;
532+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
533+
534+ triggerKeyDown ( $document , 38 ) ;
535+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 0 ) ;
536+ expect ( isFocused ( focusEl ) ) . toBe ( false ) ;
537+ } ) ;
538+
539+ it ( 'should not focus any list element when down arrow pressed if closed' , function ( ) {
540+ $document . find ( 'body' ) . append ( element ) ;
541+ triggerKeyDown ( $document , 40 ) ;
542+
543+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( false ) ;
544+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) ;
545+ expect ( isFocused ( focusEl [ 0 ] ) ) . toBe ( false ) ;
546+ expect ( isFocused ( focusEl [ 1 ] ) ) . toBe ( false ) ;
547+ } ) ;
548+
549+ it ( 'should not change focus when other keys are pressed' , function ( ) {
550+ $document . find ( 'body' ) . append ( element ) ;
551+ clickDropdownToggle ( ) ;
552+ triggerKeyDown ( $document , 37 ) ;
553+
554+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
555+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) ;
556+ expect ( isFocused ( focusEl [ 0 ] ) ) . toBe ( false ) ;
557+ expect ( isFocused ( focusEl [ 1 ] ) ) . toBe ( false ) ;
558+ } ) ;
559+
560+ it ( 'should focus second list element when down arrow pressed twice' , function ( ) {
561+ $document . find ( 'body' ) . append ( element ) ;
562+ clickDropdownToggle ( ) ;
563+ triggerKeyDown ( $document , 40 ) ;
564+ triggerKeyDown ( $document , 40 ) ;
565+
566+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
567+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 1 ) ;
568+ expect ( isFocused ( focusEl ) ) . toBe ( true ) ;
569+ } ) ;
570+
571+ it ( 'should focus first list element when down arrow pressed 2x and up pressed 1x' , function ( ) {
572+ $document . find ( 'body' ) . append ( element ) ;
573+ clickDropdownToggle ( ) ;
574+ triggerKeyDown ( $document , 40 ) ;
575+ triggerKeyDown ( $document , 40 ) ;
576+
577+ triggerKeyDown ( $document , 38 ) ;
578+
579+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
580+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 0 ) ;
581+ expect ( isFocused ( focusEl ) ) . toBe ( true ) ;
582+ } ) ;
583+
584+ it ( 'should stay focused on final list element if down pressed at list end' , function ( ) {
585+ $document . find ( 'body' ) . append ( element ) ;
586+ clickDropdownToggle ( ) ;
587+ triggerKeyDown ( $document , 40 ) ;
588+ triggerKeyDown ( $document , 40 ) ;
589+
590+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
591+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 1 ) ;
592+ expect ( isFocused ( focusEl ) ) . toBe ( true ) ;
593+
594+ triggerKeyDown ( $document , 40 ) ;
595+ expect ( isFocused ( focusEl ) ) . toBe ( true ) ;
596+ } ) ;
597+
598+ it ( 'should close if esc is pressed while focused' , function ( ) {
599+ element = dropdown ( 'disabled' ) ;
600+ $document . find ( 'body' ) . append ( element ) ;
601+ clickDropdownToggle ( ) ;
602+
603+ triggerKeyDown ( $document , 40 ) ;
604+
605+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
606+ var focusEl = element . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 0 ) ;
607+ expect ( isFocused ( focusEl ) ) . toBe ( true ) ;
608+
609+ triggerKeyDown ( $document , 27 ) ;
610+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( false ) ;
611+ } ) ;
612+ } ) ;
613+
614+ describe ( '`keyboard-nav` option with `dropdown-append-to-body` option' , function ( ) {
615+ function dropdown ( ) {
616+ return $compile ( '<li dropdown dropdown-append-to-body keyboard-nav><a href dropdown-toggle></a><ul class="dropdown-menu" id="dropdown-menu"><li><a href>Hello On Body</a></li><li><a href>Hello Again</a></li></ul></li>' ) ( $rootScope ) ;
617+ }
618+
619+ beforeEach ( function ( ) {
620+ element = dropdown ( ) ;
621+ } ) ;
622+
623+ it ( 'should focus first list element when down arrow pressed' , function ( ) {
624+ clickDropdownToggle ( ) ;
625+
626+ triggerKeyDown ( $document , 40 ) ;
627+
628+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
629+ var focusEl = $document . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) ;
630+ expect ( isFocused ( focusEl ) ) . toBe ( true ) ;
631+ } ) ;
632+
633+ it ( 'should not focus first list element when down arrow pressed if closed' , function ( ) {
634+ triggerKeyDown ( $document , 40 ) ;
635+
636+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( false ) ;
637+ var focusEl = $document . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) ;
638+ expect ( isFocused ( focusEl ) ) . toBe ( false ) ;
639+ } ) ;
640+
641+ it ( 'should focus second list element when down arrow pressed twice' , function ( ) {
642+ clickDropdownToggle ( ) ;
643+ triggerKeyDown ( $document , 40 ) ;
644+ triggerKeyDown ( $document , 40 ) ;
645+
646+ expect ( element . hasClass ( dropdownConfig . openClass ) ) . toBe ( true ) ;
647+ var elem1 = $document . find ( 'ul' ) ;
648+ var elem2 = elem1 . find ( 'a' ) ;
649+ var focusEl = $document . find ( 'ul' ) . eq ( 0 ) . find ( 'a' ) . eq ( 1 ) ;
650+ expect ( isFocused ( focusEl ) ) . toBe ( true ) ;
651+ } ) ;
652+ } ) ;
471653} ) ;
0 commit comments