Search in sources :

Example 96 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class MapSyncRpcHandlerTest method createSyncPropertyInvocation.

private static JsonObject createSyncPropertyInvocation(Element element, String property, Serializable value) {
    StateNode node = EventRpcHandlerTest.getInvocationNode(element);
    // Copied from ServerConnector
    JsonObject message = Json.createObject();
    message.put(JsonConstants.RPC_NODE, node.getId());
    message.put(JsonConstants.RPC_FEATURE, NodeFeatureRegistry.getId(ElementPropertyMap.class));
    message.put(JsonConstants.RPC_PROPERTY, property);
    message.put(JsonConstants.RPC_PROPERTY_VALUE, JsonCodec.encodeWithoutTypeInfo(value));
    return message;
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap)

Example 97 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class MapSyncRpcHandlerTest method syncJSON_jsonIsPropertyValueOfStateNode_propertySetToNode.

@Test
public void syncJSON_jsonIsPropertyValueOfStateNode_propertySetToNode() throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);
    StateNode node = element.getNode();
    // Set model value directly via ElementPropertyMap
    ElementPropertyMap propertyMap = node.getFeature(ElementPropertyMap.class);
    propertyMap.setUpdateFromClientFilter(name -> true);
    ElementPropertyMap modelMap = propertyMap.resolveModelMap("foo");
    // fake StateNode has been created for the model
    StateNode model = modelMap.getNode();
    modelMap.setProperty("bar", "baz");
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", model.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, "foo", json);
    Serializable testPropertyValue = propertyMap.getProperty("foo");
    Assert.assertTrue(testPropertyValue instanceof StateNode);
    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertSame(model, newNode);
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Serializable(java.io.Serializable) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 98 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class AngularTemplateParserTest method parseTemplateProperty.

@Test
public void parseTemplateProperty() {
    ElementTemplateNode rootNode = (ElementTemplateNode) parse("<input [value]='foo'></input>");
    Assert.assertEquals("input", rootNode.getTag());
    Assert.assertEquals(0, rootNode.getAttributeNames().count());
    Assert.assertEquals(0, rootNode.getClassNames().count());
    Assert.assertEquals(1, rootNode.getPropertyNames().count());
    Optional<BindingValueProvider> binding = rootNode.getPropertyBinding("value");
    Assert.assertTrue(binding.isPresent());
    StateNode node = new StateNode(ModelMap.class);
    // Explicitly set "foo" property to null. So model has property "foo".
    // See #970
    ModelMap.get(node).setValue("foo", null);
    Assert.assertNull(binding.get().getValue(node));
    ModelMap.get(node).setValue("foo", "bar");
    Assert.assertEquals("bar", binding.get().getValue(node));
}
Also used : BindingValueProvider(com.vaadin.flow.template.angular.BindingValueProvider) ElementTemplateNode(com.vaadin.flow.template.angular.ElementTemplateNode) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test)

Example 99 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class AngularTemplateParserTest method parseParameterizedTextTemplate.

@Test
public void parseParameterizedTextTemplate() {
    ElementTemplateNode rootNode = (ElementTemplateNode) parse("<div id='foo'>{{bar}}<input></div>");
    Assert.assertEquals("div", rootNode.getTag());
    Assert.assertEquals(1, rootNode.getAttributeNames().count());
    Assert.assertEquals("foo", rootNode.getAttributeBinding("id").get().getValue(null));
    Assert.assertEquals(2, rootNode.getChildCount());
    TextTemplateNode textChild = (TextTemplateNode) rootNode.getChild(0);
    BindingValueProvider binding = textChild.getTextBinding();
    StateNode node = new StateNode(ModelMap.class);
    // Explicitly set "bar" property to null. So model has property "bar".
    // See #970
    ModelMap.get(node).setValue("bar", null);
    Assert.assertNull(binding.getValue(node));
    String value = "someValue";
    ModelMap.get(node).setValue("bar", value);
    Assert.assertEquals(value, binding.getValue(node));
}
Also used : TextTemplateNode(com.vaadin.flow.template.angular.TextTemplateNode) BindingValueProvider(com.vaadin.flow.template.angular.BindingValueProvider) ElementTemplateNode(com.vaadin.flow.template.angular.ElementTemplateNode) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test)

Example 100 with StateNode

use of com.vaadin.flow.internal.StateNode in project flow by vaadin.

the class AngularTemplateParserTest method parseTemplateAttribute.

@Test
public void parseTemplateAttribute() {
    ElementTemplateNode rootNode = (ElementTemplateNode) parse("<input [attr.value]='foo'></input>");
    Assert.assertEquals("input", rootNode.getTag());
    Assert.assertEquals(1, rootNode.getAttributeNames().count());
    Assert.assertEquals(0, rootNode.getClassNames().count());
    Assert.assertEquals(0, rootNode.getPropertyNames().count());
    Optional<BindingValueProvider> binding = rootNode.getAttributeBinding("value");
    Assert.assertTrue(binding.isPresent());
    StateNode node = new StateNode(ModelMap.class);
    // Explicitly set "foo" property to null. So model has property "foo".
    // See #970
    ModelMap.get(node).setValue("foo", null);
    Assert.assertNull(binding.get().getValue(node));
    ModelMap.get(node).setValue("foo", "bar");
    Assert.assertEquals("bar", binding.get().getValue(node));
}
Also used : BindingValueProvider(com.vaadin.flow.template.angular.BindingValueProvider) ElementTemplateNode(com.vaadin.flow.template.angular.ElementTemplateNode) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test)

Aggregations

StateNode (com.vaadin.flow.internal.StateNode)160 Test (org.junit.Test)99 Element (com.vaadin.flow.dom.Element)29 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)19 JsonObject (elemental.json.JsonObject)19 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)18 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)16 ArrayList (java.util.ArrayList)14 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)13 Serializable (java.io.Serializable)13 StateTree (com.vaadin.flow.internal.StateTree)10 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)10 UI (com.vaadin.flow.component.UI)9 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)9 ElementTemplateNode (com.vaadin.flow.template.angular.ElementTemplateNode)9 Bean (com.vaadin.flow.templatemodel.Bean)9 ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)8 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)8 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)8 TemplateOverridesMap (com.vaadin.flow.internal.nodefeature.TemplateOverridesMap)6