-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
368 lines (322 loc) · 9.89 KB
/
test.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
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
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.
import { assert, assertEquals, assertExists, assertThrows } from "@std/assert"
import { delay } from "@std/async"
import "./dom_polyfill.ts"
import { type Context, mount, register, unmount } from "./mod.ts" // disable debug logs because it's too verbose for unit testing
// deno-lint-ignore no-explicit-any
;(globalThis as any).__DEV__ = false
Deno.test("Component body is called when the component is mounted", () => {
const name = randomName()
document.body.innerHTML = `<div class="${name}"></div>`
let called = false
function Component() {
called = true
}
register(Component, name)
assert(called)
})
Deno.test("onUnmount registers the event handler for the unmount event", () => {
const name = randomName()
document.body.innerHTML = `<div class="${name}"></div>`
let called = false
function Component({ onUnmount }: Context) {
onUnmount(() => {
console.log("onUnmount called")
called = true
})
}
register(Component, name)
mount()
assert(!called)
unmount(name, document.body.firstChild!)
assert(called)
})
Deno.test("unmount removes the event listeners", () => {
const name = randomName()
document.body.innerHTML = `<div class="${name}"></div>`
const div = queryByClass(name)
let count = 0
function Component({ on }: Context) {
on("my-event", (_e) => {
count++
})
}
register(Component, name)
assertEquals(count, 0)
div.dispatchEvent(new CustomEvent("my-event"))
assertEquals(count, 1)
div.dispatchEvent(new CustomEvent("my-event"))
assertEquals(count, 2)
unmount(name, div!)
div.dispatchEvent(new CustomEvent("my-event"))
assertEquals(count, 2)
})
Deno.test("on[event] is called when the event is dispatched", () => {
const name = randomName()
document.body.innerHTML = `<div class="${name}"></div>`
const div = queryByClass(name)
let called = false
function Component({ on }: Context) {
on("click", () => {
called = true
})
on("click", () => {
called = true
})
}
register(Component, name)
div.dispatchEvent(new Event("click"))
assert(called)
})
Deno.test("on(selector)[event] is called when the event is dispatched only under the selector", async () => {
const name = randomName()
document.body.innerHTML =
`<div class="${name}"><button class="btn1"></button><button class="btn2"></button></div>`
let onBtn1ClickCalled = false
let onBtn2ClickCalled = false
function Component({ on }: Context) {
on("click", ".btn1", () => {
onBtn1ClickCalled = true
})
on("click", ".btn2", () => {
onBtn2ClickCalled = true
})
}
register(Component, name)
const btn = queryByClass("btn1")
// FIXME(kt3k): workaround for deno_dom & deno issue
// deno_dom doesn't bubble event when the direct target dom doesn't have event handler
btn.addEventListener("click", () => {})
btn.dispatchEvent(new Event("click", { bubbles: true }))
await new Promise((r) => setTimeout(r, 100))
assert(onBtn1ClickCalled)
assert(!onBtn2ClickCalled)
})
Deno.test("on(option)[event] is called with option as AddEventListnerOptions", () => {
const name = randomName()
document.body.innerHTML = `<div class="${name}"></div>`
let count = 0
function Component({ on }: Context) {
on("click", { once: true }, (_e) => count++)
// for checking type
on("touchmove", { passive: false }, (_e) => {})
}
register(Component, name)
const div = queryByClass(name)
div.dispatchEvent(new Event("click"))
div.dispatchEvent(new Event("click"))
assertEquals(count, 1)
})
Deno.test("on.outside.event works", () => {
const name = randomName()
document.body.innerHTML =
`<div class="root"><div class="${name}"></div><div class="sibling"></div></div>`
let calledCount = 0
function Component({ onOutside }: Context) {
onOutside("click", () => {
calledCount++
})
}
register(Component, name)
assertEquals(calledCount, 0)
const sibling = queryByClass("sibling")!
// FIXME(kt3k): workaround for deno_dom & deno issue
// deno_dom doesn't bubble event when the direct target dom doesn't have event handler
sibling.addEventListener("click", () => {})
sibling.dispatchEvent(new Event("click", { bubbles: true }))
assertEquals(calledCount, 1)
const root = queryByClass("root")!
// FIXME(kt3k): workaround for deno_dom & deno issue
// deno_dom doesn't bubble event when the direct target dom doesn't have event handler
root.addEventListener("click", () => {})
root.dispatchEvent(new Event("click", { bubbles: true }))
assertEquals(calledCount, 2)
// checks if the event listener is removed after unmount
unmount(name, queryByClass(name))
sibling.dispatchEvent(new Event("click", { bubbles: true }))
root.dispatchEvent(new Event("click", { bubbles: true }))
assertEquals(calledCount, 2)
})
Deno.test("query, queryAll works", () => {
const name = randomName()
document.body.innerHTML = `
<div class="${name}">
<p>foo</p>
<p>bar</p>
<p>baz</p>
</div>
`
function Component({ query, queryAll }: Context) {
assert(query("p") !== null)
assertEquals(query("p")?.textContent, "foo")
assertEquals(queryAll("p")[0].textContent, "foo")
assertEquals(queryAll("p")[1].textContent, "bar")
assertEquals(queryAll("p")[2].textContent, "baz")
}
register(Component, name)
})
Deno.test("assign wrong type to on.event, on.outside.event, on(selector).event", () => {
function Component({ on, onOutside }: Context) {
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", "" as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", 1 as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", Symbol() as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", {} as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", [] as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", ".btn", "" as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", ".btn", 1 as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", ".btn", Symbol() as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", ".btn", {} as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on("click", ".btn", [] as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
onOutside("click", "" as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
onOutside("click", 1 as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
onOutside("click", Symbol() as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
onOutside("click", {} as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
onOutside("click", [] as any)
})
}
register(Component, randomName())
})
Deno.test("wrong event type call throws", () => {
const name = randomName()
document.body.innerHTML = `<div class="${name}"><div>`
function Component({ on }: Context) {
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on(1 as any, () => {})
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on(1n as any, () => {})
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
on(1n as any, () => {})
})
}
register(Component, name)
})
Deno.test("register() throws with non string input", () => {
function Component() {}
assertThrows(() => {
// deno-lint-ignore no-explicit-any
register(Component, 1 as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
register(Component, 1n as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
register(Component, Symbol() as any)
})
assertThrows(() => {
// empty name throws
register(Component, "")
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
register(Component, (() => {}) as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
register(Component, {} as any)
})
assertThrows(() => {
// deno-lint-ignore no-explicit-any
register(Component, [] as any)
})
})
Deno.test("register() throws with already registered name", () => {
const name = randomName()
function Component() {}
register(Component, name)
assertThrows(() => {
register(Component, name)
})
})
Deno.test("unmount() with non registered name throws", () => {
assertThrows(() => {
unmount(randomName(), document.body)
})
})
Deno.test("mount() throws with unregistered name", () => {
assertThrows(() => {
mount(randomName())
})
})
// test utils
const randomName = () => "c-" + Math.random().toString(36).slice(2)
const queryByClass = (name: string) => {
const el = document.querySelector<HTMLElement>(`.${name}`)
assertExists(el)
return el
}
Deno.test("Component with narrower HTML element type works", () => {
function Component({ el }: Context<HTMLDivElement>) {
el.classList.add("foo")
}
register(Component, randomName())
})
Deno.test("Returned string from Component is rendered", () => {
const name = randomName()
document.body.innerHTML = `<div class="${name}"><div>`
function Component() {
return "<p>hello</p>"
}
register(Component, name)
assertEquals(queryByClass(name).innerHTML, "<p>hello</p>")
})
Deno.test("Resolved string from Component is rendered as innerHTML", async () => {
const name = randomName()
document.body.innerHTML = `<div class="${name}"><div>`
function Component() {
return Promise.resolve("<p>hello</p>")
}
register(Component, name)
await delay(100)
assertEquals(queryByClass(name).innerHTML, "<p>hello</p>")
})