1- import { Component } from '@angular/core' ;
2- import { async , TestBed , ComponentFixture } from '@angular/core/testing' ;
3- import { MatSidenav , MatSidenavModule } from './index' ;
1+ import { Component , ViewChild } from '@angular/core' ;
2+ import { async , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
3+ import { MatSidenav , MatSidenavModule , MatSidenavContainer } from './index' ;
44import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
55import { By } from '@angular/platform-browser' ;
6+ import { CommonModule } from '@angular/common' ;
67
78
89describe ( 'MatSidenav' , ( ) => {
9- let fixture : ComponentFixture < SidenavWithFixedPosition > ;
10- let sidenavEl : HTMLElement ;
11-
1210 beforeEach ( async ( ( ) => {
1311 TestBed . configureTestingModule ( {
14- imports : [ MatSidenavModule , NoopAnimationsModule ] ,
15- declarations : [ SidenavWithFixedPosition ] ,
12+ imports : [ MatSidenavModule , NoopAnimationsModule , CommonModule ] ,
13+ declarations : [ SidenavWithFixedPosition , IndirectDescendantSidenav ] ,
1614 } ) ;
1715
1816 TestBed . compileComponents ( ) ;
19-
20- fixture = TestBed . createComponent ( SidenavWithFixedPosition ) ;
21- fixture . detectChanges ( ) ;
22-
23- sidenavEl = fixture . debugElement . query ( By . directive ( MatSidenav ) ) ! . nativeElement ;
2417 } ) ) ;
2518
2619 it ( 'should be fixed position when in fixed mode' , ( ) => {
20+ const fixture = TestBed . createComponent ( SidenavWithFixedPosition ) ;
21+ fixture . detectChanges ( ) ;
22+ const sidenavEl = fixture . debugElement . query ( By . directive ( MatSidenav ) ) ! . nativeElement ;
23+
2724 expect ( sidenavEl . classList ) . toContain ( 'mat-sidenav-fixed' ) ;
2825
2926 fixture . componentInstance . fixed = false ;
@@ -33,6 +30,10 @@ describe('MatSidenav', () => {
3330 } ) ;
3431
3532 it ( 'should set fixed bottom and top when in fixed mode' , ( ) => {
33+ const fixture = TestBed . createComponent ( SidenavWithFixedPosition ) ;
34+ fixture . detectChanges ( ) ;
35+ const sidenavEl = fixture . debugElement . query ( By . directive ( MatSidenav ) ) ! . nativeElement ;
36+
3637 expect ( sidenavEl . style . top ) . toBe ( '20px' ) ;
3738 expect ( sidenavEl . style . bottom ) . toBe ( '30px' ) ;
3839
@@ -42,6 +43,21 @@ describe('MatSidenav', () => {
4243 expect ( sidenavEl . style . top ) . toBeFalsy ( ) ;
4344 expect ( sidenavEl . style . bottom ) . toBeFalsy ( ) ;
4445 } ) ;
46+
47+ it ( 'should pick up sidenavs that are not direct descendants' , fakeAsync ( ( ) => {
48+ const fixture = TestBed . createComponent ( IndirectDescendantSidenav ) ;
49+ fixture . detectChanges ( ) ;
50+
51+ expect ( fixture . componentInstance . sidenav . opened ) . toBe ( false ) ;
52+
53+ fixture . componentInstance . container . open ( ) ;
54+ fixture . detectChanges ( ) ;
55+ tick ( ) ;
56+ fixture . detectChanges ( ) ;
57+
58+ expect ( fixture . componentInstance . sidenav . opened ) . toBe ( true ) ;
59+ } ) ) ;
60+
4561} ) ;
4662
4763
@@ -65,3 +81,20 @@ class SidenavWithFixedPosition {
6581 fixedTop = 20 ;
6682 fixedBottom = 30 ;
6783}
84+
85+
86+ @Component ( {
87+ // Note that we need the `ng-container` with the `ngSwitch` so that
88+ // there's a directive between the container and the sidenav.
89+ template : `
90+ <mat-sidenav-container #container>
91+ <ng-container [ngSwitch]="true">
92+ <mat-sidenav #sidenav>Sidenav.</mat-sidenav>
93+ </ng-container>
94+ <mat-sidenav-content>Some content.</mat-sidenav-content>
95+ </mat-sidenav-container>` ,
96+ } )
97+ class IndirectDescendantSidenav {
98+ @ViewChild ( 'container' , { static : false } ) container : MatSidenavContainer ;
99+ @ViewChild ( 'sidenav' , { static : false } ) sidenav : MatSidenav ;
100+ }
0 commit comments