Skip to content

fix(dialog): not closing correctly when detached externally #11516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/lib/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ export class MatDialogRef<T, R = any> {
_containerInstance._animationStateChanged.pipe(
filter(event => event.phaseName === 'done' && event.toState === 'exit'),
take(1)
)
.subscribe(() => {
this._overlayRef.dispose();
).subscribe(() => this._overlayRef.dispose());

_overlayRef.detachments().subscribe(() => {
this._beforeClose.next(this._result);
this._beforeClose.complete();
this._locationChanges.unsubscribe();
this._afterClosed.next(this._result);
this._afterClosed.complete();
this.componentInstance = null!;
this._overlayRef.dispose();
});

_overlayRef.keydownEvents()
Expand Down
29 changes: 27 additions & 2 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
import {Directionality} from '@angular/cdk/bidi';
import {MatDialogContainer} from './dialog-container';
import {OverlayContainer, ScrollStrategy} from '@angular/cdk/overlay';
import {OverlayContainer, ScrollStrategy, ScrollDispatcher, Overlay} from '@angular/cdk/overlay';
import {A, ESCAPE} from '@angular/cdk/keycodes';
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
import {
Expand All @@ -34,12 +34,14 @@ import {
MatDialogRef,
MAT_DIALOG_DEFAULT_OPTIONS
} from './index';
import {Subject} from 'rxjs';


describe('MatDialog', () => {
let dialog: MatDialog;
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let scrolledSubject = new Subject();

let testViewContainerRef: ViewContainerRef;
let viewContainerFixture: ComponentFixture<ComponentWithChildViewContainer>;
Expand All @@ -49,7 +51,10 @@ describe('MatDialog', () => {
TestBed.configureTestingModule({
imports: [MatDialogModule, DialogTestModule],
providers: [
{provide: Location, useClass: SpyLocation}
{provide: Location, useClass: SpyLocation},
{provide: ScrollDispatcher, useFactory: () => ({
scrolled: () => scrolledSubject.asObservable()
})},
],
});

Expand Down Expand Up @@ -188,6 +193,26 @@ describe('MatDialog', () => {
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeNull();
}));

it('should dispatch the beforeClose and afterClose events when the ' +
'overlay is detached externally', fakeAsync(inject([Overlay], (overlay: Overlay) => {
const dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
scrollStrategy: overlay.scrollStrategies.close()
});
const beforeCloseCallback = jasmine.createSpy('beforeClosed callback');
const afterCloseCallback = jasmine.createSpy('afterClosed callback');

dialogRef.beforeClose().subscribe(beforeCloseCallback);
dialogRef.afterClosed().subscribe(afterCloseCallback);

scrolledSubject.next();
viewContainerFixture.detectChanges();
flush();

expect(beforeCloseCallback).toHaveBeenCalledTimes(1);
expect(afterCloseCallback).toHaveBeenCalledTimes(1);
})));

it('should close a dialog and get back a result before it is closed', fakeAsync(() => {
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});

Expand Down