@@ -592,6 +592,111 @@ describe('MatDrawer', () => {
592
592
} ) ) ;
593
593
594
594
} ) ;
595
+
596
+ describe ( 'DOM position' , ( ) => {
597
+ it ( 'should project start drawer before the content' , ( ) => {
598
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
599
+ fixture . componentInstance . position = 'start' ;
600
+ fixture . detectChanges ( ) ;
601
+
602
+ const allNodes = getDrawerNodesArray ( fixture ) ;
603
+ const drawerIndex = allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer' ) ) ;
604
+ const contentIndex =
605
+ allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ) ;
606
+
607
+ expect ( drawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
608
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
609
+ expect ( drawerIndex ) . toBeLessThan ( contentIndex , 'Expected drawer to be before the content' ) ;
610
+ } ) ;
611
+
612
+ it ( 'should project end drawer after the content' , ( ) => {
613
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
614
+ fixture . componentInstance . position = 'end' ;
615
+ fixture . detectChanges ( ) ;
616
+
617
+ const allNodes = getDrawerNodesArray ( fixture ) ;
618
+ const drawerIndex = allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer' ) ) ;
619
+ const contentIndex =
620
+ allNodes . indexOf ( fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ) ;
621
+
622
+ expect ( drawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
623
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
624
+ expect ( drawerIndex ) . toBeGreaterThan ( contentIndex , 'Expected drawer to be after the content' ) ;
625
+ } ) ;
626
+
627
+ it ( 'should move the drawer before/after the content when its position changes after being ' +
628
+ 'initialized at `start`' , ( ) => {
629
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
630
+ fixture . componentInstance . position = 'start' ;
631
+ fixture . detectChanges ( ) ;
632
+
633
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
634
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
635
+
636
+ let allNodes = getDrawerNodesArray ( fixture ) ;
637
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
638
+ const startContentIndex = allNodes . indexOf ( content ) ;
639
+
640
+ expect ( startDrawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
641
+ expect ( startContentIndex )
642
+ . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
643
+ expect ( startDrawerIndex )
644
+ . toBeLessThan ( startContentIndex , 'Expected drawer to be before the content on init' ) ;
645
+
646
+ fixture . componentInstance . position = 'end' ;
647
+ fixture . detectChanges ( ) ;
648
+ allNodes = getDrawerNodesArray ( fixture ) ;
649
+
650
+ expect ( allNodes . indexOf ( drawer ) ) . toBeGreaterThan ( allNodes . indexOf ( content ) ,
651
+ 'Expected drawer to be after content when position changes to `end`' ) ;
652
+
653
+ fixture . componentInstance . position = 'start' ;
654
+ fixture . detectChanges ( ) ;
655
+ allNodes = getDrawerNodesArray ( fixture ) ;
656
+
657
+ expect ( allNodes . indexOf ( drawer ) ) . toBeLessThan ( allNodes . indexOf ( content ) ,
658
+ 'Expected drawer to be before content when position changes back to `start`' ) ;
659
+ } ) ;
660
+
661
+ it ( 'should move the drawer before/after the content when its position changes after being ' +
662
+ 'initialized at `end`' , ( ) => {
663
+ const fixture = TestBed . createComponent ( BasicTestApp ) ;
664
+ fixture . componentInstance . position = 'end' ;
665
+ fixture . detectChanges ( ) ;
666
+
667
+ const drawer = fixture . nativeElement . querySelector ( '.mat-drawer' ) ;
668
+ const content = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
669
+
670
+ let allNodes = getDrawerNodesArray ( fixture ) ;
671
+ const startDrawerIndex = allNodes . indexOf ( drawer ) ;
672
+ const startContentIndex = allNodes . indexOf ( content ) ;
673
+
674
+ expect ( startDrawerIndex ) . toBeGreaterThan ( - 1 , 'Expected drawer to be inside the container' ) ;
675
+ expect ( startContentIndex )
676
+ . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
677
+ expect ( startDrawerIndex )
678
+ . toBeGreaterThan ( startContentIndex , 'Expected drawer to be after the content on init' ) ;
679
+
680
+ fixture . componentInstance . position = 'start' ;
681
+ fixture . detectChanges ( ) ;
682
+ allNodes = getDrawerNodesArray ( fixture ) ;
683
+
684
+ expect ( allNodes . indexOf ( drawer ) ) . toBeLessThan ( allNodes . indexOf ( content ) ,
685
+ 'Expected drawer to be before content when position changes to `start`' ) ;
686
+
687
+ fixture . componentInstance . position = 'end' ;
688
+ fixture . detectChanges ( ) ;
689
+ allNodes = getDrawerNodesArray ( fixture ) ;
690
+
691
+ expect ( allNodes . indexOf ( drawer ) ) . toBeGreaterThan ( allNodes . indexOf ( content ) ,
692
+ 'Expected drawer to be after content when position changes back to `end`' ) ;
693
+ } ) ;
694
+
695
+ function getDrawerNodesArray ( fixture : ComponentFixture < any > ) : HTMLElement [ ] {
696
+ return Array . from ( fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ) ;
697
+ }
698
+
699
+ } ) ;
595
700
} ) ;
596
701
597
702
describe ( 'MatDrawerContainer' , ( ) => {
@@ -875,6 +980,32 @@ describe('MatDrawerContainer', () => {
875
980
expect ( spy ) . toHaveBeenCalled ( ) ;
876
981
subscription . unsubscribe ( ) ;
877
982
} ) ) ;
983
+
984
+ it ( 'should position the drawers before/after the content in the DOM based on their position' ,
985
+ fakeAsync ( ( ) => {
986
+ const fixture = TestBed . createComponent ( DrawerContainerTwoDrawerTestApp ) ;
987
+ fixture . detectChanges ( ) ;
988
+
989
+ const drawerDebugElements = fixture . debugElement . queryAll ( By . directive ( MatDrawer ) ) ;
990
+ const [ start , end ] = drawerDebugElements . map ( el => el . componentInstance ) ;
991
+ const [ startNode , endNode ] = drawerDebugElements . map ( el => el . nativeElement ) ;
992
+ const contentNode = fixture . nativeElement . querySelector ( '.mat-drawer-content' ) ;
993
+ const allNodes : HTMLElement [ ] =
994
+ Array . from ( fixture . nativeElement . querySelector ( '.mat-drawer-container' ) . childNodes ) ;
995
+ const startIndex = allNodes . indexOf ( startNode ) ;
996
+ const endIndex = allNodes . indexOf ( endNode ) ;
997
+ const contentIndex = allNodes . indexOf ( contentNode ) ;
998
+
999
+ expect ( start . position ) . toBe ( 'start' ) ;
1000
+ expect ( end . position ) . toBe ( 'end' ) ;
1001
+ expect ( contentIndex ) . toBeGreaterThan ( - 1 , 'Expected content to be inside the container' ) ;
1002
+ expect ( startIndex ) . toBeGreaterThan ( - 1 , 'Expected start drawer to be inside the container' ) ;
1003
+ expect ( endIndex ) . toBeGreaterThan ( - 1 , 'Expected end drawer to be inside the container' ) ;
1004
+
1005
+ expect ( startIndex ) . toBeLessThan ( contentIndex , 'Expected start drawer to be before content' ) ;
1006
+ expect ( endIndex ) . toBeGreaterThan ( contentIndex , 'Expected end drawer to be after content' ) ;
1007
+ } ) ) ;
1008
+
878
1009
} ) ;
879
1010
880
1011
@@ -898,7 +1029,7 @@ class DrawerContainerTwoDrawerTestApp {
898
1029
@Component ( {
899
1030
template : `
900
1031
<mat-drawer-container (backdropClick)="backdropClicked()" [hasBackdrop]="hasBackdrop">
901
- <mat-drawer #drawer="matDrawer" position="start "
1032
+ <mat-drawer #drawer="matDrawer" [ position]="position "
902
1033
(opened)="open()"
903
1034
(openedStart)="openStart()"
904
1035
(closed)="close()"
@@ -916,6 +1047,7 @@ class BasicTestApp {
916
1047
closeStartCount = 0 ;
917
1048
backdropClickedCount = 0 ;
918
1049
hasBackdrop : boolean | null = null ;
1050
+ position = 'start' ;
919
1051
920
1052
@ViewChild ( 'drawer' ) drawer : MatDrawer ;
921
1053
@ViewChild ( 'drawerButton' ) drawerButton : ElementRef < HTMLButtonElement > ;
0 commit comments