Skip to content

Commit 43bd51c

Browse files
committed
fix tests
1 parent c38ae56 commit 43bd51c

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

src/cdk/a11y/focus-monitor/focus-monitor.spec.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
dispatchMouseEvent,
66
patchElementFocus,
77
} from '@angular/cdk/testing';
8-
import {Component, ElementRef, ViewChild} from '@angular/core';
8+
import {Component, NgZone} from '@angular/core';
99
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
1010
import {By} from '@angular/platform-browser';
1111
import {A11yModule} from '../index';
@@ -223,7 +223,6 @@ describe('cdkMonitorFocus', () => {
223223
ButtonWithFocusClasses,
224224
ComplexComponentWithMonitorElementFocus,
225225
ComplexComponentWithMonitorSubtreeFocus,
226-
ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus,
227226
],
228227
}).compileComponents();
229228
});
@@ -424,36 +423,37 @@ describe('cdkMonitorFocus', () => {
424423
});
425424

426425
describe('FocusMonitor observable stream', () => {
427-
let fixture: ComponentFixture<MonitoredElementRequiringChangeDetection>;
426+
let fixture: ComponentFixture<PlainButton>;
428427
let buttonElement: HTMLElement;
429428
let focusMonitor: FocusMonitor;
430429

431430
beforeEach(() => {
432431
TestBed.configureTestingModule({
433432
imports: [A11yModule],
434433
declarations: [
435-
MonitoredElementRequiringChangeDetection,
434+
PlainButton,
436435
],
437436
}).compileComponents();
438437
});
439438

440439
beforeEach(inject([FocusMonitor], (fm: FocusMonitor) => {
441-
fixture = TestBed.createComponent(MonitoredElementRequiringChangeDetection);
442-
fixture.detectChanges();
443-
444-
buttonElement = fixture.componentInstance.button.nativeElement;
440+
fixture = TestBed.createComponent(PlainButton);
445441
focusMonitor = fm;
446-
442+
fixture.detectChanges();
443+
buttonElement = fixture.debugElement.nativeElement.querySelector('button');
447444
patchElementFocus(buttonElement);
448445
}));
449446

450-
it('should emit inside the NgZone', () => {
451-
fixture.detectChanges();
452-
expect(buttonElement.innerText).toBe('');
447+
it('should emit inside the NgZone', fakeAsync(() => {
448+
const spy = jasmine.createSpy('zone spy');
449+
focusMonitor.monitor(buttonElement).subscribe(() => spy(NgZone.isInAngularZone()));
450+
expect(spy).not.toHaveBeenCalled();
451+
453452
buttonElement.focus();
454453
fixture.detectChanges();
455-
expect(buttonElement.innerText).toBe('program');
456-
});
454+
tick();
455+
expect(spy).toHaveBeenCalledWith(true);
456+
}));
457457
});
458458

459459

@@ -486,21 +486,3 @@ class ComplexComponentWithMonitorSubtreeFocus {}
486486
template: `<div cdkMonitorSubtreeFocus><button cdkMonitorElementFocus></button></div>`
487487
})
488488
class ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus {}
489-
490-
@Component({
491-
template: `<button #b>{{origin}}</button>`
492-
})
493-
class MonitoredElementRequiringChangeDetection {
494-
@ViewChild('b') button: ElementRef;
495-
origin: string;
496-
497-
constructor(private _focusMonitor: FocusMonitor) {}
498-
499-
ngOnInit() {
500-
this._focusMonitor.monitor(this.button.nativeElement).subscribe(o => this.origin = o || '');
501-
}
502-
503-
ngOnDestroy() {
504-
this._focusMonitor.stopMonitoring(this.button.nativeElement);
505-
}
506-
}

0 commit comments

Comments
 (0)