vaadin / flow-components

Java counterpart of Vaadin Web Components
101 stars 66 forks source link

TestBench DateTimePickerElement.setDateTime and setTime both have no effect when millis != 0 #1994

Open skiedrowski opened 3 years ago

skiedrowski commented 3 years ago

Description

It seems impossible to set a LocalDateTime with milliseconds != 0 into a TestBench DateTimePickerElement.

The following code

final DateTimePickerElement dateTimePickerElement = ...
dateTimePickerElement.setDateTime(LocalDateTime.parse("2022-01-03T22:32:52.999587"));

has no effect (displayed date/time both do not change).

Expected outcome

(with Locale de_DE)

final DateTimePickerElement dateTimePickerElement = ...
dateTimePickerElement.setDateTime(LocalDateTime.parse("2022-01-03T22:32:52.999587"));

should cause the DatePicker to show "03.01.2022" and TimePicker to show 22:32.

Actual outcome

Neither the DatePicker nor the TimePicker gets updated.

Minimal reproducible example

Given a view with a DateTimePicker And a testbench test trying to set 3 different LocalDateTime values like that:

final DateTimePickerElement dateTimePickerElement = ...
dateTimePickerElement.setDateTime(LocalDateTime.parse("2022-01-01T22:30:00")); //OK
assertThat(dateTimePickerElement.getDate(), is(LocalDate.parse("2022-01-01")));
assertThat(dateTimePickerElement.getTime(), is(LocalTime.parse("22:30")));

dateTimePickerElement.setDateTime(LocalDateTime.parse("2022-01-02T22:31:52")); //OK
assertThat(dateTimePickerElement.getDate(), is(LocalDate.parse("2022-01-02")));
assertThat(dateTimePickerElement.getTime(), is(LocalTime.parse("22:31")));

dateTimePickerElement.setDateTime(LocalDateTime.parse("2022-01-03T22:32:52.999587"));
assertThat(dateTimePickerElement.getDate(), is(LocalDate.parse("2022-01-03"))); //NOK
assertThat(dateTimePickerElement.getTime(), is(LocalTime.parse("22:32"))); //NOK

The last two asserts fail since neither the date nor the time have been updated.

Steps to reproduce

  1. Add a DateTimePicker component to a page.
  2. Create a TestBench test as shown above
  3. Run the test

Environment

Browsers Affected

Did not test other browsers.

Side notes

fhdhsni commented 3 years ago

The problem comes from the underlining component that only supports ISO 8601 with only three digits in the decimal fraction place (ms) versus Java that supports up to 9 digits (nanosecond).

As a workaround you can use LocalDateTime.parse("2022-01-03T22:32:52.999587").truncatedTo(ChronoUnit.MILLIS).

skiedrowski commented 3 years ago

The workaround works, but it clutters the code with lots of .truncatedTo(MILLIS). It is ok as a workaround, but what about a fix?