Skip to content

Commit 969df37

Browse files
authored
test: Convert some material tests to zoneless (#29319)
1 parent 7cb6f31 commit 969df37

File tree

9 files changed

+148
-79
lines changed

9 files changed

+148
-79
lines changed

src/google-maps/map-event-manager.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {NgZone} from '@angular/core';
1+
import {type NgZone} from '@angular/core';
22
import {MapEventManager} from './map-event-manager';
33

44
describe('MapEventManager', () => {

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,6 @@ describe('MDC-based MatAutocomplete', () => {
160160
.toContain('California');
161161
});
162162

163-
it('should show the panel when the first open is after the initial zone stabilization', waitForAsync(() => {
164-
// Note that we're running outside the Angular zone, in order to be able
165-
// to test properly without the subscription from `_subscribeToClosingActions`
166-
// giving us a false positive.
167-
fixture.ngZone!.runOutsideAngular(() => {
168-
fixture.componentInstance.trigger.openPanel();
169-
170-
Promise.resolve().then(() => {
171-
expect(fixture.componentInstance.panel.showPanel)
172-
.withContext(`Expected panel to be visible.`)
173-
.toBe(true);
174-
});
175-
});
176-
}));
177-
178163
it('should close the panel when the user clicks away', waitForAsync(async () => {
179164
dispatchFakeEvent(input, 'focusin');
180165
fixture.detectChanges();

src/material/autocomplete/autocomplete.zone.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
ViewChildren,
1212
provideZoneChangeDetection,
1313
} from '@angular/core';
14-
import {TestBed, waitForAsync} from '@angular/core/testing';
14+
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
1515
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
1616
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
1717
import {Subscription} from 'rxjs';
@@ -43,6 +43,31 @@ describe('MDC-based MatAutocomplete Zone.js integration', () => {
4343

4444
return TestBed.createComponent<T>(component);
4545
}
46+
47+
describe('panel toggling', () => {
48+
let fixture: ComponentFixture<SimpleAutocomplete>;
49+
50+
beforeEach(() => {
51+
fixture = createComponent(SimpleAutocomplete);
52+
fixture.detectChanges();
53+
});
54+
55+
it('should show the panel when the first open is after the initial zone stabilization', waitForAsync(() => {
56+
// Note that we're running outside the Angular zone, in order to be able
57+
// to test properly without the subscription from `_subscribeToClosingActions`
58+
// giving us a false positive.
59+
fixture.ngZone!.runOutsideAngular(() => {
60+
fixture.componentInstance.trigger.openPanel();
61+
62+
Promise.resolve().then(() => {
63+
expect(fixture.componentInstance.panel.showPanel)
64+
.withContext(`Expected panel to be visible.`)
65+
.toBe(true);
66+
});
67+
});
68+
}));
69+
});
70+
4671
it('should emit from `autocomplete.closed` after click outside inside the NgZone', waitForAsync(async () => {
4772
const inZoneSpy = jasmine.createSpy('in zone spy');
4873

src/material/dialog/dialog.spec.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
Injectable,
2424
Injector,
2525
NgModule,
26-
NgZone,
2726
TemplateRef,
2827
ViewChild,
2928
ViewContainerRef,
@@ -1260,9 +1259,7 @@ describe('MDC-based MatDialog', () => {
12601259
document.body.appendChild(button);
12611260
button.focus();
12621261

1263-
const dialogRef = TestBed.inject(NgZone).run(() =>
1264-
dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef}),
1265-
);
1262+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
12661263

12671264
flush();
12681265
viewContainerFixture.detectChanges();
@@ -1519,12 +1516,10 @@ describe('MDC-based MatDialog', () => {
15191516
document.body.appendChild(button);
15201517
button.focus();
15211518

1522-
const dialogRef = TestBed.inject(NgZone).run(() =>
1523-
dialog.open(PizzaMsg, {
1524-
viewContainerRef: testViewContainerRef,
1525-
restoreFocus: false,
1526-
}),
1527-
);
1519+
const dialogRef = dialog.open(PizzaMsg, {
1520+
viewContainerRef: testViewContainerRef,
1521+
restoreFocus: false,
1522+
});
15281523

15291524
flush();
15301525
viewContainerFixture.detectChanges();
@@ -1557,9 +1552,7 @@ describe('MDC-based MatDialog', () => {
15571552
body.appendChild(otherButton);
15581553
button.focus();
15591554

1560-
const dialogRef = TestBed.inject(NgZone).run(() =>
1561-
dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef}),
1562-
);
1555+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
15631556

15641557
flush();
15651558
viewContainerFixture.detectChanges();

src/material/form-field/form-field.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import {
3131
ViewChild,
3232
ViewEncapsulation,
3333
afterRender,
34+
computed,
35+
contentChild,
3436
inject,
3537
} from '@angular/core';
3638
import {AbstractControlDirective} from '@angular/forms';
@@ -194,14 +196,14 @@ export class MatFormField
194196
@ViewChild(MatFormFieldNotchedOutline) _notchedOutline: MatFormFieldNotchedOutline | undefined;
195197
@ViewChild(MatFormFieldLineRipple) _lineRipple: MatFormFieldLineRipple | undefined;
196198

197-
@ContentChild(MatLabel) _labelChildNonStatic: MatLabel | undefined;
198-
@ContentChild(MatLabel, {static: true}) _labelChildStatic: MatLabel | undefined;
199199
@ContentChild(_MatFormFieldControl) _formFieldControl: MatFormFieldControl<any>;
200200
@ContentChildren(MAT_PREFIX, {descendants: true}) _prefixChildren: QueryList<MatPrefix>;
201201
@ContentChildren(MAT_SUFFIX, {descendants: true}) _suffixChildren: QueryList<MatSuffix>;
202202
@ContentChildren(MAT_ERROR, {descendants: true}) _errorChildren: QueryList<MatError>;
203203
@ContentChildren(MatHint, {descendants: true}) _hintChildren: QueryList<MatHint>;
204204

205+
private readonly _labelChild = contentChild(MatLabel);
206+
205207
/** Whether the required marker should be hidden. */
206208
@Input()
207209
get hideRequiredMarker(): boolean {
@@ -379,9 +381,7 @@ export class MatFormField
379381
/**
380382
* Gets the id of the label element. If no label is present, returns `null`.
381383
*/
382-
getLabelId(): string | null {
383-
return this._hasFloatingLabel() ? this._labelId : null;
384-
}
384+
getLabelId = computed(() => (this._hasFloatingLabel() ? this._labelId : null));
385385

386386
/**
387387
* Gets an ElementRef for the element that a overlay attached to the form field
@@ -551,9 +551,7 @@ export class MatFormField
551551
return !this._platform.isBrowser && this._prefixChildren.length && !this._shouldLabelFloat();
552552
}
553553

554-
_hasFloatingLabel() {
555-
return !!this._labelChildNonStatic || !!this._labelChildStatic;
556-
}
554+
_hasFloatingLabel = computed(() => !!this._labelChild());
557555

558556
_shouldLabelFloat() {
559557
return this._control.shouldLabelFloat || this._shouldAlwaysFloat();

0 commit comments

Comments
 (0)