@@ -16,13 +16,14 @@ import {MdInputModule} from '../input/index';
16
16
import { Dir , LayoutDirection } from '../core/rtl/dir' ;
17
17
import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
18
18
import { Subscription } from 'rxjs/Subscription' ;
19
- import { ENTER , DOWN_ARROW , SPACE , UP_ARROW , HOME , END } from '../core/keyboard/keycodes' ;
19
+ import { ENTER , DOWN_ARROW , SPACE , UP_ARROW , HOME , END , ESCAPE } from '../core/keyboard/keycodes' ;
20
20
import { MdOption } from '../core/option/option' ;
21
21
import { MdAutocomplete } from './autocomplete' ;
22
22
import { MdInputContainer } from '../input/input-container' ;
23
23
import { Observable } from 'rxjs/Observable' ;
24
24
import { Subject } from 'rxjs/Subject' ;
25
25
import { dispatchFakeEvent } from '../core/testing/dispatch-events' ;
26
+ import { createKeyboardEvent } from '../core/testing/event-objects' ;
26
27
import { typeInElement } from '../core/testing/type-in-element' ;
27
28
import { ScrollDispatcher } from '../core/overlay/scroll/scroll-dispatcher' ;
28
29
@@ -535,8 +536,8 @@ describe('MdAutocomplete', () => {
535
536
fixture . detectChanges ( ) ;
536
537
537
538
input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
538
- DOWN_ARROW_EVENT = new MockKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
539
- ENTER_EVENT = new MockKeyboardEvent ( ENTER ) as KeyboardEvent ;
539
+ DOWN_ARROW_EVENT = createKeyboardEvent ( 'keydown' , DOWN_ARROW ) ;
540
+ ENTER_EVENT = createKeyboardEvent ( 'keydown' , ENTER ) ;
540
541
541
542
fixture . componentInstance . trigger . openPanel ( ) ;
542
543
fixture . detectChanges ( ) ;
@@ -594,7 +595,7 @@ describe('MdAutocomplete', () => {
594
595
const optionEls =
595
596
overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
596
597
597
- const UP_ARROW_EVENT = new MockKeyboardEvent ( UP_ARROW ) as KeyboardEvent ;
598
+ const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
598
599
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
599
600
tick ( ) ;
600
601
fixture . detectChanges ( ) ;
@@ -668,7 +669,7 @@ describe('MdAutocomplete', () => {
668
669
typeInElement ( 'New' , input ) ;
669
670
fixture . detectChanges ( ) ;
670
671
671
- const SPACE_EVENT = new MockKeyboardEvent ( SPACE ) as KeyboardEvent ;
672
+ const SPACE_EVENT = createKeyboardEvent ( 'keydown' , SPACE ) ;
672
673
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
673
674
674
675
fixture . whenStable ( ) . then ( ( ) => {
@@ -748,7 +749,7 @@ describe('MdAutocomplete', () => {
748
749
const scrollContainer =
749
750
document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ;
750
751
751
- const UP_ARROW_EVENT = new MockKeyboardEvent ( UP_ARROW ) as KeyboardEvent ;
752
+ const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
752
753
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
753
754
tick ( ) ;
754
755
fixture . detectChanges ( ) ;
@@ -762,7 +763,7 @@ describe('MdAutocomplete', () => {
762
763
const scrollContainer =
763
764
document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ;
764
765
765
- const END_EVENT = new MockKeyboardEvent ( END ) as KeyboardEvent ;
766
+ const END_EVENT = createKeyboardEvent ( 'keydown' , END ) ;
766
767
fixture . componentInstance . trigger . _handleKeydown ( END_EVENT ) ;
767
768
tick ( ) ;
768
769
fixture . detectChanges ( ) ;
@@ -779,14 +780,31 @@ describe('MdAutocomplete', () => {
779
780
scrollContainer . scrollTop = 100 ;
780
781
fixture . detectChanges ( ) ;
781
782
782
- const HOME_EVENT = new MockKeyboardEvent ( HOME ) as KeyboardEvent ;
783
+ const HOME_EVENT = createKeyboardEvent ( 'keydown' , HOME ) ;
783
784
fixture . componentInstance . trigger . _handleKeydown ( HOME_EVENT ) ;
784
785
tick ( ) ;
785
786
fixture . detectChanges ( ) ;
786
787
787
788
expect ( scrollContainer . scrollTop ) . toEqual ( 0 , 'Expected panel to reveal the first option.' ) ;
788
789
} ) ) ;
789
790
791
+ it ( 'should close the panel when pressing escape' , async ( ( ) => {
792
+ const trigger = fixture . componentInstance . trigger ;
793
+ const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
794
+
795
+ input . focus ( ) ;
796
+
797
+ fixture . whenStable ( ) . then ( ( ) => {
798
+ expect ( document . activeElement ) . toBe ( input , 'Expected input to be focused.' ) ;
799
+ expect ( trigger . panelOpen ) . toBe ( true , 'Expected panel to be open.' ) ;
800
+
801
+ trigger . _handleKeydown ( escapeEvent ) ;
802
+
803
+ expect ( document . activeElement ) . toBe ( input , 'Expected input to continue to be focused.' ) ;
804
+ expect ( trigger . panelOpen ) . toBe ( false , 'Expected panel to be closed.' ) ;
805
+ } ) ;
806
+ } ) ) ;
807
+
790
808
} ) ;
791
809
792
810
describe ( 'aria' , ( ) => {
@@ -832,7 +850,7 @@ describe('MdAutocomplete', () => {
832
850
expect ( input . hasAttribute ( 'aria-activedescendant' ) )
833
851
. toBe ( false , 'Expected aria-activedescendant to be absent if no active item.' ) ;
834
852
835
- const DOWN_ARROW_EVENT = new MockKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
853
+ const DOWN_ARROW_EVENT = createKeyboardEvent ( 'keydown' , DOWN_ARROW ) ;
836
854
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
837
855
838
856
fixture . whenStable ( ) . then ( ( ) => {
@@ -1140,7 +1158,7 @@ describe('MdAutocomplete', () => {
1140
1158
tick ( ) ;
1141
1159
fixture . detectChanges ( ) ;
1142
1160
1143
- const DOWN_ARROW_EVENT = new MockKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
1161
+ const DOWN_ARROW_EVENT = createKeyboardEvent ( 'keydown' , DOWN_ARROW ) ;
1144
1162
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
1145
1163
tick ( ) ;
1146
1164
fixture . detectChanges ( ) ;
@@ -1418,10 +1436,3 @@ class AutocompleteWithNativeInput {
1418
1436
} ) ;
1419
1437
}
1420
1438
}
1421
-
1422
-
1423
- /** This is a mock keyboard event to test keyboard events in the autocomplete. */
1424
- class MockKeyboardEvent {
1425
- constructor ( public keyCode : number ) { }
1426
- preventDefault ( ) { }
1427
- }
0 commit comments