Skip to content

Commit c26dd86

Browse files
authored
test: move some CDK tests to zoneless (#29083)
* test: move some CDK tests to zoneless * test: rewrite tests that test zone.js implementation details * Revert "test: rewrite tests that test zone.js implementation details" This reverts commit de01346. * ci: fix lint * test: share drag&drop test utils
1 parent 5cde329 commit c26dd86

File tree

12 files changed

+1337
-284
lines changed

12 files changed

+1337
-284
lines changed

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

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import {TAB} from '@angular/cdk/keycodes';
2+
import {Platform} from '@angular/cdk/platform';
3+
import {DOCUMENT} from '@angular/common';
4+
import {Component, ViewChild} from '@angular/core';
5+
import {ComponentFixture, TestBed, fakeAsync, flush, inject, tick} from '@angular/core/testing';
6+
import {By} from '@angular/platform-browser';
27
import {
8+
createMouseEvent,
9+
dispatchEvent,
310
dispatchFakeEvent,
411
dispatchKeyboardEvent,
512
dispatchMouseEvent,
613
patchElementFocus,
7-
createMouseEvent,
8-
dispatchEvent,
914
} from '../../testing/private';
10-
import {DOCUMENT} from '@angular/common';
11-
import {Component, NgZone, ViewChild, provideZoneChangeDetection} from '@angular/core';
12-
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
13-
import {By} from '@angular/platform-browser';
14-
import {Platform} from '@angular/cdk/platform';
1515
import {A11yModule, CdkMonitorFocus} from '../index';
1616
import {TOUCH_BUFFER_MS} from '../input-modality/input-modality-detector';
1717
import {
18+
FOCUS_MONITOR_DEFAULT_OPTIONS,
1819
FocusMonitor,
1920
FocusMonitorDetectionMode,
2021
FocusOrigin,
21-
FOCUS_MONITOR_DEFAULT_OPTIONS,
2222
} from './focus-monitor';
2323

2424
describe('FocusMonitor', () => {
@@ -826,7 +826,7 @@ describe('FocusMonitor observable stream', () => {
826826
fakePlatform = {isBrowser: true} as Platform;
827827
TestBed.configureTestingModule({
828828
imports: [A11yModule, PlainButton],
829-
providers: [{provide: Platform, useValue: fakePlatform}, provideZoneChangeDetection()],
829+
providers: [{provide: Platform, useValue: fakePlatform}],
830830
}).compileComponents();
831831
});
832832

@@ -838,17 +838,6 @@ describe('FocusMonitor observable stream', () => {
838838
patchElementFocus(buttonElement);
839839
}));
840840

841-
it('should emit inside the NgZone', fakeAsync(() => {
842-
const spy = jasmine.createSpy('zone spy');
843-
focusMonitor.monitor(buttonElement).subscribe(() => spy(NgZone.isInAngularZone()));
844-
expect(spy).not.toHaveBeenCalled();
845-
846-
buttonElement.focus();
847-
fixture.detectChanges();
848-
tick();
849-
expect(spy).toHaveBeenCalledWith(true);
850-
}));
851-
852841
it('should not emit on the server', fakeAsync(() => {
853842
fakePlatform.isBrowser = false;
854843
const emitSpy = jasmine.createSpy('emit spy');
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {Platform} from '@angular/cdk/platform';
2+
import {patchElementFocus} from '@angular/cdk/testing/private';
3+
import {Component, NgZone, provideZoneChangeDetection} from '@angular/core';
4+
import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
5+
import {A11yModule} from '../a11y-module';
6+
import {FocusMonitor} from './focus-monitor';
7+
8+
describe('FocusMonitor observable stream Zone.js integration', () => {
9+
let fixture: ComponentFixture<PlainButton>;
10+
let buttonElement: HTMLElement;
11+
let focusMonitor: FocusMonitor;
12+
let fakePlatform: Platform;
13+
14+
beforeEach(() => {
15+
fakePlatform = {isBrowser: true} as Platform;
16+
TestBed.configureTestingModule({
17+
imports: [A11yModule, PlainButton],
18+
providers: [{provide: Platform, useValue: fakePlatform}, provideZoneChangeDetection()],
19+
}).compileComponents();
20+
});
21+
22+
beforeEach(inject([FocusMonitor], (fm: FocusMonitor) => {
23+
fixture = TestBed.createComponent(PlainButton);
24+
focusMonitor = fm;
25+
fixture.detectChanges();
26+
buttonElement = fixture.debugElement.nativeElement.querySelector('button');
27+
patchElementFocus(buttonElement);
28+
}));
29+
30+
it('should emit inside the NgZone', fakeAsync(() => {
31+
const spy = jasmine.createSpy('zone spy');
32+
focusMonitor.monitor(buttonElement).subscribe(() => spy(NgZone.isInAngularZone()));
33+
expect(spy).not.toHaveBeenCalled();
34+
35+
buttonElement.focus();
36+
fixture.detectChanges();
37+
tick();
38+
expect(spy).toHaveBeenCalledWith(true);
39+
}));
40+
});
41+
42+
@Component({
43+
template: `<div class="parent"><button>focus me!</button></div>`,
44+
standalone: true,
45+
imports: [A11yModule],
46+
})
47+
class PlainButton {}

src/cdk/a11y/live-announcer/live-announcer.spec.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {MutationObserverFactory} from '@angular/cdk/observers';
22
import {Overlay} from '@angular/cdk/overlay';
33
import {ComponentPortal} from '@angular/cdk/portal';
4-
import {Component, provideZoneChangeDetection} from '@angular/core';
5-
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
4+
import {Component} from '@angular/core';
5+
import {ComponentFixture, TestBed, fakeAsync, flush, inject, tick} from '@angular/core/testing';
66
import {By} from '@angular/platform-browser';
77
import {A11yModule} from '../index';
88
import {LiveAnnouncer} from './live-announcer';
@@ -21,7 +21,6 @@ describe('LiveAnnouncer', () => {
2121
describe('with default element', () => {
2222
beforeEach(() =>
2323
TestBed.configureTestingModule({
24-
providers: [provideZoneChangeDetection()],
2524
imports: [A11yModule, TestApp, TestModal],
2625
}),
2726
);
@@ -128,7 +127,6 @@ describe('LiveAnnouncer', () => {
128127
fixture.destroy();
129128

130129
TestBed.resetTestingModule().configureTestingModule({
131-
providers: [provideZoneChangeDetection()],
132130
imports: [A11yModule],
133131
});
134132

@@ -180,6 +178,7 @@ describe('LiveAnnouncer', () => {
180178
const overlayRef = overlay.create();
181179
const componentRef = overlayRef.attach(portal);
182180
const modal = componentRef.location.nativeElement;
181+
fixture.changeDetectorRef.markForCheck();
183182
fixture.detectChanges();
184183

185184
expect(ariaLiveElement.id).toBeTruthy();
@@ -200,6 +199,7 @@ describe('LiveAnnouncer', () => {
200199
const overlayRef = overlay.create();
201200
const componentRef = overlayRef.attach(portal);
202201
const modal = componentRef.location.nativeElement;
202+
fixture.changeDetectorRef.markForCheck();
203203
fixture.detectChanges();
204204

205205
componentRef.instance.ariaOwns = 'foo bar';
@@ -227,10 +227,7 @@ describe('LiveAnnouncer', () => {
227227

228228
return TestBed.configureTestingModule({
229229
imports: [A11yModule, TestApp],
230-
providers: [
231-
provideZoneChangeDetection(),
232-
{provide: LIVE_ANNOUNCER_ELEMENT_TOKEN, useValue: customLiveElement},
233-
],
230+
providers: [{provide: LIVE_ANNOUNCER_ELEMENT_TOKEN, useValue: customLiveElement}],
234231
});
235232
});
236233

@@ -254,7 +251,6 @@ describe('LiveAnnouncer', () => {
254251
return TestBed.configureTestingModule({
255252
imports: [A11yModule, TestApp],
256253
providers: [
257-
provideZoneChangeDetection(),
258254
{
259255
provide: LIVE_ANNOUNCER_DEFAULT_OPTIONS,
260256
useValue: {
@@ -303,7 +299,6 @@ describe('CdkAriaLive', () => {
303299
TestBed.configureTestingModule({
304300
imports: [A11yModule, DivWithCdkAriaLive],
305301
providers: [
306-
provideZoneChangeDetection(),
307302
{
308303
provide: MutationObserverFactory,
309304
useValue: {
@@ -325,13 +320,15 @@ describe('CdkAriaLive', () => {
325320
announcer = la;
326321
announcerSpy = spyOn(la, 'announce').and.callThrough();
327322
fixture = TestBed.createComponent(DivWithCdkAriaLive);
323+
fixture.changeDetectorRef.markForCheck();
328324
fixture.detectChanges();
329325
flush();
330326
}),
331327
));
332328

333329
it('should default politeness to polite', fakeAsync(() => {
334330
fixture.componentInstance.content = 'New content';
331+
fixture.changeDetectorRef.markForCheck();
335332
fixture.detectChanges();
336333
invokeMutationCallbacks();
337334
flush();
@@ -341,6 +338,7 @@ describe('CdkAriaLive', () => {
341338

342339
it('should dynamically update the politeness', fakeAsync(() => {
343340
fixture.componentInstance.content = 'New content';
341+
fixture.changeDetectorRef.markForCheck();
344342
fixture.detectChanges();
345343
invokeMutationCallbacks();
346344
flush();
@@ -350,6 +348,7 @@ describe('CdkAriaLive', () => {
350348
announcerSpy.calls.reset();
351349
fixture.componentInstance.politeness = 'off';
352350
fixture.componentInstance.content = 'Newer content';
351+
fixture.changeDetectorRef.markForCheck();
353352
fixture.detectChanges();
354353
invokeMutationCallbacks();
355354
flush();
@@ -359,6 +358,7 @@ describe('CdkAriaLive', () => {
359358
announcerSpy.calls.reset();
360359
fixture.componentInstance.politeness = 'assertive';
361360
fixture.componentInstance.content = 'Newest content';
361+
fixture.changeDetectorRef.markForCheck();
362362
fixture.detectChanges();
363363
invokeMutationCallbacks();
364364
flush();
@@ -368,12 +368,14 @@ describe('CdkAriaLive', () => {
368368

369369
it('should not announce the same text multiple times', fakeAsync(() => {
370370
fixture.componentInstance.content = 'Content';
371+
fixture.changeDetectorRef.markForCheck();
371372
fixture.detectChanges();
372373
invokeMutationCallbacks();
373374
flush();
374375

375376
expect(announcer.announce).toHaveBeenCalledTimes(1);
376377

378+
fixture.changeDetectorRef.markForCheck();
377379
fixture.detectChanges();
378380
invokeMutationCallbacks();
379381
flush();
@@ -384,6 +386,7 @@ describe('CdkAriaLive', () => {
384386
it('should be able to pass in a duration', fakeAsync(() => {
385387
fixture.componentInstance.content = 'New content';
386388
fixture.componentInstance.duration = 1337;
389+
fixture.changeDetectorRef.markForCheck();
387390
fixture.detectChanges();
388391
invokeMutationCallbacks();
389392
flush();

src/cdk/clipboard/copy-to-clipboard.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {Component, provideZoneChangeDetection} from '@angular/core';
2-
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
1+
import {Component} from '@angular/core';
2+
import {ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing';
33

44
import {Clipboard} from './clipboard';
55
import {ClipboardModule} from './clipboard-module';
@@ -30,7 +30,6 @@ describe('CdkCopyToClipboard', () => {
3030
beforeEach(fakeAsync(() => {
3131
TestBed.configureTestingModule({
3232
imports: [ClipboardModule, CopyToClipboardHost],
33-
providers: [provideZoneChangeDetection()],
3433
});
3534

3635
TestBed.compileComponents();
@@ -42,6 +41,7 @@ describe('CdkCopyToClipboard', () => {
4241
const host = fixture.componentInstance;
4342
host.content = COPY_CONTENT;
4443
clipboard = TestBed.inject(Clipboard);
44+
fixture.changeDetectorRef.markForCheck();
4545
fixture.detectChanges();
4646
});
4747

@@ -74,9 +74,11 @@ describe('CdkCopyToClipboard', () => {
7474
destroy: () => {},
7575
} as PendingCopy);
7676
fixture.componentInstance.attempts = maxAttempts;
77+
fixture.changeDetectorRef.markForCheck();
7778
fixture.detectChanges();
7879

7980
fixture.nativeElement.querySelector('button')!.click();
81+
fixture.changeDetectorRef.markForCheck();
8082
fixture.detectChanges();
8183
tick(3);
8284

@@ -96,9 +98,11 @@ describe('CdkCopyToClipboard', () => {
9698
destroy: () => {},
9799
} as PendingCopy);
98100
fixture.componentInstance.attempts = maxAttempts;
101+
fixture.changeDetectorRef.markForCheck();
99102
fixture.detectChanges();
100103

101104
fixture.nativeElement.querySelector('button')!.click();
105+
fixture.changeDetectorRef.markForCheck();
102106
fixture.detectChanges();
103107
tick(3);
104108

@@ -114,12 +118,15 @@ describe('CdkCopyToClipboard', () => {
114118
} as PendingCopy;
115119

116120
fixture.componentInstance.attempts = 10;
121+
fixture.changeDetectorRef.markForCheck();
117122
fixture.detectChanges();
118123

119124
spyOn(clipboard, 'beginCopy').and.returnValue(fakeCopy);
125+
fixture.changeDetectorRef.markForCheck();
120126
fixture.detectChanges();
121127

122128
fixture.nativeElement.querySelector('button')!.click();
129+
fixture.changeDetectorRef.markForCheck();
123130
fixture.detectChanges();
124131
tick(1);
125132

0 commit comments

Comments
 (0)