@@ -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,7 +167,7 @@ describe('MdAutocomplete', () => {
166
167
} ) ;
167
168
168
169
it ( 'should hide the panel when the options list is empty' , async ( ( ) => {
169
- dispatchEvent ( 'focus' , input ) ;
170
+ dispatchFakeEvent ( input , 'focus' ) ;
170
171
171
172
fixture . whenStable ( ) . then ( ( ) => {
172
173
fixture . detectChanges ( ) ;
@@ -178,7 +179,7 @@ describe('MdAutocomplete', () => {
178
179
179
180
// Filter down the option list such that no options match the value
180
181
input . value = 'af' ;
181
- dispatchEvent ( ' input' , input ) ;
182
+ dispatchFakeEvent ( input , ' input' ) ;
182
183
fixture . detectChanges ( ) ;
183
184
184
185
fixture . whenStable ( ) . then ( ( ) => {
@@ -242,14 +243,14 @@ describe('MdAutocomplete', () => {
242
243
fixture . detectChanges ( ) ;
243
244
244
245
input . value = 'a' ;
245
- dispatchEvent ( ' input' , input ) ;
246
+ dispatchFakeEvent ( input , ' input' ) ;
246
247
fixture . detectChanges ( ) ;
247
248
248
249
expect ( fixture . componentInstance . stateCtrl . value )
249
250
. toEqual ( 'a' , 'Expected control value to be updated as user types.' ) ;
250
251
251
252
input . value = 'al' ;
252
- dispatchEvent ( ' input' , input ) ;
253
+ dispatchFakeEvent ( input , ' input' ) ;
253
254
fixture . detectChanges ( ) ;
254
255
255
256
expect ( fixture . componentInstance . stateCtrl . value )
@@ -283,7 +284,7 @@ describe('MdAutocomplete', () => {
283
284
fixture . detectChanges ( ) ;
284
285
285
286
input . value = 'Californi' ;
286
- dispatchEvent ( ' input' , input ) ;
287
+ dispatchFakeEvent ( input , ' input' ) ;
287
288
fixture . detectChanges ( ) ;
288
289
289
290
expect ( fixture . componentInstance . stateCtrl . value )
@@ -340,7 +341,7 @@ describe('MdAutocomplete', () => {
340
341
341
342
it ( 'should clear the text field if value is reset programmatically' , async ( ( ) => {
342
343
input . value = 'Alabama' ;
343
- dispatchEvent ( ' input' , input ) ;
344
+ dispatchFakeEvent ( input , ' input' ) ;
344
345
fixture . detectChanges ( ) ;
345
346
346
347
fixture . whenStable ( ) . then ( ( ) => {
@@ -377,7 +378,7 @@ describe('MdAutocomplete', () => {
377
378
. toBe ( false , `Expected control to start out pristine.` ) ;
378
379
379
380
input . value = 'a' ;
380
- dispatchEvent ( ' input' , input ) ;
381
+ dispatchFakeEvent ( input , ' input' ) ;
381
382
fixture . detectChanges ( ) ;
382
383
383
384
expect ( fixture . componentInstance . stateCtrl . dirty )
@@ -419,7 +420,7 @@ describe('MdAutocomplete', () => {
419
420
expect ( fixture . componentInstance . stateCtrl . touched )
420
421
. toBe ( false , `Expected control to start out untouched.` ) ;
421
422
422
- dispatchEvent ( 'blur' , input ) ;
423
+ dispatchFakeEvent ( input , 'blur' ) ;
423
424
fixture . detectChanges ( ) ;
424
425
425
426
expect ( fixture . componentInstance . stateCtrl . touched )
@@ -439,8 +440,8 @@ describe('MdAutocomplete', () => {
439
440
fixture . detectChanges ( ) ;
440
441
441
442
input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
442
- DOWN_ARROW_EVENT = new FakeKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
443
- ENTER_EVENT = new FakeKeyboardEvent ( ENTER ) as KeyboardEvent ;
443
+ DOWN_ARROW_EVENT = new MockKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
444
+ ENTER_EVENT = new MockKeyboardEvent ( ENTER ) as KeyboardEvent ;
444
445
445
446
fixture . componentInstance . trigger . openPanel ( ) ;
446
447
fixture . detectChanges ( ) ;
@@ -501,7 +502,7 @@ describe('MdAutocomplete', () => {
501
502
const optionEls =
502
503
overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
503
504
504
- const UP_ARROW_EVENT = new FakeKeyboardEvent ( UP_ARROW ) as KeyboardEvent ;
505
+ const UP_ARROW_EVENT = new MockKeyboardEvent ( UP_ARROW ) as KeyboardEvent ;
505
506
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
506
507
507
508
fixture . whenStable ( ) . then ( ( ) => {
@@ -532,7 +533,7 @@ describe('MdAutocomplete', () => {
532
533
533
534
fixture . whenStable ( ) . then ( ( ) => {
534
535
input . value = 'o' ;
535
- dispatchEvent ( ' input' , input ) ;
536
+ dispatchFakeEvent ( input , ' input' ) ;
536
537
fixture . detectChanges ( ) ;
537
538
538
539
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
@@ -566,10 +567,10 @@ describe('MdAutocomplete', () => {
566
567
it ( 'should fill the text field, not select an option, when SPACE is entered' , async ( ( ) => {
567
568
fixture . whenStable ( ) . then ( ( ) => {
568
569
input . value = 'New' ;
569
- dispatchEvent ( ' input' , input ) ;
570
+ dispatchFakeEvent ( input , ' input' ) ;
570
571
fixture . detectChanges ( ) ;
571
572
572
- const SPACE_EVENT = new FakeKeyboardEvent ( SPACE ) as KeyboardEvent ;
573
+ const SPACE_EVENT = new MockKeyboardEvent ( SPACE ) as KeyboardEvent ;
573
574
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
574
575
fixture . componentInstance . trigger . _handleKeydown ( SPACE_EVENT ) ;
575
576
fixture . detectChanges ( ) ;
@@ -605,7 +606,7 @@ describe('MdAutocomplete', () => {
605
606
. toEqual ( '' , `Expected panel to close after ENTER key.` ) ;
606
607
607
608
input . value = 'Alabam' ;
608
- dispatchEvent ( ' input' , input ) ;
609
+ dispatchFakeEvent ( input , ' input' ) ;
609
610
fixture . detectChanges ( ) ;
610
611
611
612
expect ( fixture . componentInstance . trigger . panelOpen )
@@ -679,7 +680,7 @@ describe('MdAutocomplete', () => {
679
680
expect ( input . hasAttribute ( 'aria-activedescendant' ) )
680
681
. toBe ( false , 'Expected aria-activedescendant to be absent if no active item.' ) ;
681
682
682
- const DOWN_ARROW_EVENT = new FakeKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
683
+ const DOWN_ARROW_EVENT = new MockKeyboardEvent ( DOWN_ARROW ) as KeyboardEvent ;
683
684
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
684
685
fixture . detectChanges ( ) ;
685
686
@@ -783,7 +784,7 @@ describe('MdAutocomplete', () => {
783
784
784
785
fixture . whenStable ( ) . then ( ( ) => {
785
786
input . value = 'f' ;
786
- dispatchEvent ( ' input' , input ) ;
787
+ dispatchFakeEvent ( input , ' input' ) ;
787
788
fixture . detectChanges ( ) ;
788
789
789
790
const inputTop = input . getBoundingClientRect ( ) . top ;
@@ -809,7 +810,7 @@ describe('MdAutocomplete', () => {
809
810
810
811
const input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
811
812
input . value = 'd' ;
812
- dispatchEvent ( ' input' , input ) ;
813
+ dispatchFakeEvent ( input , ' input' ) ;
813
814
fixture . detectChanges ( ) ;
814
815
815
816
const options =
@@ -906,22 +907,8 @@ class AutocompleteWithoutForms {
906
907
907
908
}
908
909
909
- /**
910
- * TODO: Move this to core testing utility until Angular has event faking
911
- * support.
912
- *
913
- * Dispatches an event from an element.
914
- * @param eventName Name of the event
915
- * @param element The element from which the event will be dispatched.
916
- */
917
- function dispatchEvent ( eventName : string , element : HTMLElement ) : void {
918
- let event = document . createEvent ( 'Event' ) ;
919
- event . initEvent ( eventName , true , true ) ;
920
- element . dispatchEvent ( event ) ;
921
- }
922
-
923
910
/** This is a mock keyboard event to test keyboard events in the autocomplete. */
924
- class FakeKeyboardEvent {
911
+ class MockKeyboardEvent {
925
912
constructor ( public keyCode : number ) { }
926
913
preventDefault ( ) { }
927
914
}
0 commit comments