5
5
dispatchMouseEvent ,
6
6
patchElementFocus ,
7
7
} from '@angular/cdk/testing' ;
8
- import { Component } from '@angular/core' ;
8
+ import { Component , NgZone } from '@angular/core' ;
9
9
import { ComponentFixture , fakeAsync , flush , inject , TestBed , tick } from '@angular/core/testing' ;
10
10
import { By } from '@angular/platform-browser' ;
11
11
import { A11yModule } from '../index' ;
@@ -212,7 +212,6 @@ describe('FocusMonitor', () => {
212
212
213
213
expect ( buttonElement . classList . length ) . toBe ( 0 , 'button should not have any focus classes' ) ;
214
214
} ) ) ;
215
-
216
215
} ) ;
217
216
218
217
@@ -224,7 +223,7 @@ describe('cdkMonitorFocus', () => {
224
223
ButtonWithFocusClasses ,
225
224
ComplexComponentWithMonitorElementFocus ,
226
225
ComplexComponentWithMonitorSubtreeFocus ,
227
- ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus ,
226
+ ComplexComponentWithMonitorSubtreeFocusAndMonitorElementFocus ,
228
227
] ,
229
228
} ) . compileComponents ( ) ;
230
229
} ) ;
@@ -394,15 +393,15 @@ describe('cdkMonitorFocus', () => {
394
393
} ) ;
395
394
396
395
describe ( 'complex component with cdkMonitorSubtreeFocus and cdkMonitorElementFocus' , ( ) => {
397
- let fixture : ComponentFixture < ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus > ;
396
+ let fixture : ComponentFixture < ComplexComponentWithMonitorSubtreeFocusAndMonitorElementFocus > ;
398
397
let parentElement : HTMLElement ;
399
398
let childElement : HTMLElement ;
400
399
let focusMonitor : FocusMonitor ;
401
400
402
401
beforeEach ( inject ( [ FocusMonitor ] , ( fm : FocusMonitor ) => {
403
402
focusMonitor = fm ;
404
403
fixture =
405
- TestBed . createComponent ( ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus ) ;
404
+ TestBed . createComponent ( ComplexComponentWithMonitorSubtreeFocusAndMonitorElementFocus ) ;
406
405
fixture . detectChanges ( ) ;
407
406
408
407
parentElement = fixture . debugElement . query ( By . css ( 'div' ) ) . nativeElement ;
@@ -424,6 +423,40 @@ describe('cdkMonitorFocus', () => {
424
423
} ) ;
425
424
} ) ;
426
425
426
+ describe ( 'FocusMonitor observable stream' , ( ) => {
427
+ let fixture : ComponentFixture < PlainButton > ;
428
+ let buttonElement : HTMLElement ;
429
+ let focusMonitor : FocusMonitor ;
430
+
431
+ beforeEach ( ( ) => {
432
+ TestBed . configureTestingModule ( {
433
+ imports : [ A11yModule ] ,
434
+ declarations : [
435
+ PlainButton ,
436
+ ] ,
437
+ } ) . compileComponents ( ) ;
438
+ } ) ;
439
+
440
+ beforeEach ( inject ( [ FocusMonitor ] , ( fm : FocusMonitor ) => {
441
+ fixture = TestBed . createComponent ( PlainButton ) ;
442
+ focusMonitor = fm ;
443
+ fixture . detectChanges ( ) ;
444
+ buttonElement = fixture . debugElement . nativeElement . querySelector ( 'button' ) ;
445
+ patchElementFocus ( buttonElement ) ;
446
+ } ) ) ;
447
+
448
+ it ( 'should emit inside the NgZone' , fakeAsync ( ( ) => {
449
+ const spy = jasmine . createSpy ( 'zone spy' ) ;
450
+ focusMonitor . monitor ( buttonElement ) . subscribe ( ( ) => spy ( NgZone . isInAngularZone ( ) ) ) ;
451
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
452
+
453
+ buttonElement . focus ( ) ;
454
+ fixture . detectChanges ( ) ;
455
+ tick ( ) ;
456
+ expect ( spy ) . toHaveBeenCalledWith ( true ) ;
457
+ } ) ) ;
458
+ } ) ;
459
+
427
460
428
461
@Component ( {
429
462
template : `<button>focus me!</button>`
@@ -453,4 +486,4 @@ class ComplexComponentWithMonitorSubtreeFocus {}
453
486
@Component ( {
454
487
template : `<div cdkMonitorSubtreeFocus><button cdkMonitorElementFocus></button></div>`
455
488
} )
456
- class ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus { }
489
+ class ComplexComponentWithMonitorSubtreeFocusAndMonitorElementFocus { }
0 commit comments