From 08743fbe562c81dcce666fa08e67acbd8cafc3fe Mon Sep 17 00:00:00 2001 From: Serhii Kulykov Date: Mon, 29 Apr 2024 10:50:16 +0300 Subject: [PATCH] fix: do not throw on focus before adding to the DOM (#7365) --- .../src/vaadin-date-time-picker.js | 4 +++- packages/date-time-picker/test/basic.test.js | 14 +++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/date-time-picker/src/vaadin-date-time-picker.js b/packages/date-time-picker/src/vaadin-date-time-picker.js index b6256c9ffe..badb3aec63 100644 --- a/packages/date-time-picker/src/vaadin-date-time-picker.js +++ b/packages/date-time-picker/src/vaadin-date-time-picker.js @@ -456,7 +456,9 @@ class DateTimePicker extends FieldMixin(DisabledMixin(FocusMixin(ThemableMixin(E } focus() { - this.__datePicker.focus(); + if (this.__datePicker) { + this.__datePicker.focus(); + } } /** diff --git a/packages/date-time-picker/test/basic.test.js b/packages/date-time-picker/test/basic.test.js index db126b1a49..6d7d976c74 100644 --- a/packages/date-time-picker/test/basic.test.js +++ b/packages/date-time-picker/test/basic.test.js @@ -89,9 +89,17 @@ describe('Basic features', () => { expect(dateTimePicker.value).to.equal('2019-09-19T15:00'); }); - it('should delegate focus() to date picker', () => { - dateTimePicker.focus(); - expect(datePicker.hasAttribute('focused')).to.be.true; + describe('focus', () => { + it('should focus the date-picker when calling focus()', () => { + const spy = sinon.spy(datePicker, 'focus'); + dateTimePicker.focus(); + expect(spy).to.be.calledOnce; + }); + + it('should not throw on focus when not attached to the DOM', () => { + const element = document.createElement('vaadin-date-time-picker'); + expect(() => element.focus()).not.to.throw(Error); + }); }); describe('focused', () => {