|
5 | 5 | dispatchMouseEvent, |
6 | 6 | patchElementFocus, |
7 | 7 | } from '@angular/cdk/testing'; |
8 | | -import {Component} from '@angular/core'; |
| 8 | +import {Component, ElementRef, ViewChild} 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 |
|
@@ -424,6 +423,39 @@ describe('cdkMonitorFocus', () => { |
424 | 423 | }); |
425 | 424 | }); |
426 | 425 |
|
| 426 | +describe('FocusMonitor observable stream', () => { |
| 427 | + let fixture: ComponentFixture<MonitoredElementRequiringChangeDetection>; |
| 428 | + let buttonElement: HTMLElement; |
| 429 | + let focusMonitor: FocusMonitor; |
| 430 | + |
| 431 | + beforeEach(() => { |
| 432 | + TestBed.configureTestingModule({ |
| 433 | + imports: [A11yModule], |
| 434 | + declarations: [ |
| 435 | + MonitoredElementRequiringChangeDetection, |
| 436 | + ], |
| 437 | + }).compileComponents(); |
| 438 | + }); |
| 439 | + |
| 440 | + beforeEach(inject([FocusMonitor], (fm: FocusMonitor) => { |
| 441 | + fixture = TestBed.createComponent(MonitoredElementRequiringChangeDetection); |
| 442 | + fixture.detectChanges(); |
| 443 | + |
| 444 | + buttonElement = fixture.componentInstance.button.nativeElement; |
| 445 | + focusMonitor = fm; |
| 446 | + |
| 447 | + patchElementFocus(buttonElement); |
| 448 | + })); |
| 449 | + |
| 450 | + it('should emit inside the NgZone', () => { |
| 451 | + fixture.detectChanges(); |
| 452 | + expect(buttonElement.innerText).toBe(''); |
| 453 | + buttonElement.focus(); |
| 454 | + fixture.detectChanges(); |
| 455 | + expect(buttonElement.innerText).toBe('program'); |
| 456 | + }); |
| 457 | +}); |
| 458 | + |
427 | 459 |
|
428 | 460 | @Component({ |
429 | 461 | template: `<button>focus me!</button>` |
@@ -454,3 +486,21 @@ class ComplexComponentWithMonitorSubtreeFocus {} |
454 | 486 | template: `<div cdkMonitorSubtreeFocus><button cdkMonitorElementFocus></button></div>` |
455 | 487 | }) |
456 | 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