|
5 | 5 | dispatchMouseEvent,
|
6 | 6 | patchElementFocus,
|
7 | 7 | } from '@angular/cdk/testing';
|
8 |
| -import {Component, ElementRef, ViewChild} 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';
|
@@ -223,7 +223,6 @@ describe('cdkMonitorFocus', () => {
|
223 | 223 | ButtonWithFocusClasses,
|
224 | 224 | ComplexComponentWithMonitorElementFocus,
|
225 | 225 | ComplexComponentWithMonitorSubtreeFocus,
|
226 |
| - ComplexComponentWithMonitorSubtreeFocusAnfMonitorElementFocus, |
227 | 226 | ],
|
228 | 227 | }).compileComponents();
|
229 | 228 | });
|
@@ -424,36 +423,37 @@ describe('cdkMonitorFocus', () => {
|
424 | 423 | });
|
425 | 424 |
|
426 | 425 | describe('FocusMonitor observable stream', () => {
|
427 |
| - let fixture: ComponentFixture<MonitoredElementRequiringChangeDetection>; |
| 426 | + let fixture: ComponentFixture<PlainButton>; |
428 | 427 | let buttonElement: HTMLElement;
|
429 | 428 | let focusMonitor: FocusMonitor;
|
430 | 429 |
|
431 | 430 | beforeEach(() => {
|
432 | 431 | TestBed.configureTestingModule({
|
433 | 432 | imports: [A11yModule],
|
434 | 433 | declarations: [
|
435 |
| - MonitoredElementRequiringChangeDetection, |
| 434 | + PlainButton, |
436 | 435 | ],
|
437 | 436 | }).compileComponents();
|
438 | 437 | });
|
439 | 438 |
|
440 | 439 | 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); |
445 | 441 | focusMonitor = fm;
|
446 |
| - |
| 442 | + fixture.detectChanges(); |
| 443 | + buttonElement = fixture.debugElement.nativeElement.querySelector('button'); |
447 | 444 | patchElementFocus(buttonElement);
|
448 | 445 | }));
|
449 | 446 |
|
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 | + |
453 | 452 | buttonElement.focus();
|
454 | 453 | fixture.detectChanges();
|
455 |
| - expect(buttonElement.innerText).toBe('program'); |
456 |
| - }); |
| 454 | + tick(); |
| 455 | + expect(spy).toHaveBeenCalledWith(true); |
| 456 | + })); |
457 | 457 | });
|
458 | 458 |
|
459 | 459 |
|
@@ -486,21 +486,3 @@ class ComplexComponentWithMonitorSubtreeFocus {}
|
486 | 486 | template: `<div cdkMonitorSubtreeFocus><button cdkMonitorElementFocus></button></div>`
|
487 | 487 | })
|
488 | 488 | 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