1+ import { Component , ViewChild } from '@angular/core' ;
12import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
3+ import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
4+ import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
5+ import { By } from '@angular/platform-browser' ;
26import { MdDatepickerModule } from './index' ;
3- import { Component , ViewChild } from '@angular/core' ;
47import { MdDatepicker } from './datepicker' ;
58import { MdDatepickerInput } from './datepicker-input' ;
6- import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
7- import { By } from '@angular/platform-browser' ;
8- import { dispatchFakeEvent , dispatchMouseEvent } from '../core/testing/dispatch-events' ;
99import { MdInputModule } from '../input/index' ;
10- import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
1110import { MdNativeDateModule } from '../core/datetime/index' ;
11+ import { ESCAPE } from '../core' ;
12+ import {
13+ dispatchFakeEvent ,
14+ dispatchMouseEvent ,
15+ dispatchKeyboardEvent ,
16+ } from '../core/testing/dispatch-events' ;
1217
1318
1419// When constructing a Date, the month is zero-based. This can be confusing, since people are
@@ -17,7 +22,7 @@ const JAN = 0, FEB = 1, MAR = 2, APR = 3, MAY = 4, JUN = 5, JUL = 6, AUG = 7, SE
1722 NOV = 10 , DEC = 11 ;
1823
1924
20- describe ( 'MdDatepicker' , ( ) => {
25+ fdescribe ( 'MdDatepicker' , ( ) => {
2126 describe ( 'with MdNativeDateModule' , ( ) => {
2227 beforeEach ( async ( ( ) => {
2328 TestBed . configureTestingModule ( {
@@ -99,6 +104,23 @@ describe('MdDatepicker', () => {
99104 } ) ;
100105 } ) ) ;
101106
107+ it ( 'should close the popup when pressing ESCAPE' , ( ) => {
108+ testComponent . datepicker . open ( ) ;
109+ fixture . detectChanges ( ) ;
110+
111+ let content = document . querySelector ( '.cdk-overlay-pane md-datepicker-content' ) ;
112+ expect ( content ) . toBeTruthy ( 'Expected datepicker to be open.' ) ;
113+
114+ let keyboadEvent = dispatchKeyboardEvent ( content , 'keydown' , ESCAPE ) ;
115+ fixture . detectChanges ( ) ;
116+
117+ content = document . querySelector ( '.cdk-overlay-pane md-datepicker-content' ) ;
118+
119+ expect ( content ) . toBeFalsy ( 'Expected datepicker to be closed.' ) ;
120+ expect ( keyboadEvent . defaultPrevented )
121+ . toBe ( true , 'Expected default ESCAPE action to be prevented.' ) ;
122+ } ) ;
123+
102124 it ( 'close should close dialog' , async ( ( ) => {
103125 testComponent . touch = true ;
104126 fixture . detectChanges ( ) ;
@@ -388,6 +410,30 @@ describe('MdDatepicker', () => {
388410 let toggle = fixture . debugElement . query ( By . css ( 'button' ) ) . nativeElement ;
389411 expect ( toggle . getAttribute ( 'type' ) ) . toBe ( 'button' ) ;
390412 } ) ;
413+
414+ it ( 'should restore focus to the toggle after the calendar is closed' , ( ) => {
415+ let toggle = fixture . debugElement . query ( By . css ( 'button' ) ) . nativeElement ;
416+
417+ fixture . componentInstance . touchUI = false ;
418+ fixture . detectChanges ( ) ;
419+
420+ toggle . focus ( ) ;
421+ expect ( document . activeElement ) . toBe ( toggle , 'Expected toggle to be focused.' ) ;
422+
423+ fixture . componentInstance . datepicker . open ( ) ;
424+ fixture . detectChanges ( ) ;
425+
426+ let pane = document . querySelector ( '.cdk-overlay-pane' ) ;
427+
428+ expect ( pane ) . toBeTruthy ( 'Expected calendar to be open.' ) ;
429+ expect ( pane . contains ( document . activeElement ) )
430+ . toBe ( true , 'Expected focus to be inside the calendar.' ) ;
431+
432+ fixture . componentInstance . datepicker . close ( ) ;
433+ fixture . detectChanges ( ) ;
434+
435+ expect ( document . activeElement ) . toBe ( toggle , 'Expected focus to be restored to toggle.' ) ;
436+ } ) ;
391437 } ) ;
392438
393439 describe ( 'datepicker inside input-container' , ( ) => {
@@ -582,11 +628,12 @@ class DatepickerWithFormControl {
582628 template : `
583629 <input [mdDatepicker]="d">
584630 <button [mdDatepickerToggle]="d"></button>
585- <md-datepicker #d [touchUi]="true "></md-datepicker>
631+ <md-datepicker #d [touchUi]="touchUI "></md-datepicker>
586632 ` ,
587633} )
588634class DatepickerWithToggle {
589635 @ViewChild ( 'd' ) datepicker : MdDatepicker < Date > ;
636+ touchUI = true ;
590637}
591638
592639
0 commit comments