@@ -21,7 +21,7 @@ import {OverlayContainer} from '../core/overlay/overlay-container';
21
21
import { MdInputModule } from '../input/index' ;
22
22
import { Directionality , Direction } from '../core/bidi/index' ;
23
23
import { Subscription } from 'rxjs/Subscription' ;
24
- import { ENTER , DOWN_ARROW , SPACE , UP_ARROW , ESCAPE } from '../core/keyboard/keycodes' ;
24
+ import { ENTER , DOWN_ARROW , SPACE , UP_ARROW , ESCAPE , TAB } from '../core/keyboard/keycodes' ;
25
25
import { MdOption } from '../core/option/option' ;
26
26
import { MdInputContainer } from '../input/input-container' ;
27
27
import { Observable } from 'rxjs/Observable' ;
@@ -1206,6 +1206,71 @@ describe('MdAutocomplete', () => {
1206
1206
} ) ) ;
1207
1207
} ) ;
1208
1208
1209
+ describe ( 'panel closing' , ( ) => {
1210
+ let fixture : ComponentFixture < SimpleAutocomplete > ;
1211
+ let input : HTMLInputElement ;
1212
+ let trigger : MdAutocompleteTrigger ;
1213
+ let closingActionSpy : jasmine . Spy ;
1214
+ let closingActionsSub : Subscription ;
1215
+
1216
+ beforeEach ( ( ) => {
1217
+ fixture = TestBed . createComponent ( SimpleAutocomplete ) ;
1218
+ fixture . detectChanges ( ) ;
1219
+
1220
+ input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
1221
+
1222
+ fixture . componentInstance . trigger . openPanel ( ) ;
1223
+ fixture . detectChanges ( ) ;
1224
+
1225
+ trigger = fixture . componentInstance . trigger ;
1226
+ closingActionSpy = jasmine . createSpy ( 'closing action listener' ) ;
1227
+ closingActionsSub = trigger . panelClosingActions . subscribe ( closingActionSpy ) ;
1228
+ } ) ;
1229
+
1230
+ afterEach ( ( ) => {
1231
+ closingActionsSub . unsubscribe ( ) ;
1232
+ } ) ;
1233
+
1234
+ it ( 'should emit panel close event when clicking away' , async ( ( ) => {
1235
+ fixture . whenStable ( ) . then ( ( ) => {
1236
+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1237
+ dispatchFakeEvent ( document , 'click' ) ;
1238
+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1239
+ } ) ;
1240
+ } ) ) ;
1241
+
1242
+ it ( 'should emit panel close event when tabbing out' , async ( ( ) => {
1243
+ const tabEvent = createKeyboardEvent ( 'keydown' , TAB ) ;
1244
+ input . focus ( ) ;
1245
+
1246
+ fixture . whenStable ( ) . then ( ( ) => {
1247
+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1248
+ trigger . _handleKeydown ( tabEvent ) ;
1249
+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1250
+ } ) ;
1251
+ } ) ) ;
1252
+
1253
+ it ( 'should emit panel close event when selecting an option' , async ( ( ) => {
1254
+ fixture . whenStable ( ) . then ( ( ) => {
1255
+ const option = overlayContainerElement . querySelector ( 'md-option' ) as HTMLElement ;
1256
+
1257
+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1258
+ option . click ( ) ;
1259
+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1260
+ } ) ;
1261
+ } ) ) ;
1262
+
1263
+ it ( 'should close the panel when pressing escape' , async ( ( ) => {
1264
+ const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
1265
+
1266
+ fixture . whenStable ( ) . then ( ( ) => {
1267
+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1268
+ trigger . _handleKeydown ( escapeEvent ) ;
1269
+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1270
+ } ) ;
1271
+ } ) ) ;
1272
+ } ) ;
1273
+
1209
1274
describe ( 'without mdInput' , ( ) => {
1210
1275
let fixture : ComponentFixture < AutocompleteWithNativeInput > ;
1211
1276
0 commit comments