Skip to content

Commit b0f2548

Browse files
committed
fix tests
1 parent 46be183 commit b0f2548

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

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

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ 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, inject, TestBed, tick} from '@angular/core/testing';
1010
import {By} from '@angular/platform-browser';
11-
import {FocusMonitor, FocusOrigin, TOUCH_BUFFER_MS} from './focus-monitor';
1211
import {A11yModule} from '../index';
12+
import {FocusMonitor, FocusOrigin, TOUCH_BUFFER_MS} from './focus-monitor';
1313

1414

1515
describe('FocusMonitor', () => {
@@ -393,36 +393,37 @@ describe('cdkMonitorFocus', () => {
393393
});
394394

395395
describe('FocusMonitor observable stream', () => {
396-
let fixture: ComponentFixture<MonitoredElementRequiringChangeDetection>;
396+
let fixture: ComponentFixture<PlainButton>;
397397
let buttonElement: HTMLElement;
398398
let focusMonitor: FocusMonitor;
399399

400400
beforeEach(() => {
401401
TestBed.configureTestingModule({
402402
imports: [A11yModule],
403403
declarations: [
404-
MonitoredElementRequiringChangeDetection,
404+
PlainButton,
405405
],
406406
}).compileComponents();
407407
});
408408

409409
beforeEach(inject([FocusMonitor], (fm: FocusMonitor) => {
410-
fixture = TestBed.createComponent(MonitoredElementRequiringChangeDetection);
411-
fixture.detectChanges();
412-
413-
buttonElement = fixture.componentInstance.button.nativeElement;
410+
fixture = TestBed.createComponent(PlainButton);
414411
focusMonitor = fm;
415-
412+
fixture.detectChanges();
413+
buttonElement = fixture.debugElement.nativeElement.querySelector('button');
416414
patchElementFocus(buttonElement);
417415
}));
418416

419-
it('should emit inside the NgZone', () => {
420-
fixture.detectChanges();
421-
expect(buttonElement.innerText).toBe('');
417+
it('should emit inside the NgZone', fakeAsync(() => {
418+
const spy = jasmine.createSpy('zone spy');
419+
focusMonitor.monitor(buttonElement).subscribe(() => spy(NgZone.isInAngularZone()));
420+
expect(spy).not.toHaveBeenCalled();
421+
422422
buttonElement.focus();
423423
fixture.detectChanges();
424-
expect(buttonElement.innerText).toBe('program');
425-
});
424+
tick();
425+
expect(spy).toHaveBeenCalledWith(true);
426+
}));
426427
});
427428

428429

@@ -450,21 +451,3 @@ class ComplexComponentWithMonitorElementFocus {}
450451
template: `<div tabindex="0" cdkMonitorSubtreeFocus><button></button></div>`
451452
})
452453
class 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

Comments
 (0)