Search in sources :

Example 1 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class MapSyncRpcHandlerTest method syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy.

@Test
public void syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy() 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);
    ModelList modelList = propertyMap.resolveModelList("foo");
    // fake StateNode has been created for the model
    StateNode item = new StateNode(ElementPropertyMap.class);
    modelList.add(item);
    item.getFeature(ElementPropertyMap.class).setProperty("bar", "baz");
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", item.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, TEST_PROPERTY, json);
    // Now the model node should be copied and available as the
    // TEST_PROPERTY value
    Serializable testPropertyValue = propertyMap.getProperty(TEST_PROPERTY);
    Assert.assertTrue(testPropertyValue instanceof StateNode);
    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertNotEquals(item.getId(), newNode.getId());
    Assert.assertEquals("baz", newNode.getFeature(ElementPropertyMap.class).getProperty("bar"));
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) 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 2 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class MapSyncRpcHandlerTest method syncJSON_jsonIsNotListItemAndNotPropertyValue_propertySetToJSON.

@Test
public void syncJSON_jsonIsNotListItemAndNotPropertyValue_propertySetToJSON() 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();
    TestComponent anotherComonent = new TestComponent();
    StateNode anotherNode = anotherComonent.getElement().getNode();
    ElementPropertyMap.getModel(node).setUpdateFromClientFilter(name -> true);
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", anotherNode.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, "foo", json);
    Serializable testPropertyValue = node.getFeature(ElementPropertyMap.class).getProperty("foo");
    Assert.assertNotSame(anotherNode, testPropertyValue);
    Assert.assertTrue(testPropertyValue instanceof JsonValue);
}
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) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 3 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class ComponentEventBusTest method invalidEventDataInConstructor_addListener.

@Test(expected = IllegalArgumentException.class)
public void invalidEventDataInConstructor_addListener() {
    TestComponent c = new TestComponent();
    c.addListener(MappedToDomInvalidEventData.class, e -> {
    });
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Test(org.junit.Test)

Example 4 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class ComponentEventBusTest method mappedDomEvent_fire_missingData.

@Test
public void mappedDomEvent_fire_missingData() {
    TestComponent c = new TestComponent();
    EventTracker<MappedToDomEvent> eventListener = new EventTracker<>();
    c.addListener(MappedToDomEvent.class, eventListener);
    fireDomEvent(c, "dom-event", createData("event.someData", 2));
    eventListener.assertEventCalled(c, true);
    Assert.assertEquals(2, eventListener.getEvent().getSomeData());
    Assert.assertNull(eventListener.getEvent().getMoreData());
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Test(org.junit.Test)

Example 5 with TestComponent

use of com.vaadin.flow.component.ComponentTest.TestComponent in project flow by vaadin.

the class ComponentEventBusTest method invalidEventConstructor_addListener.

@Test(expected = IllegalArgumentException.class)
public void invalidEventConstructor_addListener() {
    TestComponent c = new TestComponent();
    c.addListener(InvalidMappedToDomEvent.class, e -> {
    });
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Test(org.junit.Test)

Aggregations

TestComponent (com.vaadin.flow.component.ComponentTest.TestComponent)27 Test (org.junit.Test)26 JsonObject (elemental.json.JsonObject)11 UI (com.vaadin.flow.component.UI)6 Element (com.vaadin.flow.dom.Element)6 Registration (com.vaadin.flow.shared.Registration)4 StateNode (com.vaadin.flow.internal.StateNode)3 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)3 Serializable (java.io.Serializable)3 BigDecimal (java.math.BigDecimal)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)2 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)1 JsonValue (elemental.json.JsonValue)1 Before (org.junit.Before)1