Skip to content

Commit

Permalink
Revert "test: rewrite tests that test zone.js implementation details"
Browse files Browse the repository at this point in the history
This reverts commit de01346.
  • Loading branch information
mmalerba committed May 21, 2024
1 parent de01346 commit 727e162
Show file tree
Hide file tree
Showing 6 changed files with 662 additions and 352 deletions.
34 changes: 1 addition & 33 deletions src/cdk/a11y/focus-monitor/focus-monitor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {TAB} from '@angular/cdk/keycodes';
import {Platform} from '@angular/cdk/platform';
import {DOCUMENT} from '@angular/common';
import {Component, ViewChild, provideZoneChangeDetection} from '@angular/core';
import {Component, ViewChild} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, flush, inject, tick} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {
Expand Down Expand Up @@ -34,7 +34,6 @@ describe('FocusMonitor', () => {
TestBed.configureTestingModule({
imports: [A11yModule, PlainButton],
providers: [
provideZoneChangeDetection(),
{
provide: DOCUMENT,
useFactory: () => {
Expand Down Expand Up @@ -900,25 +899,6 @@ describe('FocusMonitor input label detection', () => {
}));
});

describe('cdkMonitorFocus with change detection counter', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [A11yModule, ButtonWithCDCounter],
providers: [{provide: Platform, useValue: {isBrowser: true}}],
}).compileComponents();
});

it('should detect changes caused by focus change', async () => {
const fixture = TestBed.createComponent(ButtonWithCDCounter);
fixture.autoDetectChanges();
const buttonElement = fixture.nativeElement.querySelector('button');
buttonElement.focus();
await fixture.whenStable();
const focusChangeCounter = fixture.nativeElement.querySelector('.focus-change-counter');
expect(focusChangeCounter.innerText).toBe('1');
});
});

@Component({
template: `<div class="parent"><button>focus me!</button></div>`,
standalone: true,
Expand Down Expand Up @@ -981,15 +961,3 @@ class CheckboxWithLabel {}
class ExportedFocusMonitor {
@ViewChild('exportedDir') exportedDirRef: CdkMonitorFocus;
}

@Component({
template: `
<button cdkMonitorElementFocus (cdkFocusChange)="focusChangeCount = focusChangeCount + 1"></button>
<div class="focus-change-counter">{{focusChangeCount}}</div>
`,
standalone: true,
imports: [A11yModule],
})
class ButtonWithCDCounter {
focusChangeCount = 0;
}
47 changes: 47 additions & 0 deletions src/cdk/a11y/focus-monitor/focus-monitor.zone.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {Platform} from '@angular/cdk/platform';
import {patchElementFocus} from '@angular/cdk/testing/private';
import {Component, NgZone, provideZoneChangeDetection} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
import {A11yModule} from '../a11y-module';
import {FocusMonitor} from './focus-monitor';

describe('FocusMonitor observable stream Zone.js integration', () => {
let fixture: ComponentFixture<PlainButton>;
let buttonElement: HTMLElement;
let focusMonitor: FocusMonitor;
let fakePlatform: Platform;

beforeEach(() => {
fakePlatform = {isBrowser: true} as Platform;
TestBed.configureTestingModule({
imports: [A11yModule, PlainButton],
providers: [{provide: Platform, useValue: fakePlatform}, provideZoneChangeDetection()],
}).compileComponents();
});

beforeEach(inject([FocusMonitor], (fm: FocusMonitor) => {
fixture = TestBed.createComponent(PlainButton);
focusMonitor = fm;
fixture.detectChanges();
buttonElement = fixture.debugElement.nativeElement.querySelector('button');
patchElementFocus(buttonElement);
}));

it('should emit inside the NgZone', fakeAsync(() => {
const spy = jasmine.createSpy('zone spy');
focusMonitor.monitor(buttonElement).subscribe(() => spy(NgZone.isInAngularZone()));
expect(spy).not.toHaveBeenCalled();

buttonElement.focus();
fixture.detectChanges();
tick();
expect(spy).toHaveBeenCalledWith(true);
}));
});

@Component({
template: `<div class="parent"><button>focus me!</button></div>`,
standalone: true,
imports: [A11yModule],
})
class PlainButton {}
Loading

0 comments on commit 727e162

Please sign in to comment.