diff --git a/packages/input-date/src/LionInputDate.js b/packages/input-date/src/LionInputDate.js index f95af7cff..305fb0fa0 100644 --- a/packages/input-date/src/LionInputDate.js +++ b/packages/input-date/src/LionInputDate.js @@ -2,6 +2,12 @@ import { LionInput } from '@lion/input'; import { formatDate, LocalizeMixin, parseDate } from '@lion/localize'; import { IsDate } from '@lion/validate'; +function isValidDate(date) { + // to make sure it is a valid date we use isNaN and not Number.isNaN + // eslint-disable-next-line no-restricted-globals + return date instanceof Date && !isNaN(date); +} + /** * `LionInputDate` has a .modelValue of type Date. It parses, formats and validates based * on locale. @@ -38,7 +44,7 @@ export class LionInputDate extends LocalizeMixin(LionInput) { // eslint-disable-next-line class-methods-use-this serializer(modelValue) { - if (!(modelValue instanceof Date)) { + if (!isValidDate(modelValue)) { return ''; } // modelValue is localized, so we take the timezone offset in milliseconds and subtract it diff --git a/packages/input-date/test/lion-input-date.test.js b/packages/input-date/test/lion-input-date.test.js index be98dcc40..f84908605 100644 --- a/packages/input-date/test/lion-input-date.test.js +++ b/packages/input-date/test/lion-input-date.test.js @@ -13,29 +13,23 @@ describe('', () => { }); it('returns undefined when value is empty string', async () => { - const el = await fixture( - html` - - `, - ); + const el = await fixture(html` + + `); expect(el.parser('')).to.equal(undefined); }); it('has type="text" to activate default keyboard on mobile with all necessary symbols', async () => { - const el = await fixture( - html` - - `, - ); + const el = await fixture(html` + + `); expect(el._inputNode.type).to.equal('text'); }); it('has validator "isDate" applied by default', async () => { - const el = await fixture( - html` - - `, - ); + const el = await fixture(html` + + `); el.modelValue = '2005/11/10'; expect(el.hasFeedbackFor).to.include('error'); expect(el.validationStates).to.have.a.property('error'); @@ -47,6 +41,15 @@ describe('', () => { expect(el.validationStates.error).not.to.have.a.property('IsDate'); }); + it("does not throw on invalid dates like new Date('foo')", async () => { + const el = await fixture(html` + + `); + expect(() => { + el.modelValue = new Date('foo'); + }).to.not.throw(); + }); + it('gets validated by "MaxDate" correctly', async () => { const el = await fixture(html` ', () => { }); it('is accessible', async () => { - const el = await fixture( - html` - - `, - ); + const el = await fixture(html` + + `); await expect(el).to.be.accessible(); }); it('is accessible when readonly', async () => { - const el = await fixture( - html` - - `, - ); + const el = await fixture(html` + + `); await expect(el).to.be.accessible(); }); it('is accessible when disabled', async () => { - const el = await fixture( - html` - - `, - ); + const el = await fixture(html` + + `); await expect(el).to.be.accessible(); }); it('serializes to iso format', async () => { - const el = await fixture( - html` - - `, - ); + const el = await fixture(html` + + `); expect(el.serializedValue).to.equal('2000-12-15'); }); });