@@ -47,12 +47,36 @@ describe('MenuTrigger', () => {
4747 expect ( menuItemElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'true' ) ;
4848 } ) ;
4949
50- it ( 'should set aria-haspopup to menu' , ( ) => {
50+ it ( 'should set aria-haspopup based on whether a menu is assigned ' , ( ) => {
5151 expect ( menuItemElement . getAttribute ( 'aria-haspopup' ) ) . toEqual ( 'menu' ) ;
52+
53+ fixture . componentInstance . trigger . menuTemplateRef = null ;
54+ fixture . detectChanges ( ) ;
55+
56+ expect ( menuItemElement . hasAttribute ( 'aria-haspopup' ) ) . toBe ( false ) ;
5257 } ) ;
5358
54- it ( 'should have a menu' , ( ) => {
59+ it ( 'should have a menu based on whether a menu is assigned ' , ( ) => {
5560 expect ( menuItem . hasMenu ) . toBeTrue ( ) ;
61+
62+ fixture . componentInstance . trigger . menuTemplateRef = null ;
63+ fixture . detectChanges ( ) ;
64+
65+ expect ( menuItem . hasMenu ) . toBeFalse ( ) ;
66+ } ) ;
67+
68+ it ( 'should set aria-controls based on whether a menu is assigned' , ( ) => {
69+ expect ( menuItemElement . hasAttribute ( 'aria-controls' ) ) . toBeFalse ( ) ;
70+ } ) ;
71+
72+ it ( 'should set aria-expanded based on whether a menu is assigned' , ( ) => {
73+ expect ( menuItemElement . hasAttribute ( 'aria-expanded' ) ) . toBeTrue ( ) ;
74+ expect ( menuItemElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' ) ;
75+
76+ fixture . componentInstance . trigger . menuTemplateRef = null ;
77+ fixture . detectChanges ( ) ;
78+
79+ expect ( menuItemElement . hasAttribute ( 'aria-expanded' ) ) . toBeFalse ( ) ;
5680 } ) ;
5781 } ) ;
5882
@@ -469,6 +493,50 @@ describe('MenuTrigger', () => {
469493
470494 expect ( document . querySelector ( '.test-menu' ) ?. textContent ) . toBe ( 'Hello!' ) ;
471495 } ) ;
496+
497+ describe ( 'null triggerFor' , ( ) => {
498+ let fixture : ComponentFixture < TriggerWithNullValue > ;
499+
500+ let nativeTrigger : HTMLElement ;
501+
502+ beforeEach ( waitForAsync ( ( ) => {
503+ TestBed . configureTestingModule ( {
504+ imports : [ CdkMenuModule ] ,
505+ declarations : [ TriggerWithNullValue ] ,
506+ } ) . compileComponents ( ) ;
507+ } ) ) ;
508+
509+ beforeEach ( ( ) => {
510+ fixture = TestBed . createComponent ( TriggerWithNullValue ) ;
511+ nativeTrigger = fixture . componentInstance . nativeTrigger . nativeElement ;
512+ } ) ;
513+
514+ it ( 'should not set aria-haspopup' , ( ) => {
515+ expect ( nativeTrigger . hasAttribute ( 'aria-haspopup' ) ) . toBeFalse ( ) ;
516+ } ) ;
517+
518+ it ( 'should not set aria-controls' , ( ) => {
519+ expect ( nativeTrigger . hasAttribute ( 'aria-controls' ) ) . toBeFalse ( ) ;
520+ } ) ;
521+
522+ it ( 'should not toggle the menu on trigger' , ( ) => {
523+ expect ( fixture . componentInstance . trigger . isOpen ( ) ) . toBeFalse ( ) ;
524+
525+ nativeTrigger . click ( ) ;
526+ fixture . detectChanges ( ) ;
527+
528+ expect ( fixture . componentInstance . trigger . isOpen ( ) ) . toBeFalse ( ) ;
529+ } ) ;
530+
531+ it ( 'should not toggle the menu on keyboard events' , ( ) => {
532+ expect ( fixture . componentInstance . trigger . isOpen ( ) ) . toBeFalse ( ) ;
533+
534+ dispatchKeyboardEvent ( nativeTrigger , 'keydown' , SPACE ) ;
535+ fixture . detectChanges ( ) ;
536+
537+ expect ( fixture . componentInstance . trigger . isOpen ( ) ) . toBeFalse ( ) ;
538+ } ) ;
539+ } ) ;
472540} ) ;
473541
474542@Component ( {
@@ -477,7 +545,10 @@ describe('MenuTrigger', () => {
477545 <ng-template #noop><div cdkMenu></div></ng-template>
478546 ` ,
479547} )
480- class TriggerForEmptyMenu { }
548+ class TriggerForEmptyMenu {
549+ @ViewChild ( CdkMenuTrigger ) trigger : CdkMenuTrigger ;
550+ @ViewChild ( CdkMenuTrigger , { read : ElementRef } ) nativeTrigger : ElementRef ;
551+ }
481552
482553@Component ( {
483554 template : `
@@ -602,3 +673,16 @@ class StandaloneTriggerWithInlineMenu {
602673class TriggerWithData {
603674 menuData : unknown ;
604675}
676+
677+ @Component ( {
678+ template : `
679+ <button [cdkMenuTriggerFor]="null">First</button>
680+ ` ,
681+ } )
682+ class TriggerWithNullValue {
683+ @ViewChild ( CdkMenuTrigger , { static : true } )
684+ trigger : CdkMenuTrigger ;
685+
686+ @ViewChild ( CdkMenuTrigger , { static : true , read : ElementRef } )
687+ nativeTrigger : ElementRef < HTMLElement > ;
688+ }
0 commit comments