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