@@ -2,39 +2,70 @@ import {TestBed, async} from '@angular/core/testing';
22import { Component , ViewChild } from '@angular/core' ;
33import { By } from '@angular/platform-browser' ;
44import { MdMenuModule , MdMenuTrigger } from './menu' ;
5+ import { OverlayContainer } from '../core/overlay/overlay-container' ;
56
67
78describe ( 'MdMenu' , ( ) => {
9+ let overlayContainerElement : HTMLElement ;
810
911 beforeEach ( async ( ( ) => {
1012 TestBed . configureTestingModule ( {
1113 imports : [ MdMenuModule . forRoot ( ) ] ,
1214 declarations : [ SimpleMenu ] ,
15+ providers : [
16+ { provide : OverlayContainer , useFactory : ( ) => {
17+ overlayContainerElement = document . createElement ( 'div' ) ;
18+ return { getContainerElement : ( ) => overlayContainerElement } ;
19+ } }
20+ ]
1321 } ) ;
1422
1523 TestBed . compileComponents ( ) ;
1624 } ) ) ;
1725
1826 it ( 'should open the menu as an idempotent operation' , ( ) => {
19- let fixture = TestBed . createComponent ( SimpleMenu ) ;
27+ const fixture = TestBed . createComponent ( SimpleMenu ) ;
2028 fixture . detectChanges ( ) ;
21- let menu = fixture . debugElement . query ( By . css ( '.md-menu-panel' ) ) ;
22- expect ( menu ) . toBe ( null ) ;
29+ expect ( overlayContainerElement . textContent ) . toBe ( '' ) ;
2330 expect ( ( ) => {
2431 fixture . componentInstance . trigger . openMenu ( ) ;
2532 fixture . componentInstance . trigger . openMenu ( ) ;
2633
27- menu = fixture . debugElement . query ( By . css ( '.md-menu-panel' ) ) ;
28- expect ( menu . nativeElement . innerHTML . trim ( ) ) . toEqual ( 'Content' ) ;
34+ expect ( overlayContainerElement . textContent . trim ( ) ) . toBe ( 'Content' ) ;
2935 } ) . not . toThrowError ( ) ;
3036 } ) ;
37+
38+ it ( 'should close the menu when a menu item is clicked' , ( ) => {
39+ const fixture = TestBed . createComponent ( SimpleMenu ) ;
40+ fixture . detectChanges ( ) ;
41+ fixture . componentInstance . trigger . openMenu ( ) ;
42+
43+ const menuItem = fixture . debugElement . query ( By . css ( '[md-menu-item]' ) ) ;
44+ menuItem . nativeElement . click ( ) ;
45+ fixture . detectChanges ( ) ;
46+
47+ expect ( overlayContainerElement . textContent ) . toBe ( '' ) ;
48+ } ) ;
49+
50+ it ( 'should close the menu when a click occurs outside the menu' , ( ) => {
51+ const fixture = TestBed . createComponent ( SimpleMenu ) ;
52+ fixture . detectChanges ( ) ;
53+ fixture . componentInstance . trigger . openMenu ( ) ;
54+
55+ const backdrop = < HTMLElement > overlayContainerElement . querySelector ( '.md-overlay-backdrop' ) ;
56+ backdrop . click ( ) ;
57+ fixture . detectChanges ( ) ;
58+
59+ expect ( overlayContainerElement . textContent ) . toBe ( '' ) ;
60+ } ) ;
61+
3162} ) ;
3263
3364@Component ( {
3465 template : `
3566 <button [md-menu-trigger-for]="menu">Toggle menu</button>
3667 <md-menu #menu="mdMenu">
37- Content
68+ <button md-menu-item> Content </button>
3869 </md-menu>
3970 `
4071} )
0 commit comments