-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstamp.test.tsx
121 lines (110 loc) · 3.25 KB
/
stamp.test.tsx
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
import { JSDOM } from 'jsdom'
import { deepEqual, equal, match, ok } from 'node:assert/strict'
import { describe, it } from 'node:test'
import { NEVER, of } from 'rxjs'
import { Children, Fragment, jsx } from './jsx.js'
import { buildTree } from './static-dom.js'
import { buildStamp } from './stamp-builder.js'
import { type BindSelectors, collectBindings } from './stamp-collector.js'
import type { ElementBinds, NodeBinds } from './wiring-context.js'
describe('stamp', () => {
const { window } = new JSDOM()
const { document } = window
it('stamps a basic rebindable dynamic tree', () => {
const TestComponent = () => (
<p>
Hello <Children />
</p>
)
const tree = (
<div bind={{ className: of('test') }}>
<Fragment childrenBind={NEVER}>
<TestComponent>World</TestComponent>
<TestComponent>Friends</TestComponent>
</Fragment>
<Children />
</div>
)
const stamp = buildStamp(tree, document)
const container = document.createElement('div')
const { elementBinds, nodeBinds } = buildTree(
tree,
container,
undefined,
undefined,
undefined,
document,
)
const { nodeSelectors, elementSelectors } = collectBindings(tree)
ok(stamp.innerHTML.length > 0, 'produces html')
bindsEqual(
stamp,
container,
elementSelectors,
elementBinds,
nodeSelectors,
nodeBinds,
)
})
it('stamps an id selector', () => {
const tree = (
<div>
<div>
<div>
<h1 id="example" styleBind={{ color: of('red') }}>
Example
</h1>
</div>
</div>
</div>
)
const stamp = buildStamp(tree, document)
const container = document.createElement('div')
const { elementBinds, nodeBinds } = buildTree(
tree,
container,
undefined,
undefined,
undefined,
document,
)
const { nodeSelectors, elementSelectors } = collectBindings(tree)
ok(stamp.innerHTML.length > 0, 'produces html')
bindsEqual(
stamp,
container,
elementSelectors,
elementBinds,
nodeSelectors,
nodeBinds,
)
match(elementSelectors[0][0], /^h1#example/)
})
})
function bindsEqual(
stamp: HTMLTemplateElement,
container: HTMLDivElement,
elementSelectors: BindSelectors,
elementBinds: ElementBinds,
nodeSelectors: BindSelectors,
nodeBinds: NodeBinds,
) {
equal(stamp.content.childNodes.length, container.childNodes.length)
equal(elementSelectors.length, elementBinds.length)
equal(nodeSelectors.length, nodeBinds.length)
for (let i = 0; i < elementSelectors.length; i++) {
const [selector, selDesc] = elementSelectors[i]
const [element, bindDesc] = elementBinds[i]
const selElement = stamp.content.querySelector(selector)
deepEqual(selElement, element)
equal(selDesc, bindDesc)
}
for (let i = 0; i < nodeSelectors.length; i++) {
const [selector, selDesc] = nodeSelectors[i]
const [comment, bindDesc] = nodeBinds[i]
const selElement = stamp.content.querySelector(selector)
const selElementComment = selElement?.childNodes[0] as CharacterData
equal(selElementComment.data, (comment as CharacterData).data)
equal(selDesc, bindDesc)
}
}