@@ -1403,6 +1403,71 @@ describe('MatAutocomplete', () => {
14031403 } ) ) ;
14041404 } ) ;
14051405
1406+ describe ( 'panel closing' , ( ) => {
1407+ let fixture : ComponentFixture < SimpleAutocomplete > ;
1408+ let input : HTMLInputElement ;
1409+ let trigger : MatAutocompleteTrigger ;
1410+ let closingActionSpy : jasmine . Spy ;
1411+ let closingActionsSub : Subscription ;
1412+
1413+ beforeEach ( ( ) => {
1414+ fixture = TestBed . createComponent ( SimpleAutocomplete ) ;
1415+ fixture . detectChanges ( ) ;
1416+
1417+ input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
1418+
1419+ fixture . componentInstance . trigger . openPanel ( ) ;
1420+ fixture . detectChanges ( ) ;
1421+
1422+ trigger = fixture . componentInstance . trigger ;
1423+ closingActionSpy = jasmine . createSpy ( 'closing action listener' ) ;
1424+ closingActionsSub = trigger . panelClosingActions . subscribe ( closingActionSpy ) ;
1425+ } ) ;
1426+
1427+ afterEach ( ( ) => {
1428+ closingActionsSub . unsubscribe ( ) ;
1429+ } ) ;
1430+
1431+ it ( 'should emit panel close event when clicking away' , async ( ( ) => {
1432+ fixture . whenStable ( ) . then ( ( ) => {
1433+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1434+ dispatchFakeEvent ( document , 'click' ) ;
1435+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1436+ } ) ;
1437+ } ) ) ;
1438+
1439+ it ( 'should emit panel close event when tabbing out' , async ( ( ) => {
1440+ const tabEvent = createKeyboardEvent ( 'keydown' , TAB ) ;
1441+ input . focus ( ) ;
1442+
1443+ fixture . whenStable ( ) . then ( ( ) => {
1444+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1445+ trigger . _handleKeydown ( tabEvent ) ;
1446+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1447+ } ) ;
1448+ } ) ) ;
1449+
1450+ it ( 'should emit panel close event when selecting an option' , async ( ( ) => {
1451+ fixture . whenStable ( ) . then ( ( ) => {
1452+ const option = overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ;
1453+
1454+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1455+ option . click ( ) ;
1456+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1457+ } ) ;
1458+ } ) ) ;
1459+
1460+ it ( 'should close the panel when pressing escape' , async ( ( ) => {
1461+ const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
1462+
1463+ fixture . whenStable ( ) . then ( ( ) => {
1464+ expect ( closingActionSpy ) . not . toHaveBeenCalled ( ) ;
1465+ trigger . _handleKeydown ( escapeEvent ) ;
1466+ expect ( closingActionSpy ) . toHaveBeenCalled ( ) ;
1467+ } ) ;
1468+ } ) ) ;
1469+ } ) ;
1470+
14061471 describe ( 'without matInput' , ( ) => {
14071472 let fixture : ComponentFixture < AutocompleteWithNativeInput > ;
14081473
0 commit comments