Skip to content

Commit

Permalink
fix!: replace validated event with unparsable-change (#5572)
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen authored Oct 18, 2023
1 parent 708e69c commit 54c8e94
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public BasicValidationPage() {
}

protected DatePicker createTestField() {
return new DatePicker();
return new DatePicker() {
@Override
protected void validate() {
super.validate();
incrementServerValidationCounter();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class BinderValidationPage extends AbstractValidationPage<DatePicker> {
public static final String MAX_INPUT = "max-input";
public static final String CLEAR_VALUE_BUTTON = "clear-value-button";
public static final String EXPECTED_VALUE_INPUT = "expected-value-input";
public static final String RESET_BEAN_BUTTON = "reset-bean-button";

public static final String REQUIRED_ERROR_MESSAGE = "The field is required";
public static final String UNEXPECTED_VALUE_ERROR_MESSAGE = "The field doesn't match the expected value";
Expand Down Expand Up @@ -41,6 +42,9 @@ public BinderValidationPage() {
.withValidator(value -> value.equals(expectedValue),
UNEXPECTED_VALUE_ERROR_MESSAGE)
.bind("property");
binder.addStatusChangeListener(event -> {
incrementServerValidationCounter();
});

add(createInput(EXPECTED_VALUE_INPUT, "Set expected date", event -> {
LocalDate value = LocalDate.parse(event.getValue());
Expand All @@ -60,6 +64,10 @@ public BinderValidationPage() {
add(createButton(CLEAR_VALUE_BUTTON, "Clear value", event -> {
testField.clear();
}));

add(createButton(RESET_BEAN_BUTTON, "Reset bean", event -> {
binder.setBean(new Bean());
}));
}

protected DatePicker createTestField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void fieldIsInitiallyValid() {
@Test
public void triggerBlur_assertValidity() {
testField.sendKeys(Keys.TAB);
assertValidationCount(0);
assertServerValid();
assertClientValid();
}
Expand All @@ -32,19 +33,24 @@ public void required_triggerBlur_assertValidity() {
$("button").id(REQUIRED_BUTTON).click();

testField.sendKeys(Keys.TAB);
assertServerInvalid();
assertClientInvalid();
assertValidationCount(0);
assertServerValid();
assertClientValid();
}

@Test
public void required_changeValue_assertValidity() {
$("button").id(REQUIRED_BUTTON).click();

testField.setInputValue("1/1/2022");
assertValidationCount(1);
assertServerValid();
assertClientValid();

resetValidationCount();

testField.setInputValue("");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();
}
Expand All @@ -54,14 +60,28 @@ public void min_changeValue_assertValidity() {
$("input").id(MIN_INPUT).sendKeys("2022-03-01", Keys.ENTER);

testField.setInputValue("2/1/2022");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();

resetValidationCount();

testField.setInputValue("3/1/2022");
assertValidationCount(1);
assertClientValid();
assertServerValid();

resetValidationCount();

testField.setInputValue("4/1/2022");
assertValidationCount(1);
assertClientValid();
assertServerValid();

resetValidationCount();

testField.setInputValue("");
assertValidationCount(1);
assertClientValid();
assertServerValid();
}
Expand All @@ -71,31 +91,59 @@ public void max_changeValue_assertValidity() {
$("input").id(MAX_INPUT).sendKeys("2022-03-01", Keys.ENTER);

testField.setInputValue("4/1/2022");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();

resetValidationCount();

testField.setInputValue("3/1/2022");
assertValidationCount(1);
assertClientValid();
assertServerValid();

resetValidationCount();

testField.setInputValue("2/1/2022");
assertValidationCount(1);
assertClientValid();
assertServerValid();

resetValidationCount();

testField.setInputValue("");
assertValidationCount(1);
assertClientValid();
assertServerValid();
}

@Test
public void badInput_changeValue_assertValidity() {
testField.setInputValue("INVALID");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();

resetValidationCount();

testField.setInputValue("1/1/2022");
assertValidationCount(1);
assertServerValid();
assertClientValid();

resetValidationCount();

testField.setInputValue("INVALID");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();

resetValidationCount();

testField.setInputValue("");
assertValidationCount(1);
assertClientValid();
assertServerValid();
}

@Test
Expand All @@ -112,10 +160,14 @@ public void setValue_clearValue_assertValidity() {
@Test
public void badInput_setValue_clearValue_assertValidity() {
testField.setInputValue("INVALID");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();

resetValidationCount();

$("button").id(CLEAR_VALUE_BUTTON).click();
assertValidationCount(1);
assertServerValid();
assertClientValid();
}
Expand All @@ -124,7 +176,8 @@ public void badInput_setValue_clearValue_assertValidity() {
public void detach_attach_preservesInvalidState() {
// Make field invalid
$("button").id(REQUIRED_BUTTON).click();
testField.sendKeys(Keys.TAB);
testField.setInputValue("1/1/2022");
testField.setInputValue("");

detachAndReattachField();

Expand All @@ -145,7 +198,8 @@ public void webComponentCanNotModifyInvalidState() {
public void clientSideInvalidStateIsNotPropagatedToServer() {
// Make the field invalid
$("button").id(REQUIRED_BUTTON).click();
testField.sendKeys(Keys.TAB);
testField.setInputValue("1/1/2022");
testField.setInputValue("");

executeScript("arguments[0].invalid = false", testField);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static com.vaadin.flow.component.datepicker.validation.BinderValidationPage.REQUIRED_ERROR_MESSAGE;
import static com.vaadin.flow.component.datepicker.validation.BinderValidationPage.UNEXPECTED_VALUE_ERROR_MESSAGE;
import static com.vaadin.flow.component.datepicker.validation.BinderValidationPage.CLEAR_VALUE_BUTTON;
import static com.vaadin.flow.component.datepicker.validation.BinderValidationPage.RESET_BEAN_BUTTON;

@TestPath("vaadin-date-picker/validation/binder")
public class BinderValidationIT
Expand All @@ -27,46 +28,79 @@ public void fieldIsInitiallyValid() {
@Test
public void required_triggerBlur_assertValidity() {
testField.sendKeys(Keys.TAB);
assertServerInvalid();
assertClientInvalid();
assertErrorMessage(REQUIRED_ERROR_MESSAGE);
assertValidationCount(0);
assertServerValid();
assertClientValid();
}

@Test
public void required_changeValue_assertValidity() {
$("input").id(EXPECTED_VALUE_INPUT).sendKeys("2022-01-01", Keys.ENTER);

testField.setInputValue("1/1/2022");
assertValidationCount(1);
assertServerValid();
assertClientValid();

resetValidationCount();

testField.setInputValue("");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();
assertErrorMessage(REQUIRED_ERROR_MESSAGE);
}

@Test
public void required_setValue_resetBean_assertValidity() {
$("input").id(EXPECTED_VALUE_INPUT).sendKeys("2022-01-01", Keys.ENTER);

testField.setInputValue("1/1/2022");
assertServerValid();
assertClientValid();

$("button").id(RESET_BEAN_BUTTON).click();
assertServerValid();
assertClientValid();
}

@Test
public void min_changeValue_assertValidity() {
$("input").id(MIN_INPUT).sendKeys("2022-03-01", Keys.ENTER);
$("input").id(EXPECTED_VALUE_INPUT).sendKeys("2022-04-01", Keys.ENTER);

// Constraint validation fails:
testField.setInputValue("2/1/2022");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage("");

resetValidationCount();

// Binder validation fails:
testField.setInputValue("3/1/2022");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage(UNEXPECTED_VALUE_ERROR_MESSAGE);

resetValidationCount();

// Both validations pass:
testField.setInputValue("4/1/2022");
assertValidationCount(1);
assertClientValid();
assertServerValid();

resetValidationCount();

// Binder validation fails:
testField.setInputValue("");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage(REQUIRED_ERROR_MESSAGE);
}

@Test
Expand All @@ -76,39 +110,70 @@ public void max_changeValue_assertValidity() {

// Constraint validation fails:
testField.setInputValue("4/1/2022");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage("");

resetValidationCount();

// Binder validation fails:
testField.setInputValue("3/1/2022");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage(UNEXPECTED_VALUE_ERROR_MESSAGE);

resetValidationCount();

// Both validations pass:
testField.setInputValue("2/1/2022");
assertValidationCount(1);
assertClientValid();
assertServerValid();

resetValidationCount();

// Binder validation fails:
testField.setInputValue("");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage(REQUIRED_ERROR_MESSAGE);
}

@Test
public void badInput_changeValue_assertValidity() {
$("input").id(EXPECTED_VALUE_INPUT).sendKeys("2022-01-01", Keys.ENTER);

testField.setInputValue("INVALID");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();
assertErrorMessage("");

resetValidationCount();

testField.setInputValue("1/1/2022");
assertValidationCount(1);
assertServerValid();
assertClientValid();

resetValidationCount();

testField.setInputValue("INVALID");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();
assertErrorMessage("");

resetValidationCount();

testField.setInputValue("");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage(REQUIRED_ERROR_MESSAGE);
}

@Test
Expand All @@ -128,11 +193,15 @@ public void setValue_clearValue_assertValidity() {
@Test
public void badInput_setValue_clearValue_assertValidity() {
testField.setInputValue("INVALID");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();
assertErrorMessage("");

resetValidationCount();

$("button").id(CLEAR_VALUE_BUTTON).click();
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();
assertErrorMessage(REQUIRED_ERROR_MESSAGE);
Expand Down
Loading

0 comments on commit 54c8e94

Please sign in to comment.