@@ -124,8 +124,7 @@ describe('MdAutocomplete', () => {
124
124
125
125
fixture . whenStable ( ) . then ( ( ) => {
126
126
// Filter down the option list to a subset of original options ('Alabama', 'California')
127
- input . value = 'al' ;
128
- dispatchEvent ( 'input' , input ) ;
127
+ typeInElement ( 'al' , input ) ;
129
128
fixture . detectChanges ( ) ;
130
129
131
130
let options =
@@ -134,8 +133,7 @@ describe('MdAutocomplete', () => {
134
133
135
134
// Changing value from 'Alabama' to 'al' to re-populate the option list,
136
135
// ensuring that 'California' is created new.
137
- input . value = 'al' ;
138
- dispatchEvent ( 'input' , input ) ;
136
+ typeInElement ( 'al' , input ) ;
139
137
fixture . detectChanges ( ) ;
140
138
141
139
fixture . whenStable ( ) . then ( ( ) => {
@@ -177,8 +175,7 @@ describe('MdAutocomplete', () => {
177
175
. toContain ( 'mat-autocomplete-visible' , `Expected panel to start out visible.` ) ;
178
176
179
177
// Filter down the option list such that no options match the value
180
- input . value = 'af' ;
181
- dispatchEvent ( 'input' , input ) ;
178
+ typeInElement ( 'af' , input ) ;
182
179
fixture . detectChanges ( ) ;
183
180
184
181
fixture . whenStable ( ) . then ( ( ) => {
@@ -210,6 +207,18 @@ describe('MdAutocomplete', () => {
210
207
} ) ;
211
208
} ) ) ;
212
209
210
+ it ( 'should not open the panel when the `input` event is invoked on a non-focused input' , ( ) => {
211
+ expect ( fixture . componentInstance . trigger . panelOpen )
212
+ . toBe ( false , `Expected panel state to start out closed.` ) ;
213
+
214
+ input . value = 'Alabama' ;
215
+ dispatchEvent ( 'input' , input ) ;
216
+ fixture . detectChanges ( ) ;
217
+
218
+ expect ( fixture . componentInstance . trigger . panelOpen )
219
+ . toBe ( false , `Expected panel state to stay closed.` ) ;
220
+ } ) ;
221
+
213
222
} ) ;
214
223
215
224
it ( 'should have the correct text direction in RTL' , ( ) => {
@@ -241,15 +250,13 @@ describe('MdAutocomplete', () => {
241
250
fixture . componentInstance . trigger . openPanel ( ) ;
242
251
fixture . detectChanges ( ) ;
243
252
244
- input . value = 'a' ;
245
- dispatchEvent ( 'input' , input ) ;
253
+ typeInElement ( 'a' , input ) ;
246
254
fixture . detectChanges ( ) ;
247
255
248
256
expect ( fixture . componentInstance . stateCtrl . value )
249
257
. toEqual ( 'a' , 'Expected control value to be updated as user types.' ) ;
250
258
251
- input . value = 'al' ;
252
- dispatchEvent ( 'input' , input ) ;
259
+ typeInElement ( 'al' , input ) ;
253
260
fixture . detectChanges ( ) ;
254
261
255
262
expect ( fixture . componentInstance . stateCtrl . value )
@@ -282,8 +289,7 @@ describe('MdAutocomplete', () => {
282
289
options [ 1 ] . click ( ) ;
283
290
fixture . detectChanges ( ) ;
284
291
285
- input . value = 'Californi' ;
286
- dispatchEvent ( 'input' , input ) ;
292
+ typeInElement ( 'Californi' , input ) ;
287
293
fixture . detectChanges ( ) ;
288
294
289
295
expect ( fixture . componentInstance . stateCtrl . value )
@@ -339,8 +345,7 @@ describe('MdAutocomplete', () => {
339
345
} ) ) ;
340
346
341
347
it ( 'should clear the text field if value is reset programmatically' , async ( ( ) => {
342
- input . value = 'Alabama' ;
343
- dispatchEvent ( 'input' , input ) ;
348
+ typeInElement ( 'Alabama' , input ) ;
344
349
fixture . detectChanges ( ) ;
345
350
346
351
fixture . whenStable ( ) . then ( ( ) => {
@@ -376,8 +381,7 @@ describe('MdAutocomplete', () => {
376
381
expect ( fixture . componentInstance . stateCtrl . dirty )
377
382
. toBe ( false , `Expected control to start out pristine.` ) ;
378
383
379
- input . value = 'a' ;
380
- dispatchEvent ( 'input' , input ) ;
384
+ typeInElement ( 'a' , input ) ;
381
385
fixture . detectChanges ( ) ;
382
386
383
387
expect ( fixture . componentInstance . stateCtrl . dirty )
@@ -531,8 +535,7 @@ describe('MdAutocomplete', () => {
531
535
fixture . detectChanges ( ) ;
532
536
533
537
fixture . whenStable ( ) . then ( ( ) => {
534
- input . value = 'o' ;
535
- dispatchEvent ( 'input' , input ) ;
538
+ typeInElement ( 'o' , input ) ;
536
539
fixture . detectChanges ( ) ;
537
540
538
541
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
@@ -565,8 +568,7 @@ describe('MdAutocomplete', () => {
565
568
566
569
it ( 'should fill the text field, not select an option, when SPACE is entered' , async ( ( ) => {
567
570
fixture . whenStable ( ) . then ( ( ) => {
568
- input . value = 'New' ;
569
- dispatchEvent ( 'input' , input ) ;
571
+ typeInElement ( 'New' , input ) ;
570
572
fixture . detectChanges ( ) ;
571
573
572
574
const SPACE_EVENT = new FakeKeyboardEvent ( SPACE ) as KeyboardEvent ;
@@ -604,8 +606,7 @@ describe('MdAutocomplete', () => {
604
606
expect ( overlayContainerElement . textContent )
605
607
. toEqual ( '' , `Expected panel to close after ENTER key.` ) ;
606
608
607
- input . value = 'Alabam' ;
608
- dispatchEvent ( 'input' , input ) ;
609
+ typeInElement ( 'Alabama' , input ) ;
609
610
fixture . detectChanges ( ) ;
610
611
611
612
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -782,8 +783,7 @@ describe('MdAutocomplete', () => {
782
783
fixture . detectChanges ( ) ;
783
784
784
785
fixture . whenStable ( ) . then ( ( ) => {
785
- input . value = 'f' ;
786
- dispatchEvent ( 'input' , input ) ;
786
+ typeInElement ( 'f' , input ) ;
787
787
fixture . detectChanges ( ) ;
788
788
789
789
const inputTop = input . getBoundingClientRect ( ) . top ;
@@ -808,8 +808,7 @@ describe('MdAutocomplete', () => {
808
808
fixture . detectChanges ( ) ;
809
809
810
810
const input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
811
- input . value = 'd' ;
812
- dispatchEvent ( 'input' , input ) ;
811
+ typeInElement ( 'd' , input ) ;
813
812
fixture . detectChanges ( ) ;
814
813
815
814
const options =
@@ -826,7 +825,7 @@ describe('MdAutocomplete', () => {
826
825
<md-input-container>
827
826
<input mdInput placeholder="State" [mdAutocomplete]="auto" [formControl]="stateCtrl">
828
827
</md-input-container>
829
-
828
+
830
829
<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn">
831
830
<md-option *ngFor="let state of filteredStates" [value]="state">
832
831
<span> {{ state.code }}: {{ state.name }} </span>
@@ -881,10 +880,10 @@ class SimpleAutocomplete implements OnDestroy {
881
880
@Component ( {
882
881
template : `
883
882
<md-input-container>
884
- <input mdInput placeholder="State" [mdAutocomplete]="auto"
883
+ <input mdInput placeholder="State" [mdAutocomplete]="auto"
885
884
(input)="onInput($event.target?.value)">
886
885
</md-input-container>
887
-
886
+
888
887
<md-autocomplete #auto="mdAutocomplete">
889
888
<md-option *ngFor="let state of filteredStates" [value]="state">
890
889
<span> {{ state }} </span>
@@ -920,6 +919,19 @@ function dispatchEvent(eventName: string, element: HTMLElement): void {
920
919
element . dispatchEvent ( event ) ;
921
920
}
922
921
922
+
923
+ /**
924
+ * Focuses an input, sets its value and dispatches
925
+ * the `input` event, simulating the user typing.
926
+ * @param value Value to be set on the input.
927
+ * @param element Element onto which to set the value.
928
+ */
929
+ function typeInElement ( value : string , element : HTMLInputElement ) {
930
+ element . focus ( ) ;
931
+ element . value = value ;
932
+ dispatchEvent ( 'input' , element ) ;
933
+ }
934
+
923
935
/** This is a mock keyboard event to test keyboard events in the autocomplete. */
924
936
class FakeKeyboardEvent {
925
937
constructor ( public keyCode : number ) { }
0 commit comments