Skip to content

Commit 0787c81

Browse files
committed
feat(dialog): expose backdrop clicks
Add comment about test expectation
1 parent 215ff3c commit 0787c81

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/lib/dialog/dialog-ref.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ export class MdDialogRef<T> {
8989
return this._beforeClose.asObservable();
9090
}
9191

92+
/**
93+
* Gets an observable that emits when the overlay's backdrop has been clicked.
94+
*/
95+
backdropClick(): Observable<void> {
96+
return this._overlayRef.backdropClick();
97+
}
98+
9299
/**
93100
* Updates the dialog's position.
94101
* @param position New dialog position.

src/lib/dialog/dialog.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,30 @@ describe('MdDialog', () => {
237237
});
238238
}));
239239

240+
it('should emit the backdropClick stream when clicking on the overlay backdrop', async(() => {
241+
const dialogRef = dialog.open(PizzaMsg, {
242+
viewContainerRef: testViewContainerRef
243+
});
244+
245+
const spy = jasmine.createSpy('backdropClick spy');
246+
dialogRef.backdropClick().subscribe(spy);
247+
248+
viewContainerFixture.detectChanges();
249+
250+
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
251+
252+
backdrop.click();
253+
expect(spy).toHaveBeenCalledTimes(1);
254+
255+
viewContainerFixture.detectChanges();
256+
257+
viewContainerFixture.whenStable().then(() => {
258+
// Additional clicks after the dialog has closed should not be emitted
259+
backdrop.click();
260+
expect(spy).toHaveBeenCalledTimes(1);
261+
});
262+
}));
263+
240264
it('should notify the observers if a dialog has been opened', () => {
241265
dialog.afterOpen.subscribe(ref => {
242266
expect(dialog.open(PizzaMsg, {

0 commit comments

Comments
 (0)