@@ -642,6 +642,111 @@ describe('MatDrawer', () => {
642
642
} ) ) ;
643
643
644
644
} ) ;
645
+
646
+ describe ( 'DOM position' , ( ) => {
647
+ it ( 'should project start drawer before the content' , ( ) => {
648
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
649
+ fixture . componentInstance . position = 'start' ;
650
+ fixture . detectChanges ( ) ;
651
+
652
+ const allNodes = getDrawerNodesArray ( fixture ) ;
653
+ const drawerIndex = allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer' ) ) ;
654
+ const contentIndex =
655
+ allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ) ;
656
+
657
+ expect ( drawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
658
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
659
+ expect ( drawerIndex ) . toBeLessThan ( contentIndex , 'Expected drawer to be before the content' ) ;
660
+ } ) ;
661
+
662
+ it ( 'should project end drawer after the content' , ( ) => {
663
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
664
+ fixture . componentInstance . position = 'end' ;
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 ) . toBeGreaterThan ( contentIndex , 'Expected drawer to be after the content' ) ;
675
+ } ) ;
676
+
677
+ it ( 'should move the drawer before/after the content when its position changes after being ' +
678
+ 'initialized at `start`' , ( ) => {
679
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
680
+ fixture . componentInstance . position = 'start' ;
681
+ fixture . detectChanges ( ) ;
682
+
683
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
684
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
685
+
686
+ let allNodes = getDrawerNodesArray ( fixture ) ;
687
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
688
+ const startContentIndex = allNodes . indexOf ( content ) ;
689
+
690
+ expect ( startDrawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
691
+ expect ( startContentIndex )
692
+ . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
693
+ expect ( startDrawerIndex )
694
+ . toBeLessThan ( startContentIndex , 'Expected drawer to be before the content on init' ) ;
695
+
696
+ fixture . componentInstance . position = 'end' ;
697
+ fixture . detectChanges ( ) ;
698
+ allNodes = getDrawerNodesArray ( fixture ) ;
699
+
700
+ expect ( allNodes . indexOf ( drawer ) ) . toBeGreaterThan ( allNodes . indexOf ( content ) ,
701
+ 'Expected drawer to be after content when position changes to `end`' ) ;
702
+
703
+ fixture . componentInstance . position = 'start' ;
704
+ fixture . detectChanges ( ) ;
705
+ allNodes = getDrawerNodesArray ( fixture ) ;
706
+
707
+ expect ( allNodes . indexOf ( drawer ) ) . toBeLessThan ( allNodes . indexOf ( content ) ,
708
+ 'Expected drawer to be before content when position changes back to `start`' ) ;
709
+ } ) ;
710
+
711
+ it ( 'should move the drawer before/after the content when its position changes after being ' +
712
+ 'initialized at `end`' , ( ) => {
713
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
714
+ fixture . componentInstance . position = 'end' ;
715
+ fixture . detectChanges ( ) ;
716
+
717
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
718
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
719
+
720
+ let allNodes = getDrawerNodesArray ( fixture ) ;
721
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
722
+ const startContentIndex = allNodes . indexOf ( content ) ;
723
+
724
+ expect ( startDrawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
725
+ expect ( startContentIndex )
726
+ . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
727
+ expect ( startDrawerIndex )
728
+ . toBeGreaterThan ( startContentIndex , 'Expected drawer to be after the content on init' ) ;
729
+
730
+ fixture . componentInstance . position = 'start' ;
731
+ fixture . detectChanges ( ) ;
732
+ allNodes = getDrawerNodesArray ( fixture ) ;
733
+
734
+ expect ( allNodes . indexOf ( drawer ) ) . toBeLessThan ( allNodes . indexOf ( content ) ,
735
+ 'Expected drawer to be before content when position changes to `start`' ) ;
736
+
737
+ fixture . componentInstance . position = 'end' ;
738
+ fixture . detectChanges ( ) ;
739
+ allNodes = getDrawerNodesArray ( fixture ) ;
740
+
741
+ expect ( allNodes . indexOf ( drawer ) ) . toBeGreaterThan ( allNodes . indexOf ( content ) ,
742
+ 'Expected drawer to be after content when position changes back to `end`' ) ;
743
+ } ) ;
744
+
745
+ function getDrawerNodesArray ( fixture : ComponentFixture < any > ) : HTMLElement [ ] {
746
+ return Array . from ( fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ) ;
747
+ }
748
+
749
+ } ) ;
645
750
} ) ;
646
751
647
752
describe ( 'MatDrawerContainer' , ( ) => {
@@ -925,6 +1030,32 @@ describe('MatDrawerContainer', () => {
925
1030
expect ( spy ) . toHaveBeenCalled ( ) ;
926
1031
subscription . unsubscribe ( ) ;
927
1032
} ) ) ;
1033
+
1034
+ it ( 'should position the drawers before/after the content in the DOM based on their position' ,
1035
+ fakeAsync ( ( ) => {
1036
+ const fixture = TestBed . createComponent ( DrawerContainerTwoDrawerTestApp ) ;
1037
+ fixture . detectChanges ( ) ;
1038
+
1039
+ const drawerDebugElements = fixture . debugElement . queryAll ( By . directive ( MatDrawer ) ) ;
1040
+ const [ start , end ] = drawerDebugElements . map ( el => el . componentInstance ) ;
1041
+ const [ startNode , endNode ] = drawerDebugElements . map ( el => el . nativeElement ) ;
1042
+ const contentNode = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
1043
+ const allNodes : HTMLElement [ ] =
1044
+ Array . from ( fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ) ;
1045
+ const startIndex = allNodes . indexOf ( startNode ) ;
1046
+ const endIndex = allNodes . indexOf ( endNode ) ;
1047
+ const contentIndex = allNodes . indexOf ( contentNode ) ;
1048
+
1049
+ expect ( start . position ) . toBe ( 'start' ) ;
1050
+ expect ( end . position ) . toBe ( 'end' ) ;
1051
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
1052
+ expect ( startIndex ) . toBeGreaterThan ( - 1 , 'Expected start drawer to be inside the container' ) ;
1053
+ expect ( endIndex ) . toBeGreaterThan ( - 1 , 'Expected end drawer to be inside the container' ) ;
1054
+
1055
+ expect ( startIndex ) . toBeLessThan ( contentIndex , 'Expected start drawer to be before content' ) ;
1056
+ expect ( endIndex ) . toBeGreaterThan ( contentIndex , 'Expected end drawer to be after content' ) ;
1057
+ } ) ) ;
1058
+
928
1059
} ) ;
929
1060
930
1061
@@ -948,7 +1079,7 @@ class DrawerContainerTwoDrawerTestApp {
948
1079
@Component ( {
949
1080
template : `
950
1081
<mat-drawer-container (backdropClick)="backdropClicked()" [hasBackdrop]="hasBackdrop">
951
- <mat-drawer #drawer="matDrawer" position="start "
1082
+ <mat-drawer #drawer="matDrawer" [ position]="position "
952
1083
(opened)="open()"
953
1084
(openedStart)="openStart()"
954
1085
(closed)="close()"
@@ -974,6 +1105,7 @@ class BasicTestApp {
974
1105
closeStartCount = 0 ;
975
1106
backdropClickedCount = 0 ;
976
1107
hasBackdrop : boolean | null = null ;
1108
+ position = 'start' ;
977
1109
978
1110
@ViewChild ( 'drawer' ) drawer : MatDrawer ;
979
1111
@ViewChild ( 'drawerButton' ) drawerButton : ElementRef < HTMLButtonElement > ;
0 commit comments