Skip to content

Commit

Permalink
feat: enable testing in Deno environment without dom polyfill pre-def…
Browse files Browse the repository at this point in the history
…ined
  • Loading branch information
kt3k committed Oct 21, 2024
1 parent b44a0b8 commit 7bead28
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,33 @@ sig.update(1)
sig.update(2)
```

## Write test of components

Use `@b-fuse/deno-dom` for polyfill `document` object. An example of basic test
case of a component looks like the below:

```ts
import { DOMParser } from "@b-fuze/deno-dom"
import { assertEquals } from "@std/assert"
import { type Context, mount, register } from "@kt3k/cell"

Deno.test("A test case of Component", () => {
function Component({ el }: Context) {
el.textContent = "a"
}

register(Component, "js-component")

globalThis.document = new DOMParser().parseFromString(
`<body><div class="js-component"></div></body>`,
"text/html",
// deno-lint-ignore no-explicit-any
) as any
mount()
assertEquals(document.body.firstChild?.textContent, "a")
})
```

## Prior art

- [capsule](https://github.com/capsidjs/capsule)
Expand Down
2 changes: 2 additions & 0 deletions dom_polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.

import { DOMParser } from "@b-fuze/deno-dom" // deno-lint-ignore no-explicit-any
;(globalThis as any).document = new DOMParser().parseFromString(
"<body></body>",
Expand Down
23 changes: 23 additions & 0 deletions import_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.

import { DOMParser } from "@b-fuze/deno-dom"
import { assertEquals } from "@std/assert"
import { type Context, mount, register } from "./mod.ts"

function Component({ el }: Context) {
el.textContent = "a"
}

Deno.test("register doesn't throw without polyfill", () => {
register(Component, "js-component")
})

Deno.test("registered compenent can be mounted", () => {
globalThis.document = new DOMParser().parseFromString(
`<body><div class="js-component"></div></body>`,
"text/html",
// deno-lint-ignore no-explicit-any
) as any
mount()
assertEquals(document.body.firstChild?.textContent, "a")
})
4 changes: 3 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Cell v0.7.5 | Copyright 2024 Yoshiya Hinosawa and Capsule contributors | MIT license */
/*! Cell v0.7.5 | Copyright 2022-2024 Yoshiya Hinosawa and Capsule contributors | MIT license */
import type { GroupSignal, Signal } from "@kt3k/signal"
import { documentReady, logEvent } from "./util.ts"
export { GroupSignal, Signal } from "@kt3k/signal"
Expand Down Expand Up @@ -325,6 +325,8 @@ export function register<EL extends HTMLElement>(

registry[name] = initializer

if (!globalThis.document) return

if (document.readyState === "complete") {
mount()
} else {
Expand Down
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Yoshiya Hinosawa. All rights reserved. MIT license.
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.

import { assert, assertEquals, assertExists, assertThrows } from "@std/assert"
import { delay } from "@std/async"
Expand Down
2 changes: 1 addition & 1 deletion util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Yoshiya Hinosawa. All rights reserved. MIT license.
// Copyright 2022-2024 Yoshiya Hinosawa. All rights reserved. MIT license.

const READY_STATE_CHANGE = "readystatechange"

Expand Down

0 comments on commit 7bead28

Please sign in to comment.