Search in sources :

Example 1 with ListAddChange

use of com.vaadin.flow.internal.change.ListAddChange in project flow by vaadin.

the class ListChangeTest method testBasicJson.

@Test
public void testBasicJson() {
    StateNode child1 = StateNodeTest.createEmptyNode("child1");
    StateNode child2 = StateNodeTest.createEmptyNode("child2");
    ListAddChange<StateNode> change = new ListAddChange<>(feature, true, 0, Arrays.asList(child1, child2));
    JsonObject json = change.toJson(null);
    Assert.assertEquals(change.getNode().getId(), (int) json.getNumber(JsonConstants.CHANGE_NODE));
    Assert.assertEquals(NodeFeatureRegistry.getId(feature.getClass()), (int) json.getNumber(JsonConstants.CHANGE_FEATURE));
    Assert.assertEquals(JsonConstants.CHANGE_TYPE_SPLICE, json.getString(JsonConstants.CHANGE_TYPE));
    Assert.assertEquals(0, (int) json.getNumber(JsonConstants.CHANGE_SPLICE_INDEX));
    JsonArray addNodes = json.getArray(JsonConstants.CHANGE_SPLICE_ADD_NODES);
    Assert.assertEquals(2, addNodes.length());
    Assert.assertEquals(child1.getId(), (int) addNodes.getNumber(0));
    Assert.assertEquals(child2.getId(), (int) addNodes.getNumber(1));
}
Also used : JsonArray(elemental.json.JsonArray) ListAddChange(com.vaadin.flow.internal.change.ListAddChange) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest) AbstractNodeFeatureTest(com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)

Example 2 with ListAddChange

use of com.vaadin.flow.internal.change.ListAddChange in project flow by vaadin.

the class NodeListAddRemoveTest method verifyAdded.

private void verifyAdded(List<NodeChange> changes, List<String> items, Integer... indexes) {
    for (int i = 0; i < indexes.length; i++) {
        NodeChange nodeChange = changes.get(i);
        Assert.assertThat(nodeChange, CoreMatchers.instanceOf(ListAddChange.class));
        ListAddChange<?> change = (ListAddChange<?>) nodeChange;
        Assert.assertEquals(indexes[i].intValue(), change.getIndex());
        Assert.assertEquals(1, change.getNewItems().size());
        Assert.assertEquals(items.get(i), change.getNewItems().get(0));
    }
}
Also used : NodeChange(com.vaadin.flow.internal.change.NodeChange) ListAddChange(com.vaadin.flow.internal.change.ListAddChange)

Example 3 with ListAddChange

use of com.vaadin.flow.internal.change.ListAddChange in project flow by vaadin.

the class ListChangeTest method testZeroRemoveNotInJson.

@Test
public void testZeroRemoveNotInJson() {
    ListAddChange<StateNode> change = new ListAddChange<>(feature, false, 1, Arrays.asList());
    JsonObject json = change.toJson(null);
    Assert.assertFalse(json.hasKey(JsonConstants.CHANGE_SPLICE_REMOVE));
}
Also used : ListAddChange(com.vaadin.flow.internal.change.ListAddChange) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest) AbstractNodeFeatureTest(com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)

Example 4 with ListAddChange

use of com.vaadin.flow.internal.change.ListAddChange in project flow by vaadin.

the class ListChangeTest method testEmptyAddNotInJson.

@Test
public void testEmptyAddNotInJson() {
    ListAddChange<StateNode> change = new ListAddChange<>(feature, false, 1, Arrays.asList());
    JsonObject json = change.toJson(null);
    Assert.assertFalse(json.hasKey(JsonConstants.CHANGE_SPLICE_ADD_NODES));
}
Also used : ListAddChange(com.vaadin.flow.internal.change.ListAddChange) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest) AbstractNodeFeatureTest(com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)

Example 5 with ListAddChange

use of com.vaadin.flow.internal.change.ListAddChange in project flow by vaadin.

the class StateNodeNodeListTest method testAddingAndRemoving.

@Test
public void testAddingAndRemoving() {
    StateNode value1 = StateNodeTest.createEmptyNode("value1");
    StateNode value2 = StateNodeTest.createEmptyNode("value2");
    nodeList.add(value1);
    Assert.assertEquals(1, nodeList.size());
    Assert.assertSame(value1, nodeList.get(0));
    List<NodeChange> firstAddChanges = collectChanges(nodeList);
    Assert.assertEquals(1, firstAddChanges.size());
    ListAddChange<?> firstAddChange = (ListAddChange<?>) firstAddChanges.get(0);
    Assert.assertEquals(0, firstAddChange.getIndex());
    Assert.assertEquals(Arrays.asList(value1), firstAddChange.getNewItems());
    nodeList.add(0, value2);
    Assert.assertEquals(2, nodeList.size());
    Assert.assertSame(value2, nodeList.get(0));
    Assert.assertSame(value1, nodeList.get(1));
    List<NodeChange> secondAddChanges = collectChanges(nodeList);
    Assert.assertEquals(1, secondAddChanges.size());
    ListAddChange<?> secondAddChange = (ListAddChange<?>) secondAddChanges.get(0);
    Assert.assertEquals(0, secondAddChange.getIndex());
    Assert.assertEquals(Arrays.asList(value2), secondAddChange.getNewItems());
    StateNode removedItem = nodeList.remove(0);
    Assert.assertEquals(1, nodeList.size());
    Assert.assertSame(value1, nodeList.get(0));
    Assert.assertSame(value2, removedItem);
    List<NodeChange> removeChanges = collectChanges(nodeList);
    Assert.assertEquals(1, removeChanges.size());
    ListRemoveChange<?> removeChange = (ListRemoveChange<?>) removeChanges.get(0);
    Assert.assertEquals(0, removeChange.getIndex());
}
Also used : NodeChange(com.vaadin.flow.internal.change.NodeChange) ListAddChange(com.vaadin.flow.internal.change.ListAddChange) StateNode(com.vaadin.flow.internal.StateNode) ListRemoveChange(com.vaadin.flow.internal.change.ListRemoveChange) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest)

Aggregations

ListAddChange (com.vaadin.flow.internal.change.ListAddChange)7 StateNode (com.vaadin.flow.internal.StateNode)6 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)5 Test (org.junit.Test)5 NodeChange (com.vaadin.flow.internal.change.NodeChange)4 AbstractNodeFeatureTest (com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)3 JsonObject (elemental.json.JsonObject)3 ListRemoveChange (com.vaadin.flow.internal.change.ListRemoveChange)2 AbstractListChange (com.vaadin.flow.internal.change.AbstractListChange)1 EmptyChange (com.vaadin.flow.internal.change.EmptyChange)1 ListClearChange (com.vaadin.flow.internal.change.ListClearChange)1 JsonArray (elemental.json.JsonArray)1 Serializable (java.io.Serializable)1 AbstractSet (java.util.AbstractSet)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 IdentityHashMap (java.util.IdentityHashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1