use of com.qcadoo.view.internal.patterns.ComponentPatternMock 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"));
}
Aggregations