|
| 1 | +/* |
| 2 | + * Copyright 2000-2024 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.component.datepicker; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import com.vaadin.flow.component.datepicker.testbench.DatePickerElement; |
| 25 | +import com.vaadin.flow.testutil.TestPath; |
| 26 | +import com.vaadin.testbench.TestBenchElement; |
| 27 | +import com.vaadin.tests.AbstractComponentIT; |
| 28 | + |
| 29 | +import elemental.json.Json; |
| 30 | +import elemental.json.JsonObject; |
| 31 | + |
| 32 | +@TestPath("vaadin-date-picker/fallback-parser") |
| 33 | +public class DatePickerFallbackParserIT extends AbstractComponentIT { |
| 34 | + private DatePickerElement datePicker; |
| 35 | + private TestBenchElement valueChangeLog; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void init() { |
| 39 | + open(); |
| 40 | + datePicker = $(DatePickerElement.class).first(); |
| 41 | + valueChangeLog = $("div").id("value-change-log"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void enterShortcutValue_clearShortcutValue() { |
| 46 | + datePicker.setInputValue("newyear"); |
| 47 | + assertValueChange("", "2024-01-01"); |
| 48 | + assertInputValue("1/1/2024"); |
| 49 | + |
| 50 | + datePicker.setInputValue("newyear"); |
| 51 | + assertNoValueChange(); |
| 52 | + assertInputValue("1/1/2024"); |
| 53 | + |
| 54 | + datePicker.setInputValue(""); |
| 55 | + assertValueChange("2024-01-01", ""); |
| 56 | + assertInputValue(""); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void enterUnparsableValue_enterShortcutValue() { |
| 61 | + datePicker.setInputValue("foo"); |
| 62 | + assertNoValueChange(); |
| 63 | + assertInputValue("foo"); |
| 64 | + |
| 65 | + datePicker.setInputValue("newyear"); |
| 66 | + assertValueChange("", "2024-01-01"); |
| 67 | + assertInputValue("1/1/2024"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void enterParsableValue_enterShortcutValue() { |
| 72 | + datePicker.setInputValue("2/2/2000"); |
| 73 | + assertValueChange("", "2000-02-02"); |
| 74 | + assertInputValue("2/2/2000"); |
| 75 | + |
| 76 | + datePicker.setInputValue("newyear"); |
| 77 | + assertValueChange("2000-02-02", "2024-01-01"); |
| 78 | + assertInputValue("1/1/2024"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void enterShortcutValueThrowingException_enterParsableValue() { |
| 83 | + datePicker.setInputValue("exception"); |
| 84 | + assertNoValueChange(); |
| 85 | + assertInputValue("exception"); |
| 86 | + |
| 87 | + datePicker.setInputValue("2/2/2000"); |
| 88 | + assertValueChange("", "2000-02-02"); |
| 89 | + assertInputValue("2/2/2000"); |
| 90 | + } |
| 91 | + |
| 92 | + private void assertValueChange(String expectedOldValue, |
| 93 | + String expectedNewValue) { |
| 94 | + List<TestBenchElement> records = valueChangeLog.$("div").all(); |
| 95 | + Assert.assertEquals("ValueChangeEvent should be fired only once", 1, |
| 96 | + records.size()); |
| 97 | + |
| 98 | + JsonObject record = Json.parse(records.get(0).getText()); |
| 99 | + |
| 100 | + Assert.assertTrue("eventFromClient should be true", |
| 101 | + record.getBoolean("eventFromClient")); |
| 102 | + Assert.assertEquals("eventOldValue should contain old value", |
| 103 | + expectedOldValue, record.getString("eventOldValue")); |
| 104 | + Assert.assertEquals("eventNewValue should contain new value", |
| 105 | + expectedNewValue, record.getString("eventNewValue")); |
| 106 | + Assert.assertEquals("componentValue should contain new value", |
| 107 | + expectedNewValue, record.getString("componentValue")); |
| 108 | + Assert.assertEquals("componentValueProperty should contain new value", |
| 109 | + expectedNewValue, record.getString("componentValueProperty")); |
| 110 | + |
| 111 | + $("button").id("clear-value-change-log").click(); |
| 112 | + } |
| 113 | + |
| 114 | + private void assertNoValueChange() { |
| 115 | + Assert.assertEquals("", valueChangeLog.getText()); |
| 116 | + } |
| 117 | + |
| 118 | + private void assertInputValue(String expected) { |
| 119 | + Assert.assertEquals(expected, datePicker.getInputValue()); |
| 120 | + } |
| 121 | +} |
0 commit comments