Skip to content

ComponentEvent.isFromClient() is false despite using TestWrappers.test() in a SpringUIUnitTest #1814

@DennisSuffel

Description

@DennisSuffel

I expect ComponentEvent.isFromClient() to return true when using TestWrappers.test() in a SpringUIUnitTest to execute the action, that triggers the ComponentEvent.
Instead it returns false.

Steps to reproduce

MainView.java

package org.vaadin.example;

import com.vaadin.flow.component.html.NativeLabel;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;

@Route
public class MainView extends VerticalLayout {

  static final String TEXT_FIELD = "textField";
  static final String NATIVE_LABEL = "nativeLabel";

  public MainView() {
    TextField textField = new TextField();
    textField.setId(TEXT_FIELD);

    NativeLabel nativeLabel = new NativeLabel();
    nativeLabel.setId(NATIVE_LABEL);

    textField.addValueChangeListener(event -> {
      if (event.isFromClient()) {
        nativeLabel.setText(event.getValue());
      }
    });

    add(textField, nativeLabel);
  }
}

MainViewTest.java

package org.vaadin.example;

import static org.assertj.core.api.Assertions.*;
import static org.vaadin.example.MainView.NATIVE_LABEL;
import static org.vaadin.example.MainView.TEXT_FIELD;

import com.vaadin.flow.component.html.NativeLabel;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.testbench.unit.SpringUIUnitTest;
import org.junit.jupiter.api.Test;

class MainViewTest extends SpringUIUnitTest {

  @Test
  void whenSetTextThenSetLabel() {
    navigate(MainView.class);

    test($(TextField.class).id(TEXT_FIELD)).setValue("new value");

    String label = $(NativeLabel.class).id(NATIVE_LABEL).getText();
    assertThat(label).isEqualTo("new value");
  }
}

Expected Result

event.isFromClient() should return true, when TestWrappers.test() is used to set the value of a TextField.
Then MainViewTest#whenSetTextThenSetLabel would complete successfuly.

Actual Result

MainViewTest#whenSetTextThenSetLabel fails with this AssertionError:

org.opentest4j.AssertionFailedError:
expected: "new value"
but was: ""

Versions

Vaadin 24.4.6
Java 17
macOS 14.5

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    ✅ Closed

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions