fix(calendar): generated dates are normalized

This commit is contained in:
Thijs Louisse 2019-08-12 09:37:48 +02:00
parent 97143caedd
commit fe72ebd055
4 changed files with 45 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import { isSameDate } from './utils/isSameDate.js';
import { calendarStyle } from './calendarStyle.js';
import './utils/differentKeyNamesShimIE.js';
import { createDay } from './utils/createDay.js';
import { normalizeDateTime } from './utils/normalizeDateTime.js';
/**
* @customElement
@ -152,7 +153,7 @@ export class LionCalendar extends LocalizeMixin(LitElement) {
this.disableDates = () => false;
this.firstDayOfWeek = 0;
this.weekdayHeaderNotation = 'short';
this.__today = new Date();
this.__today = normalizeDateTime(new Date());
this.centralDate = this.__today;
this.__focusedDate = null;
this.__connectedCallbackDone = false;
@ -344,6 +345,7 @@ export class LionCalendar extends LocalizeMixin(LitElement) {
if (this.minDate && day.date < this.minDate) {
day.disabled = true;
}
if (this.maxDate && day.date > this.maxDate) {
day.disabled = true;
}

View file

@ -0,0 +1,9 @@
/**
* @desc Makes suitable for date comparisons
* @param {Date} d
* @returns {Date}
*/
export function normalizeDateTime(d) {
return new Date(d.getFullYear(), d.getMonth(), d.getDate());
}

View file

@ -338,6 +338,23 @@ describe('<lion-calendar>', () => {
clock.restore();
});
describe('Normalization', () => {
it('normalizes all generated dates', async () => {
function isNormalizedDate(d) {
return d.getHours() === 0 && d.getMinutes() === 0 && d.getSeconds() === 0;
}
const el = await fixture(
html`
<lion-calendar></lion-calendar>
`,
);
// The central date will be today's date: it's the date all other
// dates in the month view will be derived from.
expect(isNormalizedDate(el.centralDate)).to.be.true;
});
});
});
});

View file

@ -0,0 +1,16 @@
import { expect } from '@open-wc/testing';
import { normalizeDateTime } from '../../src/utils/normalizeDateTime.js';
describe('normalizeDateTime', () => {
it('returns a date with hours, minutes and seconds set to 0', () => {
const date = normalizeDateTime(new Date('2000-11-29T12:34:56'));
expect(date.getFullYear()).to.equal(2000);
expect(date.getMonth()).to.equal(10);
expect(date.getDate()).to.equal(29);
// normalized parts
expect(date.getHours()).to.equal(0);
expect(date.getMinutes()).to.equal(0);
expect(date.getSeconds()).to.equal(0);
});
});