@@ -14,6 +14,8 @@ import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
14
14
import { MdAutocomplete } from './autocomplete' ;
15
15
import { MdInputContainer } from '../input/input-container' ;
16
16
import { Observable } from 'rxjs/Observable' ;
17
+ import { dispatchFakeEvent } from '../core/testing/dispatch-events' ;
18
+
17
19
import 'rxjs/add/operator/map' ;
18
20
19
21
describe ( 'MdAutocomplete' , ( ) => {
@@ -63,7 +65,7 @@ describe('MdAutocomplete', () => {
63
65
expect ( fixture . componentInstance . trigger . panelOpen )
64
66
. toBe ( false , `Expected panel state to start out closed.` ) ;
65
67
66
- dispatchEvent ( 'focus' , input ) ;
68
+ dispatchFakeEvent ( input , 'focus' ) ;
67
69
fixture . detectChanges ( ) ;
68
70
69
71
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -90,11 +92,11 @@ describe('MdAutocomplete', () => {
90
92
} ) ;
91
93
92
94
it ( 'should close the panel when blurred' , async ( ( ) => {
93
- dispatchEvent ( 'focus' , input ) ;
95
+ dispatchFakeEvent ( input , 'focus' ) ;
94
96
fixture . detectChanges ( ) ;
95
97
96
98
fixture . whenStable ( ) . then ( ( ) => {
97
- dispatchEvent ( 'blur' , input ) ;
99
+ dispatchFakeEvent ( input , 'blur' ) ;
98
100
fixture . detectChanges ( ) ;
99
101
100
102
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -105,7 +107,7 @@ describe('MdAutocomplete', () => {
105
107
} ) ) ;
106
108
107
109
it ( 'should close the panel when an option is clicked' , async ( ( ) => {
108
- dispatchEvent ( 'focus' , input ) ;
110
+ dispatchFakeEvent ( input , 'focus' ) ;
109
111
fixture . detectChanges ( ) ;
110
112
111
113
fixture . whenStable ( ) . then ( ( ) => {
@@ -121,7 +123,7 @@ describe('MdAutocomplete', () => {
121
123
} ) ) ;
122
124
123
125
it ( 'should close the panel when a newly created option is clicked' , async ( ( ) => {
124
- dispatchEvent ( 'focus' , input ) ;
126
+ dispatchFakeEvent ( input , 'focus' ) ;
125
127
fixture . detectChanges ( ) ;
126
128
127
129
fixture . whenStable ( ) . then ( ( ) => {
@@ -166,7 +168,7 @@ describe('MdAutocomplete', () => {
166
168
} ) ;
167
169
168
170
it ( 'should hide the panel when the options list is empty' , async ( ( ) => {
169
- dispatchEvent ( 'focus' , input ) ;
171
+ dispatchFakeEvent ( input , 'focus' ) ;
170
172
171
173
fixture . whenStable ( ) . then ( ( ) => {
172
174
fixture . detectChanges ( ) ;
@@ -214,7 +216,7 @@ describe('MdAutocomplete', () => {
214
216
. toBe ( false , `Expected panel state to start out closed.` ) ;
215
217
216
218
input . value = 'Alabama' ;
217
- dispatchEvent ( ' input' , input ) ;
219
+ dispatchFakeEvent ( input , ' input' ) ;
218
220
fixture . detectChanges ( ) ;
219
221
220
222
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -467,7 +469,7 @@ describe('MdAutocomplete', () => {
467
469
expect ( fixture . componentInstance . stateCtrl . touched )
468
470
. toBe ( false , `Expected control to start out untouched.` ) ;
469
471
470
- dispatchEvent ( 'blur' , input ) ;
472
+ dispatchFakeEvent ( input , 'blur' ) ;
471
473
fixture . detectChanges ( ) ;
472
474
473
475
expect ( fixture . componentInstance . stateCtrl . touched )
@@ -487,8 +489,8 @@ describe('MdAutocomplete', () => {
487
489
fixture . detectChanges ( ) ;
488
490
489
491
input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
490
- DOWN_ARROW_EVENT = new FakeKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
491
- ENTER_EVENT = new FakeKeyboardEvent ( ENTER ) as KeyboardEvent ;
492
+ DOWN_ARROW_EVENT = new MockKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
493
+ ENTER_EVENT = new MockKeyboardEvent ( ENTER ) as KeyboardEvent ;
492
494
493
495
fixture . componentInstance . trigger . openPanel ( ) ;
494
496
fixture . detectChanges ( ) ;
@@ -549,7 +551,7 @@ describe('MdAutocomplete', () => {
549
551
const optionEls =
550
552
overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
551
553
552
- const UP_ARROW_EVENT = new FakeKeyboardEvent ( UP_ARROW ) as KeyboardEvent ;
554
+ const UP_ARROW_EVENT = new MockKeyboardEvent ( UP_ARROW ) as KeyboardEvent ;
553
555
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
554
556
555
557
fixture . whenStable ( ) . then ( ( ) => {
@@ -615,7 +617,7 @@ describe('MdAutocomplete', () => {
615
617
typeInElement ( 'New' , input ) ;
616
618
fixture . detectChanges ( ) ;
617
619
618
- const SPACE_EVENT = new FakeKeyboardEvent ( SPACE ) as KeyboardEvent ;
620
+ const SPACE_EVENT = new MockKeyboardEvent ( SPACE ) as KeyboardEvent ;
619
621
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
620
622
fixture . componentInstance . trigger . _handleKeydown ( SPACE_EVENT ) ;
621
623
fixture . detectChanges ( ) ;
@@ -724,7 +726,7 @@ describe('MdAutocomplete', () => {
724
726
expect ( input . hasAttribute ( 'aria-activedescendant' ) )
725
727
. toBe ( false , 'Expected aria-activedescendant to be absent if no active item.' ) ;
726
728
727
- const DOWN_ARROW_EVENT = new FakeKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
729
+ const DOWN_ARROW_EVENT = new MockKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
728
730
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
729
731
fixture . detectChanges ( ) ;
730
732
@@ -866,7 +868,7 @@ describe('MdAutocomplete', () => {
866
868
fixture . detectChanges ( ) ;
867
869
868
870
const input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
869
- dispatchEvent ( 'focus' , input ) ;
871
+ dispatchFakeEvent ( input , 'focus' ) ;
870
872
fixture . detectChanges ( ) ;
871
873
872
874
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -995,35 +997,20 @@ class AutocompleteWithoutForms {
995
997
996
998
}
997
999
998
- /**
999
- * TODO: Move this to core testing utility until Angular has event faking
1000
- * support.
1001
- *
1002
- * Dispatches an event from an element.
1003
- * @param eventName Name of the event
1004
- * @param element The element from which the event will be dispatched.
1005
- */
1006
- function dispatchEvent ( eventName : string , element : HTMLElement ) : void {
1007
- let event = document . createEvent ( 'Event' ) ;
1008
- event . initEvent ( eventName , true , true ) ;
1009
- element . dispatchEvent ( event ) ;
1010
- }
1011
-
1012
-
1013
1000
/**
1014
1001
* Focuses an input, sets its value and dispatches
1015
1002
* the `input` event, simulating the user typing.
1016
1003
* @param value Value to be set on the input.
1017
1004
* @param element Element onto which to set the value.
1018
1005
*/
1019
- function typeInElement ( value : string , element : HTMLInputElement ) {
1006
+ function typeInElement ( value : string , element : HTMLInputElement , autoFocus = true ) {
1020
1007
element . focus ( ) ;
1021
1008
element . value = value ;
1022
- dispatchEvent ( 'input' , element ) ;
1009
+ dispatchFakeEvent ( element , 'input' ) ;
1023
1010
}
1024
1011
1025
1012
/** This is a mock keyboard event to test keyboard events in the autocomplete. */
1026
- class FakeKeyboardEvent {
1013
+ class MockKeyboardEvent {
1027
1014
constructor ( public keyCode : number ) { }
1028
1015
preventDefault ( ) { }
1029
1016
}
0 commit comments