9
9
Output ,
10
10
TemplateRef ,
11
11
ViewChild ,
12
+ ViewChildren ,
13
+ QueryList ,
12
14
} from '@angular/core' ;
13
15
import { Direction , Directionality } from '@angular/cdk/bidi' ;
14
16
import { OverlayContainer } from '@angular/cdk/overlay' ;
@@ -21,6 +23,7 @@ import {
21
23
MatMenuTrigger ,
22
24
MenuPositionX ,
23
25
MenuPositionY ,
26
+ MatMenuItem ,
24
27
} from './index' ;
25
28
import { MENU_PANEL_TOP_PADDING } from './menu-trigger' ;
26
29
import { extendObject } from '@angular/material/core' ;
@@ -49,7 +52,8 @@ describe('MatMenu', () => {
49
52
CustomMenu ,
50
53
NestedMenu ,
51
54
NestedMenuCustomElevation ,
52
- NestedMenuRepeater
55
+ NestedMenuRepeater ,
56
+ FakeIcon
53
57
] ,
54
58
providers : [
55
59
{ provide : OverlayContainer , useFactory : ( ) => {
@@ -175,6 +179,18 @@ describe('MatMenu', () => {
175
179
expect ( fixture . destroy . bind ( fixture ) ) . not . toThrow ( ) ;
176
180
} ) ;
177
181
182
+ it ( 'should be able to extract the menu item text' , ( ) => {
183
+ const fixture = TestBed . createComponent ( SimpleMenu ) ;
184
+ fixture . detectChanges ( ) ;
185
+ expect ( fixture . componentInstance . items . first . getLabel ( ) ) . toBe ( 'Item' ) ;
186
+ } ) ;
187
+
188
+ it ( 'should filter out non-text nodes when figuring out the label' , ( ) => {
189
+ const fixture = TestBed . createComponent ( SimpleMenu ) ;
190
+ fixture . detectChanges ( ) ;
191
+ expect ( fixture . componentInstance . items . last . getLabel ( ) ) . toBe ( 'Item with an icon' ) ;
192
+ } ) ;
193
+
178
194
describe ( 'positions' , ( ) => {
179
195
let fixture : ComponentFixture < PositionedMenu > ;
180
196
let panel : HTMLElement ;
@@ -1070,7 +1086,7 @@ describe('MatMenu default overrides', () => {
1070
1086
beforeEach ( async ( ( ) => {
1071
1087
TestBed . configureTestingModule ( {
1072
1088
imports : [ MatMenuModule , NoopAnimationsModule ] ,
1073
- declarations : [ SimpleMenu ] ,
1089
+ declarations : [ SimpleMenu , FakeIcon ] ,
1074
1090
providers : [ {
1075
1091
provide : MAT_MENU_DEFAULT_OPTIONS ,
1076
1092
useValue : { overlapTrigger : false , xPosition : 'before' , yPosition : 'above' } ,
@@ -1095,13 +1111,18 @@ describe('MatMenu default overrides', () => {
1095
1111
<mat-menu class="custom-one custom-two" #menu="matMenu" (close)="closeCallback($event)">
1096
1112
<button mat-menu-item> Item </button>
1097
1113
<button mat-menu-item disabled> Disabled </button>
1114
+ <button mat-menu-item>
1115
+ <fake-icon>unicorn</fake-icon>
1116
+ Item with an icon
1117
+ </button>
1098
1118
</mat-menu>
1099
1119
`
1100
1120
} )
1101
1121
class SimpleMenu {
1102
1122
@ViewChild ( MatMenuTrigger ) trigger : MatMenuTrigger ;
1103
1123
@ViewChild ( 'triggerEl' ) triggerEl : ElementRef ;
1104
1124
@ViewChild ( MatMenu ) menu : MatMenu ;
1125
+ @ViewChildren ( MatMenuItem ) items : QueryList < MatMenuItem > ;
1105
1126
closeCallback = jasmine . createSpy ( 'menu closed callback' ) ;
1106
1127
}
1107
1128
@@ -1284,3 +1305,9 @@ class NestedMenuRepeater {
1284
1305
1285
1306
items = [ 'one' , 'two' , 'three' ] ;
1286
1307
}
1308
+
1309
+ @Component ( {
1310
+ selector : 'fake-icon' ,
1311
+ template : '<ng-content></ng-content>'
1312
+ } )
1313
+ class FakeIcon { }
0 commit comments