Hi all,
While writing UiUnit tests, I have a problem with TreeGrid, because with test().click() on a Button it returns “is not usable”.
The Problem: The issue is that getCellComponent() returns a Button instance that is not attached to any parent or the active UI state tree. Even though the TreeGrid itself is attached, the ComponentRenderer seems to generate a new, isolated instance for each call.
Debug Observations:
- Instance Mismatch: Multiple calls to
getCellComponent(row, col)return different button objects (firstCall == secondCallisfalse). - Attachment Status:
button.isAttached()returnsfalseandbutton.getParent().isPresent()is alsofalse. - Usability Check: Because the button is detached, Vaadin’s internal
isInert()logic or usability check fails, throwing:java.lang.IllegalStateException: Button[...] is not usable.
Is this a known behavior in Vaadin’s ComponentRenderer when used with uiunit testing , and is a fix planned to ensure components are automatically attached when retrieved via getCellComponent()?"
grid.addHierarchyColumn(item -> item)
.setHeader("Header")
.setSortable(true);
grid.addColumn(new ComponentRenderer<>(item -> {
if (item.contains("- Child")) {
return createButton(item);
}
return null;
})).setHeader("Action")
.setWidth("10em")
.setFlexGrow(0);
grid.setItems(
List.of("Value 1", "Value 2", "Value 3"),
parent -> {
if (!parent.contains("- Child")) {
return List.of(parent + " - Child 1", parent + " - Child 2", parent + " - Child 3");
}
return Collections.emptyList();
}
);
VerticalLayout verticalLayout = new VerticalLayout();
verticalLayout.setId("vl");
verticalLayout.add(grid);
add(verticalLayout);
}
Button createButton(String item) {
Button btn = new Button();
btn.setText("Button");
btn.addClickListener(
click -> {
showInfoNotification("Clicked");
});
btn.setMinWidth("2em");
btn.setId("btn-" + item);
return btn;
}