8
8
MatExpansionPanel ,
9
9
MatExpansionPanelHeader ,
10
10
} from './index' ;
11
- import { dispatchKeyboardEvent } from '@angular/cdk/testing' ;
11
+ import { dispatchKeyboardEvent , createKeyboardEvent , dispatchEvent } from '@angular/cdk/testing' ;
12
12
import { DOWN_ARROW , UP_ARROW , HOME , END } from '@angular/cdk/keycodes' ;
13
13
import { FocusMonitor } from '@angular/cdk/a11y' ;
14
14
@@ -201,10 +201,31 @@ describe('MatAccordion', () => {
201
201
const headers = fixture . componentInstance . headers . toArray ( ) ;
202
202
203
203
headers . forEach ( header => spyOn ( header , 'focus' ) ) ;
204
- dispatchKeyboardEvent ( headerElements [ headerElements . length - 1 ] . nativeElement , 'keydown' , HOME ) ;
204
+ const event = dispatchKeyboardEvent (
205
+ headerElements [ headerElements . length - 1 ] . nativeElement , 'keydown' , HOME ) ;
205
206
fixture . detectChanges ( ) ;
206
207
207
208
expect ( headers [ 0 ] . focus ) . toHaveBeenCalledTimes ( 1 ) ;
209
+ expect ( event . defaultPrevented ) . toBe ( true ) ;
210
+ } ) ;
211
+
212
+ it ( 'should not handle the home key when it is pressed with a modifier' , ( ) => {
213
+ const fixture = TestBed . createComponent ( SetOfItems ) ;
214
+ fixture . detectChanges ( ) ;
215
+
216
+ const headerElements = fixture . debugElement . queryAll ( By . css ( 'mat-expansion-panel-header' ) ) ;
217
+ const headers = fixture . componentInstance . headers . toArray ( ) ;
218
+
219
+ headers . forEach ( header => spyOn ( header , 'focus' ) ) ;
220
+ const eventTarget = headerElements [ headerElements . length - 1 ] . nativeElement ;
221
+ const event = createKeyboardEvent ( 'keydown' , HOME , eventTarget ) ;
222
+ Object . defineProperty ( event , 'altKey' , { get : ( ) => true } ) ;
223
+
224
+ dispatchEvent ( eventTarget , event ) ;
225
+ fixture . detectChanges ( ) ;
226
+
227
+ expect ( headers [ 0 ] . focus ) . not . toHaveBeenCalled ( ) ;
228
+ expect ( event . defaultPrevented ) . toBe ( false ) ;
208
229
} ) ;
209
230
210
231
it ( 'should focus the last header when pressing the end key' , ( ) => {
@@ -215,10 +236,31 @@ describe('MatAccordion', () => {
215
236
const headers = fixture . componentInstance . headers . toArray ( ) ;
216
237
217
238
headers . forEach ( header => spyOn ( header , 'focus' ) ) ;
218
- dispatchKeyboardEvent ( headerElements [ 0 ] . nativeElement , 'keydown' , END ) ;
239
+ const event = dispatchKeyboardEvent ( headerElements [ 0 ] . nativeElement , 'keydown' , END ) ;
219
240
fixture . detectChanges ( ) ;
220
241
221
242
expect ( headers [ headers . length - 1 ] . focus ) . toHaveBeenCalledTimes ( 1 ) ;
243
+ expect ( event . defaultPrevented ) . toBe ( true ) ;
244
+ } ) ;
245
+
246
+ it ( 'should not handle the end key when it is pressed with a modifier' , ( ) => {
247
+ const fixture = TestBed . createComponent ( SetOfItems ) ;
248
+ fixture . detectChanges ( ) ;
249
+
250
+ const headerElements = fixture . debugElement . queryAll ( By . css ( 'mat-expansion-panel-header' ) ) ;
251
+ const headers = fixture . componentInstance . headers . toArray ( ) ;
252
+
253
+ headers . forEach ( header => spyOn ( header , 'focus' ) ) ;
254
+
255
+ const eventTarget = headerElements [ 0 ] . nativeElement ;
256
+ const event = createKeyboardEvent ( 'keydown' , END , eventTarget ) ;
257
+ Object . defineProperty ( event , 'altKey' , { get : ( ) => true } ) ;
258
+
259
+ dispatchEvent ( eventTarget , event ) ;
260
+ fixture . detectChanges ( ) ;
261
+
262
+ expect ( headers [ headers . length - 1 ] . focus ) . not . toHaveBeenCalled ( ) ;
263
+ expect ( event . defaultPrevented ) . toBe ( false ) ;
222
264
} ) ;
223
265
224
266
} ) ;
0 commit comments