@@ -817,6 +817,37 @@ describe('MDC-based MatMenu', () => {
817
817
flush ( ) ;
818
818
} ) ) ;
819
819
820
+ it ( 'should sync the focus order when an item is focused programmatically' , fakeAsync ( ( ) => {
821
+ const fixture = createComponent ( SimpleMenuWithRepeater ) ;
822
+
823
+ // Add some more items to work with.
824
+ for ( let i = 0 ; i < 5 ; i ++ ) {
825
+ fixture . componentInstance . items . push ( { label : `Extra ${ i } ` , disabled : false } ) ;
826
+ }
827
+
828
+ fixture . detectChanges ( ) ;
829
+ fixture . componentInstance . trigger . openMenu ( ) ;
830
+ fixture . detectChanges ( ) ;
831
+ tick ( 500 ) ;
832
+
833
+ const menuPanel = document . querySelector ( '.mat-mdc-menu-panel' ) ! ;
834
+ const items = menuPanel . querySelectorAll ( '.mat-mdc-menu-panel [mat-menu-item]' ) ;
835
+
836
+ expect ( document . activeElement ) . toBe ( items [ 0 ] , 'Expected first item to be focused on open' ) ;
837
+
838
+ fixture . componentInstance . itemInstances . toArray ( ) [ 3 ] . focus ( ) ;
839
+ fixture . detectChanges ( ) ;
840
+
841
+ expect ( document . activeElement ) . toBe ( items [ 3 ] , 'Expected fourth item to be focused' ) ;
842
+
843
+ dispatchKeyboardEvent ( menuPanel , 'keydown' , DOWN_ARROW ) ;
844
+ fixture . detectChanges ( ) ;
845
+ tick ( ) ;
846
+
847
+ expect ( document . activeElement ) . toBe ( items [ 4 ] , 'Expected fifth item to be focused' ) ;
848
+ flush ( ) ;
849
+ } ) ) ;
850
+
820
851
it ( 'should focus the menu panel if all items are disabled' , fakeAsync ( ( ) => {
821
852
const fixture = createComponent ( SimpleMenuWithRepeater , [ ] , [ FakeIcon ] ) ;
822
853
fixture . componentInstance . items . forEach ( item => item . disabled = true ) ;
@@ -2454,6 +2485,7 @@ class MenuWithCheckboxItems {
2454
2485
class SimpleMenuWithRepeater {
2455
2486
@ViewChild ( MatMenuTrigger ) trigger : MatMenuTrigger ;
2456
2487
@ViewChild ( MatMenu ) menu : MatMenu ;
2488
+ @ViewChildren ( MatMenuItem ) itemInstances : QueryList < MatMenuItem > ;
2457
2489
items = [ { label : 'Pizza' , disabled : false } , { label : 'Pasta' , disabled : false } ] ;
2458
2490
}
2459
2491
0 commit comments