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 : ( ) => {
@@ -103,10 +94,6 @@ describe('MdChipList', () => {
103
94
expect ( manager . activeItemIndex ) . toEqual ( 2 ) ;
104
95
} ) ;
105
96
106
- let LEFT_EVENT = createKeyboardEvent ( 'keydown' , LEFT_ARROW , lastNativeChip ) ;
107
- let array = chips . toArray ( ) ;
108
- let lastIndex = array . length - 1 ;
109
- let lastItem = array [ lastIndex ] ;
110
97
111
98
it ( 'focuses the previous item' , ( ) => {
112
99
let array = chips . toArray ( ) ;
@@ -134,15 +121,11 @@ describe('MdChipList', () => {
134
121
manager = chipListInstance . _keyManager ;
135
122
} ) ) ;
136
123
137
- let RIGHT_EVENT = createKeyboardEvent ( 'keydown' , RIGHT_ARROW , firstNativeChip ) ;
138
- let array = chips . toArray ( ) ;
139
- let firstItem = array [ 0 ] ;
140
-
141
124
it ( 'LEFT ARROW focuses previous item' , ( ) => {
142
125
let nativeChips = chipListNativeElement . querySelectorAll ( 'md-chip' ) ;
143
126
let lastNativeChip = nativeChips [ nativeChips . length - 1 ] as HTMLElement ;
144
127
145
- let LEFT_EVENT = new FakeKeyboardEvent ( LEFT_ARROW , lastNativeChip ) as any ;
128
+ let LEFT_EVENT = createKeyboardEvent ( 'keydown' , LEFT_ARROW , lastNativeChip ) ;
146
129
let array = chips . toArray ( ) ;
147
130
let lastIndex = array . length - 1 ;
148
131
let lastItem = array [ lastIndex ] ;
@@ -164,7 +147,7 @@ describe('MdChipList', () => {
164
147
let firstNativeChip = nativeChips [ 0 ] as HTMLElement ;
165
148
166
149
let RIGHT_EVENT : KeyboardEvent =
167
- new FakeKeyboardEvent ( RIGHT_ARROW , firstNativeChip ) as any ;
150
+ createKeyboardEvent ( 'keydown' , RIGHT_ARROW , firstNativeChip ) ;
168
151
let array = chips . toArray ( ) ;
169
152
let firstItem = array [ 0 ] ;
170
153
@@ -189,17 +172,12 @@ describe('MdChipList', () => {
189
172
manager = chipListInstance . _keyManager ;
190
173
} ) ) ;
191
174
192
- let SPACE_EVENT = createKeyboardEvent ( 'keydown' , SPACE , firstNativeChip ) ;
193
- let firstChip : MdChip = chips . toArray ( ) [ 0 ] ;
194
-
195
175
it ( 'RIGHT ARROW focuses previous item' , ( ) => {
196
- fixture . detectChanges ( ) ;
197
-
198
176
let nativeChips = chipListNativeElement . querySelectorAll ( 'md-chip' ) ;
199
177
let lastNativeChip = nativeChips [ nativeChips . length - 1 ] as HTMLElement ;
200
178
201
179
let RIGHT_EVENT : KeyboardEvent =
202
- new FakeKeyboardEvent ( RIGHT_ARROW , lastNativeChip ) as any ;
180
+ createKeyboardEvent ( 'keydown' , RIGHT_ARROW , lastNativeChip ) ;
203
181
let array = chips . toArray ( ) ;
204
182
let lastIndex = array . length - 1 ;
205
183
let lastItem = array [ lastIndex ] ;
@@ -220,7 +198,8 @@ describe('MdChipList', () => {
220
198
let nativeChips = chipListNativeElement . querySelectorAll ( 'md-chip' ) ;
221
199
let firstNativeChip = nativeChips [ 0 ] as HTMLElement ;
222
200
223
- let LEFT_EVENT : KeyboardEvent = new FakeKeyboardEvent ( LEFT_ARROW , firstNativeChip ) as any ;
201
+ let LEFT_EVENT : KeyboardEvent =
202
+ createKeyboardEvent ( 'keydown' , LEFT_ARROW , firstNativeChip ) ;
224
203
let array = chips . toArray ( ) ;
225
204
let firstItem = array [ 0 ] ;
226
205
@@ -236,18 +215,17 @@ describe('MdChipList', () => {
236
215
expect ( manager . activeItemIndex ) . toEqual ( 1 ) ;
237
216
} ) ;
238
217
239
- } ) ;
240
-
241
- it ( 'allow focus to escape when tabbing away' , fakeAsync ( ( ) => {
242
- chipListInstance . _keyManager . onKeydown ( createKeyboardEvent ( 'keydown' , TAB ) ) ;
218
+ it ( 'allow focus to escape when tabbing away' , fakeAsync ( ( ) => {
219
+ chipListInstance . _keyManager . onKeydown ( createKeyboardEvent ( 'keydown' , TAB ) ) ;
243
220
244
- expect ( chipListInstance . _tabIndex )
221
+ expect ( chipListInstance . _tabIndex )
245
222
. toBe ( - 1 , 'Expected tabIndex to be set to -1 temporarily.' ) ;
246
223
247
- tick ( ) ;
224
+ tick ( ) ;
248
225
249
- expect ( chipListInstance . _tabIndex ) . toBe ( 0 , 'Expected tabIndex to be reset back to 0' ) ;
250
- } ) ) ;
226
+ expect ( chipListInstance . _tabIndex ) . toBe ( 0 , 'Expected tabIndex to be reset back to 0' ) ;
227
+ } ) ) ;
228
+ } ) ;
251
229
} ) ;
252
230
} ) ;
253
231
@@ -271,7 +249,8 @@ describe('MdChipList', () => {
271
249
272
250
it ( 'DELETE focuses the last chip' , ( ) => {
273
251
let nativeInput = chipListNativeElement . querySelector ( 'input' ) ;
274
- let DELETE_EVENT : KeyboardEvent = new FakeKeyboardEvent ( DELETE , nativeInput ) as any ;
252
+ let DELETE_EVENT : KeyboardEvent =
253
+ createKeyboardEvent ( 'keydown' , DELETE , nativeInput ) ;
275
254
276
255
// Focus the input
277
256
nativeInput . focus ( ) ;
@@ -287,7 +266,8 @@ describe('MdChipList', () => {
287
266
288
267
it ( 'BACKSPACE focuses the last chip' , ( ) => {
289
268
let nativeInput = chipListNativeElement . querySelector ( 'input' ) ;
290
- let BACKSPACE_EVENT : KeyboardEvent = new FakeKeyboardEvent ( BACKSPACE , nativeInput ) as any ;
269
+ let BACKSPACE_EVENT : KeyboardEvent =
270
+ createKeyboardEvent ( 'keydown' , BACKSPACE , nativeInput ) ;
291
271
292
272
// Focus the input
293
273
nativeInput . focus ( ) ;
@@ -360,7 +340,6 @@ class StandardChipList {
360
340
<md-chip>Chip 1</md-chip>
361
341
<md-chip>Chip 1</md-chip>
362
342
<md-chip>Chip 1</md-chip>
363
-
364
343
<input mdInput name="test" />
365
344
</md-chip-list>
366
345
</md-input-container>
0 commit comments