|
1 | 1 | package com.contentstack.utils;
|
2 | 2 |
|
3 | 3 | import com.contentstack.utils.helper.Metadata;
|
| 4 | +import com.contentstack.utils.interfaces.NodeCallback; |
4 | 5 | import com.contentstack.utils.render.DefaultOption;
|
| 6 | + |
| 7 | + |
5 | 8 | import org.json.JSONObject;
|
| 9 | +import org.json.JSONArray; |
6 | 10 | import org.jsoup.nodes.Attributes;
|
7 | 11 | import org.junit.Assert;
|
8 | 12 | import org.junit.BeforeClass;
|
9 | 13 | import org.junit.FixMethodOrder;
|
10 | 14 | import org.junit.Test;
|
11 | 15 | import org.junit.runners.MethodSorters;
|
12 | 16 |
|
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | + |
13 | 19 | import java.io.IOException;
|
14 | 20 | import java.util.logging.Level;
|
15 | 21 | import java.util.logging.Logger;
|
@@ -82,5 +88,99 @@ public void testEmbeddedDefaultDisplayable() {
|
82 | 88 | String result = defaultOptions.renderOptions(localJsonObj.optJSONObject("_embedded_items"), metadata);
|
83 | 89 | Assert.assertEquals("<img src=\"\" alt=\"\" />", result);
|
84 | 90 | }
|
| 91 | + @Test |
| 92 | + public void testRenderNodeWithVoidTd() { |
| 93 | + DefaultOption defaultOptions = new DefaultOption(); |
| 94 | + JSONObject nodeObject = new JSONObject(); |
| 95 | + JSONObject attrs = new JSONObject(); |
| 96 | + attrs.put("void", true); |
| 97 | + nodeObject.put("attrs", attrs); |
| 98 | + nodeObject.put("children", new JSONArray()); |
| 99 | + |
| 100 | + NodeCallback callback = children -> { |
| 101 | + // Simple callback implementation for testing purposes |
| 102 | + StringBuilder sb = new StringBuilder(); |
| 103 | + for (int i = 0; i < children.length(); i++) { |
| 104 | + sb.append(children.getJSONObject(i).getString("type")); |
| 105 | + } |
| 106 | + return sb.toString(); |
| 107 | + }; |
| 108 | + |
| 109 | + String result = defaultOptions.renderNode("td", nodeObject, callback); |
| 110 | + Assert.assertEquals("", result); |
| 111 | + } |
| 112 | + @Test |
| 113 | + public void testRenderNodeWithVoidTh() { |
| 114 | + DefaultOption defaultOptions = new DefaultOption(); |
| 115 | + JSONObject nodeObject = new JSONObject(); |
| 116 | + JSONObject attrs = new JSONObject(); |
| 117 | + attrs.put("void", true); |
| 118 | + nodeObject.put("attrs", attrs); |
| 119 | + nodeObject.put("children", new JSONArray()); |
| 120 | + |
| 121 | + NodeCallback callback = children -> { |
| 122 | + // Simple callback implementation for testing purposes |
| 123 | + StringBuilder sb = new StringBuilder(); |
| 124 | + for (int i = 0; i < children.length(); i++) { |
| 125 | + sb.append(children.getJSONObject(i).getString("type")); |
| 126 | + } |
| 127 | + return sb.toString(); |
| 128 | + }; |
| 129 | + |
| 130 | + String result = defaultOptions.renderNode("th", nodeObject, callback); |
| 131 | + Assert.assertEquals("", result); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testRenderNodeWithoutVoidTd() { |
| 136 | + DefaultOption defaultOptions = new DefaultOption(); |
| 137 | + JSONObject nodeObject = new JSONObject(); |
| 138 | + JSONObject attrs = new JSONObject(); |
| 139 | + attrs.put("class", "example"); |
| 140 | + nodeObject.put("attrs", attrs); |
| 141 | + JSONArray children = new JSONArray(); |
| 142 | + JSONObject child = new JSONObject(); |
| 143 | + child.put("type", "text"); |
| 144 | + child.put("content", "example content"); |
| 145 | + children.put(child); |
| 146 | + nodeObject.put("children", children); |
| 147 | + |
| 148 | + NodeCallback callback = childrenArray -> { |
| 149 | + StringBuilder sb = new StringBuilder(); |
| 150 | + for (int i = 0; i < childrenArray.length(); i++) { |
| 151 | + sb.append(childrenArray.getJSONObject(i).getString("content")); |
| 152 | + } |
| 153 | + return sb.toString(); |
| 154 | + }; |
| 155 | + |
| 156 | + String result = defaultOptions.renderNode("td", nodeObject, callback); |
| 157 | + Assert.assertEquals("<td class=\"example\">example content</td>", result); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testRenderNodeWithoutVoidTh() { |
| 162 | + DefaultOption defaultOptions = new DefaultOption(); |
| 163 | + JSONObject nodeObject = new JSONObject(); |
| 164 | + JSONObject attrs = new JSONObject(); |
| 165 | + attrs.put("class", "example"); |
| 166 | + nodeObject.put("attrs", attrs); |
| 167 | + JSONArray children = new JSONArray(); |
| 168 | + JSONObject child = new JSONObject(); |
| 169 | + child.put("type", "text"); |
| 170 | + child.put("content", "example content"); |
| 171 | + children.put(child); |
| 172 | + nodeObject.put("children", children); |
| 173 | + |
| 174 | + NodeCallback callback = childrenArray -> { |
| 175 | + StringBuilder sb = new StringBuilder(); |
| 176 | + for (int i = 0; i < childrenArray.length(); i++) { |
| 177 | + sb.append(childrenArray.getJSONObject(i).getString("content")); |
| 178 | + } |
| 179 | + return sb.toString(); |
| 180 | + }; |
| 181 | + |
| 182 | + String result = defaultOptions.renderNode("th", nodeObject, callback); |
| 183 | + Assert.assertEquals("<th class=\"example\">example content</th>", result); |
| 184 | + } |
85 | 185 |
|
86 | 186 | }
|
0 commit comments