|
| 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, DateAdapter, NativeDateAdapter} 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
|
@@ -107,6 +112,23 @@ describe('MdDatepicker', () => {
|
107 | 112 | });
|
108 | 113 | }));
|
109 | 114 |
|
| 115 | + it('should close the popup when pressing ESCAPE', () => { |
| 116 | + testComponent.datepicker.open(); |
| 117 | + fixture.detectChanges(); |
| 118 | + |
| 119 | + let content = document.querySelector('.cdk-overlay-pane md-datepicker-content'); |
| 120 | + expect(content).toBeTruthy('Expected datepicker to be open.'); |
| 121 | + |
| 122 | + let keyboadEvent = dispatchKeyboardEvent(content, 'keydown', ESCAPE); |
| 123 | + fixture.detectChanges(); |
| 124 | + |
| 125 | + content = document.querySelector('.cdk-overlay-pane md-datepicker-content'); |
| 126 | + |
| 127 | + expect(content).toBeFalsy('Expected datepicker to be closed.'); |
| 128 | + expect(keyboadEvent.defaultPrevented) |
| 129 | + .toBe(true, 'Expected default ESCAPE action to be prevented.'); |
| 130 | + }); |
| 131 | + |
110 | 132 | it('close should close dialog', async(() => {
|
111 | 133 | testComponent.touch = true;
|
112 | 134 | fixture.detectChanges();
|
@@ -425,6 +447,30 @@ describe('MdDatepicker', () => {
|
425 | 447 | let toggle = fixture.debugElement.query(By.css('button')).nativeElement;
|
426 | 448 | expect(toggle.getAttribute('type')).toBe('button');
|
427 | 449 | });
|
| 450 | + |
| 451 | + it('should restore focus to the toggle after the calendar is closed', () => { |
| 452 | + let toggle = fixture.debugElement.query(By.css('button')).nativeElement; |
| 453 | + |
| 454 | + fixture.componentInstance.touchUI = false; |
| 455 | + fixture.detectChanges(); |
| 456 | + |
| 457 | + toggle.focus(); |
| 458 | + expect(document.activeElement).toBe(toggle, 'Expected toggle to be focused.'); |
| 459 | + |
| 460 | + fixture.componentInstance.datepicker.open(); |
| 461 | + fixture.detectChanges(); |
| 462 | + |
| 463 | + let pane = document.querySelector('.cdk-overlay-pane'); |
| 464 | + |
| 465 | + expect(pane).toBeTruthy('Expected calendar to be open.'); |
| 466 | + expect(pane.contains(document.activeElement)) |
| 467 | + .toBe(true, 'Expected focus to be inside the calendar.'); |
| 468 | + |
| 469 | + fixture.componentInstance.datepicker.close(); |
| 470 | + fixture.detectChanges(); |
| 471 | + |
| 472 | + expect(document.activeElement).toBe(toggle, 'Expected focus to be restored to toggle.'); |
| 473 | + }); |
428 | 474 | });
|
429 | 475 |
|
430 | 476 | describe('datepicker inside input-container', () => {
|
@@ -767,11 +813,12 @@ class DatepickerWithFormControl {
|
767 | 813 | template: `
|
768 | 814 | <input [mdDatepicker]="d">
|
769 | 815 | <button [mdDatepickerToggle]="d"></button>
|
770 |
| - <md-datepicker #d [touchUi]="true"></md-datepicker> |
| 816 | + <md-datepicker #d [touchUi]="touchUI"></md-datepicker> |
771 | 817 | `,
|
772 | 818 | })
|
773 | 819 | class DatepickerWithToggle {
|
774 | 820 | @ViewChild('d') datepicker: MdDatepicker<Date>;
|
| 821 | + touchUI = true; |
775 | 822 | }
|
776 | 823 |
|
777 | 824 |
|
|
0 commit comments