Search in sources :

Example 1 with ComponentStateMock

use of com.qcadoo.view.internal.states.ComponentStateMock in project qcadoo by qcadoo.

the class ViewDefinitionTest method shouldCallEvent.

@Test
public void shouldCallEvent() throws Exception {
    // given
    InternalViewDefinition viewDefinition = new ViewDefinitionImpl("name", "plugin", mock(DataDefinition.class), true, null);
    TestEvent event = mock(TestEvent.class);
    ComponentStateMock state = new ComponentStateMock(new JSONObject(of("asd", "123")));
    state.registerTestEvent("eventName", event);
    ComponentPatternMock pattern = new ComponentPatternMock(getComponentDefinition("componentName", viewDefinition), state);
    viewDefinition.addComponentPattern(pattern);
    JSONObject eventJson = new JSONObject();
    eventJson.put(InternalViewDefinition.JSON_EVENT_NAME, "eventName");
    eventJson.put(InternalViewDefinition.JSON_EVENT_COMPONENT, "componentName");
    eventJson.put(InternalViewDefinition.JSON_EVENT_ARGS, new JSONArray(newArrayList("arg1", "arg2")));
    JSONObject contentJson = new JSONObject();
    contentJson.put("asd", "qwe");
    JSONObject componentJson = new JSONObject();
    componentJson.put(AbstractComponentState.JSON_CONTENT, contentJson);
    JSONObject json = new JSONObject();
    json.put(InternalViewDefinition.JSON_EVENT, eventJson);
    json.put(InternalViewDefinition.JSON_COMPONENTS, new JSONObject(of("componentName", componentJson)));
    // when
    JSONObject result = ((InternalComponentState) viewDefinition.performEvent(json, Locale.ENGLISH)).render();
    // then
    assertEquals(contentJson, state.getContent());
    verify(event).invoke(new String[] { "arg1", "arg2" });
    Assert.assertEquals("123", result.getJSONObject("components").getJSONObject("componentName").getJSONObject("content").get("asd"));
}
Also used : ViewDefinitionImpl(com.qcadoo.view.internal.internal.ViewDefinitionImpl) JSONObject(org.json.JSONObject) TestEvent(com.qcadoo.view.internal.states.ComponentStateMock.TestEvent) InternalViewDefinition(com.qcadoo.view.internal.api.InternalViewDefinition) JSONArray(org.json.JSONArray) ComponentPatternMock(com.qcadoo.view.internal.patterns.ComponentPatternMock) DataDefinition(com.qcadoo.model.api.DataDefinition) ComponentStateMock(com.qcadoo.view.internal.states.ComponentStateMock) InternalComponentState(com.qcadoo.view.internal.api.InternalComponentState) Test(org.junit.Test) AbstractPatternTest(com.qcadoo.view.internal.patterns.AbstractPatternTest)

Example 2 with ComponentStateMock

use of com.qcadoo.view.internal.states.ComponentStateMock in project qcadoo by qcadoo.

the class FieldAndScopeListenerPatternTest method shouldUpdateStateScopeListeners.

@Test
public void shouldUpdateStateScopeListeners() throws Exception {
    // given
    InternalViewDefinition viewDefinition = mock(InternalViewDefinition.class);
    AbstractContainerPattern parent = new ContainerPatternMock(getComponentDefinition("f1", viewDefinition));
    ComponentPatternMock child1 = new ComponentPatternMock(getComponentDefinition("t1", null, "field1", parent, viewDefinition));
    ComponentPatternMock child2 = new ComponentPatternMock(getComponentDefinition("t2", null, "field2", parent, viewDefinition));
    parent.addChild(child1);
    parent.addChild(child2);
    parent.initialize();
    child1.initialize();
    child2.initialize();
    ComponentStateMock f1State = new ComponentStateMock();
    ComponentState t1State = Mockito.mock(ComponentState.class, withSettings().extraInterfaces(ScopeEntityIdChangeListener.class));
    ComponentState t2State = Mockito.mock(ComponentState.class, withSettings().extraInterfaces(ScopeEntityIdChangeListener.class));
    ViewDefinitionState viewDefinitions = Mockito.mock(ViewDefinitionState.class);
    BDDMockito.given(viewDefinitions.getComponentByReference("f1")).willReturn(f1State);
    BDDMockito.given(viewDefinitions.getComponentByReference("f1.t1")).willReturn(t1State);
    BDDMockito.given(viewDefinitions.getComponentByReference("f1.t2")).willReturn(t2State);
    // when
    parent.updateComponentStateListeners(viewDefinitions);
    // then
    assertEquals(t1State, f1State.getPublicScopeFieldEntityIdChangeListeners().get("field1"));
    assertEquals(t2State, f1State.getPublicScopeFieldEntityIdChangeListeners().get("field2"));
}
Also used : InternalViewDefinition(com.qcadoo.view.internal.api.InternalViewDefinition) ScopeEntityIdChangeListener(com.qcadoo.view.internal.ScopeEntityIdChangeListener) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) ComponentStateMock(com.qcadoo.view.internal.states.ComponentStateMock) ComponentState(com.qcadoo.view.api.ComponentState) Test(org.junit.Test)

