1
- import { Component } from '@angular/core' ;
1
+ import { Component , Type } from '@angular/core' ;
2
2
import { ComponentFixture , TestBed , async } from '@angular/core/testing' ;
3
3
import { By } from '@angular/platform-browser' ;
4
4
import { CdkMenuModule } from './menu-module' ;
@@ -61,27 +61,59 @@ describe('MenuItem', () => {
61
61
} ) ;
62
62
63
63
describe ( 'with complex inner elements' , ( ) => {
64
- let fixture : ComponentFixture < ComplexMenuItem > ;
65
64
let menuItem : CdkMenuItem ;
66
65
67
- beforeEach ( async ( ( ) => {
66
+ /**
67
+ * Build a component for testing and render it.
68
+ * @param componentClass the component to create
69
+ */
70
+ function createComponent < T > ( componentClass : Type < T > ) {
71
+ let fixture : ComponentFixture < T > ;
72
+
68
73
TestBed . configureTestingModule ( {
69
74
imports : [ CdkMenuModule ] ,
70
- declarations : [ ComplexMenuItem ] ,
75
+ declarations : [ componentClass , MatIcon ] ,
71
76
providers : [ { provide : CDK_MENU , useClass : CdkMenu } ] ,
72
77
} ) . compileComponents ( ) ;
73
- } ) ) ;
74
78
75
- beforeEach ( ( ) => {
76
- fixture = TestBed . createComponent ( ComplexMenuItem ) ;
79
+ fixture = TestBed . createComponent ( componentClass ) ;
77
80
fixture . detectChanges ( ) ;
78
81
79
82
menuItem = fixture . debugElement . query ( By . directive ( CdkMenuItem ) ) . injector . get ( CdkMenuItem ) ;
83
+ }
84
+
85
+ it ( 'should get the text for a simple menu item with no nested or wrapped elements' , ( ) => {
86
+ createComponent ( SingleMenuItem ) ;
87
+ expect ( menuItem . getLabel ( ) ) . toEqual ( 'Click me!' ) ;
80
88
} ) ;
81
89
82
- it ( 'should be able to extract the label text, with text nested in bold tag' , ( ) => {
83
- expect ( menuItem . getLabel ( ) ) . toBe ( 'Click me!' ) ;
90
+ it ( 'should get the text for menu item with a single nested mat icon component' , ( ) => {
91
+ createComponent ( MenuItemWithIcon ) ;
92
+ expect ( menuItem . getLabel ( ) ) . toEqual ( 'Click me!' ) ;
84
93
} ) ;
94
+
95
+ it (
96
+ 'should get the text for menu item with single nested component with the material ' +
97
+ 'icon class' ,
98
+ ( ) => {
99
+ createComponent ( MenuItemWithIconClass ) ;
100
+ expect ( menuItem . getLabel ( ) ) . toEqual ( 'Click me!' ) ;
101
+ }
102
+ ) ;
103
+
104
+ it ( 'should get the text for a menu item with bold marked text' , ( ) => {
105
+ createComponent ( MenuItemWithBoldElement ) ;
106
+ expect ( menuItem . getLabel ( ) ) . toEqual ( 'Click me!' ) ;
107
+ } ) ;
108
+
109
+ it (
110
+ 'should get the text for a menu item with nested icon, nested icon class and nested ' +
111
+ 'wrapping elements' ,
112
+ ( ) => {
113
+ createComponent ( MenuItemWithMultipleNestings ) ;
114
+ expect ( menuItem . getLabel ( ) ) . toEqual ( 'Click me!' ) ;
115
+ }
116
+ ) ;
85
117
} ) ;
86
118
} ) ;
87
119
@@ -91,6 +123,47 @@ describe('MenuItem', () => {
91
123
class SingleMenuItem { }
92
124
93
125
@Component ( {
94
- template : ` <button cdkMenuItem>Click <b>me!</b></button> ` ,
126
+ template : `
127
+ <button cdkMenuItem>
128
+ <mat-icon>unicorn</mat-icon>
129
+ Click me!
130
+ </button>
131
+ ` ,
132
+ } )
133
+ class MenuItemWithIcon { }
134
+ @Component ( {
135
+ template : `
136
+ <button cdkMenuItem>
137
+ <div class="material-icons">unicorn</div>
138
+ Click me!
139
+ </button>
140
+ ` ,
141
+ } )
142
+ class MenuItemWithIconClass { }
143
+
144
+ @Component ( {
145
+ template : ` <button cdkMenuItem><b>Click</b> me!</button> ` ,
146
+ } )
147
+ class MenuItemWithBoldElement { }
148
+
149
+ @Component ( {
150
+ template : `
151
+ <button cdkMenuItem>
152
+ <div>
153
+ <div class="material-icons">unicorn</div>
154
+ <div>
155
+ Click
156
+ </div>
157
+ <mat-icon>menu</mat-icon>
158
+ <div>me<b>!</b></div>
159
+ </div>
160
+ </button>
161
+ ` ,
162
+ } )
163
+ class MenuItemWithMultipleNestings { }
164
+
165
+ @Component ( {
166
+ selector : 'mat-icon' ,
167
+ template : '<ng-content></ng-content>' ,
95
168
} )
96
- class ComplexMenuItem { }
169
+ class MatIcon { }
0 commit comments