SubmitErrorsListData implements PageData { @Override public Data create() { StringBuffer sb = new StringBuffer(); sb.append(String.format("if($data.%s && $data.%s && $data.%s.explanations){",FormOutputData.NAME,SUBMIT_ERROR_DATA,SUBMIT_ERROR_DATA)); sb.append("\n\t"); sb.append(String.format("const liElements = $data.%s.explanations",SUBMIT_ERROR_DATA)); sb.append("\n\t\t"); sb.append(".filter(cause => cause !== null)"); sb.append("\n\t\t"); sb.append(".map(cause => \"<li>\" + cause + \"</li>\")"); sb.append("\n\t\t"); sb.append(".join('');"); sb.append("\n\t"); sb.append("if(liElements){"); sb.append("\n\t\t"); sb.append("return \"<ul>\" + liElements + \"</ul>\";"); sb.append("\n\t"); sb.append("}"); sb.append("\n"); sb.append("}"); return new Data(EXPRESSION,sb.toString()); }  @Override String name(); @Override Data create(); static final String NAME; static final String SUBMIT_ERROR_DATA; }
@Test public void should_generate_a_script_to_create_errors_list() throws Exception { SubmitErrorsListData submitErrorsListData = new SubmitErrorsListData(); assertThat(submitErrorsListData.create().getValue()).isEqualTo("if($data.formOutput && $data.formOutput._submitError && $data.formOutput._submitError.explanations){\n" + "\tconst liElements = $data.formOutput._submitError.explanations\n" + "\t\t.filter(cause => cause !== null)\n" + "\t\t.map(cause => \"<li>\" + cause + \"</li>\")\n" + "\t\t.join('');\n" + "\tif(liElements){\n" + "\t\treturn \"<ul>\" + liElements + \"</ul>\";\n" + "\t}\n" + "}"); }