|
1 | 1 | import {TAB} from '@angular/cdk/keycodes';
|
2 | 2 | import {Platform} from '@angular/cdk/platform';
|
3 | 3 | import {DOCUMENT} from '@angular/common';
|
4 |
| -import {Component, ViewChild} from '@angular/core'; |
| 4 | +import {Component, ViewChild, provideZoneChangeDetection} from '@angular/core'; |
5 | 5 | import {ComponentFixture, TestBed, fakeAsync, flush, inject, tick} from '@angular/core/testing';
|
6 | 6 | import {By} from '@angular/platform-browser';
|
7 | 7 | import {
|
@@ -34,6 +34,7 @@ describe('FocusMonitor', () => {
|
34 | 34 | TestBed.configureTestingModule({
|
35 | 35 | imports: [A11yModule, PlainButton],
|
36 | 36 | providers: [
|
| 37 | + provideZoneChangeDetection(), |
37 | 38 | {
|
38 | 39 | provide: DOCUMENT,
|
39 | 40 | useFactory: () => {
|
@@ -899,6 +900,25 @@ describe('FocusMonitor input label detection', () => {
|
899 | 900 | }));
|
900 | 901 | });
|
901 | 902 |
|
| 903 | +describe('cdkMonitorFocus with change detection counter', () => { |
| 904 | + beforeEach(() => { |
| 905 | + TestBed.configureTestingModule({ |
| 906 | + imports: [A11yModule, ButtonWithCDCounter], |
| 907 | + providers: [{provide: Platform, useValue: {isBrowser: true}}], |
| 908 | + }).compileComponents(); |
| 909 | + }); |
| 910 | + |
| 911 | + it('should detect changes caused by focus change', async () => { |
| 912 | + const fixture = TestBed.createComponent(ButtonWithCDCounter); |
| 913 | + fixture.autoDetectChanges(); |
| 914 | + const buttonElement = fixture.nativeElement.querySelector('button'); |
| 915 | + buttonElement.focus(); |
| 916 | + await fixture.whenStable(); |
| 917 | + const focusChangeCounter = fixture.nativeElement.querySelector('.focus-change-counter'); |
| 918 | + expect(focusChangeCounter.innerText).toBe('1'); |
| 919 | + }); |
| 920 | +}); |
| 921 | + |
902 | 922 | @Component({
|
903 | 923 | template: `<div class="parent"><button>focus me!</button></div>`,
|
904 | 924 | standalone: true,
|
@@ -961,3 +981,15 @@ class CheckboxWithLabel {}
|
961 | 981 | class ExportedFocusMonitor {
|
962 | 982 | @ViewChild('exportedDir') exportedDirRef: CdkMonitorFocus;
|
963 | 983 | }
|
| 984 | + |
| 985 | +@Component({ |
| 986 | + template: ` |
| 987 | + <button cdkMonitorElementFocus (cdkFocusChange)="focusChangeCount = focusChangeCount + 1"></button> |
| 988 | + <div class="focus-change-counter">{{focusChangeCount}}</div> |
| 989 | + `, |
| 990 | + standalone: true, |
| 991 | + imports: [A11yModule], |
| 992 | +}) |
| 993 | +class ButtonWithCDCounter { |
| 994 | + focusChangeCount = 0; |
| 995 | +} |
0 commit comments