|
| 1 | +import {Component, ViewChild} from '@angular/core'; |
1 | 2 | import {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'; |
2 | 6 | import {MdDatepickerModule} from './index';
|
3 |
| -import {Component, ViewChild} from '@angular/core'; |
4 | 7 | import {MdDatepicker} from './datepicker';
|
5 | 8 | import {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'; |
9 | 9 | import {MdInputModule} from '../input/index';
|
10 |
| -import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
11 | 10 | import {MdNativeDateModule} from '../core/datetime/index';
|
| 11 | +import {ESCAPE} from '../core'; |
| 12 | +import { |
| 13 | + dispatchFakeEvent, |
| 14 | + dispatchMouseEvent, |
| 15 | + dispatchKeyboardEvent, |
| 16 | +} from '../core/testing/dispatch-events'; |
12 | 17 |
|
13 | 18 |
|
14 | 19 | // When constructing a Date, the month is zero-based. This can be confusing, since people are
|
@@ -99,6 +104,23 @@ describe('MdDatepicker', () => {
|
99 | 104 | });
|
100 | 105 | }));
|
101 | 106 |
|
| 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 | + |
102 | 124 | it('close should close dialog', async(() => {
|
103 | 125 | testComponent.touch = true;
|
104 | 126 | fixture.detectChanges();
|
@@ -388,6 +410,30 @@ describe('MdDatepicker', () => {
|
388 | 410 | let toggle = fixture.debugElement.query(By.css('button')).nativeElement;
|
389 | 411 | expect(toggle.getAttribute('type')).toBe('button');
|
390 | 412 | });
|
| 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 | + }); |
391 | 437 | });
|
392 | 438 |
|
393 | 439 | describe('datepicker inside input-container', () => {
|
@@ -582,11 +628,12 @@ class DatepickerWithFormControl {
|
582 | 628 | template: `
|
583 | 629 | <input [mdDatepicker]="d">
|
584 | 630 | <button [mdDatepickerToggle]="d"></button>
|
585 |
| - <md-datepicker #d [touchUi]="true"></md-datepicker> |
| 631 | + <md-datepicker #d [touchUi]="touchUI"></md-datepicker> |
586 | 632 | `,
|
587 | 633 | })
|
588 | 634 | class DatepickerWithToggle {
|
589 | 635 | @ViewChild('d') datepicker: MdDatepicker<Date>;
|
| 636 | + touchUI = true; |
590 | 637 | }
|
591 | 638 |
|
592 | 639 |
|
|
0 commit comments