Skip to content

Commit 5491868

Browse files
committed
refactor: remove field validation util and add integration test
1 parent 031cb09 commit 5491868

File tree

4 files changed

+91
-80
lines changed

4 files changed

+91
-80
lines changed

vaadin-custom-field-flow-parent/vaadin-custom-field-flow-integration-tests/src/main/java/com/vaadin/flow/component/customfield/tests/ValidationPage.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@
1515
*/
1616
package com.vaadin.flow.component.customfield.tests;
1717

18+
import java.util.concurrent.atomic.AtomicInteger;
19+
1820
import com.vaadin.flow.component.customfield.CustomField;
1921
import com.vaadin.flow.component.html.Div;
2022
import com.vaadin.flow.component.html.NativeButton;
2123
import com.vaadin.flow.component.html.Span;
24+
import com.vaadin.flow.component.textfield.IntegerField;
2225
import com.vaadin.flow.component.textfield.TextField;
26+
import com.vaadin.flow.data.binder.Binder;
2327
import com.vaadin.flow.router.Route;
2428

2529
@Route("vaadin-custom-field/validation")
2630
public class ValidationPage extends Div {
2731
public ValidationPage() {
2832
MyField customField = new MyField();
33+
customField.setLabel("Custom field");
34+
customField.setId("custom-field");
2935

3036
NativeButton setInvalid = new NativeButton("Set invalid", e -> {
3137
customField.setInvalid(true);
@@ -53,6 +59,23 @@ public ValidationPage() {
5359

5460
add(customField);
5561
add(new Div(setInvalid, attach, detach, logInvalidState, logOutput));
62+
63+
var customFieldWithDelegatedValidation = new CustomFieldWithDelegatedValidation();
64+
customFieldWithDelegatedValidation
65+
.setLabel("CustomField with delegated validation");
66+
customFieldWithDelegatedValidation
67+
.setId("custom-field-with-delegated-validation");
68+
69+
var binder = new Binder<AtomicInteger>();
70+
binder.forField(customFieldWithDelegatedValidation)
71+
.asRequired("Cannot be empty")
72+
.bind(AtomicInteger::get, AtomicInteger::set);
73+
74+
var validate = new NativeButton("Validate", e -> binder.validate());
75+
validate.setId("validate");
76+
77+
add(customFieldWithDelegatedValidation);
78+
add(new Div(validate));
5679
}
5780

5881
private class MyField extends CustomField<Integer> {
@@ -78,7 +101,45 @@ protected Integer generateModelValue() {
78101

79102
@Override
80103
protected void setPresentationValue(Integer integer) {
104+
}
105+
}
106+
107+
private static class CustomFieldWithDelegatedValidation
108+
extends CustomField<Integer> {
109+
private final IntegerField field = new IntegerField();
110+
111+
CustomFieldWithDelegatedValidation() {
112+
add(field);
113+
}
114+
115+
@Override
116+
protected Integer generateModelValue() {
117+
return field.getValue();
118+
}
81119

120+
@Override
121+
protected void setPresentationValue(Integer newPresentationValue) {
122+
field.setValue(newPresentationValue);
123+
}
124+
125+
@Override
126+
public void setErrorMessage(String errorMessage) {
127+
field.setErrorMessage(errorMessage);
128+
}
129+
130+
@Override
131+
public String getErrorMessage() {
132+
return field.getErrorMessage();
133+
}
134+
135+
@Override
136+
public void setInvalid(boolean invalid) {
137+
field.setInvalid(invalid);
138+
}
139+
140+
@Override
141+
public boolean isInvalid() {
142+
return field.isInvalid();
82143
}
83144
}
84145
}

vaadin-custom-field-flow-parent/vaadin-custom-field-flow-integration-tests/src/test/java/com/vaadin/flow/component/customfield/tests/ValidationIT.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,72 @@
1818
import org.junit.Assert;
1919
import org.junit.Before;
2020
import org.junit.Test;
21+
import org.openqa.selenium.Keys;
2122

23+
import com.vaadin.flow.component.customfield.testbench.CustomFieldElement;
24+
import com.vaadin.flow.component.textfield.testbench.IntegerFieldElement;
2225
import com.vaadin.flow.testutil.TestPath;
2326
import com.vaadin.testbench.TestBenchElement;
2427
import com.vaadin.tests.AbstractComponentIT;
2528

2629
@TestPath("vaadin-custom-field/validation")
2730
public class ValidationIT extends AbstractComponentIT {
2831

29-
private TestBenchElement customField;
30-
private TestBenchElement setInvalid;
31-
private TestBenchElement attach;
32-
private TestBenchElement detach;
33-
private TestBenchElement logInvalidState;
32+
private CustomFieldElement customField;
3433
private TestBenchElement logOutput;
3534

35+
private CustomFieldElement customFieldWithDelegatedValidation;
36+
3637
@Before
3738
public void init() {
3839
open();
3940

40-
customField = $("vaadin-custom-field").waitForFirst();
41-
setInvalid = $("button").id("set-invalid");
42-
attach = $("button").id("attach");
43-
detach = $("button").id("detach");
44-
logInvalidState = $("button").id("log-invalid-state");
41+
customField = $(CustomFieldElement.class).id("custom-field");
4542
logOutput = $("span").id("log-output");
43+
customFieldWithDelegatedValidation = $(CustomFieldElement.class)
44+
.id("custom-field-with-delegated-validation");
4645
}
4746

4847
@Test
4948
public void overridesClientValidation() {
50-
setInvalid.click();
49+
clickElementWithJs("set-invalid");
5150

52-
executeScript("arguments[0].validate()", customField);
51+
executeScript("arguments[0]._requestValidation()", customField);
5352

5453
Assert.assertEquals(true, customField.getPropertyBoolean("invalid"));
5554
}
5655

5756
@Test
5857
public void detach_reattach_overridesClientValidation() {
59-
setInvalid.click();
60-
detach.click();
61-
attach.click();
58+
clickElementWithJs("set-invalid");
59+
clickElementWithJs("detach");
60+
clickElementWithJs("attach");
6261

63-
customField = $("vaadin-custom-field").waitForFirst();
64-
executeScript("arguments[0].validate()", customField);
62+
customField = $(CustomFieldElement.class).id("custom-field");
63+
executeScript("arguments[0]._requestValidation()", customField);
6564

6665
Assert.assertEquals(true, customField.getPropertyBoolean("invalid"));
6766
}
6867

6968
@Test
7069
public void changeInvalidOnClient_notSynchronizedToServer() {
71-
setInvalid.click();
70+
clickElementWithJs("set-invalid");
7271

7372
executeScript("arguments[0].invalid = false", customField);
74-
logInvalidState.click();
73+
clickElementWithJs("log-invalid-state");
7574

7675
Assert.assertEquals("true", logOutput.getText());
7776
}
77+
78+
@Test
79+
public void delegatedValidation_initiallyInvalid_focus_blur_noClientValidation() {
80+
clickElementWithJs("validate");
81+
customFieldWithDelegatedValidation.focus();
82+
customFieldWithDelegatedValidation.sendKeys(Keys.TAB);
83+
84+
var innerField = customFieldWithDelegatedValidation
85+
.$(IntegerFieldElement.class).first();
86+
87+
Assert.assertTrue(innerField.getPropertyBoolean("invalid"));
88+
}
7889
}

vaadin-custom-field-flow-parent/vaadin-custom-field-flow/src/main/java/com/vaadin/flow/component/customfield/CustomField.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.slf4j.LoggerFactory;
2323

2424
import com.vaadin.flow.component.AbstractField;
25-
import com.vaadin.flow.component.AttachEvent;
2625
import com.vaadin.flow.component.Component;
2726
import com.vaadin.flow.component.Focusable;
2827
import com.vaadin.flow.component.HasTheme;
@@ -76,13 +75,6 @@ public CustomField(T defaultValue) {
7675
getElement().setProperty("manualValidation", true);
7776
}
7877

79-
@Override
80-
protected void onAttach(AttachEvent attachEvent) {
81-
super.onAttach(attachEvent);
82-
83-
FieldValidationUtil.disableClientValidation(this);
84-
}
85-
8678
/**
8779
* This method should return the value of the field, based on value of the
8880
* internal fields.

vaadin-custom-field-flow-parent/vaadin-custom-field-flow/src/main/java/com/vaadin/flow/component/customfield/FieldValidationUtil.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)