ExecuteJavaScriptProcessor { public void execute(JsonArray invocations) { for (int i = 0; i < invocations.length(); i++) { JsonArray invocation = invocations.getArray(i); handleInvocation(invocation); } } ExecuteJavaScriptProcessor(Registry registry); void execute(JsonArray invocations);  }
@Test public void execute_parametersAndCodeAreValidAndNoNodeParameters() { CollectingExecuteJavaScriptProcessor processor = new CollectingExecuteJavaScriptProcessor(); JsonArray invocation1 = JsonUtils.createArray(Json.create("script1")); JsonArray invocation2 = Stream.of("param1", "param2", "script2") .map(Json::create).collect(JsonUtils.asArray()); JsonArray invocations = JsonUtils.createArray(invocation1, invocation2); processor.execute(invocations); Assert.assertEquals(2, processor.parameterNamesAndCodeList.size()); Assert.assertEquals(2, processor.parametersList.size()); Assert.assertEquals(2, processor.nodeParametersList.size()); Assert.assertArrayEquals(new String[] { "script1" }, processor.parameterNamesAndCodeList.get(0)); Assert.assertEquals(0, processor.parametersList.get(0).length()); Assert.assertArrayEquals(new String[] { "$0", "$1", "script2" }, processor.parameterNamesAndCodeList.get(1)); Assert.assertEquals(2, processor.parametersList.get(1).length()); Assert.assertEquals("param1", processor.parametersList.get(1).get(0)); Assert.assertEquals("param2", processor.parametersList.get(1).get(1)); Assert.assertEquals(0, processor.nodeParametersList.get(0).size()); Assert.assertEquals(0, processor.nodeParametersList.get(1).size()); }
@Test public void execute_nodeParametersAreCorrectlyPassed() { Registry registry = new Registry() { { StateTree tree = new StateTree(this); set(StateTree.class, tree); set(ExistingElementMap.class, new ExistingElementMap()); } }; CollectingExecuteJavaScriptProcessor processor = new CollectingExecuteJavaScriptProcessor( registry); StateNode node = new StateNode(10, registry.getStateTree()); registry.getStateTree().registerNode(node); JsElement element = new JsElement() { }; node.setDomNode(element); JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId())); JsonArray invocation = Stream.of(json, Json.create("$0")) .collect(JsonUtils.asArray()); processor.execute(JsonUtils.createArray(invocation)); Assert.assertEquals(1, processor.nodeParametersList.size()); Assert.assertEquals(1, processor.nodeParametersList.get(0).size()); JsMap<Object, StateNode> map = processor.nodeParametersList.get(0); StateNode stateNode = map.get(element); Assert.assertEquals(node, stateNode); }
@Test public void execute_nodeParameterIsVirtualChildAwaitingInit() { CollectingExecuteJavaScriptProcessor processor = new CollectingExecuteJavaScriptProcessor(); Registry registry = processor.getRegistry(); StateNode node = new StateNode(11, registry.getStateTree()); JsonObject object = Json.createObject(); object.put(NodeProperties.TYPE, NodeProperties.INJECT_BY_ID); node.getMap(NodeFeatures.ELEMENT_DATA) .getProperty(NodeProperties.PAYLOAD).setValue(object); registry.getStateTree().registerNode(node); JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId())); JsonArray invocation = Stream.of(json, Json.create("$0")) .collect(JsonUtils.asArray()); processor.execute(JsonUtils.createArray(invocation)); Assert.assertEquals(0, processor.nodeParametersList.size()); JsElement element = new JsElement() { }; node.setDomNode(element); Reactive.flush(); Assert.assertEquals(1, processor.nodeParametersList.size()); Assert.assertEquals(1, processor.nodeParametersList.get(0).size()); JsMap<Object, StateNode> map = processor.nodeParametersList.get(0); StateNode stateNode = map.get(element); Assert.assertEquals(node, stateNode); }
@Test public void execute_nodeParameterIsHidden() { CollectingExecuteJavaScriptProcessor processor = new CollectingExecuteJavaScriptProcessor(); Registry registry = processor.getRegistry(); StateNode node = new StateNode(31, registry.getStateTree()); processor.isBound = false; registry.getStateTree().registerNode(node); JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId())); JsonArray invocation = Stream.of(json, Json.create("$0")) .collect(JsonUtils.asArray()); processor.execute(JsonUtils.createArray(invocation)); Assert.assertEquals(0, processor.nodeParametersList.size()); JsElement element = new JsElement() { }; node.setDomNode(element); processor.isBound = true; Reactive.flush(); Assert.assertEquals(1, processor.nodeParametersList.size()); Assert.assertEquals(1, processor.nodeParametersList.get(0).size()); JsMap<Object, StateNode> map = processor.nodeParametersList.get(0); StateNode stateNode = map.get(element); Assert.assertEquals(node, stateNode); }
@Test public void execute_nodeParameterIsNotVirtualChild() { CollectingExecuteJavaScriptProcessor processor = new CollectingExecuteJavaScriptProcessor(); Registry registry = processor.getRegistry(); StateNode node = new StateNode(12, registry.getStateTree()); registry.getStateTree().registerNode(node); JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId())); JsonArray invocation = Stream.of(json, Json.create("$0")) .collect(JsonUtils.asArray()); processor.execute(JsonUtils.createArray(invocation)); Assert.assertEquals(1, processor.nodeParametersList.size()); }