Search in sources :

Example 1 with AttachExistingElementRpcHandler

use of com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler in project flow by vaadin.

the class AttachExistingElementRpcHandlerTest method handleNode_requestedIdAndAssignedIdAreDifferent.

@Test
public void handleNode_requestedIdAndAssignedIdAreDifferent() {
    AttachExistingElementRpcHandler handler = new AttachExistingElementRpcHandler();
    int requestedId = 1;
    int assignedId = 2;
    int index = 3;
    JsonObject object = Json.createObject();
    object.put(JsonConstants.RPC_ATTACH_REQUESTED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_ASSIGNED_ID, assignedId);
    object.put(JsonConstants.RPC_ATTACH_TAG_NAME, "div");
    object.put(JsonConstants.RPC_ATTACH_INDEX, index);
    StateNode node = Mockito.mock(StateNode.class);
    StateNode requested = Mockito.mock(StateNode.class);
    StateNode assigned = Mockito.mock(StateNode.class);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(requested);
    Mockito.when(tree.getNodeById(assignedId)).thenReturn(assigned);
    Mockito.when(assigned.hasFeature(Mockito.any())).thenReturn(true);
    AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
    Node<?> parentNode = Mockito.mock(Node.class);
    ChildElementConsumer consumer = Mockito.mock(ChildElementConsumer.class);
    Element sibling = Mockito.mock(Element.class);
    feature.register(parentNode, sibling, requested, consumer);
    Mockito.when(node.getFeature(AttachExistingElementFeature.class)).thenReturn(feature);
    handler.handleNode(node, object);
    assertNodeIsUnregistered(node, requested, feature);
    Mockito.verify(parentNode, Mockito.times(0)).insertChild(index, Element.get(assigned));
    Mockito.verify(consumer).accept(Element.get(assigned));
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature) AttachExistingElementRpcHandler(com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler) Test(org.junit.Test)

Example 2 with AttachExistingElementRpcHandler

use of com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler in project flow by vaadin.

the class AttachExistingElementRpcHandlerTest method handleNode_error.

@Test
public void handleNode_error() {
    AttachExistingElementRpcHandler handler = new AttachExistingElementRpcHandler();
    int requestedId = 1;
    JsonObject object = Json.createObject();
    object.put(JsonConstants.RPC_ATTACH_REQUESTED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_ASSIGNED_ID, -1);
    object.put(JsonConstants.RPC_ATTACH_TAG_NAME, "div");
    object.put(JsonConstants.RPC_ATTACH_INDEX, -1);
    StateNode node = Mockito.mock(StateNode.class);
    StateNode requested = Mockito.mock(StateNode.class);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(requested);
    AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
    Node<?> parentNode = Mockito.mock(Node.class);
    ChildElementConsumer consumer = Mockito.mock(ChildElementConsumer.class);
    Element sibling = Mockito.mock(Element.class);
    feature.register(parentNode, sibling, requested, consumer);
    Mockito.when(node.getFeature(AttachExistingElementFeature.class)).thenReturn(feature);
    handler.handleNode(node, object);
    Mockito.verify(consumer).onError(parentNode, "div", sibling);
    assertNodeIsUnregistered(node, requested, feature);
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature) AttachExistingElementRpcHandler(com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler) Test(org.junit.Test)

Example 3 with AttachExistingElementRpcHandler

use of com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler in project flow by vaadin.

the class AttachExistingElementRpcHandlerTest method handleNode_requestedIdEqualsAssignedId.

@Test
public void handleNode_requestedIdEqualsAssignedId() {
    AttachExistingElementRpcHandler handler = new AttachExistingElementRpcHandler();
    int requestedId = 1;
    int index = 2;
    JsonObject object = Json.createObject();
    object.put(JsonConstants.RPC_ATTACH_REQUESTED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_ASSIGNED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_TAG_NAME, "div");
    object.put(JsonConstants.RPC_ATTACH_INDEX, index);
    StateNode node = Mockito.mock(StateNode.class);
    StateNode requested = Mockito.mock(StateNode.class);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(requested);
    Mockito.when(requested.hasFeature(Mockito.any())).thenReturn(true);
    AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
    Node<?> parentNode = Mockito.mock(Node.class);
    ChildElementConsumer consumer = Mockito.mock(ChildElementConsumer.class);
    Element sibling = Mockito.mock(Element.class);
    feature.register(parentNode, sibling, requested, consumer);
    Mockito.when(node.getFeature(AttachExistingElementFeature.class)).thenReturn(feature);
    handler.handleNode(node, object);
    assertNodeIsUnregistered(node, requested, feature);
    Mockito.verify(parentNode).insertChild(index, Element.get(requested));
    Mockito.verify(consumer).accept(Element.get(requested));
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature) AttachExistingElementRpcHandler(com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler) Test(org.junit.Test)

Aggregations

ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)3 Element (com.vaadin.flow.dom.Element)3 StateNode (com.vaadin.flow.internal.StateNode)3 StateTree (com.vaadin.flow.internal.StateTree)3 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)3 AttachExistingElementRpcHandler (com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler)3 JsonObject (elemental.json.JsonObject)3 Test (org.junit.Test)3