@@ -25,7 +25,7 @@ import {
25
25
} from './index' ;
26
26
import { MdInputModule } from '../input/index' ;
27
27
import { 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' ;
29
29
import { MdOption } from '@angular/material/core' ;
30
30
import { MdFormField , MdFormFieldModule } from '@angular/material/form-field' ;
31
31
import { Observable } from 'rxjs/Observable' ;
@@ -1359,6 +1359,71 @@ describe('MdAutocomplete', () => {
1359
1359
} ) ) ;
1360
1360
} ) ;
1361
1361
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
+
1362
1427
describe ( 'without mdInput' , ( ) => {
1363
1428
let fixture : ComponentFixture < AutocompleteWithNativeInput > ;
1364
1429
0 commit comments