Skip to content

Commit 897e319

Browse files
committed
fix tests
1 parent e8b4c49 commit 897e319

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';
@@ -203,7 +204,7 @@ describe('MatDatepicker', () => {
203204
});
204205
}));
205206

206-
it('setting selected should update input and close calendar', async(() => {
207+
it('setting selected via click should update input and close calendar', async(() => {
207208
testComponent.touch = true;
208209
fixture.detectChanges();
209210

@@ -223,8 +224,31 @@ describe('MatDatepicker', () => {
223224
});
224225
}));
225226

227+
it('setting selected via enter press should update input and close calendar', () => {
228+
testComponent.touch = true;
229+
fixture.detectChanges();
230+
231+
testComponent.datepicker.open();
232+
fixture.detectChanges();
233+
234+
expect(document.querySelector('mat-dialog-container')).not.toBeNull();
235+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
236+
237+
let calendarBodyEl = document.querySelector('.mat-calendar-content') as HTMLElement;
238+
239+
dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
240+
fixture.detectChanges();
241+
dispatchKeyboardEvent(calendarBodyEl, 'keydown', ENTER);
242+
fixture.detectChanges();
243+
244+
fixture.whenStable().then(() => {
245+
expect(document.querySelector('mat-dialog-container')).toBeNull();
246+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 2));
247+
});
248+
});
249+
226250
it('clicking the currently selected date should close the calendar ' +
227-
'without firing selectedChanged', () => {
251+
'without firing selectedChanged', () => {
228252
const selectedChangedSpy =
229253
spyOn(testComponent.datepicker.selectedChanged, 'emit').and.callThrough();
230254

@@ -246,6 +270,28 @@ describe('MatDatepicker', () => {
246270
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 2));
247271
});
248272

273+
it('pressing enter on the currently selected date should close the calendar without ' +
274+
'firing selectedChanged', () => {
275+
const selectedChangedSpy =
276+
spyOn(testComponent.datepicker.selectedChanged, 'emit').and.callThrough();
277+
278+
testComponent.datepicker.open();
279+
fixture.detectChanges();
280+
281+
let calendarBodyEl = document.querySelector('.mat-calendar-content') as HTMLElement;
282+
expect(calendarBodyEl).not.toBeNull();
283+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
284+
285+
dispatchKeyboardEvent(calendarBodyEl, 'keydown', ENTER);
286+
fixture.detectChanges();
287+
288+
fixture.whenStable().then(() => {
289+
expect(selectedChangedSpy.calls.count()).toEqual(0);
290+
expect(document.querySelector('mat-dialog-container')).toBeNull();
291+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
292+
});
293+
});
294+
249295
it('startAt should fallback to input value', () => {
250296
expect(testComponent.datepicker.startAt).toEqual(new Date(2020, JAN, 1));
251297
});

0 commit comments

Comments
 (0)