1
1
import { fakeAsync , async , tick , ComponentFixture , TestBed } from '@angular/core/testing' ;
2
- import { Component , ViewChild } from '@angular/core' ;
2
+ import { Component , ElementRef , ViewChild } from '@angular/core' ;
3
3
import { By } from '@angular/platform-browser' ;
4
4
import { MdSidenav , MdSidenavModule , MdSidenavToggleResult , MdSidenavContainer } from './index' ;
5
5
import { A11yModule } from '../core/a11y/index' ;
@@ -310,19 +310,20 @@ describe('MdSidenav', () => {
310
310
expect ( testComponent . closeCount ) . toBe ( 0 ) ;
311
311
} ) ) ;
312
312
313
- it ( 'should restore focus to the trigger element on close ' , fakeAsync ( ( ) => {
313
+ it ( 'should restore focus on close if focus is inside sidenav ' , fakeAsync ( ( ) => {
314
314
let fixture = TestBed . createComponent ( BasicTestApp ) ;
315
315
let sidenav : MdSidenav = fixture . debugElement
316
316
. query ( By . directive ( MdSidenav ) ) . componentInstance ;
317
- let trigger = document . createElement ( 'button' ) ;
317
+ let openButton = fixture . componentInstance . openButton . nativeElement ;
318
+ let sidenavButton = fixture . componentInstance . sidenavButton . nativeElement ;
318
319
319
- document . body . appendChild ( trigger ) ;
320
- trigger . focus ( ) ;
320
+ openButton . focus ( ) ;
321
321
sidenav . open ( ) ;
322
322
323
323
fixture . detectChanges ( ) ;
324
324
endSidenavTransition ( fixture ) ;
325
325
tick ( ) ;
326
+ sidenavButton . focus ( ) ;
326
327
327
328
sidenav . close ( ) ;
328
329
@@ -331,9 +332,32 @@ describe('MdSidenav', () => {
331
332
tick ( ) ;
332
333
333
334
expect ( document . activeElement )
334
- . toBe ( trigger , 'Expected focus to be restored to the trigger on close.' ) ;
335
+ . toBe ( openButton , 'Expected focus to be restored to the open button on close.' ) ;
336
+ } ) ) ;
337
+
338
+ it ( 'should not restore focus on close if focus is outside sidenav' , fakeAsync ( ( ) => {
339
+ let fixture = TestBed . createComponent ( BasicTestApp ) ;
340
+ let sidenav : MdSidenav = fixture . debugElement
341
+ . query ( By . directive ( MdSidenav ) ) . componentInstance ;
342
+ let openButton = fixture . componentInstance . openButton . nativeElement ;
343
+ let closeButton = fixture . componentInstance . closeButton . nativeElement ;
344
+
345
+ openButton . focus ( ) ;
346
+ sidenav . open ( ) ;
347
+
348
+ fixture . detectChanges ( ) ;
349
+ endSidenavTransition ( fixture ) ;
350
+ tick ( ) ;
351
+ closeButton . focus ( ) ;
352
+
353
+ sidenav . close ( ) ;
335
354
336
- trigger . parentNode . removeChild ( trigger ) ;
355
+ fixture . detectChanges ( ) ;
356
+ endSidenavTransition ( fixture ) ;
357
+ tick ( ) ;
358
+
359
+ expect ( document . activeElement )
360
+ . toBe ( closeButton , 'Expected focus not to be restored to the open button on close.' ) ;
337
361
} ) ) ;
338
362
} ) ;
339
363
@@ -508,10 +532,10 @@ class SidenavContainerTwoSidenavTestApp {
508
532
(open)="open()"
509
533
(close-start)="closeStart()"
510
534
(close)="close()">
511
- Content.
535
+ <button #sidenavButton> Content.</button>
512
536
</md-sidenav>
513
- <button (click)="sidenav.open()" class="open"></button>
514
- <button (click)="sidenav.close()" class="close"></button>
537
+ <button (click)="sidenav.open()" class="open" #openButton ></button>
538
+ <button (click)="sidenav.close()" class="close" #closeButton ></button>
515
539
</md-sidenav-container>` ,
516
540
} )
517
541
class BasicTestApp {
@@ -521,6 +545,10 @@ class BasicTestApp {
521
545
closeCount : number = 0 ;
522
546
backdropClickedCount : number = 0 ;
523
547
548
+ @ViewChild ( 'sidenavButton' ) sidenavButton : ElementRef ;
549
+ @ViewChild ( 'openButton' ) openButton : ElementRef ;
550
+ @ViewChild ( 'closeButton' ) closeButton : ElementRef ;
551
+
524
552
openStart ( ) {
525
553
this . openStartCount ++ ;
526
554
}
0 commit comments