-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdom_reg_test.mjs
226 lines (176 loc) · 5.57 KB
/
dom_reg_test.mjs
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
import './internal_test_init.mjs'
import * as t from '../test.mjs'
import * as l from '../lang.mjs'
import * as i from '../iter.mjs'
import * as dr from '../dom_reg.mjs'
import * as ds from '../dom_shim.mjs'
class Empty extends l.Emp {}
// `Reg` is tested below. This is a sanity check.
t.test(function test_cer() {l.reqInst(dr.Reg.main, dr.Reg)})
/*
`Reg..reg` is checked more thoroughly below.
This is a sanity check to verify that the global function uses
this on the default instance.
*/
t.test(function test_reg() {
class SomeDetails extends ds.global.HTMLDetailsElement {}
dr.reg(SomeDetails)
testCerMatch(dr.Reg.main, SomeDetails, `details`, `some-details`)
})
t.test(function test_CustomElementRegistry() {
const reg = new dr.CustomElementRegistry()
reg.define(`one-two`, Empty)
t.test(function test_invalid() {
t.throws(() => reg.define(), TypeError, `expected variant of isCustomName, got undefined`)
t.throws(() => reg.define(`one`, Empty), TypeError, `expected variant of isCustomName, got "one"`)
t.throws(() => reg.define(`one-two`, 10), TypeError, `expected variant of isCls, got 10`)
})
t.test(function test_redundant() {
t.throws(() => reg.define(`one-two`, l.nop), Error, `redundant registration of "one-two"`)
t.throws(() => reg.define(`two-three`, Empty), Error, `redundant registration of [function Empty]`)
})
t.test(function test_get() {
t.is(reg.get(`one-two`), Empty)
t.is(reg.get(`two-three`), undefined)
})
})
t.test(function test_Reg() {
t.test(function test_misc() {
const reg = new dr.Reg()
class SomeLink extends ds.global.HTMLAnchorElement {}
t.no(reg.hasCls(SomeLink))
t.no(reg.hasTag(`some-link`))
reg.reg(SomeLink)
testCerMatch(reg, SomeLink, `a`, `some-link`)
})
t.test(function test_with_localName() {
const reg = new dr.Reg()
class SomeLink extends ds.global.HTMLAnchorElement {
static customName = `my-link`
}
reg.reg(SomeLink)
testCerMatch(reg, SomeLink, `a`, `my-link`)
})
t.test(function test_reg() {
t.test(function test_multiple_sequential_regs() {
const reg = new dr.Reg()
class Details extends ds.global.HTMLDetailsElement {}
function test0() {
reg.reg(Details)
testCerMatch(reg, Details, `details`, `a-details`)
}
test0()
test0()
test0()
class SomeBtn extends ds.global.HTMLButtonElement {}
function test1() {
reg.reg(SomeBtn)
testCerMatch(reg, SomeBtn, `button`, `some-btn`)
}
test1()
test1()
test1()
class SubBtn123 extends SomeBtn {}
function test2() {
reg.reg(SubBtn123)
testCerMatch(reg, SubBtn123, `button`, `sub-btn123`)
}
test2()
test2()
test2()
})
t.test(function test_salting() {
const reg = new dr.Reg()
{
class SomeBtn extends ds.global.HTMLButtonElement {}
reg.reg(SomeBtn)
testCerMatch(reg, SomeBtn, `button`, `some-btn`)
}
{
class SomeBtn extends ds.global.HTMLButtonElement {}
reg.reg(SomeBtn)
testCerMatch(reg, SomeBtn, `button`, `some-btn-1`)
}
{
class SomeBtn extends ds.global.HTMLButtonElement {}
reg.reg(SomeBtn)
testCerMatch(reg, SomeBtn, `button`, `some-btn-2`)
}
})
})
t.test(function test_tag_ambiguity() {
const reg = new dr.Reg()
class HeadCell extends ds.global.HTMLTableCellElement {
static localName = `th`
}
class BodyCell extends ds.global.HTMLTableCellElement {
static localName = `td`
}
t.is(dr.ClsToTag.main.localName(HeadCell), undefined)
t.is(dr.ClsToTag.main.localName(BodyCell), undefined)
reg.reg(HeadCell)
reg.reg(BodyCell)
testCerMatch(reg, HeadCell, `th`, `head-cell`)
testCerMatch(reg, BodyCell, `td`, `body-cell`)
})
t.test(function test_setDefiner() {
const reg = new dr.Reg()
reg.setDefiner()
t.is(reg.definer, undefined)
class Cls0 extends ds.global.HTMLElement {}
class Cls1 extends ds.global.HTMLElement {}
class Cls2 extends ds.global.HTMLElement {}
reg.reg(Cls0)
reg.reg(Cls1)
t.eq(reg.tagToCls, i.mapOf(
`a-cls0`, Cls0,
`a-cls1`, Cls1,
))
t.eq(reg.clsToTag, i.mapOf(
Cls0, `a-cls0`,
Cls1, `a-cls1`,
))
class Definer extends Array {
define(tag, cls, opt) {
this.push([tag, cls, opt])
if (tag === `a-cls0`) reg.reg(Cls2)
}
}
const def = new Definer()
reg.setDefiner(def)
t.is(reg.definer, def)
t.eq(def, Definer.of(
[`a-cls0`, Cls0, undefined],
[`a-cls2`, Cls2, undefined],
[`a-cls1`, Cls1, undefined],
))
})
})
t.test(function test_MixReg() {
class SomeElem extends dr.MixReg(ds.global.HTMLElement) {
static customName = `elem-47bd69`
}
t.no(dr.Reg.main.hasCls(SomeElem))
t.no(dr.Reg.main.hasTag(`elem-47bd69`))
t.is(dr.Reg.main.clsTag(SomeElem), undefined)
t.is(dr.Reg.main.tagCls(`elem-47bd69`), undefined)
l.nop(new SomeElem())
t.ok(dr.Reg.main.hasCls(SomeElem))
t.ok(dr.Reg.main.hasTag(`elem-47bd69`))
t.is(dr.Reg.main.clsTag(SomeElem), `elem-47bd69`)
t.is(dr.Reg.main.tagCls(`elem-47bd69`), SomeElem)
l.nop(new SomeElem())
l.nop(new SomeElem())
})
/* Util */
function testCerMatch(reg, cls, local, custom) {
t.ok(reg.hasCls(cls))
t.ok(reg.hasTag(custom))
t.is(reg.clsTag(cls), custom)
t.is(reg.tagCls(custom), cls)
t.ok(l.hasOwn(cls, `localName`))
t.is(cls.localName, local)
t.ok(l.hasOwn(cls, `customName`))
t.is(cls.customName, custom)
}
if (import.meta.main) console.log(`[test] ok!`)