__tests__/vnode.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
const h3 = require("../h3.js").default; describe("VNode", () => { it("should provide a from method to initialize itself from an object", () => { const fn = () => true; const obj = { id: "test", type: "input", value: "AAA", $html: "", data: { a: "1", b: "2" }, eventListeners: { click: fn }, children: [], attributes: { title: "test" }, classList: ["a1", "a2"], style: "padding: 2px", }; const vnode1 = h3("br"); vnode1.from(obj); const vnode2 = h3("input#test.a1.a2", { value: "AAA", $html: "", data: { a: "1", b: "2" }, onclick: fn, title: "test", style: "padding: 2px", }); expect(vnode1).toEqual(vnode2); }); it("should provide a render method able to render textual nodes", () => { const createTextNode = jest.spyOn(document, "createTextNode"); const vnode = h3({ type: "#text", value: "test" }); const node = vnode.render(); expect(createTextNode).toHaveBeenCalledWith("test"); expect(node.constructor).toEqual(Text); }); it("should provide a render method able to render simple element nodes", () => { const createElement = jest.spyOn(document, "createElement"); const vnode = h3("br"); const node = vnode.render(); expect(createElement).toHaveBeenCalledWith("br"); expect(node.constructor).toEqual(HTMLBRElement); }); it("should provide a render method able to render element nodes with attributes and classes", () => { const createElement = jest.spyOn(document, "createElement"); const vnode = h3("span.test1.test2", { title: "test", falsy: false }); const node = vnode.render(); expect(createElement).toHaveBeenCalledWith("span"); expect(node.constructor).toEqual(HTMLSpanElement); expect(node.getAttribute("title")).toEqual("test"); expect(node.classList.value).toEqual("test1 test2"); }); it("should provide a render method able to render element nodes with children", () => { const vnode = h3("ul", [h3("li", "test1"), h3("li", "test2")]); const createElement = jest.spyOn(document, "createElement"); const node = vnode.render(); expect(createElement).toHaveBeenCalledWith("ul"); expect(createElement).toHaveBeenCalledWith("li"); expect(node.constructor).toEqual(HTMLUListElement); expect(node.childNodes.length).toEqual(2); expect(node.childNodes[1].constructor).toEqual(HTMLLIElement); expect(node.childNodes[0].childNodes[0].data).toEqual("test1"); }); it("should handle boolean attributes when redrawing", () => { const vnode1 = h3("input", { type: "checkbox", checked: true }); const node = vnode1.render(); expect(node.checked).toEqual(true); const vnode = h3("input", { type: "checkbox", checked: false }); vnode1.redraw({ node, vnode }); expect(node.checked).toEqual(false); }); it("should provide a render method able to render element nodes with a value", () => { const vnode = h3("input", { value: "test" }); const createElement = jest.spyOn(document, "createElement"); const node = vnode.render(); expect(createElement).toHaveBeenCalledWith("input"); expect(node.constructor).toEqual(HTMLInputElement); expect(node.value).toEqual("test"); }); it("should provide a render method able to render element nodes with event handlers", () => { const handler = () => { console.log("test"); }; const vnode = h3("button", { onclick: handler }); const button = document.createElement("button"); const createElement = jest .spyOn(document, "createElement") .mockImplementationOnce(() => { return button; }); const addEventListener = jest.spyOn(button, "addEventListener"); const node = vnode.render(); expect(createElement).toHaveBeenCalledWith("button"); expect(node.constructor).toEqual(HTMLButtonElement); expect(addEventListener).toHaveBeenCalledWith("click", handler); }); it("it should provide a render method able to render elements with special attributes", () => { const vnode = h3("div", { id: "test", style: "margin: auto;", data: { test: "aaa" }, $html: "<p>Hello!</p>", }); const createElement = jest.spyOn(document, "createElement"); const node = vnode.render(); expect(createElement).toHaveBeenCalledWith("div"); expect(node.constructor).toEqual(HTMLDivElement); expect(node.style.cssText).toEqual("margin: auto;"); expect(node.id).toEqual("test"); expect(node.dataset["test"]).toEqual("aaa"); expect(node.childNodes[0].textContent).toEqual("Hello!"); }); it("should provide a redraw method that is able to add new DOM nodes", () => { const oldvnode = h3("div#test", h3("span")); const newvnodeNoChildren = h3("div"); const newvnode = h3("div", [h3("span#a"), h3("span")]); const node = oldvnode.render(); const span = node.childNodes[0]; oldvnode.redraw({ node: node, vnode: newvnodeNoChildren }); expect(oldvnode.children.length).toEqual(0); oldvnode.redraw({ node: node, vnode: newvnode }); expect(oldvnode).toEqual(newvnode); expect(oldvnode.children.length).toEqual(2); expect(node.childNodes.length).toEqual(2); expect(node.childNodes[0].id).toEqual("a"); expect(span).toEqual(node.childNodes[1]); }); it("should provide a redraw method that is able to remove existing DOM nodes", () => { let oldvnode = h3("div", [h3("span#a"), h3("span")]); let newvnode = h3("div", [h3("span")]); let node = oldvnode.render(); oldvnode.redraw({ node: node, vnode: newvnode }); expect(oldvnode).toEqual(newvnode); expect(oldvnode.children.length).toEqual(1); expect(node.childNodes.length).toEqual(1); oldvnode = h3("div.test-children", [h3("span.a"), h3("span.b")]); node = oldvnode.render(); newvnode = h3("div.test-children", [h3("div.c")]); oldvnode.redraw({ node: node, vnode: newvnode }); expect(oldvnode).toEqual(newvnode); expect(oldvnode.children.length).toEqual(1); expect(node.childNodes.length).toEqual(1); expect(oldvnode.children[0].classList[0]).toEqual("c"); }); it("should provide a redraw method that is able to figure out differences in children", () => { const oldvnode = h3("div", [h3("span", "a"), h3("span"), h3("span", "b")]); const newvnode = h3("div", [ h3("span", "a"), h3("span", "c"), h3("span", "b"), ]); const node = oldvnode.render(); oldvnode.redraw({ node: node, vnode: newvnode }); expect(node.childNodes[1].textContent).toEqual("c"); }); it("should provide a redraw method that is able to figure out differences in existing children", () => { const oldvnode = h3("div", [ h3("span.test", "a"), h3("span.test", "b"), h3("span.test", "c"), ]); const newvnode = h3("div", [ h3("span.test", "a"), h3("span.test1", "b"), h3("span.test", "c"), ]); const node = oldvnode.render(); oldvnode.redraw({ node: node, vnode: newvnode }); expect(node.childNodes[0].classList[0]).toEqual("test"); expect(node.childNodes[1].classList[0]).toEqual("test1"); expect(node.childNodes[2].classList[0]).toEqual("test"); }); it("should provide a redraw method that is able to update different attributes", () => { const oldvnode = h3("span", { title: "a", something: "b" }); const newvnode = h3("span", { title: "b", id: "bbb" }); const node = oldvnode.render(); oldvnode.redraw({ node: node, vnode: newvnode }); expect(oldvnode).toEqual(newvnode); expect(node.getAttribute("title")).toEqual("b"); expect(node.getAttribute("id")).toEqual("bbb"); expect(node.hasAttribute("something")).toEqual(false); }); it("should provide a redraw method that is able to update different classes", () => { const oldvnode = h3("span.a.b", { title: "b" }); const newvnode = h3("span.a.c", { title: "b" }); const node = oldvnode.render(); oldvnode.redraw({ node: node, vnode: newvnode }); expect(oldvnode).toEqual(newvnode); expect(node.classList.value).toEqual("a c"); }); it("should provide redraw method to detect changed nodes if they have different elements", () => { const oldvnode = h3("span.c", { title: "b" }); const newvnode = h3("div.c", { title: "b" }); const container = document.createElement("div"); const node = oldvnode.render(); container.appendChild(node); oldvnode.redraw({ node: node, vnode: newvnode }); expect(node).not.toEqual(container.childNodes[0]); expect(node.constructor).toEqual(HTMLSpanElement); expect(container.childNodes[0].constructor).toEqual(HTMLDivElement); }); it("should provide redraw method to detect position changes in child nodes", () => { const v1 = h3("ul", [h3("li.a"), h3("li.b"), h3("li.c"), h3("li.d")]); const v2 = h3("ul", [h3("li.c"), h3("li.b"), h3("li.a"), h3("li.d")]); const n = v1.render(); expect(n.childNodes[0].classList[0]).toEqual("a"); v1.redraw({ node: n, vnode: v2 }); expect(n.childNodes[0].classList[0]).toEqual("c"); }); it("should provide redraw method to detect changed nodes if they have different node types", () => { const oldvnode = h3("span.c", { title: "b" }); const newvnode = h3({ type: "#text", value: "test" }); const container = document.createElement("div"); const node = oldvnode.render(); container.appendChild(node); expect(node.constructor).toEqual(HTMLSpanElement); oldvnode.redraw({ node: node, vnode: newvnode }); expect(node).not.toEqual(container.childNodes[0]); expect(container.childNodes[0].data).toEqual("test"); }); it("should provide redraw method to detect changed nodes if they have different text", () => { const oldvnode = h3({ type: "#text", value: "test1" }); const newvnode = h3({ type: "#text", value: "test2" }); const container = document.createElement("div"); const node = oldvnode.render(); container.appendChild(node); expect(node.data).toEqual("test1"); oldvnode.redraw({ node: node, vnode: newvnode }); expect(container.childNodes[0].data).toEqual("test2"); }); it("should provide redraw method to detect changed nodes and recurse", () => { const oldvnode = h3("ul.c", { title: "b" }, [ h3("li#aaa"), h3("li#bbb"), h3("li#ccc"), ]); const newvnode = h3("ul.c", { title: "b" }, [h3("li#aaa"), h3("li#ccc")]); const node = oldvnode.render(); oldvnode.redraw({ node: node, vnode: newvnode }); expect(oldvnode).toEqual(newvnode); expect(node.childNodes.length).toEqual(2); expect(node.childNodes[0].getAttribute("id")).toEqual("aaa"); expect(node.childNodes[1].getAttribute("id")).toEqual("ccc"); }); it("should provide a redraw method able to detect specific changes to style, data, value, attributes, $onrender and eventListeners", () => { const fn = () => false; const oldvnode = h3("input", { style: "margin: auto;", data: { a: 111, b: 222, d: 444 }, value: "Test...", title: "test", label: "test", onkeydown: () => true, onclick: () => true, onkeypress: () => true, }); const newvnode = h3("input", { style: false, data: { a: 111, b: 223, c: 333 }, title: "test #2", label: "test", placeholder: "test", onkeydown: () => true, onkeypress: () => false, $onrender: () => true, onhover: () => true, }); const container = document.createElement("div"); const node = oldvnode.render(); container.appendChild(node); oldvnode.redraw({ node: node, vnode: newvnode }); expect(oldvnode).toEqual(newvnode); expect(node.style.cssText).toEqual(""); expect(node.dataset["a"]).toEqual("111"); expect(node.dataset["c"]).toEqual("333"); expect(node.dataset["b"]).toEqual("223"); expect(node.dataset["d"]).toEqual(undefined); expect(node.getAttribute("title")).toEqual("test #2"); expect(node.getAttribute("placeholder")).toEqual("test"); expect(node.value).toEqual(""); }); it("should provide a redraw method able to detect changes in child content", () => { const v1 = h3("ul", [h3("li", "a"), h3("li", "b")]); const n1 = v1.render(); const v2 = h3("ul", { $html: "<li>a</li><li>b</li>", $onrender: (node) => node.classList.add("test"), }); const v3 = h3("ul", [h3("li", "a")]); const v4 = h3("ul", [h3("li", "b")]); const n2 = v2.render(); const n3 = v3.render(); expect(n2.childNodes[0].childNodes[0].data).toEqual( n1.childNodes[0].childNodes[0].data ); v1.redraw({ node: n1, vnode: v2 }); expect(n1.classList[0]).toEqual("test"); expect(v1).toEqual(v2); v3.redraw({ node: n3, vnode: v4 }); expect(v3).toEqual(v4); }); it("should execute $onrender callbacks whenever a child node is added to the DOM", async () => { let n; const $onrender = (node) => { n = node; }; const vn1 = h3("ul", [h3("li")]); const vn2 = h3("ul", [h3("li"), h3("li.vn2", { $onrender })]); const n1 = vn1.render(); vn1.redraw({ node: n1, vnode: vn2 }); expect(n.classList.value).toEqual("vn2"); const vn3 = h3("ul", [h3("span.vn3", { $onrender })]); vn1.redraw({ node: n1, vnode: vn3 }); expect(n.classList.value).toEqual("vn3"); const rc = () => h3("div.rc", { $onrender }); await h3.init(rc); expect(n.classList.value).toEqual("rc"); const rc2 = () => vn2; await h3.init(rc2); expect(n.classList.value).toEqual("vn2"); }); }); |