Skip to content

Commit 60be42f

Browse files
committed
test: rewrite tests that test zone.js implementation details
1 parent 3a48d1f commit 60be42f

File tree

6 files changed

+61
-356
lines changed

6 files changed

+61
-356
lines changed

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {TAB} from '@angular/cdk/keycodes';
22
import {Platform} from '@angular/cdk/platform';
33
import {DOCUMENT} from '@angular/common';
4-
import {Component, ViewChild} from '@angular/core';
4+
import {Component, ViewChild, provideZoneChangeDetection} from '@angular/core';
55
import {ComponentFixture, TestBed, fakeAsync, flush, inject, tick} from '@angular/core/testing';
66
import {By} from '@angular/platform-browser';
77
import {
@@ -34,6 +34,7 @@ describe('FocusMonitor', () => {
3434
TestBed.configureTestingModule({
3535
imports: [A11yModule, PlainButton],
3636
providers: [
37+
provideZoneChangeDetection(),
3738
{
3839
provide: DOCUMENT,
3940
useFactory: () => {
@@ -899,6 +900,28 @@ describe('FocusMonitor input label detection', () => {
899900
}));
900901
});
901902

903+
describe('cdkMonitorFocus with change detection counter', () => {
904+
beforeEach(() => {
905+
TestBed.configureTestingModule({
906+
imports: [A11yModule, ButtonWithCDCounter],
907+
908+
providers: [
909+
provideZoneChangeDetection(), // TODO: Make this work with zoneless.
910+
{provide: Platform, useValue: {isBrowser: true}},
911+
],
912+
}).compileComponents();
913+
});
914+
915+
it('should detect changes caused by focus change', () => {
916+
const fixture = TestBed.createComponent(ButtonWithCDCounter);
917+
fixture.autoDetectChanges();
918+
const buttonElement = fixture.nativeElement.querySelector('button');
919+
buttonElement.focus();
920+
const focusChangeCounter = fixture.nativeElement.querySelector('.focus-change-counter');
921+
expect(focusChangeCounter.innerText).toBe('1');
922+
});
923+
});
924+
902925
@Component({
903926
template: `<div class="parent"><button>focus me!</button></div>`,
904927
standalone: true,
@@ -961,3 +984,15 @@ class CheckboxWithLabel {}
961984
class ExportedFocusMonitor {
962985
@ViewChild('exportedDir') exportedDirRef: CdkMonitorFocus;
963986
}
987+
988+
@Component({
989+
template: `
990+
<button cdkMonitorElementFocus (cdkFocusChange)="focusChangeCount = focusChangeCount + 1"></button>
991+
<div class="focus-change-counter">{{focusChangeCount}}</div>
992+
`,
993+
standalone: true,
994+
imports: [A11yModule],
995+
})
996+
class ButtonWithCDCounter {
997+
focusChangeCount = 0;
998+
}

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

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/cdk/drag-drop/directives/drag.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,16 @@ describe('CdkDrag', () => {
566566
subscription.unsubscribe();
567567
});
568568

569+
it('should detect changes cased by `cdkDragMoved`', () => {
570+
const fixture = createComponent(StandaloneDraggable);
571+
fixture.autoDetectChanges();
572+
573+
dragElementViaMouse(fixture, fixture.componentInstance.dragElement.nativeElement, 10, 20);
574+
575+
const movedCounter = fixture.nativeElement.querySelector('.moved-counter');
576+
expect(movedCounter.innerText).toBe('1');
577+
});
578+
569579
it('should complete the `moved` stream on destroy', () => {
570580
const fixture = createComponent(StandaloneDraggable);
571581
fixture.changeDetectorRef.markForCheck();
@@ -7268,9 +7278,11 @@ describe('CdkDrag', () => {
72687278
(cdkDragStarted)="startedSpy($event)"
72697279
(cdkDragReleased)="releasedSpy($event)"
72707280
(cdkDragEnded)="endedSpy($event)"
7281+
(cdkDragMoved)="movedCount = movedCount + 1"
72717282
#dragElement
72727283
style="width: 100px; height: 100px; background: red;"></div>
72737284
</div>
7285+
<div class="moved-counter">{{movedCount}}</div>
72747286
`,
72757287
})
72767288
class StandaloneDraggable {
@@ -7283,6 +7295,7 @@ class StandaloneDraggable {
72837295
dragStartDelay: number | string | {touch: number; mouse: number};
72847296
constrainPosition: (point: Point) => Point;
72857297
freeDragPosition?: {x: number; y: number};
7298+
movedCount = 0;
72867299
}
72877300

72887301
@Component({

src/cdk/drag-drop/directives/drag.zone.spec.ts

Lines changed: 0 additions & 158 deletions
This file was deleted.

src/cdk/overlay/overlay-directives.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,15 @@ describe('Overlay directives', () => {
680680
.toBe(true);
681681
});
682682

683+
it('should detect changes caused by position change', () => {
684+
fixture.destroy();
685+
fixture = TestBed.createComponent(ConnectedOverlayDirectiveTest);
686+
fixture.componentInstance.isOpen = true;
687+
fixture.autoDetectChanges();
688+
const positionChangeCounter = fixture.nativeElement.querySelector('.position-change-counter');
689+
expect(positionChangeCounter.innerText).toBe('1');
690+
});
691+
683692
it('should emit when attached', () => {
684693
expect(fixture.componentInstance.attachHandler).not.toHaveBeenCalled();
685694
fixture.componentInstance.isOpen = true;
@@ -764,6 +773,7 @@ describe('Overlay directives', () => {
764773
<button cdk-overlay-origin id="trigger" #trigger="cdkOverlayOrigin">Toggle menu</button>
765774
<button cdk-overlay-origin id="otherTrigger" #otherTrigger="cdkOverlayOrigin">Toggle menu</button>
766775
<button id="nonDirectiveTrigger" #nonDirectiveTrigger>Toggle menu</button>
776+
<div class="position-change-counter">{{positionChangeCount}}</div>
767777
768778
<ng-template cdk-connected-overlay
769779
[cdkConnectedOverlayOpen]="isOpen"
@@ -783,7 +793,7 @@ describe('Overlay directives', () => {
783793
(backdropClick)="backdropClickHandler($event)"
784794
[cdkConnectedOverlayOffsetX]="offsetX"
785795
[cdkConnectedOverlayOffsetY]="offsetY"
786-
(positionChange)="positionChangeHandler($event)"
796+
(positionChange)="positionChangeCount = positionChangeCount + 1; positionChangeHandler($event)"
787797
(attach)="attachHandler()"
788798
(detach)="detachHandler()"
789799
(overlayKeydown)="keydownHandler($event)"
@@ -829,6 +839,7 @@ class ConnectedOverlayDirectiveTest {
829839
detachHandler = jasmine.createSpy('detachHandler');
830840
attachResult: HTMLElement;
831841
transformOriginSelector: string;
842+
positionChangeCount = 0;
832843
}
833844

834845
@Component({

0 commit comments

Comments
 (0)