Skip to content
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: ensure that invalid state is set from the server #6922

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.combobox.test.validation;

import java.util.List;

import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.router.Route;
import com.vaadin.tests.validation.AbstractValidationPage;

@Route("vaadin-combo-box/validation/initial-value")
public class ComboBoxInitialValueValidationPage
extends AbstractValidationPage<ComboBox<String>> {
public static final String SET_NULL = "set-null-button";
public static final String SET_EMPTY_STRING = "set-empty-string-button";
public static final String REQUIRED_ERROR_MESSAGE = "Field is required";

public ComboBoxInitialValueValidationPage() {
testField.setI18n(new ComboBox.ComboBoxI18n()
.setRequiredErrorMessage(REQUIRED_ERROR_MESSAGE));

add(createButton(SET_NULL, "Set null", event -> {
testField.setValue(null);
}));

add(createButton(SET_EMPTY_STRING, "Set empty string", event -> {
testField.setValue("");
}));
}

@Override
protected ComboBox<String> createTestField() {
ComboBox<String> comboBox = new ComboBox<>() {
@Override
protected void validate() {
super.validate();
incrementServerValidationCounter();
}
};
comboBox.setItems(List.of("foo", "bar", "baz"));
comboBox.setRequired(true);

return comboBox;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.combobox.test.validation;

import static com.vaadin.flow.component.combobox.test.validation.ComboBoxInitialValueValidationPage.REQUIRED_ERROR_MESSAGE;
import static com.vaadin.flow.component.combobox.test.validation.ComboBoxInitialValueValidationPage.SET_EMPTY_STRING;
import static com.vaadin.flow.component.combobox.test.validation.ComboBoxInitialValueValidationPage.SET_NULL;
import static com.vaadin.tests.validation.AbstractValidationPage.ATTACH_FIELD_BUTTON;
import static com.vaadin.tests.validation.AbstractValidationPage.DETACH_FIELD_BUTTON;

import org.junit.Test;
import org.openqa.selenium.support.ui.ExpectedConditions;

import com.vaadin.flow.component.combobox.testbench.ComboBoxElement;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.tests.validation.AbstractValidationIT;

@TestPath("vaadin-combo-box/validation/initial-value")
public class ComboBoxInitialValueValidationIT
extends AbstractValidationIT<ComboBoxElement> {

@Test
public void fieldWithEmptyString_fieldIsValid() {
detachButton();

clickButton(SET_EMPTY_STRING);

attachButton();
assertClientValid();
assertServerValid();
}

@Test
public void requiredFieldWithNull_fieldIsInvalid() {
detachButton();

// Need to "force" value change on the field
clickButton(SET_EMPTY_STRING);
clickButton(SET_NULL);

attachButton();
assertClientInvalid();
assertServerInvalid();
assertErrorMessage(REQUIRED_ERROR_MESSAGE);
}

private void attachButton() {
clickButton(ATTACH_FIELD_BUTTON);
// Retrieve new element instance
testField = getTestField();
}

private void detachButton() {
clickButton(DETACH_FIELD_BUTTON);
// Verify element has been removed
waitUntil(ExpectedConditions.stalenessOf(testField));
}

private void clickButton(String buttonId) {
$("button").id(buttonId).click();
}

@Override
protected ComboBoxElement getTestField() {
return $(ComboBoxElement.class).first();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ public static <C extends Component & HasValidation> void preventWebComponentFrom
StringBuilder expression = new StringBuilder(
"this._shouldSetInvalid = function (invalid) { return false };");

if (component.isInvalid()) {
/*
* By default the invalid flag is set to false. Workaround the case
* where the client side validation overrides the invalid state
* before the `_shouldSetInvalid` method is overridden above.
*/
expression.append("this.invalid = true;");
}
/*
* Workaround the case where the client side validation overrides the
* invalid state before the `_shouldSetInvalid` method is overridden
* above.
*/
expression.append("this.invalid = ").append(component.isInvalid())
.append(";");

component.getElement().executeJs(expression.toString());
}
Expand Down