@@ -15,6 +15,7 @@ import {MdAutocomplete} from './autocomplete';
15
15
import { MdInputContainer } from '../input/input-container' ;
16
16
import { Observable } from 'rxjs/Observable' ;
17
17
import { dispatchFakeEvent } from '../core/testing/dispatch-events' ;
18
+ import { typeInElement } from '../core/testing/type-in-element' ;
18
19
19
20
import 'rxjs/add/operator/map' ;
20
21
@@ -66,35 +67,39 @@ describe('MdAutocomplete', () => {
66
67
input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
67
68
} ) ;
68
69
69
- it ( 'should open the panel when the input is focused' , ( ) => {
70
+ it ( 'should open the panel when the input is focused' , async ( ( ) => {
70
71
expect ( fixture . componentInstance . trigger . panelOpen )
71
72
. toBe ( false , `Expected panel state to start out closed.` ) ;
72
73
73
74
dispatchFakeEvent ( input , 'focus' ) ;
74
- fixture . detectChanges ( ) ;
75
+ fixture . whenStable ( ) . then ( ( ) => {
76
+ fixture . detectChanges ( ) ;
75
77
76
- expect ( fixture . componentInstance . trigger . panelOpen )
77
- . toBe ( true , `Expected panel state to read open when input is focused.` ) ;
78
- expect ( overlayContainerElement . textContent )
79
- . toContain ( 'Alabama' , `Expected panel to display when input is focused.` ) ;
80
- expect ( overlayContainerElement . textContent )
81
- . toContain ( 'California' , `Expected panel to display when input is focused.` ) ;
82
- } ) ;
78
+ expect ( fixture . componentInstance . trigger . panelOpen )
79
+ . toBe ( true , `Expected panel state to read open when input is focused.` ) ;
80
+ expect ( overlayContainerElement . textContent )
81
+ . toContain ( 'Alabama' , `Expected panel to display when input is focused.` ) ;
82
+ expect ( overlayContainerElement . textContent )
83
+ . toContain ( 'California' , `Expected panel to display when input is focused.` ) ;
84
+ } ) ;
85
+ } ) ) ;
83
86
84
- it ( 'should open the panel programmatically' , ( ) => {
87
+ it ( 'should open the panel programmatically' , async ( ( ) => {
85
88
expect ( fixture . componentInstance . trigger . panelOpen )
86
89
. toBe ( false , `Expected panel state to start out closed.` ) ;
87
90
88
91
fixture . componentInstance . trigger . openPanel ( ) ;
89
- fixture . detectChanges ( ) ;
92
+ fixture . whenStable ( ) . then ( ( ) => {
93
+ fixture . detectChanges ( ) ;
90
94
91
- expect ( fixture . componentInstance . trigger . panelOpen )
92
- . toBe ( true , `Expected panel state to read open when opened programmatically.` ) ;
93
- expect ( overlayContainerElement . textContent )
94
- . toContain ( 'Alabama' , `Expected panel to display when opened programmatically.` ) ;
95
- expect ( overlayContainerElement . textContent )
96
- . toContain ( 'California' , `Expected panel to display when opened programmatically.` ) ;
97
- } ) ;
95
+ expect ( fixture . componentInstance . trigger . panelOpen )
96
+ . toBe ( true , `Expected panel state to read open when opened programmatically.` ) ;
97
+ expect ( overlayContainerElement . textContent )
98
+ . toContain ( 'Alabama' , `Expected panel to display when opened programmatically.` ) ;
99
+ expect ( overlayContainerElement . textContent )
100
+ . toContain ( 'California' , `Expected panel to display when opened programmatically.` ) ;
101
+ } ) ;
102
+ } ) ) ;
98
103
99
104
it ( 'should close the panel when blurred' , async ( ( ) => {
100
105
dispatchFakeEvent ( input , 'focus' ) ;
@@ -190,8 +195,6 @@ describe('MdAutocomplete', () => {
190
195
fixture . whenStable ( ) . then ( ( ) => {
191
196
fixture . detectChanges ( ) ;
192
197
193
- expect ( fixture . componentInstance . trigger . panelOpen )
194
- . toBe ( true , `Expected panel to stay open when options list is empty.` ) ;
195
198
expect ( panel . classList )
196
199
. toContain ( 'mat-autocomplete-hidden' , `Expected panel to hide itself when empty.` ) ;
197
200
} ) ;
@@ -774,20 +777,43 @@ describe('MdAutocomplete', () => {
774
777
. toBe ( 'false' , 'Expected aria-expanded to be false while panel is closed.' ) ;
775
778
776
779
fixture . componentInstance . trigger . openPanel ( ) ;
777
- fixture . detectChanges ( ) ;
780
+ fixture . whenStable ( ) . then ( ( ) => {
781
+ fixture . detectChanges ( ) ;
778
782
779
- expect ( input . getAttribute ( 'aria-expanded' ) )
780
- . toBe ( 'true' , 'Expected aria-expanded to be true while panel is open.' ) ;
783
+ expect ( input . getAttribute ( 'aria-expanded' ) )
784
+ . toBe ( 'true' , 'Expected aria-expanded to be true while panel is open.' ) ;
781
785
782
- fixture . componentInstance . trigger . closePanel ( ) ;
783
- fixture . detectChanges ( ) ;
786
+ fixture . componentInstance . trigger . closePanel ( ) ;
787
+ fixture . detectChanges ( ) ;
784
788
785
- fixture . whenStable ( ) . then ( ( ) => {
786
- expect ( input . getAttribute ( 'aria-expanded' ) )
787
- . toBe ( 'false' , 'Expected aria-expanded to be false when panel closes again.' ) ;
789
+ fixture . whenStable ( ) . then ( ( ) => {
790
+ expect ( input . getAttribute ( 'aria-expanded' ) )
791
+ . toBe ( 'false' , 'Expected aria-expanded to be false when panel closes again.' ) ;
792
+ } ) ;
788
793
} ) ;
789
794
} ) ) ;
790
795
796
+ it ( 'should set aria-expanded properly when the panel is hidden' , async ( ( ) => {
797
+ fixture . componentInstance . trigger . openPanel ( ) ;
798
+
799
+ fixture . whenStable ( ) . then ( ( ) => {
800
+ fixture . detectChanges ( ) ;
801
+ expect ( input . getAttribute ( 'aria-expanded' ) )
802
+ . toBe ( 'true' , 'Expected aria-expanded to be true while panel is open.' ) ;
803
+
804
+ typeInElement ( 'zz' , input ) ;
805
+ fixture . whenStable ( ) . then ( ( ) => {
806
+ fixture . detectChanges ( ) ;
807
+
808
+ fixture . whenStable ( ) . then ( ( ) => {
809
+ fixture . detectChanges ( ) ;
810
+ expect ( input . getAttribute ( 'aria-expanded' ) )
811
+ . toBe ( 'false' , 'Expected aria-expanded to be false when panel hides itself.' ) ;
812
+ } ) ;
813
+ } ) ;
814
+ } ) ;
815
+ } ) ) ;
816
+
791
817
it ( 'should set aria-owns based on the attached autocomplete' , ( ) => {
792
818
fixture . componentInstance . trigger . openPanel ( ) ;
793
819
fixture . detectChanges ( ) ;
@@ -901,21 +927,24 @@ describe('MdAutocomplete', () => {
901
927
} ) ;
902
928
} ) ) ;
903
929
904
- it ( 'should work when input is wrapped in ngIf' , ( ) => {
930
+ it ( 'should work when input is wrapped in ngIf' , async ( ( ) => {
905
931
const fixture = TestBed . createComponent ( NgIfAutocomplete ) ;
906
932
fixture . detectChanges ( ) ;
907
933
908
934
const input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
909
935
dispatchFakeEvent ( input , 'focus' ) ;
910
- fixture . detectChanges ( ) ;
911
936
912
- expect ( fixture . componentInstance . trigger . panelOpen )
913
- . toBe ( true , `Expected panel state to read open when input is focused.` ) ;
914
- expect ( overlayContainerElement . textContent )
915
- . toContain ( 'One' , `Expected panel to display when input is focused.` ) ;
916
- expect ( overlayContainerElement . textContent )
917
- . toContain ( 'Two' , `Expected panel to display when input is focused.` ) ;
918
- } ) ;
937
+ fixture . whenStable ( ) . then ( ( ) => {
938
+ fixture . detectChanges ( ) ;
939
+
940
+ expect ( fixture . componentInstance . trigger . panelOpen )
941
+ . toBe ( true , `Expected panel state to read open when input is focused.` ) ;
942
+ expect ( overlayContainerElement . textContent )
943
+ . toContain ( 'One' , `Expected panel to display when input is focused.` ) ;
944
+ expect ( overlayContainerElement . textContent )
945
+ . toContain ( 'Two' , `Expected panel to display when input is focused.` ) ;
946
+ } ) ;
947
+ } ) ) ;
919
948
920
949
it ( 'should filter properly with ngIf after setting the active item' , fakeAsync ( ( ) => {
921
950
const fixture = TestBed . createComponent ( NgIfAutocomplete ) ;
@@ -1086,18 +1115,6 @@ class AutocompleteWithNgModel {
1086
1115
1087
1116
}
1088
1117
1089
- /**
1090
- * Focuses an input, sets its value and dispatches
1091
- * the `input` event, simulating the user typing.
1092
- * @param value Value to be set on the input.
1093
- * @param element Element onto which to set the value.
1094
- */
1095
- function typeInElement ( value : string , element : HTMLInputElement , autoFocus = true ) {
1096
- element . focus ( ) ;
1097
- element . value = value ;
1098
- dispatchFakeEvent ( element , 'input' ) ;
1099
- }
1100
-
1101
1118
/** This is a mock keyboard event to test keyboard events in the autocomplete. */
1102
1119
class MockKeyboardEvent {
1103
1120
constructor ( public keyCode : number ) { }
0 commit comments