@@ -658,6 +658,110 @@ describe('MatDrawer', () => {
658
658
expect ( scrollable . getElementRef ( ) . nativeElement ) . toBe ( content . nativeElement ) ;
659
659
} ) ;
660
660
661
+ describe ( 'DOM position' , ( ) => {
662
+ it ( 'should project start drawer before the content' , ( ) => {
663
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
664
+ fixture . componentInstance . position = 'start' ;
665
+ fixture . detectChanges ( ) ;
666
+
667
+ const allNodes = getDrawerNodesArray ( fixture ) ;
668
+ const drawerIndex = allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer' ) ) ;
669
+ const contentIndex =
670
+ allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ) ;
671
+
672
+ expect ( drawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
673
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
674
+ expect ( drawerIndex ) . toBeLessThan ( contentIndex , 'Expected drawer to be before the content' ) ;
675
+ } ) ;
676
+
677
+ it ( 'should project end drawer after the content' , ( ) => {
678
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
679
+ fixture . componentInstance . position = 'end' ;
680
+ fixture . detectChanges ( ) ;
681
+
682
+ const allNodes = getDrawerNodesArray ( fixture ) ;
683
+ const drawerIndex = allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer' ) ) ;
684
+ const contentIndex =
685
+ allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ) ;
686
+
687
+ expect ( drawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
688
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
689
+ expect ( drawerIndex ) . toBeGreaterThan ( contentIndex , 'Expected drawer to be after the content' ) ;
690
+ } ) ;
691
+
692
+ it ( 'should move the drawer before/after the content when its position changes after being ' +
693
+ 'initialized at `start`' , ( ) => {
694
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
695
+ fixture . componentInstance . position = 'start' ;
696
+ fixture . detectChanges ( ) ;
697
+
698
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
699
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
700
+
701
+ let allNodes = getDrawerNodesArray ( fixture ) ;
702
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
703
+ const startContentIndex = allNodes . indexOf ( content ) ;
704
+
705
+ expect ( startDrawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
706
+ expect ( startContentIndex )
707
+ . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
708
+ expect ( startDrawerIndex )
709
+ . toBeLessThan ( startContentIndex , 'Expected drawer to be before the content on init' ) ;
710
+
711
+ fixture . componentInstance . position = 'end' ;
712
+ fixture . detectChanges ( ) ;
713
+ allNodes = getDrawerNodesArray ( fixture ) ;
714
+
715
+ expect ( allNodes . indexOf ( drawer ) ) . toBeGreaterThan ( allNodes . indexOf ( content ) ,
716
+ 'Expected drawer to be after content when position changes to `end`' ) ;
717
+
718
+ fixture . componentInstance . position = 'start' ;
719
+ fixture . detectChanges ( ) ;
720
+ allNodes = getDrawerNodesArray ( fixture ) ;
721
+
722
+ expect ( allNodes . indexOf ( drawer ) ) . toBeLessThan ( allNodes . indexOf ( content ) ,
723
+ 'Expected drawer to be before content when position changes back to `start`' ) ;
724
+ } ) ;
725
+
726
+ it ( 'should move the drawer before/after the content when its position changes after being ' +
727
+ 'initialized at `end`' , ( ) => {
728
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
729
+ fixture . componentInstance . position = 'end' ;
730
+ fixture . detectChanges ( ) ;
731
+
732
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
733
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
734
+
735
+ let allNodes = getDrawerNodesArray ( fixture ) ;
736
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
737
+ const startContentIndex = allNodes . indexOf ( content ) ;
738
+
739
+ expect ( startDrawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
740
+ expect ( startContentIndex )
741
+ . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
742
+ expect ( startDrawerIndex )
743
+ . toBeGreaterThan ( startContentIndex , 'Expected drawer to be after the content on init' ) ;
744
+
745
+ fixture . componentInstance . position = 'start' ;
746
+ fixture . detectChanges ( ) ;
747
+ allNodes = getDrawerNodesArray ( fixture ) ;
748
+
749
+ expect ( allNodes . indexOf ( drawer ) ) . toBeLessThan ( allNodes . indexOf ( content ) ,
750
+ 'Expected drawer to be before content when position changes to `start`' ) ;
751
+
752
+ fixture . componentInstance . position = 'end' ;
753
+ fixture . detectChanges ( ) ;
754
+ allNodes = getDrawerNodesArray ( fixture ) ;
755
+
756
+ expect ( allNodes . indexOf ( drawer ) ) . toBeGreaterThan ( allNodes . indexOf ( content ) ,
757
+ 'Expected drawer to be after content when position changes back to `end`' ) ;
758
+ } ) ;
759
+
760
+ function getDrawerNodesArray ( fixture : ComponentFixture < any > ) : HTMLElement [ ] {
761
+ return Array . from ( fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ) ;
762
+ }
763
+
764
+ } ) ;
661
765
} ) ;
662
766
663
767
describe ( 'MatDrawerContainer' , ( ) => {
@@ -943,6 +1047,32 @@ describe('MatDrawerContainer', () => {
943
1047
expect ( spy ) . toHaveBeenCalled ( ) ;
944
1048
subscription . unsubscribe ( ) ;
945
1049
} ) ) ;
1050
+
1051
+ it ( 'should position the drawers before/after the content in the DOM based on their position' ,
1052
+ fakeAsync ( ( ) => {
1053
+ const fixture = TestBed . createComponent ( DrawerContainerTwoDrawerTestApp ) ;
1054
+ fixture . detectChanges ( ) ;
1055
+
1056
+ const drawerDebugElements = fixture . debugElement . queryAll ( By . directive ( MatDrawer ) ) ;
1057
+ const [ start , end ] = drawerDebugElements . map ( el => el . componentInstance ) ;
1058
+ const [ startNode , endNode ] = drawerDebugElements . map ( el => el . nativeElement ) ;
1059
+ const contentNode = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
1060
+ const allNodes : HTMLElement [ ] =
1061
+ Array . from ( fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ) ;
1062
+ const startIndex = allNodes . indexOf ( startNode ) ;
1063
+ const endIndex = allNodes . indexOf ( endNode ) ;
1064
+ const contentIndex = allNodes . indexOf ( contentNode ) ;
1065
+
1066
+ expect ( start . position ) . toBe ( 'start' ) ;
1067
+ expect ( end . position ) . toBe ( 'end' ) ;
1068
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
1069
+ expect ( startIndex ) . toBeGreaterThan ( - 1 , 'Expected start drawer to be inside the container' ) ;
1070
+ expect ( endIndex ) . toBeGreaterThan ( - 1 , 'Expected end drawer to be inside the container' ) ;
1071
+
1072
+ expect ( startIndex ) . toBeLessThan ( contentIndex , 'Expected start drawer to be before content' ) ;
1073
+ expect ( endIndex ) . toBeGreaterThan ( contentIndex , 'Expected end drawer to be after content' ) ;
1074
+ } ) ) ;
1075
+
946
1076
} ) ;
947
1077
948
1078
@@ -966,7 +1096,7 @@ class DrawerContainerTwoDrawerTestApp {
966
1096
@Component ( {
967
1097
template : `
968
1098
<mat-drawer-container (backdropClick)="backdropClicked()" [hasBackdrop]="hasBackdrop">
969
- <mat-drawer #drawer="matDrawer" position="start "
1099
+ <mat-drawer #drawer="matDrawer" [ position]="position "
970
1100
(opened)="open()"
971
1101
(openedStart)="openStart()"
972
1102
(closed)="close()"
@@ -992,6 +1122,7 @@ class BasicTestApp {
992
1122
closeStartCount = 0 ;
993
1123
backdropClickedCount = 0 ;
994
1124
hasBackdrop : boolean | null = null ;
1125
+ position = 'start' ;
995
1126
996
1127
@ViewChild ( 'drawer' ) drawer : MatDrawer ;
997
1128
@ViewChild ( 'drawerButton' ) drawerButton : ElementRef < HTMLButtonElement > ;
0 commit comments