Skip to content

Commit 7bead28

Browse files
committed
feat: enable testing in Deno environment without dom polyfill pre-defined
1 parent b44a0b8 commit 7bead28

6 files changed

Lines changed: 57 additions & 3 deletions

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,33 @@ sig.update(1)
174174
sig.update(2)
175175
```
176176

177+
## Write test of components
178+
179+
Use `@b-fuse/deno-dom` for polyfill `document` object. An example of basic test
180+
case of a component looks like the below:
181+
182+
```ts
183+
import { DOMParser } from "@b-fuze/deno-dom"
184+
import { assertEquals } from "@std/assert"
185+
import { type Context, mount, register } from "@kt3k/cell"
186+
187+
Deno.test("A test case of Component", () => {
188+
function Component({ el }: Context) {
189+
el.textContent = "a"
190+
}
191+
192+
register(Component, "js-component")
193+
194+
globalThis.document = new DOMParser().parseFromString(
195+
`<body><div class="js-component"></div></body>`,
196+
"text/html",
197+
// deno-lint-ignore no-explicit-any
198+
) as any
199+
mount()
200+
assertEquals(document.body.firstChild?.textContent, "a")
201+
})
202+
```
203+
177204
## Prior art
178205

179206
- [capsule](https://github.com/capsidjs/capsule)

dom_polyfill.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.
2+
13
import { DOMParser } from "@b-fuze/deno-dom" // deno-lint-ignore no-explicit-any
24
;(globalThis as any).document = new DOMParser().parseFromString(
35
"<body></body>",

import_test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.
2+
3+
import { DOMParser } from "@b-fuze/deno-dom"
4+
import { assertEquals } from "@std/assert"
5+
import { type Context, mount, register } from "./mod.ts"
6+
7+
function Component({ el }: Context) {
8+
el.textContent = "a"
9+
}
10+
11+
Deno.test("register doesn't throw without polyfill", () => {
12+
register(Component, "js-component")
13+
})
14+
15+
Deno.test("registered compenent can be mounted", () => {
16+
globalThis.document = new DOMParser().parseFromString(
17+
`<body><div class="js-component"></div></body>`,
18+
"text/html",
19+
// deno-lint-ignore no-explicit-any
20+
) as any
21+
mount()
22+
assertEquals(document.body.firstChild?.textContent, "a")
23+
})

mod.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! Cell v0.7.5 | Copyright 2024 Yoshiya Hinosawa and Capsule contributors | MIT license */
1+
/*! Cell v0.7.5 | Copyright 2022-2024 Yoshiya Hinosawa and Capsule contributors | MIT license */
22
import type { GroupSignal, Signal } from "@kt3k/signal"
33
import { documentReady, logEvent } from "./util.ts"
44
export { GroupSignal, Signal } from "@kt3k/signal"
@@ -325,6 +325,8 @@ export function register<EL extends HTMLElement>(
325325

326326
registry[name] = initializer
327327

328+
if (!globalThis.document) return
329+
328330
if (document.readyState === "complete") {
329331
mount()
330332
} else {

test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 Yoshiya Hinosawa. All rights reserved. MIT license.
1+
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.
22

33
import { assert, assertEquals, assertExists, assertThrows } from "@std/assert"
44
import { delay } from "@std/async"

util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 Yoshiya Hinosawa. All rights reserved. MIT license.
1+
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.
22

33
const READY_STATE_CHANGE = "readystatechange"
44

0 commit comments

Comments
 (0)