diff --git a/flow-data/src/main/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalDataCommunicator.java b/flow-data/src/main/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalDataCommunicator.java index a39d3032fe5..49d97b9524a 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalDataCommunicator.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalDataCommunicator.java @@ -160,16 +160,18 @@ public void reset() { update.enqueue("$connector.ensureHierarchy"); Collection expandedItems = getHierarchyMapper().getExpandedItems(); - update.enqueue("$connector.expandItems", - expandedItems - .stream() - .map(getKeyMapper()::key) - .map(key -> { - JsonObject json = Json.createObject(); - json.put("key", key); - return json; - }).collect( - JsonUtils.asArray())); + if (!expandedItems.isEmpty()) { + update.enqueue("$connector.expandItems", + expandedItems + .stream() + .map(getKeyMapper()::key) + .map(key -> { + JsonObject json = Json.createObject(); + json.put("key", key); + return json; + }).collect( + JsonUtils.asArray())); + } requestFlush(update); } @@ -240,15 +242,16 @@ parentKey, getKeyMapper(), mapper, */ public SerializableConsumer setDataProvider( HierarchicalDataProvider dataProvider, F initialFilter) { - SerializableConsumer consumer = super.setDataProvider(dataProvider, - initialFilter); - - // Remove old mapper + // Remove old mapper before super.setDataProvider(...) prevents calling + // reset() before clearing the already expanded items: if (mapper != null) { mapper.destroyAllData(); } mapper = createHierarchyMapper(dataProvider); + SerializableConsumer consumer = super.setDataProvider(dataProvider, + initialFilter); + // Set up mapper for requests mapper.setBackEndSorting(getBackEndSorting()); mapper.setInMemorySorting(getInMemorySorting()); diff --git a/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorTest.java b/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorTest.java index b2c22faec56..37375221445 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorTest.java @@ -178,7 +178,7 @@ private void testItemRemove(String item, boolean refreshAll) { dataProvider.refreshItem(item); } - int number = refreshAll ? 6 : 5; + int number = refreshAll ? 7 : 6; ArgumentCaptor attachCaptor = ArgumentCaptor .forClass(SerializableConsumer.class); @@ -213,11 +213,28 @@ public void reset_noDataControllers_hierarchicalUpdateIsCalled() { // any data controllers communicator.reset(); - Assert.assertEquals(2, enqueueFunctions.size()); + Assert.assertEquals(1, enqueueFunctions.size()); Assert.assertEquals("$connector.ensureHierarchy", enqueueFunctions.get(0)); + } + + @Test + public void reset_expandSomeItems_hierarchicalUpdateContainsExpandItems() { + enqueueFunctions.clear(); + + communicator.expand(ROOT); + + communicator.reset(); + + // One expandItems for calling expand(...) + // One expandItems and one ensureHierarchy for calling reset() + Assert.assertEquals(3, enqueueFunctions.size()); Assert.assertEquals("$connector.expandItems", + enqueueFunctions.get(0)); + Assert.assertEquals("$connector.ensureHierarchy", enqueueFunctions.get(1)); + Assert.assertEquals("$connector.expandItems", + enqueueFunctions.get(2)); } @Test @@ -247,7 +264,6 @@ public void reset_expandSomeItems_updateContainsProperJsonObjectsToExpand() { dataCommunicator.reset(); - Assert.assertTrue(enqueueFunctionsWithParams .containsKey("$connector.expandItems")); JsonArray arguments = (JsonArray) enqueueFunctionsWithParams diff --git a/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchyMapperWithDataTest.java b/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchyMapperWithDataTest.java index 914171d5169..2aeaf780c98 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchyMapperWithDataTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchyMapperWithDataTest.java @@ -422,6 +422,9 @@ public boolean hasChildren(TreeNode item) { protected Stream fetchChildrenFromBackEnd( HierarchicalQuery query) { if (query.getParent() == null) { + return Arrays.stream(new TreeNode[] {root}); + } + if(query.getParent() == root) { return Arrays.stream(secondLevelNodes); } return Arrays.stream(thirdLevelNodes)