@@ -653,6 +653,110 @@ describe('MatDrawer', () => {
653
653
expect ( scrollable . getElementRef ( ) . nativeElement ) . toBe ( content . nativeElement ) ;
654
654
} ) ;
655
655
656
+ describe ( 'DOM position' , ( ) => {
657
+ it ( 'should project start drawer before the content' , ( ) => {
658
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
659
+ fixture . componentInstance . position = 'start' ;
660
+ fixture . detectChanges ( ) ;
661
+
662
+ const allNodes = getDrawerNodesArray ( fixture ) ;
663
+ const drawerIndex = allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer' ) ) ;
664
+ const contentIndex =
665
+ allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ) ;
666
+
667
+ expect ( drawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
668
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
669
+ expect ( drawerIndex ) . toBeLessThan ( contentIndex , 'Expected drawer to be before the content' ) ;
670
+ } ) ;
671
+
672
+ it ( 'should project end drawer after the content' , ( ) => {
673
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
674
+ fixture . componentInstance . position = 'end' ;
675
+ fixture . detectChanges ( ) ;
676
+
677
+ const allNodes = getDrawerNodesArray ( fixture ) ;
678
+ const drawerIndex = allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer' ) ) ;
679
+ const contentIndex =
680
+ allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ) ;
681
+
682
+ expect ( drawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
683
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
684
+ expect ( drawerIndex ) . toBeGreaterThan ( contentIndex , 'Expected drawer to be after the content' ) ;
685
+ } ) ;
686
+
687
+ it ( 'should move the drawer before/after the content when its position changes after being ' +
688
+ 'initialized at `start`' , ( ) => {
689
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
690
+ fixture . componentInstance . position = 'start' ;
691
+ fixture . detectChanges ( ) ;
692
+
693
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
694
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
695
+
696
+ let allNodes = getDrawerNodesArray ( fixture ) ;
697
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
698
+ const startContentIndex = allNodes . indexOf ( content ) ;
699
+
700
+ expect ( startDrawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
701
+ expect ( startContentIndex )
702
+ . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
703
+ expect ( startDrawerIndex )
704
+ . toBeLessThan ( startContentIndex , 'Expected drawer to be before the content on init' ) ;
705
+
706
+ fixture . componentInstance . position = 'end' ;
707
+ fixture . detectChanges ( ) ;
708
+ allNodes = getDrawerNodesArray ( fixture ) ;
709
+
710
+ expect ( allNodes . indexOf ( drawer ) ) . toBeGreaterThan ( allNodes . indexOf ( content ) ,
711
+ 'Expected drawer to be after content when position changes to `end`' ) ;
712
+
713
+ fixture . componentInstance . position = 'start' ;
714
+ fixture . detectChanges ( ) ;
715
+ allNodes = getDrawerNodesArray ( fixture ) ;
716
+
717
+ expect ( allNodes . indexOf ( drawer ) ) . toBeLessThan ( allNodes . indexOf ( content ) ,
718
+ 'Expected drawer to be before content when position changes back to `start`' ) ;
719
+ } ) ;
720
+
721
+ it ( 'should move the drawer before/after the content when its position changes after being ' +
722
+ 'initialized at `end`' , ( ) => {
723
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
724
+ fixture . componentInstance . position = 'end' ;
725
+ fixture . detectChanges ( ) ;
726
+
727
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
728
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
729
+
730
+ let allNodes = getDrawerNodesArray ( fixture ) ;
731
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
732
+ const startContentIndex = allNodes . indexOf ( content ) ;
733
+
734
+ expect ( startDrawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
735
+ expect ( startContentIndex )
736
+ . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
737
+ expect ( startDrawerIndex )
738
+ . toBeGreaterThan ( startContentIndex , 'Expected drawer to be after the content on init' ) ;
739
+
740
+ fixture . componentInstance . position = 'start' ;
741
+ fixture . detectChanges ( ) ;
742
+ allNodes = getDrawerNodesArray ( fixture ) ;
743
+
744
+ expect ( allNodes . indexOf ( drawer ) ) . toBeLessThan ( allNodes . indexOf ( content ) ,
745
+ 'Expected drawer to be before content when position changes to `start`' ) ;
746
+
747
+ fixture . componentInstance . position = 'end' ;
748
+ fixture . detectChanges ( ) ;
749
+ allNodes = getDrawerNodesArray ( fixture ) ;
750
+
751
+ expect ( allNodes . indexOf ( drawer ) ) . toBeGreaterThan ( allNodes . indexOf ( content ) ,
752
+ 'Expected drawer to be after content when position changes back to `end`' ) ;
753
+ } ) ;
754
+
755
+ function getDrawerNodesArray ( fixture : ComponentFixture < any > ) : HTMLElement [ ] {
756
+ return Array . from ( fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ) ;
757
+ }
758
+
759
+ } ) ;
656
760
} ) ;
657
761
658
762
describe ( 'MatDrawerContainer' , ( ) => {
@@ -936,6 +1040,32 @@ describe('MatDrawerContainer', () => {
936
1040
expect ( spy ) . toHaveBeenCalled ( ) ;
937
1041
subscription . unsubscribe ( ) ;
938
1042
} ) ) ;
1043
+
1044
+ it ( 'should position the drawers before/after the content in the DOM based on their position' ,
1045
+ fakeAsync ( ( ) => {
1046
+ const fixture = TestBed . createComponent ( DrawerContainerTwoDrawerTestApp ) ;
1047
+ fixture . detectChanges ( ) ;
1048
+
1049
+ const drawerDebugElements = fixture . debugElement . queryAll ( By . directive ( MatDrawer ) ) ;
1050
+ const [ start , end ] = drawerDebugElements . map ( el => el . componentInstance ) ;
1051
+ const [ startNode , endNode ] = drawerDebugElements . map ( el => el . nativeElement ) ;
1052
+ const contentNode = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
1053
+ const allNodes : HTMLElement [ ] =
1054
+ Array . from ( fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ) ;
1055
+ const startIndex = allNodes . indexOf ( startNode ) ;
1056
+ const endIndex = allNodes . indexOf ( endNode ) ;
1057
+ const contentIndex = allNodes . indexOf ( contentNode ) ;
1058
+
1059
+ expect ( start . position ) . toBe ( 'start' ) ;
1060
+ expect ( end . position ) . toBe ( 'end' ) ;
1061
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
1062
+ expect ( startIndex ) . toBeGreaterThan ( - 1 , 'Expected start drawer to be inside the container' ) ;
1063
+ expect ( endIndex ) . toBeGreaterThan ( - 1 , 'Expected end drawer to be inside the container' ) ;
1064
+
1065
+ expect ( startIndex ) . toBeLessThan ( contentIndex , 'Expected start drawer to be before content' ) ;
1066
+ expect ( endIndex ) . toBeGreaterThan ( contentIndex , 'Expected end drawer to be after content' ) ;
1067
+ } ) ) ;
1068
+
939
1069
} ) ;
940
1070
941
1071
@@ -959,7 +1089,7 @@ class DrawerContainerTwoDrawerTestApp {
959
1089
@Component ( {
960
1090
template : `
961
1091
<mat-drawer-container (backdropClick)="backdropClicked()" [hasBackdrop]="hasBackdrop">
962
- <mat-drawer #drawer="matDrawer" position="start "
1092
+ <mat-drawer #drawer="matDrawer" [ position]="position "
963
1093
(opened)="open()"
964
1094
(openedStart)="openStart()"
965
1095
(closed)="close()"
@@ -985,6 +1115,7 @@ class BasicTestApp {
985
1115
closeStartCount = 0 ;
986
1116
backdropClickedCount = 0 ;
987
1117
hasBackdrop : boolean | null = null ;
1118
+ position = 'start' ;
988
1119
989
1120
@ViewChild ( 'drawer' ) drawer : MatDrawer ;
990
1121
@ViewChild ( 'drawerButton' ) drawerButton : ElementRef < HTMLButtonElement > ;
0 commit comments