@@ -740,10 +740,11 @@ describe('MenuBar', () => {
740
740
} ) ;
741
741
742
742
describe ( 'background click closeout' , ( ) => {
743
- let fixture : ComponentFixture < MenuBarWithMenus > ;
743
+ let fixture : ComponentFixture < MenuBarWithMenusAndInlineMenu > ;
744
744
745
- let menus : CdkMenu [ ] ;
745
+ let popoutMenus : CdkMenu [ ] ;
746
746
let triggers : CdkMenuItemTrigger [ ] ;
747
+ let nativeInlineMenuItem : HTMLElement ;
747
748
748
749
/** open the attached menu. */
749
750
function openMenu ( ) {
@@ -753,8 +754,9 @@ describe('MenuBar', () => {
753
754
754
755
/** set the menus and triggers arrays. */
755
756
function grabElementsForTesting ( ) {
756
- menus = fixture . componentInstance . menus . toArray ( ) ;
757
+ popoutMenus = fixture . componentInstance . menus . toArray ( ) . filter ( el => ! el . _isInline ( ) ) ;
757
758
triggers = fixture . componentInstance . triggers . toArray ( ) ;
759
+ nativeInlineMenuItem = fixture . componentInstance . nativeInlineMenuItem . nativeElement ;
758
760
}
759
761
760
762
/** run change detection and, extract and set the rendered elements. */
@@ -766,77 +768,86 @@ describe('MenuBar', () => {
766
768
beforeEach ( async ( ( ) => {
767
769
TestBed . configureTestingModule ( {
768
770
imports : [ CdkMenuModule ] ,
769
- declarations : [ MenuBarWithMenus ] ,
771
+ declarations : [ MenuBarWithMenusAndInlineMenu ] ,
770
772
} ) . compileComponents ( ) ;
771
773
} ) ) ;
772
774
773
775
beforeEach ( ( ) => {
774
- fixture = TestBed . createComponent ( MenuBarWithMenus ) ;
776
+ fixture = TestBed . createComponent ( MenuBarWithMenusAndInlineMenu ) ;
775
777
detectChanges ( ) ;
776
778
} ) ;
777
779
778
780
it ( 'should close out all open menus when clicked outside the menu tree' , ( ) => {
779
781
openMenu ( ) ;
780
- expect ( menus . length ) . toBe ( 1 ) ;
782
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
781
783
782
784
fixture . debugElement . query ( By . css ( '#container' ) ) . nativeElement . click ( ) ;
783
785
detectChanges ( ) ;
784
786
785
- expect ( menus . length ) . toBe ( 0 ) ;
787
+ expect ( popoutMenus . length ) . toBe ( 0 ) ;
786
788
} ) ;
787
789
788
790
it ( 'should not close open menus when clicking on a menu group' , ( ) => {
789
791
openMenu ( ) ;
790
- expect ( menus . length ) . toBe ( 1 ) ;
792
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
791
793
792
794
const menuGroups = fixture . debugElement . queryAll ( By . directive ( CdkMenuGroup ) ) ;
793
795
menuGroups [ 2 ] . nativeElement . click ( ) ;
794
796
detectChanges ( ) ;
795
797
796
- expect ( menus . length ) . toBe ( 1 ) ;
798
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
797
799
} ) ;
798
800
799
801
it ( 'should not close open menus when clicking on a menu' , ( ) => {
800
802
openMenu ( ) ;
801
- expect ( menus . length ) . toBe ( 1 ) ;
803
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
802
804
803
805
fixture . debugElement . query ( By . directive ( CdkMenu ) ) . nativeElement . click ( ) ;
804
806
detectChanges ( ) ;
805
807
806
- expect ( menus . length ) . toBe ( 1 ) ;
808
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
807
809
} ) ;
808
810
809
811
it ( 'should not close open menus when clicking on a menu bar' , ( ) => {
810
812
openMenu ( ) ;
811
- expect ( menus . length ) . toBe ( 1 ) ;
813
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
812
814
813
815
fixture . debugElement . query ( By . directive ( CdkMenuBar ) ) . nativeElement . click ( ) ;
814
816
detectChanges ( ) ;
815
817
816
- expect ( menus . length ) . toBe ( 1 ) ;
818
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
817
819
} ) ;
818
820
819
821
it ( 'should not close when clicking on a CdkMenuItemCheckbox element' , ( ) => {
820
822
openMenu ( ) ;
821
- expect ( menus . length ) . toBe ( 1 ) ;
823
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
822
824
823
825
fixture . debugElement . query ( By . directive ( CdkMenuItemCheckbox ) ) . nativeElement . click ( ) ;
824
826
fixture . detectChanges ( ) ;
825
827
826
- expect ( menus . length ) . toBe ( 1 ) ;
828
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
827
829
} ) ;
828
830
829
831
it ( 'should not close when clicking on a non-menu element inside menu' , ( ) => {
830
832
openMenu ( ) ;
831
- expect ( menus . length ) . toBe ( 1 ) ;
833
+ expect ( popoutMenus . length ) . toBe ( 1 ) ;
832
834
833
835
fixture . debugElement . query ( By . css ( '#inner-element' ) ) . nativeElement . click ( ) ;
834
836
detectChanges ( ) ;
835
837
836
- expect ( menus . length )
838
+ expect ( popoutMenus . length )
837
839
. withContext ( 'menu should stay open if clicking on an inner span element' )
838
840
. toBe ( 1 ) ;
839
841
} ) ;
842
+
843
+ it ( 'should close the open menu when clicking on an inline menu item' , ( ) => {
844
+ openMenu ( ) ;
845
+
846
+ nativeInlineMenuItem . click ( ) ;
847
+ detectChanges ( ) ;
848
+
849
+ expect ( popoutMenus . length ) . toBe ( 0 ) ;
850
+ } ) ;
840
851
} ) ;
841
852
842
853
describe ( 'Mouse handling' , ( ) => {
@@ -1171,10 +1182,16 @@ class MenuWithRadioButtons {
1171
1182
</div>
1172
1183
</ng-template>
1173
1184
</div>
1185
+
1186
+ <div cdkMenu>
1187
+ <button #inline_menu_item cdkMenuItem></button>
1188
+ </div>
1174
1189
` ,
1175
1190
} )
1176
- class MenuBarWithMenus {
1191
+ class MenuBarWithMenusAndInlineMenu {
1177
1192
@ViewChildren ( CdkMenu ) menus : QueryList < CdkMenu > ;
1178
1193
1179
1194
@ViewChildren ( CdkMenuItemTrigger ) triggers : QueryList < CdkMenuItemTrigger > ;
1195
+
1196
+ @ViewChild ( 'inline_menu_item' ) nativeInlineMenuItem : ElementRef ;
1180
1197
}
0 commit comments