@@ -958,72 +958,88 @@ describe('MatDialog', () => {
958958 } ) ;
959959
960960 describe ( 'dialog content elements' , ( ) => {
961- let dialogRef : MatDialogRef < ContentElementDialog > ;
961+ let dialogRef : MatDialogRef < any > ;
962962
963- beforeEach ( fakeAsync ( ( ) => {
964- dialogRef = dialog . open ( ContentElementDialog , { viewContainerRef : testViewContainerRef } ) ;
965- viewContainerFixture . detectChanges ( ) ;
966- flush ( ) ;
967- } ) ) ;
968-
969- it ( 'should close the dialog when clicking on the close button' , fakeAsync ( ( ) => {
970- expect ( overlayContainerElement . querySelectorAll ( '.mat-dialog-container' ) . length ) . toBe ( 1 ) ;
963+ describe ( 'inside component dialog' , ( ) => {
964+ beforeEach ( fakeAsync ( ( ) => {
965+ dialogRef = dialog . open ( ContentElementDialog , { viewContainerRef : testViewContainerRef } ) ;
966+ viewContainerFixture . detectChanges ( ) ;
967+ flush ( ) ;
968+ } ) ) ;
971969
972- ( overlayContainerElement . querySelector ( 'button[mat-dialog-close]' ) as HTMLElement ) . click ( ) ;
973- viewContainerFixture . detectChanges ( ) ;
974- flush ( ) ;
970+ runContentElementTests ( ) ;
971+ } ) ;
975972
976- expect ( overlayContainerElement . querySelectorAll ( '.mat-dialog-container' ) . length ) . toBe ( 0 ) ;
977- } ) ) ;
973+ describe ( 'inside template portal' , ( ) => {
974+ beforeEach ( fakeAsync ( ( ) => {
975+ const fixture = TestBed . createComponent ( ComponentWithContentElementTemplateRef ) ;
976+ fixture . detectChanges ( ) ;
978977
979- it ( 'should not close the dialog if [mat-dialog-close] is applied on a non-button node' , ( ) => {
980- expect ( overlayContainerElement . querySelectorAll ( '.mat-dialog-container' ) . length ) . toBe ( 1 ) ;
978+ dialogRef = dialog . open ( fixture . componentInstance . templateRef , {
979+ viewContainerRef : testViewContainerRef
980+ } ) ;
981981
982- ( overlayContainerElement . querySelector ( 'div[mat-dialog-close]' ) as HTMLElement ) . click ( ) ;
982+ viewContainerFixture . detectChanges ( ) ;
983+ flush ( ) ;
984+ } ) ) ;
983985
984- expect ( overlayContainerElement . querySelectorAll ( '.mat-dialog-container' ) . length ) . toBe ( 1 ) ;
986+ runContentElementTests ( ) ;
985987 } ) ;
986988
987- it ( 'should allow for a user-specified aria-label on the close button' , fakeAsync ( ( ) => {
988- let button = overlayContainerElement . querySelector ( 'button[mat-dialog-close]' ) ! ;
989+ function runContentElementTests ( ) {
990+ it ( 'should close the dialog when clicking on the close button' , fakeAsync ( ( ) => {
991+ expect ( overlayContainerElement . querySelectorAll ( '.mat-dialog-container' ) . length ) . toBe ( 1 ) ;
989992
990- dialogRef . componentInstance . closeButtonAriaLabel = 'Best close button ever' ;
991- viewContainerFixture . detectChanges ( ) ;
992- flush ( ) ;
993+ ( overlayContainerElement . querySelector ( 'button[mat-dialog- close]' ) as HTMLElement ) . click ( ) ;
994+ viewContainerFixture . detectChanges ( ) ;
995+ flush ( ) ;
993996
994- expect ( button . getAttribute ( 'aria-label' ) ) . toBe ( 'Best close button ever' ) ;
995- } ) ) ;
997+ expect ( overlayContainerElement . querySelectorAll ( '.mat-dialog-container' ) . length ) . toBe ( 0 ) ;
998+ } ) ) ;
996999
997- it ( 'should override the "type" attribute of the close button' , ( ) => {
998- let button = overlayContainerElement . querySelector ( 'button[ mat-dialog-close]' ) ! ;
1000+ it ( 'should not close if [mat-dialog-close] is applied on a non- button node ' , ( ) => {
1001+ expect ( overlayContainerElement . querySelectorAll ( '. mat-dialog-container' ) . length ) . toBe ( 1 ) ;
9991002
1000- expect ( button . getAttribute ( 'type' ) ) . toBe ( 'button' ) ;
1001- } ) ;
1003+ ( overlayContainerElement . querySelector ( 'div[mat-dialog-close]' ) as HTMLElement ) . click ( ) ;
10021004
1003- it ( 'should return the [mat-dialog-close] result when clicking the close button' ,
1004- fakeAsync ( ( ) => {
1005- let afterCloseCallback = jasmine . createSpy ( 'afterClose callback' ) ;
1006- dialogRef . afterClosed ( ) . subscribe ( afterCloseCallback ) ;
1007-
1008- ( overlayContainerElement . querySelector ( 'button.close-with-true' ) as HTMLElement ) . click ( ) ;
1009- viewContainerFixture . detectChanges ( ) ;
1010- flush ( ) ;
1005+ expect ( overlayContainerElement . querySelectorAll ( '.mat-dialog-container' ) . length ) . toBe ( 1 ) ;
1006+ } ) ;
10111007
1012- expect ( afterCloseCallback ) . toHaveBeenCalledWith ( true ) ;
1008+ it ( 'should allow for a user-specified aria-label on the close button' , fakeAsync ( ( ) => {
1009+ let button = overlayContainerElement . querySelector ( '.close-with-aria-label' ) ! ;
1010+ expect ( button . getAttribute ( 'aria-label' ) ) . toBe ( 'Best close button ever' ) ;
10131011 } ) ) ;
10141012
1015- it ( 'should set the aria-labelledby attribute to the id of the title' , fakeAsync ( ( ) => {
1016- let title = overlayContainerElement . querySelector ( '[mat-dialog-title]' ) ! ;
1017- let container = overlayContainerElement . querySelector ( 'mat-dialog-container' ) ! ;
1013+ it ( 'should override the "type" attribute of the close button' , ( ) => {
1014+ let button = overlayContainerElement . querySelector ( 'button[mat-dialog-close]' ) ! ;
10181015
1019- flush ( ) ;
1020- viewContainerFixture . detectChanges ( ) ;
1016+ expect ( button . getAttribute ( 'type' ) ) . toBe ( 'button' ) ;
1017+ } ) ;
10211018
1022- expect ( title . id ) . toBeTruthy ( 'Expected title element to have an id.' ) ;
1023- expect ( container . getAttribute ( 'aria-labelledby' ) )
1024- . toBe ( title . id , 'Expected the aria-labelledby to match the title id.' ) ;
1025- } ) ) ;
1019+ it ( 'should return the [mat-dialog-close] result when clicking the close button' ,
1020+ fakeAsync ( ( ) => {
1021+ let afterCloseCallback = jasmine . createSpy ( 'afterClose callback' ) ;
1022+ dialogRef . afterClosed ( ) . subscribe ( afterCloseCallback ) ;
1023+
1024+ ( overlayContainerElement . querySelector ( 'button.close-with-true' ) as HTMLElement ) . click ( ) ;
1025+ viewContainerFixture . detectChanges ( ) ;
1026+ flush ( ) ;
1027+
1028+ expect ( afterCloseCallback ) . toHaveBeenCalledWith ( true ) ;
1029+ } ) ) ;
1030+
1031+ it ( 'should set the aria-labelledby attribute to the id of the title' , fakeAsync ( ( ) => {
1032+ let title = overlayContainerElement . querySelector ( '[mat-dialog-title]' ) ! ;
1033+ let container = overlayContainerElement . querySelector ( 'mat-dialog-container' ) ! ;
10261034
1035+ flush ( ) ;
1036+ viewContainerFixture . detectChanges ( ) ;
1037+
1038+ expect ( title . id ) . toBeTruthy ( 'Expected title element to have an id.' ) ;
1039+ expect ( container . getAttribute ( 'aria-labelledby' ) )
1040+ . toBe ( title . id , 'Expected the aria-labelledby to match the title id.' ) ;
1041+ } ) ) ;
1042+ }
10271043 } ) ;
10281044
10291045 describe ( 'aria-label' , ( ) => {
@@ -1277,14 +1293,37 @@ class PizzaMsg {
12771293 <h1 mat-dialog-title>This is the title</h1>
12781294 <mat-dialog-content>Lorem ipsum dolor sit amet.</mat-dialog-content>
12791295 <mat-dialog-actions>
1280- <button mat-dialog-close [aria-label]="closeButtonAriaLabel" >Close</button>
1296+ <button mat-dialog-close>Close</button>
12811297 <button class="close-with-true" [mat-dialog-close]="true">Close and return true</button>
1298+ <button
1299+ class="close-with-aria-label"
1300+ aria-label="Best close button ever"
1301+ [mat-dialog-close]="true">Close</button>
12821302 <div mat-dialog-close>Should not close</div>
12831303 </mat-dialog-actions>
12841304 `
12851305} )
1286- class ContentElementDialog {
1287- closeButtonAriaLabel : string ;
1306+ class ContentElementDialog { }
1307+
1308+ @Component ( {
1309+ template : `
1310+ <ng-template>
1311+ <h1 mat-dialog-title>This is the title</h1>
1312+ <mat-dialog-content>Lorem ipsum dolor sit amet.</mat-dialog-content>
1313+ <mat-dialog-actions>
1314+ <button mat-dialog-close>Close</button>
1315+ <button class="close-with-true" [mat-dialog-close]="true">Close and return true</button>
1316+ <button
1317+ class="close-with-aria-label"
1318+ aria-label="Best close button ever"
1319+ [mat-dialog-close]="true">Close</button>
1320+ <div mat-dialog-close>Should not close</div>
1321+ </mat-dialog-actions>
1322+ </ng-template>
1323+ `
1324+ } )
1325+ class ComponentWithContentElementTemplateRef {
1326+ @ViewChild ( TemplateRef ) templateRef : TemplateRef < any > ;
12881327}
12891328
12901329@Component ( {
@@ -1314,7 +1353,8 @@ const TEST_DIRECTIVES = [
13141353 ComponentWithOnPushViewContainer ,
13151354 ContentElementDialog ,
13161355 DialogWithInjectedData ,
1317- DialogWithoutFocusableElements
1356+ DialogWithoutFocusableElements ,
1357+ ComponentWithContentElementTemplateRef ,
13181358] ;
13191359
13201360@NgModule ( {
0 commit comments