forked from thi-ng/umbrella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
130 lines (108 loc) · 2.92 KB
/
index.ts
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
import { defAtom, defView } from "@thi.ng/atom";
import { derefContext } from "@thi.ng/hiccup";
import { map, range } from "@thi.ng/iterators";
import { group } from "@thi.ng/testament";
import * as assert from "assert";
import { normalizeTree } from "../src/index.js";
const _check = (a: any, b: any, ctx: any = null) =>
assert.deepStrictEqual(
normalizeTree({ ctx, keys: false, span: false }, a),
b
);
const check = (id: string, a: any, b: any) => ({ [id]: () => _check(a, b) });
group("hdom", {
...check("undefined", undefined, undefined),
...check("null", null, undefined),
...check("empty tree", [], undefined),
...check("simple div", ["div", "foo"], ["div", {}, "foo"]),
...check("emmet id", ["div#foo", "hi"], ["div", { id: "foo" }, "hi"]),
...check(
"emmet id + id attr",
["div#foo", { id: "bar" }],
["div", { id: "foo" }]
),
...check(
"emmet id + class",
["div#id.foo.bar", "hi"],
["div", { id: "id", class: "foo bar" }, "hi"]
),
...check(
"emmet class + class attr",
["div.foo.bar", { class: "baz" }],
["div", { class: "foo bar baz" }]
),
...check(
"emmet id + class + attrib",
["div#id.foo.bar", { extra: 23 }, "hi"],
["div", { id: "id", class: "foo bar", extra: 23 }, "hi"]
),
...check(
"emmet class merging (string)",
["div.foo", { class: "bar baz" }],
["div", { class: "foo bar baz" }]
),
...check(
"emmet class merging (obj)",
["div.foo", { class: { foo: false, bar: true } }],
["div", { class: "bar" }]
),
...check("root fn", () => ["div"], ["div", {}]),
...check(
"tag fn w/ args",
[(_: any, id: string, body: any) => ["div#" + id, body], "foo", "bar"],
["div", { id: "foo" }, "bar"]
),
...check(
"child fn",
["div", (x: any) => ["span", x]],
["div", {}, ["span", {}]]
),
...check(
"child arrays",
["section", [["div", "foo"], "bar"]],
["section", {}, ["div", {}, "foo"], "bar"]
),
...check(
"iterator",
["div", map((x) => [`div#id${x}`, x], range(3))],
[
"div",
{},
["div", { id: "id0" }, "0"],
["div", { id: "id1" }, "1"],
["div", { id: "id2" }, "2"],
]
),
...check("deref toplevel", defAtom(["a"]), ["a", {}]),
...check("deref child", ["a", defAtom(["b"])], ["a", {}, ["b", {}]]),
"life cycle": () => {
let src: any = { render: () => ["div", "foo"] };
let res: any = ["div", {}, ["span", {}, "foo"]];
res.__this = src;
res.__init = res.__release = undefined;
res.__args = [undefined];
assert.deepStrictEqual(normalizeTree({ keys: false }, [src]), res);
res = ["div", { key: "0" }, ["span", { key: "0-0" }, "foo"]];
res.__this = src;
res.__init = res.__release = undefined;
res.__args = [undefined];
assert.deepStrictEqual(normalizeTree({}, [src]), res);
},
"dyn context": () => {
assert.deepStrictEqual(
derefContext(
{
a: 23,
b: defAtom(42),
c: defView(defAtom({ foo: { bar: 66 } }), ["foo", "bar"]),
},
["a", "b", "c"]
),
{
a: 23,
b: 42,
c: 66,
}
);
},
});