@@ -661,6 +661,144 @@ describe('MatDrawer', () => {
661
661
expect ( scrollable ) . toBeTruthy ( ) ;
662
662
expect ( scrollable . getElementRef ( ) . nativeElement ) . toBe ( content . nativeElement ) ;
663
663
} ) ;
664
+
665
+ describe ( 'DOM position' , ( ) => {
666
+ it ( 'should project start drawer before the content' , ( ) => {
667
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
668
+ fixture . componentInstance . position = 'start' ;
669
+ fixture . detectChanges ( ) ;
670
+
671
+ const allNodes = getDrawerNodesArray ( fixture ) ;
672
+ const drawerIndex = allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer' ) ) ;
673
+ const contentIndex = allNodes . indexOf (
674
+ fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ,
675
+ ) ;
676
+
677
+ expect ( drawerIndex )
678
+ . withContext ( 'Expected drawer to be inside the container' )
679
+ . toBeGreaterThan ( - 1 ) ;
680
+ expect ( contentIndex )
681
+ . withContext ( 'Expected content to be inside the container' )
682
+ . toBeGreaterThan ( - 1 ) ;
683
+ expect ( drawerIndex )
684
+ . withContext ( 'Expected drawer to be before the content' )
685
+ . toBeLessThan ( contentIndex ) ;
686
+ } ) ;
687
+
688
+ it ( 'should project end drawer after the content' , ( ) => {
689
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
690
+ fixture . componentInstance . position = 'end' ;
691
+ fixture . detectChanges ( ) ;
692
+
693
+ const allNodes = getDrawerNodesArray ( fixture ) ;
694
+ const drawerIndex = allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer' ) ) ;
695
+ const contentIndex = allNodes . indexOf (
696
+ fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ,
697
+ ) ;
698
+
699
+ expect ( drawerIndex )
700
+ . withContext ( 'Expected drawer to be inside the container' )
701
+ . toBeGreaterThan ( - 1 ) ;
702
+ expect ( contentIndex )
703
+ . withContext ( 'Expected content to be inside the container' )
704
+ . toBeGreaterThan ( - 1 ) ;
705
+ expect ( drawerIndex )
706
+ . withContext ( 'Expected drawer to be after the content' )
707
+ . toBeGreaterThan ( contentIndex ) ;
708
+ } ) ;
709
+
710
+ it (
711
+ 'should move the drawer before/after the content when its position changes after being ' +
712
+ 'initialized at `start`' ,
713
+ ( ) => {
714
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
715
+ fixture . componentInstance . position = 'start' ;
716
+ fixture . detectChanges ( ) ;
717
+
718
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
719
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
720
+
721
+ let allNodes = getDrawerNodesArray ( fixture ) ;
722
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
723
+ const startContentIndex = allNodes . indexOf ( content ) ;
724
+
725
+ expect ( startDrawerIndex )
726
+ . withContext ( 'Expected drawer to be inside the container' )
727
+ . toBeGreaterThan ( - 1 ) ;
728
+ expect ( startContentIndex )
729
+ . withContext ( 'Expected content to be inside the container' )
730
+ . toBeGreaterThan ( - 1 ) ;
731
+ expect ( startDrawerIndex )
732
+ . withContext ( 'Expected drawer to be before the content on init' )
733
+ . toBeLessThan ( startContentIndex ) ;
734
+
735
+ fixture . componentInstance . position = 'end' ;
736
+ fixture . detectChanges ( ) ;
737
+ allNodes = getDrawerNodesArray ( fixture ) ;
738
+
739
+ expect ( allNodes . indexOf ( drawer ) )
740
+ . withContext ( 'Expected drawer to be after content when position changes to `end`' )
741
+ . toBeGreaterThan ( allNodes . indexOf ( content ) ) ;
742
+
743
+ fixture . componentInstance . position = 'start' ;
744
+ fixture . detectChanges ( ) ;
745
+ allNodes = getDrawerNodesArray ( fixture ) ;
746
+
747
+ expect ( allNodes . indexOf ( drawer ) )
748
+ . withContext ( 'Expected drawer to be before content when position changes back to `start`' )
749
+ . toBeLessThan ( allNodes . indexOf ( content ) ) ;
750
+ } ,
751
+ ) ;
752
+
753
+ it (
754
+ 'should move the drawer before/after the content when its position changes after being ' +
755
+ 'initialized at `end`' ,
756
+ ( ) => {
757
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
758
+ fixture . componentInstance . position = 'end' ;
759
+ fixture . detectChanges ( ) ;
760
+
761
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
762
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
763
+
764
+ let allNodes = getDrawerNodesArray ( fixture ) ;
765
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
766
+ const startContentIndex = allNodes . indexOf ( content ) ;
767
+
768
+ expect ( startDrawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
769
+ expect ( startContentIndex ) . toBeGreaterThan (
770
+ - 1 ,
771
+ 'Expected content to be inside the container' ,
772
+ ) ;
773
+ expect ( startDrawerIndex ) . toBeGreaterThan (
774
+ startContentIndex ,
775
+ 'Expected drawer to be after the content on init' ,
776
+ ) ;
777
+
778
+ fixture . componentInstance . position = 'start' ;
779
+ fixture . detectChanges ( ) ;
780
+ allNodes = getDrawerNodesArray ( fixture ) ;
781
+
782
+ expect ( allNodes . indexOf ( drawer ) ) . toBeLessThan (
783
+ allNodes . indexOf ( content ) ,
784
+ 'Expected drawer to be before content when position changes to `start`' ,
785
+ ) ;
786
+
787
+ fixture . componentInstance . position = 'end' ;
788
+ fixture . detectChanges ( ) ;
789
+ allNodes = getDrawerNodesArray ( fixture ) ;
790
+
791
+ expect ( allNodes . indexOf ( drawer ) ) . toBeGreaterThan (
792
+ allNodes . indexOf ( content ) ,
793
+ 'Expected drawer to be after content when position changes back to `end`' ,
794
+ ) ;
795
+ } ,
796
+ ) ;
797
+
798
+ function getDrawerNodesArray ( fixture : ComponentFixture < any > ) : HTMLElement [ ] {
799
+ return Array . from ( fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ) ;
800
+ }
801
+ } ) ;
664
802
} ) ;
665
803
666
804
describe ( 'MatDrawerContainer' , ( ) => {
@@ -949,6 +1087,41 @@ describe('MatDrawerContainer', () => {
949
1087
expect ( spy ) . toHaveBeenCalled ( ) ;
950
1088
subscription . unsubscribe ( ) ;
951
1089
} ) ) ;
1090
+
1091
+ it ( 'should position the drawers before/after the content in the DOM based on their position' , fakeAsync ( ( ) => {
1092
+ const fixture = TestBed . createComponent ( DrawerContainerTwoDrawerTestApp ) ;
1093
+ fixture . detectChanges ( ) ;
1094
+
1095
+ const drawerDebugElements = fixture . debugElement . queryAll ( By . directive ( MatDrawer ) ) ;
1096
+ const [ start , end ] = drawerDebugElements . map ( el => el . componentInstance ) ;
1097
+ const [ startNode , endNode ] = drawerDebugElements . map ( el => el . nativeElement ) ;
1098
+ const contentNode = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
1099
+ const allNodes : HTMLElement [ ] = Array . from (
1100
+ fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ,
1101
+ ) ;
1102
+ const startIndex = allNodes . indexOf ( startNode ) ;
1103
+ const endIndex = allNodes . indexOf ( endNode ) ;
1104
+ const contentIndex = allNodes . indexOf ( contentNode ) ;
1105
+
1106
+ expect ( start . position ) . toBe ( 'start' ) ;
1107
+ expect ( end . position ) . toBe ( 'end' ) ;
1108
+ expect ( contentIndex )
1109
+ . withContext ( 'Expected content to be inside the container' )
1110
+ . toBeGreaterThan ( - 1 ) ;
1111
+ expect ( startIndex )
1112
+ . withContext ( 'Expected start drawer to be inside the container' )
1113
+ . toBeGreaterThan ( - 1 ) ;
1114
+ expect ( endIndex )
1115
+ . withContext ( 'Expected end drawer to be inside the container' )
1116
+ . toBeGreaterThan ( - 1 ) ;
1117
+
1118
+ expect ( startIndex )
1119
+ . withContext ( 'Expected start drawer to be before content' )
1120
+ . toBeLessThan ( contentIndex ) ;
1121
+ expect ( endIndex )
1122
+ . withContext ( 'Expected end drawer to be after content' )
1123
+ . toBeGreaterThan ( contentIndex ) ;
1124
+ } ) ) ;
952
1125
} ) ;
953
1126
954
1127
/** Test component that contains an MatDrawerContainer but no MatDrawer. */
@@ -971,7 +1144,7 @@ class DrawerContainerTwoDrawerTestApp {
971
1144
@Component ( {
972
1145
template : `
973
1146
<mat-drawer-container (backdropClick)="backdropClicked()" [hasBackdrop]="hasBackdrop">
974
- <mat-drawer #drawer="matDrawer" position="start "
1147
+ <mat-drawer #drawer="matDrawer" [ position]="position "
975
1148
(opened)="open()"
976
1149
(openedStart)="openStart()"
977
1150
(closed)="close()"
@@ -997,6 +1170,7 @@ class BasicTestApp {
997
1170
closeStartCount = 0 ;
998
1171
backdropClickedCount = 0 ;
999
1172
hasBackdrop : boolean | null = null ;
1173
+ position = 'start' ;
1000
1174
1001
1175
@ViewChild ( 'drawer' ) drawer : MatDrawer ;
1002
1176
@ViewChild ( 'drawerButton' ) drawerButton : ElementRef < HTMLButtonElement > ;
0 commit comments