Skip to content
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
29 changes: 24 additions & 5 deletions src/cdk/overlay/overlay-directives.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Component, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {ComponentFixture, TestBed, async, inject} from '@angular/core/testing';
import {ComponentFixture, TestBed, async, inject, fakeAsync, tick} from '@angular/core/testing';
import {Directionality} from '@angular/cdk/bidi';
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
import {ESCAPE, A} from '@angular/cdk/keycodes';
Expand Down Expand Up @@ -210,18 +210,37 @@ describe('Overlay directives', () => {
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
expect(backdrop).toBeTruthy();
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
});

it('should not create the backdrop by default', () => {
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
expect(backdrop).toBeNull();
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeNull();
});

it('should be able to change hasBackdrop after the overlay has been initialized',
fakeAsync(() => {
// Open once with a backdrop
fixture.componentInstance.hasBackdrop = true;
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();

fixture.componentInstance.isOpen = false;
fixture.detectChanges();
tick(500);

// Open again without a backdrop.
fixture.componentInstance.hasBackdrop = false;
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
}));

it('should set the custom backdrop class', () => {
fixture.componentInstance.hasBackdrop = true;
fixture.componentInstance.isOpen = true;
Expand Down
19 changes: 9 additions & 10 deletions src/cdk/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
}

ngOnDestroy() {
this._destroyOverlay();
if (this._overlayRef) {
this._overlayRef.dispose();
}

this._backdropSubscription.unsubscribe();
}

ngOnChanges(changes: SimpleChanges) {
Expand Down Expand Up @@ -351,6 +355,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
height: this.height,
minHeight: this.minHeight,
});

this._overlayRef.getConfig().hasBackdrop = this.hasBackdrop;
}

if (!this._overlayRef.hasAttached()) {
Expand All @@ -362,6 +368,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
this._backdropSubscription = this._overlayRef.backdropClick().subscribe(event => {
this.backdropClick.emit(event);
});
} else {
this._backdropSubscription.unsubscribe();
}
}

Expand All @@ -374,15 +382,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {

this._backdropSubscription.unsubscribe();
}

/** Destroys the overlay created by this directive. */
private _destroyOverlay() {
if (this._overlayRef) {
this._overlayRef.dispose();
}

this._backdropSubscription.unsubscribe();
}
}


Expand Down