Skip to content

Commit 5d538d7

Browse files
crisbetoandrewseguin
authored andcommitted
fix(experimental/dialog): disableClose option not working (#13584)
* Fixes the `disableClose` option not working for `cdk-experimental/dialog`. * Updates our unit tests that were supposed to catch the issue, but didn't. Also updates similar tests for `material/dialog` and `material/bottom-sheet`. Fixes #13583.
1 parent ba7d53d commit 5d538d7

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

src/cdk-experimental/dialog/dialog.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ describe('Dialog', () => {
696696
});
697697

698698
describe('disableClose option', () => {
699-
it('should prevent closing via clicks on the backdrop', () => {
699+
it('should prevent closing via clicks on the backdrop', fakeAsync(() => {
700700
dialog.openFromComponent(PizzaMsg, {
701701
disableClose: true,
702702
viewContainerRef: testViewContainerRef
@@ -706,21 +706,25 @@ describe('Dialog', () => {
706706

707707
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
708708
backdrop.click();
709+
viewContainerFixture.detectChanges();
710+
flush();
709711

710712
expect(overlayContainerElement.querySelector('cdk-dialog-container')).toBeTruthy();
711-
});
713+
}));
712714

713-
it('should prevent closing via the escape key', () => {
715+
it('should prevent closing via the escape key', fakeAsync(() => {
714716
dialog.openFromComponent(PizzaMsg, {
715717
disableClose: true,
716718
viewContainerRef: testViewContainerRef
717719
});
718720

719721
viewContainerFixture.detectChanges();
720722
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
723+
viewContainerFixture.detectChanges();
724+
flush();
721725

722726
expect(overlayContainerElement.querySelector('cdk-dialog-container')).toBeTruthy();
723-
});
727+
}));
724728

725729
it('should allow for the disableClose option to be updated while open', fakeAsync(() => {
726730
let dialogRef = dialog.openFromComponent(PizzaMsg, {

src/cdk-experimental/dialog/dialog.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,12 @@ export class Dialog implements OnDestroy {
225225
// Create a reference to the dialog we're creating in order to give the user a handle
226226
// to modify and close it.
227227
const dialogRef = new this.dialogRefConstructor(overlayRef, dialogContainer, config.id);
228-
229228
const injector = this._createInjector<T>(config, dialogRef, dialogContainer);
230229
const contentRef = dialogContainer.attachComponentPortal(
231230
new ComponentPortal(componentOrTemplateRef, undefined, injector));
231+
232232
dialogRef.componentInstance = contentRef.instance;
233+
dialogRef.disableClose = config.disableClose;
233234

234235
dialogRef.updateSize({width: config.width, height: config.height})
235236
.updatePosition(config.position);

src/lib/bottom-sheet/bottom-sheet.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ describe('MatBottomSheet', () => {
429429
});
430430

431431
describe('disableClose option', () => {
432-
it('should prevent closing via clicks on the backdrop', () => {
432+
it('should prevent closing via clicks on the backdrop', fakeAsync(() => {
433433
bottomSheet.open(PizzaMsg, {
434434
disableClose: true,
435435
viewContainerRef: testViewContainerRef
@@ -439,21 +439,25 @@ describe('MatBottomSheet', () => {
439439

440440
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
441441
backdrop.click();
442+
viewContainerFixture.detectChanges();
443+
flush();
442444

443445
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
444-
});
446+
}));
445447

446-
it('should prevent closing via the escape key', () => {
448+
it('should prevent closing via the escape key', fakeAsync(() => {
447449
bottomSheet.open(PizzaMsg, {
448450
disableClose: true,
449451
viewContainerRef: testViewContainerRef
450452
});
451453

452454
viewContainerFixture.detectChanges();
453455
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
456+
viewContainerFixture.detectChanges();
457+
flush();
454458

455459
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
456-
});
460+
}));
457461

458462
});
459463

src/lib/dialog/dialog.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ describe('MatDialog', () => {
814814
}));
815815

816816
describe('disableClose option', () => {
817-
it('should prevent closing via clicks on the backdrop', () => {
817+
it('should prevent closing via clicks on the backdrop', fakeAsync(() => {
818818
dialog.open(PizzaMsg, {
819819
disableClose: true,
820820
viewContainerRef: testViewContainerRef
@@ -824,21 +824,25 @@ describe('MatDialog', () => {
824824

825825
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
826826
backdrop.click();
827+
viewContainerFixture.detectChanges();
828+
flush();
827829

828830
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeTruthy();
829-
});
831+
}));
830832

831-
it('should prevent closing via the escape key', () => {
833+
it('should prevent closing via the escape key', fakeAsync(() => {
832834
dialog.open(PizzaMsg, {
833835
disableClose: true,
834836
viewContainerRef: testViewContainerRef
835837
});
836838

837839
viewContainerFixture.detectChanges();
838840
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
841+
viewContainerFixture.detectChanges();
842+
flush();
839843

840844
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeTruthy();
841-
});
845+
}));
842846

843847
it('should allow for the disableClose option to be updated while open', fakeAsync(() => {
844848
let dialogRef = dialog.open(PizzaMsg, {

0 commit comments

Comments
 (0)