Example 3 with ComponentStateMock

use of com.qcadoo.view.internal.states.ComponentStateMock in project qcadoo by qcadoo.

the class FieldAndScopeListenerPatternTest method shouldUpdateStateFieldListeners.

@Test
public void shouldUpdateStateFieldListeners() throws Exception {
    // given
    InternalViewDefinition viewDefinition = mock(InternalViewDefinition.class);
    AbstractContainerPattern parent = new ContainerPatternMock(getComponentDefinition("f1", viewDefinition));
    ComponentPatternMock child1 = new ComponentPatternMock(getComponentDefinition("t1", "field1", null, parent, viewDefinition));
    ComponentPatternMock child2 = new ComponentPatternMock(getComponentDefinition("t2", "field2", null, parent, viewDefinition));
    parent.addChild(child1);
    parent.addChild(child2);
    parent.initialize();
    child1.initialize();
    child2.initialize();
    ComponentStateMock f1State = new ComponentStateMock();
    ComponentState t1State = Mockito.mock(ComponentState.class, withSettings().extraInterfaces(FieldEntityIdChangeListener.class));
    ComponentState t2State = Mockito.mock(ComponentState.class, withSettings().extraInterfaces(FieldEntityIdChangeListener.class));
    ViewDefinitionState viewDefinitions = Mockito.mock(ViewDefinitionState.class);
    BDDMockito.given(viewDefinitions.getComponentByReference("f1")).willReturn(f1State);
    BDDMockito.given(viewDefinitions.getComponentByReference("f1.t1")).willReturn(t1State);
    BDDMockito.given(viewDefinitions.getComponentByReference("f1.t2")).willReturn(t2State);
    // when
    parent.updateComponentStateListeners(viewDefinitions);
    // then
    assertEquals(t1State, f1State.getPublicFieldEntityIdChangeListeners().get("field1"));
    assertEquals(t2State, f1State.getPublicFieldEntityIdChangeListeners().get("field2"));
}
Also used : InternalViewDefinition(com.qcadoo.view.internal.api.InternalViewDefinition) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) FieldEntityIdChangeListener(com.qcadoo.view.internal.FieldEntityIdChangeListener) ComponentStateMock(com.qcadoo.view.internal.states.ComponentStateMock) ComponentState(com.qcadoo.view.api.ComponentState) Test(org.junit.Test)

Aggregations

InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)3 ComponentStateMock (com.qcadoo.view.internal.states.ComponentStateMock)3 Test (org.junit.Test)3 ComponentState (com.qcadoo.view.api.ComponentState)2 ViewDefinitionState (com.qcadoo.view.api.ViewDefinitionState)2 DataDefinition (com.qcadoo.model.api.DataDefinition)1 FieldEntityIdChangeListener (com.qcadoo.view.internal.FieldEntityIdChangeListener)1 ScopeEntityIdChangeListener (com.qcadoo.view.internal.ScopeEntityIdChangeListener)1 InternalComponentState (com.qcadoo.view.internal.api.InternalComponentState)1 ViewDefinitionImpl (com.qcadoo.view.internal.internal.ViewDefinitionImpl)1 AbstractPatternTest (com.qcadoo.view.internal.patterns.AbstractPatternTest)1 ComponentPatternMock (com.qcadoo.view.internal.patterns.ComponentPatternMock)1 TestEvent (com.qcadoo.view.internal.states.ComponentStateMock.TestEvent)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1