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