Skip to content

Commit

Permalink
chore: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed Nov 10, 2024
1 parent b5a65c5 commit 3eef60a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 3 deletions.
9 changes: 9 additions & 0 deletions @mizu/bind/mod_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,15 @@
</expect>
</test>

<test name="[:] with null object is effectless">
<render>
<p :="null"></p>
</render>
<expect>
<p></p>
</expect>
</test>

<test name="[:] (error) empty shorthand expects an object">
<render>
<p :="false"></p>
Expand Down
9 changes: 9 additions & 0 deletions @mizu/event/mod_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@
</script>
</test>

<test name="[@] with null object is effectless">
<render>
<p @="null"></p>
</render>
<expect>
<p></p>
</expect>
</test>

<test name="[@] (error) empty shorthand expects an object">
<render>
<p @="false"></p>
Expand Down
34 changes: 34 additions & 0 deletions @mizu/event/mod_test.ts
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
await import("@mizu/internal/testing").then(({ test }) => test(import.meta))
import { expect, fn, test, type testing } from "@libs/testing"
import { Window } from "@mizu/internal/vdom"
import { type Directive, Phase, Renderer } from "@mizu/internal/engine"
import directive from "./mod.ts"

test()("[@event] supports `_event` internal api", async () => {
await using window = new Window()
const tested = {
name: "~tested",
phase: Phase.TESTING,
init: directive.init,
execute: (renderer, element, options) => directive.execute(renderer, element, { ...options, _event: "testing" } as testing),
} as Directive
const renderer = await new Renderer(window, { directives: [tested] }).ready
const element = Object.assign(renderer.createElement("div", { attributes: { [`${tested.name}`]: "" } }), { addEventListener: fn() })
await renderer.render(element)
expect(element.addEventListener).toHaveBeenCalledWith("testing", expect.any(Function), expect.any(Object))
})

test()("[@event] supports `_callback` internal api", async () => {
await using window = new Window()
const callback = fn()
const tested = {
name: "~tested",
phase: Phase.TESTING,
init: directive.init,
execute: (renderer, element, options) => directive.execute(renderer, element, { ...options, _event: "testing", _callback: callback } as testing),
} as Directive
const renderer = await new Renderer(window, { directives: [tested] }).ready
const element = renderer.createElement("div", { attributes: { [`${tested.name}`]: "" } })
await renderer.render(element)
element.dispatchEvent(new renderer.window.Event("testing"))
expect(callback).toHaveBeenCalled()
})
1 change: 1 addition & 0 deletions @mizu/internal/engine/renderer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ for (
['and rounds values with `"ms"` unit', `*foo=".9ms"`, { value: 1 }, { type: Date }],
['and rounds values with `"s"` unit', `*foo=".9s"`, { value: 900 }, { type: Date }],
['and rounds values with `"m"` unit', `*foo=".9m"`, { value: 54_000 }, { type: Date }],
["and fallbacks to `0` on negative values", `*foo="-1"`, { value: 0 }, { type: Date }],
["and fallbacks to `0` on invalid values", `*foo="bar"`, { value: 0 }, { type: Date }],
["and fallbacks to `default` on invalid values", `*foo="bar"`, { value: 1 }, { type: Date, default: 1 }],
["and fallbacks on invalid units", `*foo="1xx"`, { value: 0 }, { type: Date }],
Expand Down
3 changes: 2 additions & 1 deletion @mizu/render/client/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type * from "./client.ts"
export default Client.default as Client

// Start the client-side renderer if this module is the main entry point
if ((import.meta.main) && (globalThis.window?.document)) {
// @ts-expect-error: iife handling
if ((globalThis.MIZU_IIFE) && (globalThis.window?.document)) {
Client.default.render()
}
15 changes: 14 additions & 1 deletion @mizu/render/client/mod_test.ts
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
import "./mod.ts"
import { expect, test, type testing } from "@libs/testing"
import { Window } from "@mizu/internal/vdom"

test()("`Client.render()` is automatically called in iife mode", async () => {
try {
await using window = new Window(`<body *mizu><p *text="'foo'"></p></body>`)
Object.assign(globalThis, { MIZU_IIFE: true, window })
await import("./mod.ts")
expect(window.document.querySelector("p")?.textContent).toBe("foo")
} finally {
delete (globalThis as testing).MIZU_IIFE
delete (globalThis as testing).window
}
})
2 changes: 1 addition & 1 deletion www/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function js(exported: string, options = {} as Pick<NonNullable<Arg<typeof
if (options?.format === "iife") {
options.raw ??= {}
options.raw.target = "es2020"
options.raw.define = { "import.meta.main": "true" }
options.raw.define = { "globalThis.MIZU_IIFE": "true" }
}
return bundle(new URL(url), { ...options, banner })
}
Expand Down

0 comments on commit 3eef60a

Please sign in to comment.