import { ComponentType } from '@angular/cdk/portal' ; import { Provider } from '@angular/core' ; import { NoopAnimationsModule } from '@angular/platform-browser/animations' ; import { Observable , Subject } from 'rxjs' ; import { Dialog , DialogConfig , DialogModule } from '../../../src/browser/ui/dialog' ; export class MockDialogRef < T , D = any , R = any > { readonly _beforeOpened = new Subject < void > ( ) ; readonly _afterOpened = new Subject < void > ( ) ; readonly _afterClosed = new Subject < any > ( ) ; readonly _beforeClosed = new Subject < any > ( ) ; constructor ( private dialog , public component , public config ? ) { } beforeOpened ( ) { return this . _beforeOpened . asObservable ( ) ; } afterOpened ( ) { return this . _afterOpened . asObservable ( ) ; } beforeClosed ( ) { return this . _beforeClosed . asObservable ( ) ; } afterClosed ( ) { return this . _afterClosed . asObservable ( ) ; } close ( result ? ) { this . _beforeClosed . next ( result ) ; this . _afterClosed . next ( result ) ; this . dialog . _closeDialog ( this ) ; } } export class MockDialog { openDialogs : MockDialogRef < any > [ ] = [ ] ; static imports ( ) { return [ DialogModule , NoopAnimationsModule ] ; } static providers ( ) { return [ { provide : Dialog , useClass : MockDialog } ] ; } open < T , D , R > ( component , config ? ) { const dialogRef = new MockDialogRef ( this , component , config ) ; this . openDialogs . push ( dialogRef ) ; dialogRef . _beforeOpened . next ( ) ; dialogRef . _afterOpened . next ( ) ; return dialogRef ; } closeAll ( ) { this . openDialogs . forEach ( ( dialogRef ) => { this . _closeDialog ( dialogRef ) ; } ) ; } getByComponent < T , D = any , R = any > ( component ) { return this . openDialogs . find ( dialogRef => dialogRef . component === component ) ; } _closeDialog ( dialogRef ) { const index = this . openDialogs . indexOf ( dialogRef ) ; if ( index !== - 1 ) { this . openDialogs . splice ( index , 1 ) ; } } }