-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: check for input value populated state (WIP) #3550
Closed
yuriy-fix
wants to merge
9
commits into
feat/date-field/validated-event
from
feat/date-field/validate-input-value
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f6ce84b
feat: implement validation status change method
DiegoCardoso 292fa39
test: add constraint validation
DiegoCardoso a2c8bd1
test: add test for invalid format with binder
DiegoCardoso 166351d
Merge branch 'master' into feat/date-field/validated-event
DiegoCardoso 7da7c24
run formatter
DiegoCardoso 6343362
refactor: make sonar happier
DiegoCardoso 8ea97ef
run formatter
DiegoCardoso 417a5bf
fix: check for input value populated state
yuriy-fix 09c6635
Update setInputValue to notify change
yuriy-fix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...rc/main/java/com/vaadin/flow/component/datepicker/DatePickerConstraintValidationPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.vaadin.flow.component.datepicker; | ||
|
||
import com.vaadin.flow.component.ClickEvent; | ||
import com.vaadin.flow.component.ComponentEventListener; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.Route; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Page for testing DatePicker validation constraints | ||
*/ | ||
@Route("vaadin-date-picker/constraint-validation") | ||
public class DatePickerConstraintValidationPage extends Div { | ||
|
||
public static final String MIN_DATE_BUTTON = "min-date-button"; | ||
public static final String MAX_DATE_BUTTON = "max-date-button"; | ||
public static final String REQUIRED_BUTTON = "required-button"; | ||
|
||
public static final LocalDate CONSTRAINT_DATE_VALUE = LocalDate.of(2022, 1, | ||
1); | ||
public static final String SERVER_VALIDITY_STATE_BUTTON = "server-validity-state-button"; | ||
public static final String VALIDITY_STATE = "validity-state"; | ||
|
||
public DatePickerConstraintValidationPage() { | ||
var field = new DatePicker("Select date"); | ||
field.setLocale(Locale.US); | ||
|
||
var required = addButton("required", REQUIRED_BUTTON, | ||
e -> field.setRequired(true)); | ||
var minDate = addButton("min date", MIN_DATE_BUTTON, | ||
e -> field.setMin(CONSTRAINT_DATE_VALUE)); | ||
var maxDate = addButton("max date", MAX_DATE_BUTTON, | ||
e -> field.setMax(CONSTRAINT_DATE_VALUE)); | ||
|
||
var validityState = new Div(); | ||
validityState.setId(VALIDITY_STATE); | ||
|
||
var retrieveValidityState = addButton("server validity state", | ||
SERVER_VALIDITY_STATE_BUTTON, | ||
e -> validityState.setText(String.valueOf(field.isInvalid()))); | ||
|
||
add(field, required, minDate, maxDate, | ||
new Div(retrieveValidityState, validityState)); | ||
} | ||
|
||
private NativeButton addButton(String label, String id, | ||
ComponentEventListener<ClickEvent<NativeButton>> clickListener) { | ||
var button = new NativeButton(label, clickListener); | ||
button.setId(id); | ||
return button; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
.../src/test/java/com/vaadin/flow/component/datepicker/DatePickerConstraintValidationIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.vaadin.flow.component.datepicker; | ||
|
||
import com.vaadin.flow.component.datepicker.testbench.DatePickerElement; | ||
import com.vaadin.flow.testutil.TestPath; | ||
import com.vaadin.tests.AbstractComponentIT; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.Keys; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
@TestPath("vaadin-date-picker/constraint-validation") | ||
public class DatePickerConstraintValidationIT extends AbstractComponentIT { | ||
|
||
DatePickerElement datePickerElement; | ||
|
||
@Before | ||
public void init() { | ||
open(); | ||
datePickerElement = $(DatePickerElement.class).first(); | ||
} | ||
|
||
@Test | ||
public void setRequired_focusAndBlurField_fieldIsInvalid() { | ||
clickButton(DatePickerConstraintValidationPage.REQUIRED_BUTTON); | ||
datePickerElement.focus(); | ||
datePickerElement.sendKeys(Keys.TAB); | ||
|
||
assertClientInvalid(); | ||
assertServerInvalid(); | ||
} | ||
|
||
@Test | ||
public void setRequired_fillWithValidValue_fieldIsValid() { | ||
clickButton(DatePickerConstraintValidationPage.REQUIRED_BUTTON); | ||
datePickerElement.setInputValue("01/01/2022"); | ||
|
||
assertClientValid(); | ||
assertServerValid(); | ||
} | ||
|
||
@Test | ||
public void setRequired_fillAndEmptyValue_fieldIsInvalid() { | ||
clickButton(DatePickerConstraintValidationPage.REQUIRED_BUTTON); | ||
datePickerElement.setInputValue("01/01/2022"); | ||
|
||
datePickerElement.clear(); | ||
assertClientInvalid(); | ||
assertServerInvalid(); | ||
} | ||
|
||
@Test | ||
public void setMinDate_enterDateLessThanMin_fieldIsInvalid() { | ||
clickButton(DatePickerConstraintValidationPage.MIN_DATE_BUTTON); | ||
String value = dateToString( | ||
DatePickerConstraintValidationPage.CONSTRAINT_DATE_VALUE | ||
.minusDays(1)); | ||
datePickerElement.setInputValue(value); | ||
|
||
assertClientInvalid(); | ||
assertServerInvalid(); | ||
} | ||
|
||
@Test | ||
public void setMaxDate_enterDateGreaterThanMax_fieldIsInvalid() { | ||
clickButton(DatePickerConstraintValidationPage.MAX_DATE_BUTTON); | ||
String value = dateToString( | ||
DatePickerConstraintValidationPage.CONSTRAINT_DATE_VALUE | ||
.plusDays(1)); | ||
datePickerElement.setInputValue(value); | ||
|
||
assertClientInvalid(); | ||
assertServerInvalid(); | ||
} | ||
|
||
@Test | ||
public void emptyField_invalidDateFormat_fieldIsInvalid() { | ||
datePickerElement.setInputValue("invalid_format"); | ||
|
||
assertClientInvalid(); | ||
assertServerInvalid(); | ||
} | ||
|
||
@Test | ||
public void fieldInvalid_validDateAdded_fieldIsValid() { | ||
datePickerElement.setInputValue("invalid_format"); | ||
datePickerElement.setInputValue("01/01/2022"); | ||
|
||
assertClientValid(); | ||
assertServerValid(); | ||
} | ||
|
||
private void assertServerInvalid() { | ||
Assert.assertEquals("Server should be invalid", "true", | ||
getValidityState()); | ||
} | ||
|
||
private void assertServerValid() { | ||
Assert.assertEquals("Server should be valid", "false", | ||
getValidityState()); | ||
} | ||
|
||
private String getValidityState() { | ||
clickButton( | ||
DatePickerConstraintValidationPage.SERVER_VALIDITY_STATE_BUTTON); | ||
return findElement( | ||
By.id(DatePickerConstraintValidationPage.VALIDITY_STATE)) | ||
.getText(); | ||
} | ||
|
||
private void assertClientInvalid() { | ||
Assert.assertTrue("Client should be invalid", isClientInvalid()); | ||
} | ||
|
||
private void assertClientValid() { | ||
Assert.assertFalse("Client should be valid", isClientInvalid()); | ||
} | ||
|
||
private boolean isClientInvalid() { | ||
return datePickerElement.getPropertyBoolean("invalid"); | ||
} | ||
|
||
private void setValue(String value) { | ||
datePickerElement.sendKeys(value); | ||
datePickerElement.sendKeys(Keys.ENTER); | ||
} | ||
|
||
private String dateToString(LocalDate date) { | ||
return date.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); | ||
} | ||
|
||
private void clickButton(String id) { | ||
findElement(By.id(id)).click(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we name it
hasInputValue
?