-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsx.test.tsx
243 lines (188 loc) · 6.09 KB
/
jsx.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
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
/// <reference no-default-lib="true" />
/// <reference lib="DOM" />
/// <reference lib="deno.ns" />
/** @jsx h */
/** @jsxFrag Fragment */
import { h, render } from './jsx.ts'
import { Fragment } from './cmp.tsx'
import { useWait, useLink } from './use.ts'
import { delay } from './util.ts'
import './jsx.mock.ts'
import { assertEquals, assertThrows, assertStrictEquals } from "https://deno.land/std@0.120.0/testing/asserts.ts";
const { test } = Deno
test("render simple components", () => {
const StringComponent = ({ value }: { value: string }) => value
const BooleanComponent = ({ value }: { value: boolean }) => value
const NumberComponent = ({ value }: { value: number }) => value
const root = document.createElement("container")
render(root, (<>
<StringComponent value="hello" />
<BooleanComponent value={false} />
<NumberComponent value={12345} />
</>))
assertEquals(root.childNodes.length, 3)
assertEquals(root.childNodes[0].textContent, "hello")
assertEquals(root.childNodes[1].textContent, "false")
assertEquals(root.childNodes[2].textContent, "12345")
})
test("render static and plural components with socket", async () => {
const root = document.createElement("container")
const [lock, release] = useWait()
const [socket, plug] = useLink()
const clickHandler = async ({ target }: Event) => {
assertStrictEquals(target, await plug)
release()
}
render(root, (
<ul
style={{ borderStyle: "solid", borderColor: "black", borderWidth: "1px" }}
socket={socket} onClick={clickHandler}
>
<li class={["primary", "selected"]}>item 0</li>
<li class="secondary">item 1</li>
<li class="tertiary">item 2</li>
</ul>
))
assertEquals(root.childNodes.length, 1)
const ul = root.childNodes[0] as Element
assertEquals(ul.tagName, "UL")
assertEquals(ul.childNodes.length, 3)
assertEquals(ul.getAttribute("style"), "border-style: solid; border-color: black; border-width: 1px")
ul.dispatchEvent(new CustomEvent("click"))
await lock()
assertStrictEquals(ul, await plug)
const expectedClasses = ["primary selected", "secondary", "tertiary"]
for (let i = 0; i < 3; i++) {
const li = ul.childNodes[i] as Element
assertEquals(li.tagName, "LI")
assertEquals(li.textContent, `item ${i}`)
assertEquals(li.getAttribute("class"), expectedClasses[i])
}
})
test("render future component", async () => {
const root = document.createElement("container")
const FutureComponent = async ({ text, ms }: { text: string, ms: number }) => {
await delay(ms)
return <div>{text}</div>
}
render(root, <FutureComponent text="hello from future" ms={1} />)
assertEquals(root.childNodes.length, 0)
await delay()
assertEquals(root.childNodes.length, 1)
const div = root.childNodes[0] as Element
assertEquals(div.tagName, "DIV")
assertEquals(div.childNodes.length, 1)
assertEquals(div.childNodes[0].textContent, "hello from future")
})
test("render active component with return", async () => {
const root = document.createElement("container")
const ActiveComponent = async function* ({ lock, count }: { lock: () => Promise<void>, count: number }) {
let i
for (i = 0; i < count; i++) {
if (i % 2)
yield "odd"
else
yield
await lock()
}
return i
}
const [wait, next] = useWait()
render(root, <ActiveComponent lock={wait} count={10} />)
await delay()
let i
for (i = 0; i < 10; i++) {
if (i % 2) {
assertEquals(root.childNodes.length, 1)
assertEquals(root.childNodes[0].textContent, `odd`)
} else {
assertEquals(root.childNodes.length, 0)
}
next()
await delay()
}
await delay()
assertEquals(root.childNodes.length, 1)
assertEquals(root.childNodes[0].textContent, `${i}`)
next()
await delay()
assertEquals(root.childNodes.length, 1)
assertEquals(root.childNodes[0].textContent, `${i}`)
})
test("render active component without return", async () => {
const root = document.createElement("container")
const ActiveComponent = async function* ({ lock, count }: { lock: () => Promise<void>, count: number }) {
let i
for (i = 0; i < count; i++) {
yield i
await lock()
}
}
const [wait, next] = useWait()
render(root, <ActiveComponent lock={wait} count={10} />)
await delay()
let i
for (i = 0; i < 10; i++) {
assertEquals(root.childNodes.length, 1)
assertEquals(root.childNodes[0].textContent, `${i}`)
next()
await delay()
}
await delay()
assertEquals(root.childNodes.length, 0)
next()
await delay()
assertEquals(root.childNodes.length, 0)
})
test("unmount in active lifecycle", async () => {
const root = document.createElement("container")
const UnmountedComponent = () => (
<ul>
<li>this</li>
<li>should</li>
<li>not</li>
<li>be</li>
<li>visible</li>
</ul>
)
const ActiveComponent = async function* () {
yield <UnmountedComponent />
return "this should be visible"
}
render(root, <ActiveComponent />)
await delay()
assertEquals(root.childNodes.length, 1)
assertEquals(root.childNodes[0].textContent, "this should be visible")
})
// TODO: figure out why this doesn't work
test({
name: "unmount active in active",
ignore: true,
async fn() {
const root = document.createElement("container")
const ActiveChildElement = async function* () {
for (let i = 0; i < 10; i++) {
const result: boolean = yield i
await delay()
if (!result) return
}
}
const ActiveParentElement = async function* () {
yield <ActiveChildElement />
return "this should be visible"
}
render(root, <ActiveParentElement />)
await delay(100)
assertEquals(root.childNodes.length, 1)
assertEquals(root.childNodes[0].textContent, "this should be visible")
}
})
test("try render undefined", () => {
const root = document.createElement("container")
const WrongComponent = (): JSX.Element => undefined as unknown as JSX.Element
assertThrows(
() => render(root, <WrongComponent />),
Error,
'unknown element: type="undefined", data="undefined"'
)
})