Skip to content

Commit d03a553

Browse files
committed
fix tests
1 parent cce33e4 commit d03a553

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/lib/datepicker/calendar.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import {
2020
JUL,
2121
JUN,
2222
MAR,
23-
MAY,
2423
MatNativeDateModule,
24+
MAY,
2525
NOV,
26+
OCT,
2627
SEP,
2728
} from '@angular/material/core';
2829
import {By} from '@angular/platform-browser';
@@ -341,46 +342,46 @@ describe('MatCalendar', () => {
341342
dispatchKeyboardEvent(calendarBodyEl, 'keydown', UP_ARROW);
342343
fixture.detectChanges();
343344

344-
expect(calendarInstance._activeDate).toEqual(new Date(2016, AUG, 31));
345+
expect(calendarInstance._activeDate).toEqual(new Date(2016, SEP, 30));
345346

346347
calendarInstance._activeDate = new Date(2017, JUL, 1);
347348
fixture.detectChanges();
348349

349350
dispatchKeyboardEvent(calendarBodyEl, 'keydown', UP_ARROW);
350351
fixture.detectChanges();
351352

352-
expect(calendarInstance._activeDate).toEqual(new Date(2016, JUL, 1));
353+
expect(calendarInstance._activeDate).toEqual(new Date(2017, MAR, 1));
353354

354355
calendarInstance._activeDate = new Date(2017, DEC, 10);
355356
fixture.detectChanges();
356357

357358
dispatchKeyboardEvent(calendarBodyEl, 'keydown', UP_ARROW);
358359
fixture.detectChanges();
359360

360-
expect(calendarInstance._activeDate).toEqual(new Date(2017, MAY, 10));
361+
expect(calendarInstance._activeDate).toEqual(new Date(2017, AUG, 10));
361362
});
362363

363364
it('should go down a row on down arrow press', () => {
364365
dispatchKeyboardEvent(calendarBodyEl, 'keydown', DOWN_ARROW);
365366
fixture.detectChanges();
366367

367-
expect(calendarInstance._activeDate).toEqual(new Date(2017, AUG, 31));
368+
expect(calendarInstance._activeDate).toEqual(new Date(2017, MAY, 31));
368369

369370
calendarInstance._activeDate = new Date(2017, JUN, 1);
370371
fixture.detectChanges();
371372

372373
dispatchKeyboardEvent(calendarBodyEl, 'keydown', DOWN_ARROW);
373374
fixture.detectChanges();
374375

375-
expect(calendarInstance._activeDate).toEqual(new Date(2018, JUN, 1));
376+
expect(calendarInstance._activeDate).toEqual(new Date(2017, OCT, 1));
376377

377378
calendarInstance._activeDate = new Date(2017, SEP, 30);
378379
fixture.detectChanges();
379380

380381
dispatchKeyboardEvent(calendarBodyEl, 'keydown', DOWN_ARROW);
381382
fixture.detectChanges();
382383

383-
expect(calendarInstance._activeDate).toEqual(new Date(2018, FEB, 28));
384+
expect(calendarInstance._activeDate).toEqual(new Date(2018, JAN, 30));
384385
});
385386

386387
it('should go to first month of the year on home press', () => {

src/lib/datepicker/datepicker.spec.ts

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import {ESCAPE} from '@angular/cdk/keycodes';
1+
import {ENTER, ESCAPE, RIGHT_ARROW} from '@angular/cdk/keycodes';
22
import {OverlayContainer} from '@angular/cdk/overlay';
33
import {
44
createKeyboardEvent,
55
dispatchEvent,
66
dispatchFakeEvent,
7+
dispatchKeyboardEvent,
78
dispatchMouseEvent,
89
} from '@angular/cdk/testing';
910
import {Component, ViewChild} from '@angular/core';
@@ -183,7 +184,7 @@ describe('MatDatepicker', () => {
183184
});
184185
});
185186

186-
it('setting selected should update input and close calendar', () => {
187+
it('setting selected via click should update input and close calendar', () => {
187188
testComponent.touch = true;
188189
fixture.detectChanges();
189190

@@ -203,8 +204,31 @@ describe('MatDatepicker', () => {
203204
});
204205
});
205206

207+
it('setting selected via enter press should update input and close calendar', () => {
208+
testComponent.touch = true;
209+
fixture.detectChanges();
210+
211+
testComponent.datepicker.open();
212+
fixture.detectChanges();
213+
214+
expect(document.querySelector('mat-dialog-container')).not.toBeNull();
215+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
216+
217+
let calendarBodyEl = document.querySelector('.mat-calendar-content') as HTMLElement;
218+
219+
dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
220+
fixture.detectChanges();
221+
dispatchKeyboardEvent(calendarBodyEl, 'keydown', ENTER);
222+
fixture.detectChanges();
223+
224+
fixture.whenStable().then(() => {
225+
expect(document.querySelector('mat-dialog-container')).toBeNull();
226+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 2));
227+
});
228+
});
229+
206230
it('clicking the currently selected date should close the calendar ' +
207-
'without firing selectedChanged', () => {
231+
'without firing selectedChanged', () => {
208232
const selectedChangedSpy =
209233
spyOn(testComponent.datepicker.selectedChanged, 'emit').and.callThrough();
210234
for (let changeCount = 1; changeCount < 3; changeCount++) {
@@ -227,6 +251,28 @@ describe('MatDatepicker', () => {
227251
});
228252
});
229253

254+
it('pressing enter on the currently selected date should close the calendar without ' +
255+
'firing selectedChanged', () => {
256+
const selectedChangedSpy =
257+
spyOn(testComponent.datepicker.selectedChanged, 'emit').and.callThrough();
258+
259+
testComponent.datepicker.open();
260+
fixture.detectChanges();
261+
262+
let calendarBodyEl = document.querySelector('.mat-calendar-content') as HTMLElement;
263+
expect(calendarBodyEl).not.toBeNull();
264+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
265+
266+
dispatchKeyboardEvent(calendarBodyEl, 'keydown', ENTER);
267+
fixture.detectChanges();
268+
269+
fixture.whenStable().then(() => {
270+
expect(selectedChangedSpy.calls.count()).toEqual(0);
271+
expect(document.querySelector('mat-dialog-container')).toBeNull();
272+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
273+
});
274+
});
275+
230276
it('startAt should fallback to input value', () => {
231277
expect(testComponent.datepicker.startAt).toEqual(new Date(2020, JAN, 1));
232278
});

0 commit comments

Comments
 (0)