all repos — h3 @ 81a9f748508237430b413be387d6dd51a7655681

A tiny, extremely minimalist JavaScript microframework.

__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
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
const mod = require("../h3.js");
const h3 = mod.h3;
const h = mod.h;

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: [],
            props: { title: "test" },
            classList: ["a1", "a2"],
            style: "padding: 2px",
        };
        const vnode1 = h("br");
        vnode1.from(obj);
        const vnode2 = h("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 = h({ 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 = h("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 props and classes", () => {
        const createElement = jest.spyOn(document, "createElement");
        const vnode = h("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 = h("ul", [h("li", "test1"), h("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 props when redrawing", () => {
        const vnode1 = h("input", { type: "checkbox", checked: true });
        const node = vnode1.render();
        expect(node.checked).toEqual(true);
        const vnode = h("input", { type: "checkbox", checked: false });
        vnode1.redraw({ node, vnode });
        expect(node.checked).toEqual(false);
    });

    it("should handle falsy props when redrawing", () => {
        const vnode1 = h("test-element", { q: 1});
        const node = vnode1.render();
        expect(node.q).toEqual(1);
        const vnode = h("test-element", { q: 0});
        vnode1.redraw({ node, vnode });
        expect(node.q).toEqual(0);
        expect(vnode1.props.q).toEqual(0);
    });

    it("should handle non-string props as properties and not create attributes", () => {
        const v = h("div", {
            test: true,
            obj: { a: 1, b: 2 },
            arr: [1, 2, 3],
            num: 2.3,
            str: "test",
            s: "",
            title: "testing!",
            value: false,
        });
        const v2 = h("div", {
            test: true,
            obj: { a: 1, b: 2 },
            arr: [1, 2, 3],
            s: "",
            title: "testing!",
            value: "true",
        });
        const n = v.render();
        expect(n.test).toEqual(true);
        expect(n.obj).toEqual({ a: 1, b: 2 });
        expect(n.arr).toEqual([1, 2, 3]);
        expect(n.num).toEqual(2.3);
        expect(n.str).toEqual("test");
        expect(n.getAttribute("str")).toEqual("test");
        expect(n.s).toEqual("");
        expect(n.getAttribute("s")).toEqual("");
        expect(n.title).toEqual("testing!");
        expect(n.getAttribute("title")).toEqual("testing!");
        expect(n.value).toEqual(undefined);
        expect(n.getAttribute("value")).toEqual(null);
        v.redraw({ node: n, vnode: v2 });
        expect(n.getAttribute("value")).toEqual("true");
        v2.value = null;
        v.redraw({ node: n, vnode: v2 });
        expect(n.getAttribute("value")).toEqual("");
    });

    it("should provide a render method able to render element nodes with a value", () => {
        let vnode = h("input", { value: "test" });
        const createElement = jest.spyOn(document, "createElement");
        let node = vnode.render();
        expect(createElement).toHaveBeenCalledWith("input");
        expect(node.constructor).toEqual(HTMLInputElement);
        expect(node.value).toEqual("test");
        vnode = h("input", { value: null });
        node = vnode.render();
        expect(node.value).toEqual("");
        vnode = h("test", { value: 123 });
        node = vnode.render();
        expect(node.getAttribute("value")).toEqual("123");
        expect(node.value).toEqual(undefined);
    });

    it("should provide a render method able to render element nodes with event handlers", () => {
        const handler = () => {
            console.log("test");
        };
        const vnode = h("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 props", () => {
        const vnode = h("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 = h("div#test", h("span"));
        const newvnodeNoChildren = h("div");
        const newvnode = h("div", [h("span#a"), h("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 = h("div", [h("span#a"), h("span")]);
        let newvnode = h("div", [h("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 = h("div.test-children", [h("span.a"), h("span.b")]);
        node = oldvnode.render();
        newvnode = h("div.test-children", [h("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 = h("div", [h("span", "a"), h("span"), h("span", "b")]);
        const newvnode = h("div", [
            h("span", "a"),
            h("span", "c"),
            h("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 = h("div", [
            h("span.test", "a"),
            h("span.test", "b"),
            h("span.test", "c"),
        ]);
        const newvnode = h("div", [
            h("span.test", "a"),
            h("span.test1", "b"),
            h("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 props", () => {
        const oldvnode = h("span", { title: "a", something: "b" });
        const newvnode = h("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 = h("span.a.b", { title: "b" });
        const newvnode = h("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 = h("span.c", { title: "b" });
        const newvnode = h("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 = h("ul", [h("li.a"), h("li.b"), h("li.c"), h("li.d")]);
        const v2 = h("ul", [h("li.c"), h("li.b"), h("li.a"), h("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 optimize insertion and deletions when redrawing if all old/new children exist", () => {
        const v = h("div", h("a"), h("d"));
        const vnode = h("div", h("a"), h("b"), h("c"), h("d"));
        const node = v.render();
        v.redraw({ node, vnode });
        expect(v.children.length).toEqual(4);
    });

    it("should provide redraw method to detect changed nodes if they have different node types", () => {
        const oldvnode = h("span.c", { title: "b" });
        const newvnode = h({ 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 = h({ type: "#text", value: "test1" });
        const newvnode = h({ 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 = h("ul.c", { title: "b" }, [
            h("li#aaa"),
            h("li#bbb"),
            h("li#ccc"),
        ]);
        const newvnode = h("ul.c", { title: "b" }, [h("li#aaa"), h("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, props, $onrender and eventListeners", () => {
        const fn = () => false;
        const oldvnode = h("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 = h("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 = h("ul", [h("li", "a"), h("li", "b")]);
        const n1 = v1.render();
        const v2 = h("ul", {
            $html: "<li>a</li><li>b</li>",
            $onrender: (node) => node.classList.add("test"),
        });
        const v3 = h("ul", [h("li", "a")]);
        const v4 = h("ul", [h("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 = h("ul", [h("li")]);
        const vn2 = h("ul", [h("li"), h("li.vn2", { $onrender })]);
        const n1 = vn1.render();
        vn1.redraw({ node: n1, vnode: vn2 });
        expect(n.classList.value).toEqual("vn2");
        const vn3 = h("ul", [h("span.vn3", { $onrender })]);
        vn1.redraw({ node: n1, vnode: vn3 });
        expect(n.classList.value).toEqual("vn3");
        const rc = () => h("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");
    });
});