1
1
import { async , ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
2
2
import { Component , DebugElement , QueryList } from '@angular/core' ;
3
3
import { By } from '@angular/platform-browser' ;
4
+ import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
4
5
import { MdChip , MdChipList , MdChipsModule } from './index' ;
5
6
import { FocusKeyManager } from '../core/a11y/focus-key-manager' ;
6
- import { SPACE , LEFT_ARROW , RIGHT_ARROW , TAB } from '../core/keyboard/keycodes' ;
7
7
import { createKeyboardEvent } from '../core/testing/event-objects' ;
8
8
9
9
import { MdInputModule } from '../input/index' ;
10
- import { FakeEvent } from '../core/a11y/list-key-manager.spec' ;
11
- import { LEFT_ARROW , RIGHT_ARROW , BACKSPACE , DELETE } from '../core/keyboard/keycodes' ;
10
+ import { LEFT_ARROW , RIGHT_ARROW , BACKSPACE , DELETE , SPACE , TAB } from '../core/keyboard/keycodes' ;
12
11
import { Dir } from '../core/rtl/dir' ;
13
12
14
- export class FakeKeyboardEvent extends FakeEvent {
15
- constructor ( keyCode : number , protected target : HTMLElement ) {
16
- super ( keyCode ) ;
17
-
18
- this . target = target ;
19
- }
20
- }
21
-
22
13
describe ( 'MdChipList' , ( ) => {
23
14
let fixture : ComponentFixture < any > ;
24
15
let chipListDebugElement : DebugElement ;
@@ -32,9 +23,9 @@ describe('MdChipList', () => {
32
23
33
24
beforeEach ( async ( ( ) => {
34
25
TestBed . configureTestingModule ( {
35
- imports : [ MdChipsModule , MdInputModule ] ,
26
+ imports : [ MdChipsModule , MdInputModule , NoopAnimationsModule ] ,
36
27
declarations : [
37
- StaticChipList , StandardChipList , InputContainerChipList
28
+ StandardChipList , InputContainerChipList
38
29
] ,
39
30
providers : [ {
40
31
provide : Dir , useFactory : ( ) => {
@@ -122,10 +113,6 @@ describe('MdChipList', () => {
122
113
expect ( manager . activeItemIndex ) . toEqual ( 2 ) ;
123
114
} ) ;
124
115
125
- let LEFT_EVENT = createKeyboardEvent ( 'keydown' , LEFT_ARROW , lastNativeChip ) ;
126
- let array = chips . toArray ( ) ;
127
- let lastIndex = array . length - 1 ;
128
- let lastItem = array [ lastIndex ] ;
129
116
130
117
it ( 'focuses the previous item' , ( ) => {
131
118
let array = chips . toArray ( ) ;
@@ -153,15 +140,11 @@ describe('MdChipList', () => {
153
140
manager = chipListInstance . _keyManager ;
154
141
} ) ) ;
155
142
156
- let RIGHT_EVENT = createKeyboardEvent ( 'keydown' , RIGHT_ARROW , firstNativeChip ) ;
157
- let array = chips . toArray ( ) ;
158
- let firstItem = array [ 0 ] ;
159
-
160
143
it ( 'LEFT ARROW focuses previous item' , ( ) => {
161
144
let nativeChips = chipListNativeElement . querySelectorAll ( 'md-chip' ) ;
162
145
let lastNativeChip = nativeChips [ nativeChips . length - 1 ] as HTMLElement ;
163
146
164
- let LEFT_EVENT = new FakeKeyboardEvent ( LEFT_ARROW , lastNativeChip ) as any ;
147
+ let LEFT_EVENT = createKeyboardEvent ( 'keydown' , LEFT_ARROW , lastNativeChip ) ;
165
148
let array = chips . toArray ( ) ;
166
149
let lastIndex = array . length - 1 ;
167
150
let lastItem = array [ lastIndex ] ;
@@ -183,7 +166,7 @@ describe('MdChipList', () => {
183
166
let firstNativeChip = nativeChips [ 0 ] as HTMLElement ;
184
167
185
168
let RIGHT_EVENT : KeyboardEvent =
186
- new FakeKeyboardEvent ( RIGHT_ARROW , firstNativeChip ) as any ;
169
+ createKeyboardEvent ( 'keydown' , RIGHT_ARROW , firstNativeChip ) ;
187
170
let array = chips . toArray ( ) ;
188
171
let firstItem = array [ 0 ] ;
189
172
@@ -208,17 +191,12 @@ describe('MdChipList', () => {
208
191
manager = chipListInstance . _keyManager ;
209
192
} ) ) ;
210
193
211
- let SPACE_EVENT = createKeyboardEvent ( 'keydown' , SPACE , firstNativeChip ) ;
212
- let firstChip : MdChip = chips . toArray ( ) [ 0 ] ;
213
-
214
194
it ( 'RIGHT ARROW focuses previous item' , ( ) => {
215
- fixture . detectChanges ( ) ;
216
-
217
195
let nativeChips = chipListNativeElement . querySelectorAll ( 'md-chip' ) ;
218
196
let lastNativeChip = nativeChips [ nativeChips . length - 1 ] as HTMLElement ;
219
197
220
198
let RIGHT_EVENT : KeyboardEvent =
221
- new FakeKeyboardEvent ( RIGHT_ARROW , lastNativeChip ) as any ;
199
+ createKeyboardEvent ( 'keydown' , RIGHT_ARROW , lastNativeChip ) ;
222
200
let array = chips . toArray ( ) ;
223
201
let lastIndex = array . length - 1 ;
224
202
let lastItem = array [ lastIndex ] ;
@@ -239,7 +217,8 @@ describe('MdChipList', () => {
239
217
let nativeChips = chipListNativeElement . querySelectorAll ( 'md-chip' ) ;
240
218
let firstNativeChip = nativeChips [ 0 ] as HTMLElement ;
241
219
242
- let LEFT_EVENT : KeyboardEvent = new FakeKeyboardEvent ( LEFT_ARROW , firstNativeChip ) as any ;
220
+ let LEFT_EVENT : KeyboardEvent =
221
+ createKeyboardEvent ( 'keydown' , LEFT_ARROW , firstNativeChip ) ;
243
222
let array = chips . toArray ( ) ;
244
223
let firstItem = array [ 0 ] ;
245
224
@@ -255,18 +234,17 @@ describe('MdChipList', () => {
255
234
expect ( manager . activeItemIndex ) . toEqual ( 1 ) ;
256
235
} ) ;
257
236
258
- } ) ;
259
-
260
- it ( 'allow focus to escape when tabbing away' , fakeAsync ( ( ) => {
261
- chipListInstance . _keyManager . onKeydown ( createKeyboardEvent ( 'keydown' , TAB ) ) ;
237
+ it ( 'allow focus to escape when tabbing away' , fakeAsync ( ( ) => {
238
+ chipListInstance . _keyManager . onKeydown ( createKeyboardEvent ( 'keydown' , TAB ) ) ;
262
239
263
- expect ( chipListInstance . _tabIndex )
240
+ expect ( chipListInstance . _tabIndex )
264
241
. toBe ( - 1 , 'Expected tabIndex to be set to -1 temporarily.' ) ;
265
242
266
- tick ( ) ;
243
+ tick ( ) ;
267
244
268
- expect ( chipListInstance . _tabIndex ) . toBe ( 0 , 'Expected tabIndex to be reset back to 0' ) ;
269
- } ) ) ;
245
+ expect ( chipListInstance . _tabIndex ) . toBe ( 0 , 'Expected tabIndex to be reset back to 0' ) ;
246
+ } ) ) ;
247
+ } ) ;
270
248
} ) ;
271
249
} ) ;
272
250
@@ -290,7 +268,8 @@ describe('MdChipList', () => {
290
268
291
269
it ( 'DELETE focuses the last chip' , ( ) => {
292
270
let nativeInput = chipListNativeElement . querySelector ( 'input' ) ;
293
- let DELETE_EVENT : KeyboardEvent = new FakeKeyboardEvent ( DELETE , nativeInput ) as any ;
271
+ let DELETE_EVENT : KeyboardEvent =
272
+ createKeyboardEvent ( 'keydown' , DELETE , nativeInput ) ;
294
273
295
274
// Focus the input
296
275
nativeInput . focus ( ) ;
@@ -306,7 +285,8 @@ describe('MdChipList', () => {
306
285
307
286
it ( 'BACKSPACE focuses the last chip' , ( ) => {
308
287
let nativeInput = chipListNativeElement . querySelector ( 'input' ) ;
309
- let BACKSPACE_EVENT : KeyboardEvent = new FakeKeyboardEvent ( BACKSPACE , nativeInput ) as any ;
288
+ let BACKSPACE_EVENT : KeyboardEvent =
289
+ createKeyboardEvent ( 'keydown' , BACKSPACE , nativeInput ) ;
310
290
311
291
// Focus the input
312
292
nativeInput . focus ( ) ;
@@ -376,7 +356,6 @@ class StandardChipList {
376
356
<md-chip>Chip 1</md-chip>
377
357
<md-chip>Chip 1</md-chip>
378
358
<md-chip>Chip 1</md-chip>
379
-
380
359
<input mdInput name="test" />
381
360
</md-chip-list>
382
361
</md-input-container>
0 commit comments