feat!: add TreeGridTester with expand and collapse - #189
Conversation
TreeGrid resolved to GridTester through the superclass walk in TesterRegistry, which left the one interaction that distinguishes a TreeGrid from a Grid unreachable: tests had to call treeGrid.expand(item) directly, and the resulting ExpandEvent reported isFromClient() as false. TreeGridTester mirrors what the hierarchy column's tree toggle does on the client: it guards on HierarchicalDataCommunicator.hasChildren(item) and then calls the protected expand/collapse overloads with userOriginated set, so ExpandEvent and CollapseEvent are reported as coming from the client. Row indexes address displayed rows, since GridTester.getRow already walks the TreeGrid row sequence and skips children of collapsed nodes. Toggling a leaf row, or a row that is already in the target state, throws IllegalStateException the way DetailsTester does, since neither is something a user can do. Fixes #176
isExpanded and hasChildren both document that they require a visible component, but only expand and collapse were exercised against a hidden tree grid. Extend the existing case to cover all four.
The expand toggle lives in the hierarchy column, and TreeGrid can be configured with none, one or several of them. expand and collapse now require at least one visible hierarchy column, since without one the user is shown no toggle to click; several hierarchy columns each render a toggle for the same node, so they need no special handling. getCellText also returns null for a hierarchy column added through a value provider, because that column renders the item through a LitRenderer over vaadin-grid-tree-toggle rather than through a column path. Read it from the toggle's own value provider instead. The component variant is a ComponentRenderer and is already handled by GridTester. Also split the expand and collapse tests so that the row bookkeeping and the client originated events are asserted separately, and pin the root row positions before expanding.
Test Results1 340 tests 1 340 ✅ 39s ⏱️ Results for commit 445f1a7. ♻️ This comment has been updated with latest results. |
|
CI was red on Root cause is that this branch was based on an older After the merge the suite is green: 1278 tests in Unrelated heads-up while verifying: |
The TreeGrid overloads of test() are more specific than the Grid ones, so a TreeGrid argument now resolves to TreeGridTester. Spell that out on the overloads themselves, together with what code that relied on the old static type has to do instead. BREAKING CHANGE: test(treeGrid) and test(treeGrid, itemType) now return TreeGridTester<TreeGrid<V>, V> instead of GridTester<Grid<V>, V>. Code that assigned the result to an explicitly typed GridTester<Grid<V>, V>, or passed it to a parameter of that type, no longer compiles. Widen the declaration to GridTester<? extends Grid<V>, V>, use var, or chain the call directly. Binary compatibility is unaffected: test(Grid) and test(Grid, Class) are unchanged, so already compiled code keeps working.
|
Looks good to me, but introduces a breaking change. To be released in the next minor. |
|
@mcollovati Thanks. Nothing further needed on the branch for that — it is already set up to land in a minor rather than a patch:
One thing worth your call: |
Summary
Adds
TreeGridTester, a tester made forTreeGrid, so tests can expand and collapse rows the same way a user does in the browser. Until nowTreeGridfell back toGridTester, and tests had to calltreeGrid.expand(item)directly, which produced anExpandEventwithisFromClient()asfalse. Fixes #176.What changed
Breaking (source only):
test(treeGrid)andtest(treeGrid, itemType)now returnTreeGridTester<TreeGrid<V>, V>instead ofGridTester<Grid<V>, V>. The new overloads are more specific, so aTreeGridargument resolves to them. This only affects code that assigns the result to an explicitly typedGridTester<Grid<V>, V>variable or passes it to a parameter of that type — that code no longer compiles. Fix it by widening the declaration toGridTester<? extends Grid<V>, V>, usingvar, or chaining the call directly. Binary compatibility is unaffected:test(Grid)andtest(Grid, Class)are unchanged, so already compiled code keeps working.New
TreeGridTester(extendsGridTester):expand(int row)/collapse(int row)mirror a click on the tree toggle. They call the user-originatedexpand/collapseoverloads, soExpandEventandCollapseEventreportisFromClient()astrue.isExpanded(int row)andhasChildren(int row)read the state of a row.expand/collapsethrowIllegalStateExceptionwhen the tree grid has no visible hierarchy column (the user would see no toggle), when the row is a leaf, or when the row is already in the target state — the same style asDetailsTester. Several hierarchy columns need no special handling, since each one renders a toggle for the same node.getCellText(row, column)is overridden: a hierarchy column added withaddHierarchyColumn(ValueProvider)renders through aLitRendererovervaadin-grid-tree-toggle, not a column path, so the text is read from the toggle's own value provider. All other columns, including the component variant, are read as before byGridTester.Use case
You test a view with a file tree. The app loads a folder's contents only when the user opens it, and it tracks opened folders for analytics — both hang off an expand listener that checks
isFromClient(). WithGridTesteryou could not trigger that path; now you can.API Changes
com.vaadin.browserless.TesterWrappers
com.vaadin.flow.component.treegrid.TreeGridTester
Test summary
TreeGridpassed totest(...)yields aTreeGridTester, even when the static type isGridGridTesterexpand(row)makes children visible, and row indexes keep following the displayed rows when nesting deepercollapse(row)hides the children againExpandEventandCollapseEventcarry the right items and reportisFromClient() == trueisExpandedandhasChildrenfollow the state of the row before and after expandingIllegalStateExceptionDetailsTester; prevents impossible interactionsexpand,collapse,isExpandedandhasChildrenall throwexpand/collapsethrow when there is no hierarchy column, or when the only one is hidden;hasChildrenstill worksexpand(0)toggles the node oncegetCellTextreads the value-provider hierarchy column, the component hierarchy column and a plain columnnullbefore the overridetest(treeGrid, itemType)returns a workingTreeGridTesterTreeGridTesterTest.treeGrid_resolvesToTreeGridTester— 1TreeGridTesterTest.expand_childRowsBecomeVisible— 2TreeGridTesterTest.expand_nestedRow_rowIndexesFollowTheDisplayedRows— 2TreeGridTesterTest.collapse_childRowsHidden— 3TreeGridTesterTest.expand_firesExpandEventFromClient— 4TreeGridTesterTest.collapse_firesCollapseEventFromClient— 4TreeGridTesterTest.isExpandedAndHasChildren_reflectRowState— 5TreeGridTesterTest.expandOrCollapseLeafRow_throws— 6TreeGridTesterTest.toggleRowAlreadyInTargetState_throws— 7TreeGridTesterTest.hiddenTreeGrid_throws— 8TreeGridTesterTest.withoutHierarchyColumn_expandAndCollapseThrow— 9TreeGridTesterTest.hiddenHierarchyColumn_expandThrows— 9TreeGridTesterTest.multipleHierarchyColumns_expandWorksAndTogglesTheSameNode— 10TreeGridTesterTest.getCellText_readsHierarchyAndPlainColumns— 11TreeGridTesterTest.getCellText_readsComponentHierarchyColumn— 11Left untested on purpose: the constructor and the inherited
GridTesterbehaviour (size,getRow), which every test above already goes through.