@@ -25,7 +25,7 @@ import {
2525} from './index' ;
2626import { MdInputModule } from '../input/index' ;
2727import { Subscription } from 'rxjs/Subscription' ;
28- import { DOWN_ARROW , ENTER , ESCAPE , SPACE , UP_ARROW } from '@angular/material/core' ;
28+ import { DOWN_ARROW , ENTER , ESCAPE , SPACE , TAB , UP_ARROW } from '@angular/material/core' ;
2929import { MdOption } from '@angular/material/core' ;
3030import { MdFormField , MdFormFieldModule } from '@angular/material/form-field' ;
3131import { Observable } from 'rxjs/Observable' ;
@@ -1359,6 +1359,71 @@ describe('MdAutocomplete', () => {
13591359 } ) ) ;
13601360 } ) ;
13611361
1362+ describe ( 'panel closing' , ( ) => {
1363+ let fixture : ComponentFixture < SimpleAutocomplete > ;
1364+ let input : HTMLInputElement ;
1365+ let trigger : MdAutocompleteTrigger ;
1366+ let closingActionSpy : jasmine . Spy ;
1367+ let closingActionsSub : Subscription ;
1368+
1369+ beforeEach ( ( ) => {
1370+ fixture = TestBed . createComponent ( SimpleAutocomplete ) ;
1371+ fixture . detectChanges ( ) ;
1372+
1373+ input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
1374+
1375+ fixture . componentInstance . trigger . openPanel ( ) ;
1376+ fixture . detectChanges ( ) ;
1377+
1378+ trigger = fixture . componentInstance . trigger ;
1379+ closingActionSpy = jasmine . createSpy ( 'closing action listener' ) ;
1380+ closingActionsSub = trigger . panelClosingActions . subscribe ( closingActionSpy ) ;
1381+ } ) ;
1382+
1383+ afterEach ( ( ) => {
1384+ closingActionsSub . unsubscribe ( ) ;
1385+ } ) ;
1386+
1387+ it ( 'should emit panel close event when clicking away' , async ( ( ) => {
1388+ fixture . whenStable ( ) . then ( ( ) => {
1389+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1390+ dispatchFakeEvent ( document , 'click' ) ;
1391+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1392+ } ) ;
1393+ } ) ) ;
1394+
1395+ it ( 'should emit panel close event when tabbing out' , async ( ( ) => {
1396+ const tabEvent = createKeyboardEvent ( 'keydown' , TAB ) ;
1397+ input . focus ( ) ;
1398+
1399+ fixture . whenStable ( ) . then ( ( ) => {
1400+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1401+ trigger . _handleKeydown ( tabEvent ) ;
1402+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1403+ } ) ;
1404+ } ) ) ;
1405+
1406+ it ( 'should emit panel close event when selecting an option' , async ( ( ) => {
1407+ fixture . whenStable ( ) . then ( ( ) => {
1408+ const option = overlayContainerElement . querySelector ( 'md-option' ) as HTMLElement ;
1409+
1410+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1411+ option . click ( ) ;
1412+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1413+ } ) ;
1414+ } ) ) ;
1415+
1416+ it ( 'should close the panel when pressing escape' , async ( ( ) => {
1417+ const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
1418+
1419+ fixture . whenStable ( ) . then ( ( ) => {
1420+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1421+ trigger . _handleKeydown ( escapeEvent ) ;
1422+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1423+ } ) ;
1424+ } ) ) ;
1425+ } ) ;
1426+
13621427 describe ( 'without mdInput' , ( ) => {
13631428 let fixture : ComponentFixture < AutocompleteWithNativeInput > ;
13641429
0 commit comments