@@ -958,72 +958,88 @@ describe('MatDialog', () => {
958
958
} ) ;
959
959
960
960
describe ( 'dialog content elements' , ( ) => {
961
- let dialogRef : MatDialogRef < ContentElementDialog > ;
961
+ let dialogRef : MatDialogRef < any > ;
962
962
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
+ } ) ) ;
971
969
972
- ( overlayContainerElement . querySelector ( 'button[mat-dialog-close]' ) as HTMLElement ) . click ( ) ;
973
- viewContainerFixture . detectChanges ( ) ;
974
- flush ( ) ;
970
+ runContentElementTests ( ) ;
971
+ } ) ;
975
972
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 ( ) ;
978
977
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
+ } ) ;
981
981
982
- ( overlayContainerElement . querySelector ( 'div[mat-dialog-close]' ) as HTMLElement ) . click ( ) ;
982
+ viewContainerFixture . detectChanges ( ) ;
983
+ flush ( ) ;
984
+ } ) ) ;
983
985
984
- expect ( overlayContainerElement . querySelectorAll ( '.mat-dialog-container' ) . length ) . toBe ( 1 ) ;
986
+ runContentElementTests ( ) ;
985
987
} ) ;
986
988
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 ) ;
989
992
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 ( ) ;
993
996
994
- expect ( button . getAttribute ( 'aria-label' ) ) . toBe ( 'Best close button ever' ) ;
995
- } ) ) ;
997
+ expect ( overlayContainerElement . querySelectorAll ( '.mat-dialog-container' ) . length ) . toBe ( 0 ) ;
998
+ } ) ) ;
996
999
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 ) ;
999
1002
1000
- expect ( button . getAttribute ( 'type' ) ) . toBe ( 'button' ) ;
1001
- } ) ;
1003
+ ( overlayContainerElement . querySelector ( 'div[mat-dialog-close]' ) as HTMLElement ) . click ( ) ;
1002
1004
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
+ } ) ;
1011
1007
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' ) ;
1013
1011
} ) ) ;
1014
1012
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]' ) ! ;
1018
1015
1019
- flush ( ) ;
1020
- viewContainerFixture . detectChanges ( ) ;
1016
+ expect ( button . getAttribute ( 'type' ) ) . toBe ( 'button' ) ;
1017
+ } ) ;
1021
1018
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' ) ! ;
1026
1034
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
+ }
1027
1043
} ) ;
1028
1044
1029
1045
describe ( 'aria-label' , ( ) => {
@@ -1277,14 +1293,37 @@ class PizzaMsg {
1277
1293
<h1 mat-dialog-title>This is the title</h1>
1278
1294
<mat-dialog-content>Lorem ipsum dolor sit amet.</mat-dialog-content>
1279
1295
<mat-dialog-actions>
1280
- <button mat-dialog-close [aria-label]="closeButtonAriaLabel" >Close</button>
1296
+ <button mat-dialog-close>Close</button>
1281
1297
<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>
1282
1302
<div mat-dialog-close>Should not close</div>
1283
1303
</mat-dialog-actions>
1284
1304
`
1285
1305
} )
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 > ;
1288
1327
}
1289
1328
1290
1329
@Component ( {
@@ -1314,7 +1353,8 @@ const TEST_DIRECTIVES = [
1314
1353
ComponentWithOnPushViewContainer ,
1315
1354
ContentElementDialog ,
1316
1355
DialogWithInjectedData ,
1317
- DialogWithoutFocusableElements
1356
+ DialogWithoutFocusableElements ,
1357
+ ComponentWithContentElementTemplateRef ,
1318
1358
] ;
1319
1359
1320
1360
@NgModule ( {
0 commit comments