@@ -13,6 +13,7 @@ import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
13
13
import { FakeViewportRuler } from '../core/overlay/position/fake-viewport-ruler' ;
14
14
import { MdAutocomplete } from './autocomplete' ;
15
15
import { MdInputContainer } from '../input/input-container' ;
16
+ import { dispatchFakeEvent } from '../core/testing/dispatch-events' ;
16
17
17
18
describe ( 'MdAutocomplete' , ( ) => {
18
19
let overlayContainerElement : HTMLElement ;
@@ -61,7 +62,7 @@ describe('MdAutocomplete', () => {
61
62
expect ( fixture . componentInstance . trigger . panelOpen )
62
63
. toBe ( false , `Expected panel state to start out closed.` ) ;
63
64
64
- dispatchEvent ( 'focus' , input ) ;
65
+ dispatchFakeEvent ( input , 'focus' ) ;
65
66
fixture . detectChanges ( ) ;
66
67
67
68
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -88,11 +89,11 @@ describe('MdAutocomplete', () => {
88
89
} ) ;
89
90
90
91
it ( 'should close the panel when blurred' , async ( ( ) => {
91
- dispatchEvent ( 'focus' , input ) ;
92
+ dispatchFakeEvent ( input , 'focus' ) ;
92
93
fixture . detectChanges ( ) ;
93
94
94
95
fixture . whenStable ( ) . then ( ( ) => {
95
- dispatchEvent ( 'blur' , input ) ;
96
+ dispatchFakeEvent ( input , 'blur' ) ;
96
97
fixture . detectChanges ( ) ;
97
98
98
99
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -103,7 +104,7 @@ describe('MdAutocomplete', () => {
103
104
} ) ) ;
104
105
105
106
it ( 'should close the panel when an option is clicked' , async ( ( ) => {
106
- dispatchEvent ( 'focus' , input ) ;
107
+ dispatchFakeEvent ( input , 'focus' ) ;
107
108
fixture . detectChanges ( ) ;
108
109
109
110
fixture . whenStable ( ) . then ( ( ) => {
@@ -119,13 +120,13 @@ describe('MdAutocomplete', () => {
119
120
} ) ) ;
120
121
121
122
it ( 'should close the panel when a newly created option is clicked' , async ( ( ) => {
122
- dispatchEvent ( 'focus' , input ) ;
123
+ dispatchFakeEvent ( input , 'focus' ) ;
123
124
fixture . detectChanges ( ) ;
124
125
125
126
fixture . whenStable ( ) . then ( ( ) => {
126
127
// Filter down the option list to a subset of original options ('Alabama', 'California')
127
128
input . value = 'al' ;
128
- dispatchEvent ( ' input' , input ) ;
129
+ dispatchFakeEvent ( input , ' input' ) ;
129
130
fixture . detectChanges ( ) ;
130
131
131
132
let options =
@@ -135,7 +136,7 @@ describe('MdAutocomplete', () => {
135
136
// Changing value from 'Alabama' to 'al' to re-populate the option list,
136
137
// ensuring that 'California' is created new.
137
138
input . value = 'al' ;
138
- dispatchEvent ( ' input' , input ) ;
139
+ dispatchFakeEvent ( input , ' input' ) ;
139
140
fixture . detectChanges ( ) ;
140
141
141
142
fixture . whenStable ( ) . then ( ( ) => {
@@ -166,13 +167,13 @@ describe('MdAutocomplete', () => {
166
167
} ) ;
167
168
168
169
it ( 'should close the panel when the options list is empty' , async ( ( ) => {
169
- dispatchEvent ( 'focus' , input ) ;
170
+ dispatchFakeEvent ( input , 'focus' ) ;
170
171
fixture . detectChanges ( ) ;
171
172
172
173
fixture . whenStable ( ) . then ( ( ) => {
173
174
// Filter down the option list such that no options match the value
174
175
input . value = 'af' ;
175
- dispatchEvent ( ' input' , input ) ;
176
+ dispatchFakeEvent ( input , ' input' ) ;
176
177
fixture . detectChanges ( ) ;
177
178
178
179
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -232,14 +233,14 @@ describe('MdAutocomplete', () => {
232
233
fixture . detectChanges ( ) ;
233
234
234
235
input . value = 'a' ;
235
- dispatchEvent ( ' input' , input ) ;
236
+ dispatchFakeEvent ( input , ' input' ) ;
236
237
fixture . detectChanges ( ) ;
237
238
238
239
expect ( fixture . componentInstance . stateCtrl . value )
239
240
. toEqual ( 'a' , 'Expected control value to be updated as user types.' ) ;
240
241
241
242
input . value = 'al' ;
242
- dispatchEvent ( ' input' , input ) ;
243
+ dispatchFakeEvent ( input , ' input' ) ;
243
244
fixture . detectChanges ( ) ;
244
245
245
246
expect ( fixture . componentInstance . stateCtrl . value )
@@ -273,7 +274,7 @@ describe('MdAutocomplete', () => {
273
274
fixture . detectChanges ( ) ;
274
275
275
276
input . value = 'Californi' ;
276
- dispatchEvent ( ' input' , input ) ;
277
+ dispatchFakeEvent ( input , ' input' ) ;
277
278
fixture . detectChanges ( ) ;
278
279
279
280
expect ( fixture . componentInstance . stateCtrl . value )
@@ -330,7 +331,7 @@ describe('MdAutocomplete', () => {
330
331
331
332
it ( 'should clear the text field if value is reset programmatically' , async ( ( ) => {
332
333
input . value = 'Alabama' ;
333
- dispatchEvent ( ' input' , input ) ;
334
+ dispatchFakeEvent ( input , ' input' ) ;
334
335
fixture . detectChanges ( ) ;
335
336
336
337
fixture . whenStable ( ) . then ( ( ) => {
@@ -367,7 +368,7 @@ describe('MdAutocomplete', () => {
367
368
. toBe ( false , `Expected control to start out pristine.` ) ;
368
369
369
370
input . value = 'a' ;
370
- dispatchEvent ( ' input' , input ) ;
371
+ dispatchFakeEvent ( input , ' input' ) ;
371
372
fixture . detectChanges ( ) ;
372
373
373
374
expect ( fixture . componentInstance . stateCtrl . dirty )
@@ -409,7 +410,7 @@ describe('MdAutocomplete', () => {
409
410
expect ( fixture . componentInstance . stateCtrl . touched )
410
411
. toBe ( false , `Expected control to start out untouched.` ) ;
411
412
412
- dispatchEvent ( 'blur' , input ) ;
413
+ dispatchFakeEvent ( input , 'blur' ) ;
413
414
fixture . detectChanges ( ) ;
414
415
415
416
expect ( fixture . componentInstance . stateCtrl . touched )
@@ -429,8 +430,8 @@ describe('MdAutocomplete', () => {
429
430
fixture . detectChanges ( ) ;
430
431
431
432
input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
432
- DOWN_ARROW_EVENT = new FakeKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
433
- ENTER_EVENT = new FakeKeyboardEvent ( ENTER ) as KeyboardEvent ;
433
+ DOWN_ARROW_EVENT = new MockKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
434
+ ENTER_EVENT = new MockKeyboardEvent ( ENTER ) as KeyboardEvent ;
434
435
435
436
fixture . componentInstance . trigger . openPanel ( ) ;
436
437
fixture . detectChanges ( ) ;
@@ -491,7 +492,7 @@ describe('MdAutocomplete', () => {
491
492
const optionEls =
492
493
overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
493
494
494
- const UP_ARROW_EVENT = new FakeKeyboardEvent ( UP_ARROW ) as KeyboardEvent ;
495
+ const UP_ARROW_EVENT = new MockKeyboardEvent ( UP_ARROW ) as KeyboardEvent ;
495
496
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
496
497
497
498
fixture . whenStable ( ) . then ( ( ) => {
@@ -522,7 +523,7 @@ describe('MdAutocomplete', () => {
522
523
523
524
fixture . whenStable ( ) . then ( ( ) => {
524
525
input . value = 'o' ;
525
- dispatchEvent ( ' input' , input ) ;
526
+ dispatchFakeEvent ( input , ' input' ) ;
526
527
fixture . detectChanges ( ) ;
527
528
528
529
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
@@ -556,10 +557,10 @@ describe('MdAutocomplete', () => {
556
557
it ( 'should fill the text field, not select an option, when SPACE is entered' , async ( ( ) => {
557
558
fixture . whenStable ( ) . then ( ( ) => {
558
559
input . value = 'New' ;
559
- dispatchEvent ( ' input' , input ) ;
560
+ dispatchFakeEvent ( input , ' input' ) ;
560
561
fixture . detectChanges ( ) ;
561
562
562
- const SPACE_EVENT = new FakeKeyboardEvent ( SPACE ) as KeyboardEvent ;
563
+ const SPACE_EVENT = new MockKeyboardEvent ( SPACE ) as KeyboardEvent ;
563
564
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
564
565
fixture . componentInstance . trigger . _handleKeydown ( SPACE_EVENT ) ;
565
566
fixture . detectChanges ( ) ;
@@ -595,7 +596,7 @@ describe('MdAutocomplete', () => {
595
596
. toEqual ( '' , `Expected panel to close after ENTER key.` ) ;
596
597
597
598
input . value = 'Alabam' ;
598
- dispatchEvent ( ' input' , input ) ;
599
+ dispatchFakeEvent ( input , ' input' ) ;
599
600
fixture . detectChanges ( ) ;
600
601
601
602
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -669,7 +670,7 @@ describe('MdAutocomplete', () => {
669
670
expect ( input . hasAttribute ( 'aria-activedescendant' ) )
670
671
. toBe ( false , 'Expected aria-activedescendant to be absent if no active item.' ) ;
671
672
672
- const DOWN_ARROW_EVENT = new FakeKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
673
+ const DOWN_ARROW_EVENT = new MockKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
673
674
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
674
675
fixture . detectChanges ( ) ;
675
676
@@ -773,7 +774,7 @@ describe('MdAutocomplete', () => {
773
774
774
775
fixture . whenStable ( ) . then ( ( ) => {
775
776
input . value = 'f' ;
776
- dispatchEvent ( ' input' , input ) ;
777
+ dispatchFakeEvent ( input , ' input' ) ;
777
778
fixture . detectChanges ( ) ;
778
779
779
780
const inputTop = input . getBoundingClientRect ( ) . top ;
@@ -848,24 +849,8 @@ class SimpleAutocomplete implements OnDestroy {
848
849
849
850
}
850
851
851
-
852
-
853
- /**
854
- * TODO: Move this to core testing utility until Angular has event faking
855
- * support.
856
- *
857
- * Dispatches an event from an element.
858
- * @param eventName Name of the event
859
- * @param element The element from which the event will be dispatched.
860
- */
861
- function dispatchEvent ( eventName : string , element : HTMLElement ) : void {
862
- let event = document . createEvent ( 'Event' ) ;
863
- event . initEvent ( eventName , true , true ) ;
864
- element . dispatchEvent ( event ) ;
865
- }
866
-
867
852
/** This is a mock keyboard event to test keyboard events in the autocomplete. */
868
- class FakeKeyboardEvent {
853
+ class MockKeyboardEvent {
869
854
constructor ( public keyCode : number ) { }
870
855
preventDefault ( ) { }
871
856
}
0 commit comments