@@ -2,6 +2,7 @@ import {fakeAsync, async, tick, ComponentFixture, TestBed} from '@angular/core/t
22import { Component } from '@angular/core' ;
33import { By } from '@angular/platform-browser' ;
44import { MdSidenav , MdSidenavModule } from './sidenav' ;
5+ import { A11yModule } from "../core/a11y/index" ;
56
67
78function endSidenavTransition ( fixture : ComponentFixture < any > ) {
@@ -18,14 +19,15 @@ describe('MdSidenav', () => {
1819
1920 beforeEach ( async ( ( ) => {
2021 TestBed . configureTestingModule ( {
21- imports : [ MdSidenavModule . forRoot ( ) ] ,
22+ imports : [ MdSidenavModule . forRoot ( ) , A11yModule . forRoot ( ) ] ,
2223 declarations : [
2324 BasicTestApp ,
2425 SidenavLayoutTwoSidenavTestApp ,
2526 SidenavLayoutNoSidenavTestApp ,
2627 SidenavSetToOpenedFalse ,
2728 SidenavSetToOpenedTrue ,
2829 SidenavDynamicAlign ,
30+ SidenavWitFocusableElements ,
2931 ] ,
3032 } ) ;
3133
@@ -197,7 +199,6 @@ describe('MdSidenav', () => {
197199 } ) ;
198200
199201 describe ( 'attributes' , ( ) => {
200-
201202 it ( 'should correctly parse opened="false"' , ( ) => {
202203 let fixture = TestBed . createComponent ( SidenavSetToOpenedFalse ) ;
203204 fixture . detectChanges ( ) ;
@@ -251,6 +252,55 @@ describe('MdSidenav', () => {
251252 } ) ;
252253 } ) ;
253254
255+ describe ( 'focus trapping behavior' , ( ) => {
256+ let fixture : ComponentFixture < SidenavWitFocusableElements > ;
257+ let testComponent : SidenavWitFocusableElements ;
258+ let sidenav : MdSidenav ;
259+ let link1Element : HTMLElement ;
260+ let link2Element : HTMLElement ;
261+
262+ beforeEach ( ( ) => {
263+ fixture = TestBed . createComponent ( SidenavWitFocusableElements ) ;
264+ testComponent = fixture . debugElement . componentInstance ;
265+ sidenav = fixture . debugElement . query ( By . directive ( MdSidenav ) ) . componentInstance ;
266+ link1Element = fixture . debugElement . query ( By . css ( '.link1' ) ) . nativeElement ;
267+ link2Element = fixture . debugElement . query ( By . css ( '.link1' ) ) . nativeElement ;
268+ link2Element . focus ( ) ;
269+ } ) ;
270+
271+ it ( 'should trp focus when opened in "over" mode' , fakeAsync ( ( ) => {
272+ testComponent . mode = 'over' ;
273+ link2Element . focus ( ) ;
274+
275+ sidenav . open ( ) ;
276+ endSidenavTransition ( fixture ) ;
277+ tick ( ) ;
278+
279+ expect ( document . activeElement ) . toBe ( link1Element ) ;
280+ } ) ) ;
281+
282+ it ( 'should trap tabs when opened in "push" mode' , fakeAsync ( ( ) => {
283+ testComponent . mode = 'push' ;
284+ link2Element . focus ( ) ;
285+
286+ sidenav . open ( ) ;
287+ endSidenavTransition ( fixture ) ;
288+ tick ( ) ;
289+
290+ expect ( document . activeElement ) . toBe ( link1Element ) ;
291+ } ) ) ;
292+
293+ it ( 'should not trap tabs when opened in "side" mode' , fakeAsync ( ( ) => {
294+ testComponent . mode = 'side' ;
295+ link2Element . focus ( ) ;
296+
297+ sidenav . open ( ) ;
298+ endSidenavTransition ( fixture ) ;
299+ tick ( ) ;
300+
301+ expect ( document . activeElement ) . toBe ( link2Element ) ;
302+ } ) ) ;
303+ } ) ;
254304} ) ;
255305
256306
@@ -337,3 +387,16 @@ class SidenavDynamicAlign {
337387 sidenav1Align = 'start' ;
338388 sidenav2Align = 'end' ;
339389}
390+
391+ @Component ( {
392+ template : `
393+ <md-sidenav-layout>
394+ <md-sidenav align="start" [mode]="mode">
395+ <a class="link1" href="#">link1</a>
396+ </md-sidenav>
397+ <a class="link2" href="#">link2</a>
398+ </md-sidenav-layout>` ,
399+ } )
400+ class SidenavWitFocusableElements {
401+ mode : string = 'over' ;
402+ }
0 commit comments