@@ -6,10 +6,10 @@ import {
66 patchElementFocus ,
77} from '@angular/cdk/testing' ;
88import { 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' ;
1010import { By } from '@angular/platform-browser' ;
11- import { FocusMonitor , FocusOrigin , TOUCH_BUFFER_MS } from './focus-monitor' ;
1211import { A11yModule } from '../index' ;
12+ import { FocusMonitor , FocusOrigin , TOUCH_BUFFER_MS } from './focus-monitor' ;
1313
1414
1515describe ( 'FocusMonitor' , ( ) => {
@@ -224,6 +224,7 @@ describe('cdkMonitorFocus', () => {
224224 ButtonWithFocusClasses ,
225225 ComplexComponentWithMonitorElementFocus ,
226226 ComplexComponentWithMonitorSubtreeFocus ,
227+ ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus ,
227228 ] ,
228229 } ) . compileComponents ( ) ;
229230 } ) ;
@@ -391,6 +392,36 @@ describe('cdkMonitorFocus', () => {
391392 expect ( parentElement . classList . length ) . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
392393 } ) ) ;
393394 } ) ;
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+ } ) ;
394425} ) ;
395426
396427
@@ -418,3 +449,8 @@ class ComplexComponentWithMonitorElementFocus {}
418449 template : `<div tabindex="0" cdkMonitorSubtreeFocus><button></button></div>`
419450} )
420451class ComplexComponentWithMonitorSubtreeFocus { }
452+
453+ @Component ( {
454+ template : `<div cdkMonitorSubtreeFocus><button cdkMonitorElementFocus></button></div>`
455+ } )
456+ class ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus { }
0 commit comments