88 MatExpansionPanel ,
99 MatExpansionPanelHeader ,
1010} from './index' ;
11- import { dispatchKeyboardEvent } from '@angular/cdk/testing' ;
11+ import { dispatchKeyboardEvent , createKeyboardEvent , dispatchEvent } from '@angular/cdk/testing' ;
1212import { DOWN_ARROW , UP_ARROW , HOME , END } from '@angular/cdk/keycodes' ;
1313import { FocusMonitor } from '@angular/cdk/a11y' ;
1414
@@ -201,10 +201,31 @@ describe('MatAccordion', () => {
201201 const headers = fixture . componentInstance . headers . toArray ( ) ;
202202
203203 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 ) ;
205206 fixture . detectChanges ( ) ;
206207
207208 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 ) ;
208229 } ) ;
209230
210231 it ( 'should focus the last header when pressing the end key' , ( ) => {
@@ -215,10 +236,31 @@ describe('MatAccordion', () => {
215236 const headers = fixture . componentInstance . headers . toArray ( ) ;
216237
217238 headers . forEach ( header => spyOn ( header , 'focus' ) ) ;
218- dispatchKeyboardEvent ( headerElements [ 0 ] . nativeElement , 'keydown' , END ) ;
239+ const event = dispatchKeyboardEvent ( headerElements [ 0 ] . nativeElement , 'keydown' , END ) ;
219240 fixture . detectChanges ( ) ;
220241
221242 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 ) ;
222264 } ) ;
223265
224266} ) ;
0 commit comments