|
| 1 | +package com.vaadin.flow.data.binder; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.mockito.Mockito; |
| 10 | + |
| 11 | +import com.vaadin.flow.data.binder.testcomponents.TestHasValidatorDatePicker; |
| 12 | +import com.vaadin.flow.component.HasValue; |
| 13 | +import com.vaadin.flow.tests.data.bean.Person; |
| 14 | + |
| 15 | +import static com.vaadin.flow.data.binder.testcomponents.TestHasValidatorDatePicker.INVALID_DATE_FORMAT; |
| 16 | + |
| 17 | +public class BinderValidationStatusChangeListenerTest |
| 18 | + extends BinderTestBase<Binder<Person>, Person> { |
| 19 | + |
| 20 | + private static final String BIRTH_DATE_PROPERTY = "birthDate"; |
| 21 | + |
| 22 | + private final Map<HasValue<?, ?>, String> componentErrors = new HashMap<>(); |
| 23 | + |
| 24 | + @Before |
| 25 | + public void setUp() { |
| 26 | + binder = new Binder<>(Person.class) { |
| 27 | + @Override |
| 28 | + protected void handleError(HasValue<?, ?> field, |
| 29 | + ValidationResult result) { |
| 30 | + componentErrors.put(field, result.getErrorMessage()); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected void clearError(HasValue<?, ?> field) { |
| 35 | + super.clearError(field); |
| 36 | + componentErrors.remove(field); |
| 37 | + } |
| 38 | + }; |
| 39 | + item = new Person(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void fieldWithHasValidatorDefaults_bindIsCalled_addValidationStatusListenerIsCalled() { |
| 44 | + var field = Mockito.spy( |
| 45 | + TestHasValidatorDatePicker.DatePickerHasValidatorDefaults.class); |
| 46 | + binder.bind(field, BIRTH_DATE_PROPERTY); |
| 47 | + Mockito.verify(field, Mockito.times(1)) |
| 48 | + .addValidationStatusChangeListener(Mockito.any()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void fieldWithHasValidatorOnlyGetDefaultValidatorOverridden_bindIsCalled_addValidationStatusListenerIsCalled() { |
| 53 | + var field = Mockito.spy( |
| 54 | + TestHasValidatorDatePicker.DataPickerHasValidatorGetDefaultValidatorOverridden.class); |
| 55 | + binder.bind(field, BIRTH_DATE_PROPERTY); |
| 56 | + Mockito.verify(field, Mockito.times(1)) |
| 57 | + .addValidationStatusChangeListener(Mockito.any()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void fieldWithHasValidatorOnlyAddListenerOverridden_bindIsCalled_addValidationStatusListenerIsCalled() { |
| 62 | + var field = Mockito.spy( |
| 63 | + TestHasValidatorDatePicker.DataPickerHasValidatorAddListenerOverridden.class); |
| 64 | + binder.bind(field, BIRTH_DATE_PROPERTY); |
| 65 | + Mockito.verify(field, Mockito.times(1)) |
| 66 | + .addValidationStatusChangeListener(Mockito.any()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void fieldWithHasValidatorFullyOverridden_bindIsCalled_addValidationStatusChangeListenerIsCalled() { |
| 71 | + var field = Mockito.spy( |
| 72 | + TestHasValidatorDatePicker.DataPickerHasValidatorOverridden.class); |
| 73 | + binder.bind(field, BIRTH_DATE_PROPERTY); |
| 74 | + Mockito.verify(field, Mockito.times(1)) |
| 75 | + .addValidationStatusChangeListener(Mockito.any()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void fieldWithHasValidatorFullyOverridden_fieldValidationStatusChangesToFalse_binderHandleErrorIsCalled() { |
| 80 | + var field = new TestHasValidatorDatePicker.DataPickerHasValidatorOverridden(); |
| 81 | + binder.bind(field, BIRTH_DATE_PROPERTY); |
| 82 | + Assert.assertEquals(0, componentErrors.size()); |
| 83 | + |
| 84 | + field.fireValidationStatusChangeEvent(false); |
| 85 | + Assert.assertEquals(1, componentErrors.size()); |
| 86 | + Assert.assertEquals(INVALID_DATE_FORMAT, componentErrors.get(field)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void fieldWithHasValidatorFullyOverridden_fieldValidationStatusChangesToTrue_binderClearErrorIsCalled() { |
| 91 | + var field = new TestHasValidatorDatePicker.DataPickerHasValidatorOverridden(); |
| 92 | + binder.bind(field, BIRTH_DATE_PROPERTY); |
| 93 | + Assert.assertEquals(0, componentErrors.size()); |
| 94 | + |
| 95 | + field.fireValidationStatusChangeEvent(false); |
| 96 | + Assert.assertEquals(1, componentErrors.size()); |
| 97 | + Assert.assertEquals(INVALID_DATE_FORMAT, componentErrors.get(field)); |
| 98 | + |
| 99 | + field.fireValidationStatusChangeEvent(true); |
| 100 | + Assert.assertEquals(0, componentErrors.size()); |
| 101 | + Assert.assertNull(componentErrors.get(field)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void fieldWithHasValidatorOnlyAddListenerOverriddenAndCustomValidation_fieldValidationStatusChangesToFalse_binderHandleErrorIsCalled() { |
| 106 | + var field = new TestHasValidatorDatePicker.DataPickerHasValidatorAddListenerOverridden(); |
| 107 | + binder.forField(field).withValidator(field::customValidation) |
| 108 | + .bind(BIRTH_DATE_PROPERTY); |
| 109 | + |
| 110 | + field.fireValidationStatusChangeEvent(false); |
| 111 | + Assert.assertEquals(1, componentErrors.size()); |
| 112 | + Assert.assertEquals(INVALID_DATE_FORMAT, componentErrors.get(field)); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void fieldWithHasValidatorOnlyAddListenerOverriddenAndCustomValidation_fieldValidationStatusChangesToTrue_binderClearErrorIsCalled() { |
| 117 | + var field = new TestHasValidatorDatePicker.DataPickerHasValidatorAddListenerOverridden(); |
| 118 | + binder.forField(field).withValidator(field::customValidation) |
| 119 | + .bind(BIRTH_DATE_PROPERTY); |
| 120 | + |
| 121 | + field.fireValidationStatusChangeEvent(false); |
| 122 | + Assert.assertEquals(1, componentErrors.size()); |
| 123 | + Assert.assertEquals(INVALID_DATE_FORMAT, componentErrors.get(field)); |
| 124 | + |
| 125 | + field.fireValidationStatusChangeEvent(true); |
| 126 | + Assert.assertEquals(0, componentErrors.size()); |
| 127 | + Assert.assertNull(componentErrors.get(field)); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments