Skip to content

Commit

Permalink
fix!: replace validated event with unparsable-change (#5564)
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen authored Oct 18, 2023
1 parent 7848170 commit 708e69c
Show file tree
Hide file tree
Showing 11 changed files with 480 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import com.vaadin.tests.validation.AbstractValidationPage;

@Route("vaadin-integer-field/validation/basic")
public class IntegerFieldValidationBasicPage
public class IntegerFieldBasicValidationPage
extends AbstractValidationPage<IntegerField> {
public static final String REQUIRED_BUTTON = "required-button";
public static final String STEP_INPUT = "step-input";
public static final String MIN_INPUT = "min-input";
public static final String MAX_INPUT = "max-input";
public static final String CLEAR_VALUE_BUTTON = "clear-value-button";

public IntegerFieldValidationBasicPage() {
public IntegerFieldBasicValidationPage() {
super();

add(createButton(REQUIRED_BUTTON, "Enable required", event -> {
Expand Down Expand Up @@ -56,6 +56,12 @@ public IntegerFieldValidationBasicPage() {
}

protected IntegerField createTestField() {
return new IntegerField();
return new IntegerField() {
@Override
protected void validate() {
super.validate();
incrementServerValidationCounter();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import com.vaadin.tests.validation.AbstractValidationPage;

@Route("vaadin-integer-field/validation/binder")
public class IntegerFieldValidationBinderPage
public class IntegerFieldBinderValidationPage
extends AbstractValidationPage<IntegerField> {
public static final String STEP_INPUT = "step-input";
public static final String MIN_INPUT = "min-input";
public static final String MAX_INPUT = "max-input";
public static final String EXPECTED_VALUE_INPUT = "expected-value-input";
public static final String CLEAR_VALUE_BUTTON = "clear-value-button";
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 All @@ -48,14 +49,17 @@ public void setProperty(Integer property) {

private Integer expectedValue;

public IntegerFieldValidationBinderPage() {
public IntegerFieldBinderValidationPage() {
super();

binder = new Binder<>(Bean.class);
binder.forField(testField).asRequired(REQUIRED_ERROR_MESSAGE)
.withValidator(value -> value.equals(expectedValue),
UNEXPECTED_VALUE_ERROR_MESSAGE)
.bind("property");
binder.addStatusChangeListener(event -> {
incrementServerValidationCounter();
});

add(createInput(EXPECTED_VALUE_INPUT, "Set expected value", event -> {
expectedValue = Integer.parseInt(event.getValue());
Expand All @@ -79,6 +83,10 @@ public IntegerFieldValidationBinderPage() {
add(createButton(CLEAR_VALUE_BUTTON, "Clear value", event -> {
testField.clear();
}));

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

protected IntegerField createTestField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public NumberFieldBasicValidationPage() {
}

protected NumberField createTestField() {
return new NumberField();
return new NumberField() {
@Override
protected void validate() {
super.validate();
incrementServerValidationCounter();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class NumberFieldBinderValidationPage
public static final String MAX_INPUT = "max-input";
public static final String EXPECTED_VALUE_INPUT = "expected-value-input";
public static final String CLEAR_VALUE_BUTTON = "clear-value-button";
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 @@ -56,6 +57,9 @@ public NumberFieldBinderValidationPage() {
.withValidator(value -> value.equals(expectedValue),
UNEXPECTED_VALUE_ERROR_MESSAGE)
.bind("property");
binder.addStatusChangeListener(event -> {
incrementServerValidationCounter();
});

add(createInput(EXPECTED_VALUE_INPUT, "Set expected value", event -> {
expectedValue = Double.parseDouble(event.getValue());
Expand All @@ -79,6 +83,10 @@ public NumberFieldBinderValidationPage() {
add(createButton(CLEAR_VALUE_BUTTON, "Clear value", event -> {
testField.clear();
}));

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

protected NumberField createTestField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
*/
package com.vaadin.flow.component.textfield.tests.validation;

import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.Keys;

import com.vaadin.flow.component.textfield.testbench.IntegerFieldElement;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.tests.validation.AbstractValidationIT;

import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldValidationBasicPage.MIN_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldValidationBasicPage.MAX_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldValidationBasicPage.STEP_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldValidationBasicPage.REQUIRED_BUTTON;
import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldValidationBasicPage.CLEAR_VALUE_BUTTON;
import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldBasicValidationPage.MIN_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldBasicValidationPage.MAX_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldBasicValidationPage.STEP_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldBasicValidationPage.REQUIRED_BUTTON;
import static com.vaadin.flow.component.textfield.tests.validation.IntegerFieldBasicValidationPage.CLEAR_VALUE_BUTTON;

@TestPath("vaadin-integer-field/validation/basic")
public class IntegerFieldBasicValidationIT
Expand All @@ -40,6 +41,7 @@ public void fieldIsInitiallyValid() {
@Test
public void triggerBlur_assertValidity() {
testField.sendKeys(Keys.TAB);
assertValidationCount(0);
assertServerValid();
assertClientValid();
}
Expand All @@ -49,114 +51,148 @@ 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.setValue("1234");
assertValidationCount(1);
assertServerValid();
assertClientValid();

resetValidationCount();

testField.setValue("");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();
}

@Test
public void min_triggerBlur_assertValidity() {
$("input").id(MIN_INPUT).sendKeys("2", Keys.ENTER);

testField.sendKeys(Keys.TAB);
assertServerValid();
assertClientValid();
}

@Test
public void min_changeValue_assertValidity() {
$("input").id(MIN_INPUT).sendKeys("2", Keys.ENTER);

testField.setValue("1");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();

resetValidationCount();

testField.setValue("2");
assertValidationCount(1);
assertClientValid();
assertServerValid();

resetValidationCount();

testField.setValue("3");
assertValidationCount(1);
assertClientValid();
assertServerValid();
}

@Test
public void max_triggerBlur_assertValidity() {
$("input").id(MAX_INPUT).sendKeys("2", Keys.ENTER);
resetValidationCount();

testField.sendKeys(Keys.TAB);
assertServerValid();
testField.setValue("");
assertValidationCount(1);
assertClientValid();
assertServerValid();
}

@Test
public void max_changeValue_assertValidity() {
$("input").id(MAX_INPUT).sendKeys("2", Keys.ENTER);

testField.setValue("3");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();

resetValidationCount();

testField.setValue("2");
assertValidationCount(1);
assertClientValid();
assertServerValid();

resetValidationCount();

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

@Test
public void step_triggerBlur_assertValidity() {
$("input").id(STEP_INPUT).sendKeys("2", Keys.ENTER);
resetValidationCount();

testField.sendKeys(Keys.TAB);
assertServerValid();
testField.setValue("");
assertValidationCount(1);
assertClientValid();
assertServerValid();
}

@Test
public void step_changeValue_assertValidity() {
$("input").id(STEP_INPUT).sendKeys("2", Keys.ENTER);

testField.setValue("1");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();

resetValidationCount();

testField.setValue("2");
assertValidationCount(1);
assertClientValid();
assertServerValid();

resetValidationCount();

testField.setValue("3");
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();

resetValidationCount();

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

@Test
public void badInput_changeValue_assertValidity() {
testField.sendKeys("--2", Keys.TAB);
testField.sendKeys("--2", Keys.ENTER);
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();

resetValidationCount();

testField.setValue("2");
assertValidationCount(1);
assertServerValid();
assertClientValid();

testField.sendKeys("--2", Keys.TAB);
resetValidationCount();

testField.sendKeys("--2", Keys.ENTER);
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();

resetValidationCount();

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

@Test
Expand All @@ -172,34 +208,55 @@ public void setValue_clearValue_assertValidity() {

@Test
public void badInput_setValue_clearValue_assertValidity() {
testField.sendKeys("--2", Keys.TAB);
testField.sendKeys("--2", Keys.ENTER);
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();

resetValidationCount();

$("button").id(CLEAR_VALUE_BUTTON).click();
assertValidationCount(1);
assertServerValid();
assertClientValid();
}

@Test
public void integerOverflow_setValueExceedingMaxInteger_assertValidity() {
testField.sendKeys("999999999999", Keys.TAB);
public void maxIntegerOverflow_changeValue_assertValidity() {
testField.setValue("999999999999");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();

resetValidationCount();

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

@Test
public void integerOverflow_setValueExceedingMinInteger_assertValidity() {
testField.sendKeys("-999999999999", Keys.TAB);
public void minIntegerOverflow_changeValue_assertValidity() {
testField.setValue("-999999999999");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();

resetValidationCount();

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

@Test
public void detach_attach_preservesInvalidState() {
// Make field invalid
$("button").id(REQUIRED_BUTTON).click();
testField.sendKeys(Keys.TAB);
testField.setValue("2");
testField.setValue("");

detachAndReattachField();

Expand All @@ -220,7 +277,8 @@ public void webComponentCanNotModifyInvalidState() {
public void clientSideInvalidStateIsNotPropagatedToServer() {
// Make the field invalid
$("button").id(REQUIRED_BUTTON).click();
testField.sendKeys(Keys.TAB);
testField.setValue("2");
testField.setValue("");

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

Expand Down
Loading

0 comments on commit 708e69c

Please sign in to comment.