@@ -47,12 +47,36 @@ describe('MenuTrigger', () => {
47
47
expect ( menuItemElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'true' ) ;
48
48
} ) ;
49
49
50
- it ( 'should set aria-haspopup to menu' , ( ) => {
50
+ it ( 'should set aria-haspopup based on whether a menu is assigned ' , ( ) => {
51
51
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 ) ;
52
57
} ) ;
53
58
54
- it ( 'should have a menu' , ( ) => {
59
+ it ( 'should have a menu based on whether a menu is assigned ' , ( ) => {
55
60
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 ( ) ;
56
80
} ) ;
57
81
} ) ;
58
82
@@ -469,6 +493,50 @@ describe('MenuTrigger', () => {
469
493
470
494
expect ( document . querySelector ( '.test-menu' ) ?. textContent ) . toBe ( 'Hello!' ) ;
471
495
} ) ;
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
+ } ) ;
472
540
} ) ;
473
541
474
542
@Component ( {
@@ -477,7 +545,10 @@ describe('MenuTrigger', () => {
477
545
<ng-template #noop><div cdkMenu></div></ng-template>
478
546
` ,
479
547
} )
480
- class TriggerForEmptyMenu { }
548
+ class TriggerForEmptyMenu {
549
+ @ViewChild ( CdkMenuTrigger ) trigger : CdkMenuTrigger ;
550
+ @ViewChild ( CdkMenuTrigger , { read : ElementRef } ) nativeTrigger : ElementRef ;
551
+ }
481
552
482
553
@Component ( {
483
554
template : `
@@ -602,3 +673,16 @@ class StandaloneTriggerWithInlineMenu {
602
673
class TriggerWithData {
603
674
menuData : unknown ;
604
675
}
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