@@ -6,10 +6,10 @@ import {
6
6
patchElementFocus ,
7
7
} from '@angular/cdk/testing' ;
8
8
import { Component } from '@angular/core' ;
9
- import { ComponentFixture , fakeAsync , inject , TestBed , tick } from '@angular/core/testing' ;
9
+ import { ComponentFixture , fakeAsync , flush , inject , TestBed , tick } from '@angular/core/testing' ;
10
10
import { By } from '@angular/platform-browser' ;
11
- import { FocusMonitor , FocusOrigin , TOUCH_BUFFER_MS } from './focus-monitor' ;
12
11
import { A11yModule } from '../index' ;
12
+ import { FocusMonitor , FocusOrigin , TOUCH_BUFFER_MS } from './focus-monitor' ;
13
13
14
14
15
15
describe ( 'FocusMonitor' , ( ) => {
@@ -224,6 +224,7 @@ describe('cdkMonitorFocus', () => {
224
224
ButtonWithFocusClasses ,
225
225
ComplexComponentWithMonitorElementFocus ,
226
226
ComplexComponentWithMonitorSubtreeFocus ,
227
+ ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus ,
227
228
] ,
228
229
} ) . compileComponents ( ) ;
229
230
} ) ;
@@ -391,6 +392,36 @@ describe('cdkMonitorFocus', () => {
391
392
expect ( parentElement . classList . length ) . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
392
393
} ) ) ;
393
394
} ) ;
395
+
396
+ describe ( 'complex component with cdkMonitorSubtreeFocus and cdkMonitorElementFocus' , ( ) => {
397
+ let fixture : ComponentFixture < ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus > ;
398
+ let parentElement : HTMLElement ;
399
+ let childElement : HTMLElement ;
400
+ let focusMonitor : FocusMonitor ;
401
+
402
+ beforeEach ( inject ( [ FocusMonitor ] , ( fm : FocusMonitor ) => {
403
+ focusMonitor = fm ;
404
+ fixture =
405
+ TestBed . createComponent ( ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus ) ;
406
+ fixture . detectChanges ( ) ;
407
+
408
+ parentElement = fixture . debugElement . query ( By . css ( 'div' ) ) . nativeElement ;
409
+ childElement = fixture . debugElement . query ( By . css ( 'button' ) ) . nativeElement ;
410
+
411
+ patchElementFocus ( parentElement ) ;
412
+ patchElementFocus ( childElement ) ;
413
+ } ) ) ;
414
+
415
+ it ( 'should add keyboard focus classes on both elements when child is focused via keyboard' ,
416
+ fakeAsync ( ( ) => {
417
+ focusMonitor . focusVia ( childElement , 'keyboard' ) ;
418
+ fixture . detectChanges ( ) ;
419
+ flush ( ) ;
420
+
421
+ expect ( parentElement . classList ) . toContain ( 'cdk-keyboard-focused' ) ;
422
+ expect ( childElement . classList ) . toContain ( 'cdk-keyboard-focused' ) ;
423
+ } ) ) ;
424
+ } ) ;
394
425
} ) ;
395
426
396
427
@@ -418,3 +449,8 @@ class ComplexComponentWithMonitorElementFocus {}
418
449
template : `<div tabindex="0" cdkMonitorSubtreeFocus><button></button></div>`
419
450
} )
420
451
class ComplexComponentWithMonitorSubtreeFocus { }
452
+
453
+ @Component ( {
454
+ template : `<div cdkMonitorSubtreeFocus><button cdkMonitorElementFocus></button></div>`
455
+ } )
456
+ class ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus { }
0 commit comments