From faf82b1e23ea6b3f7dc0efd44d323749426996a7 Mon Sep 17 00:00:00 2001 From: Miles Malerba Date: Thu, 6 Jun 2024 09:18:01 -0700 Subject: [PATCH] test(material/dialog): Open dialogs inside NgZone (#29205) --- src/material/dialog/testing/dialog-opener.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/material/dialog/testing/dialog-opener.ts b/src/material/dialog/testing/dialog-opener.ts index 8711bf9f12d3..561d7f755367 100644 --- a/src/material/dialog/testing/dialog-opener.ts +++ b/src/material/dialog/testing/dialog-opener.ts @@ -11,8 +11,10 @@ import { ChangeDetectionStrategy, Component, NgModule, + NgZone, OnDestroy, ViewEncapsulation, + inject, } from '@angular/core'; import {MatDialog, MatDialogConfig, MatDialogModule, MatDialogRef} from '@angular/material/dialog'; import {NoopAnimationsModule} from '@angular/platform-browser/animations'; @@ -41,6 +43,8 @@ export class MatTestDialogOpener implements OnDestroy private readonly _afterClosedSubscription: Subscription; + private readonly _ngZone = inject(NgZone); + /** Static method that prepares this class to open the provided component. */ static withComponent( component: ComponentType, @@ -56,9 +60,11 @@ export class MatTestDialogOpener implements OnDestroy throw new Error(`MatTestDialogOpener does not have a component provided.`); } - this.dialogRef = this.dialog.open( - MatTestDialogOpener.component as ComponentType, - MatTestDialogOpener.config || {}, + this.dialogRef = this._ngZone.run(() => + this.dialog.open( + MatTestDialogOpener.component as ComponentType, + MatTestDialogOpener.config || {}, + ), ); this._afterClosedSubscription = this.dialogRef.afterClosed().subscribe(result => { this.closedResult = result;