55 dispatchMouseEvent ,
66 patchElementFocus ,
77} from '@angular/cdk/testing' ;
8- import { Component } from '@angular/core' ;
8+ import { Component , ElementRef , ViewChild } from '@angular/core' ;
99import { ComponentFixture , fakeAsync , inject , TestBed , tick } from '@angular/core/testing' ;
1010import { By } from '@angular/platform-browser' ;
1111import { FocusMonitor , FocusOrigin , TOUCH_BUFFER_MS } from './focus-monitor' ;
@@ -212,7 +212,6 @@ describe('FocusMonitor', () => {
212212
213213 expect ( buttonElement . classList . length ) . toBe ( 0 , 'button should not have any focus classes' ) ;
214214 } ) ) ;
215-
216215} ) ;
217216
218217
@@ -393,6 +392,39 @@ describe('cdkMonitorFocus', () => {
393392 } ) ;
394393} ) ;
395394
395+ describe ( 'FocusMonitor observable stream' , ( ) => {
396+ let fixture : ComponentFixture < MonitoredElementRequiringChangeDetection > ;
397+ let buttonElement : HTMLElement ;
398+ let focusMonitor : FocusMonitor ;
399+
400+ beforeEach ( ( ) => {
401+ TestBed . configureTestingModule ( {
402+ imports : [ A11yModule ] ,
403+ declarations : [
404+ MonitoredElementRequiringChangeDetection ,
405+ ] ,
406+ } ) . compileComponents ( ) ;
407+ } ) ;
408+
409+ beforeEach ( inject ( [ FocusMonitor ] , ( fm : FocusMonitor ) => {
410+ fixture = TestBed . createComponent ( MonitoredElementRequiringChangeDetection ) ;
411+ fixture . detectChanges ( ) ;
412+
413+ buttonElement = fixture . componentInstance . button . nativeElement ;
414+ focusMonitor = fm ;
415+
416+ patchElementFocus ( buttonElement ) ;
417+ } ) ) ;
418+
419+ it ( 'should emit inside the NgZone' , ( ) => {
420+ fixture . detectChanges ( ) ;
421+ expect ( buttonElement . innerText ) . toBe ( '' ) ;
422+ buttonElement . focus ( ) ;
423+ fixture . detectChanges ( ) ;
424+ expect ( buttonElement . innerText ) . toBe ( 'program' ) ;
425+ } ) ;
426+ } ) ;
427+
396428
397429@Component ( {
398430 template : `<button>focus me!</button>`
@@ -418,3 +450,21 @@ class ComplexComponentWithMonitorElementFocus {}
418450 template : `<div tabindex="0" cdkMonitorSubtreeFocus><button></button></div>`
419451} )
420452class ComplexComponentWithMonitorSubtreeFocus { }
453+
454+ @Component ( {
455+ template : `<button #b>{{origin}}</button>`
456+ } )
457+ class MonitoredElementRequiringChangeDetection {
458+ @ViewChild ( 'b' ) button : ElementRef ;
459+ origin : string ;
460+
461+ constructor ( private _focusMonitor : FocusMonitor ) { }
462+
463+ ngOnInit ( ) {
464+ this . _focusMonitor . monitor ( this . button . nativeElement ) . subscribe ( o => this . origin = o || '' ) ;
465+ }
466+
467+ ngOnDestroy ( ) {
468+ this . _focusMonitor . stopMonitoring ( this . button . nativeElement ) ;
469+ }
470+ }
0 commit comments