Skip to content
// EFFECT DAYS 2026

TestGlobalEvents unstable

A fake HtmlSubscription.GlobalEvents for whole-app tests, so a test fires document and window events without a document, in Node too.

Provide layer next to TestHost.layer(), and fire an event with fire, or a custom one with fireCustomEvent. Every listener the mounted components' HtmlSubscription.onDocument, onCustomEvent and onWindow added runs, as the browser would run them. Both return whether one cancelled the default. Run TestHost.settle after it to see the update.

Example Pressing Escape in a whole-app test

import { UiSchema } from "effect/ui"
import { Html, HtmlSubscription } from "effect/ui/html"
import { TestGlobalEvents } from "effect/ui/html/testing"
import { TestHost } from "effect/ui/testing"
import { Effect, Layer, Schema } from "effect"
const Message = UiSchema.TaggedEnum({ PressedEscape: {} })
const Menu = Html.component({
name: "Menu",
model: Schema.Boolean,
message: Message
}).make({
init: () => true,
update: { PressedEscape: () => false },
view: (h, open) => h.p(open ? "open" : "closed"),
subscriptions: ({ model: open }) => [
open && HtmlSubscription.onDocument("keydown", ({ key }) =>
key === "Escape" ? Message.PressedEscape() : undefined
)
]
})
Effect.gen(function*() {
const root = yield* TestHost.mount(Menu, {})
yield* TestHost.settle
yield* TestGlobalEvents.fire("document", "keydown", { key: "Escape" })
yield* TestHost.settle
root.model() // => false
}).pipe(Effect.provide(Layer.merge(TestHost.layer(), TestGlobalEvents.layer)), Effect.scoped, Effect.runPromise)
7 exports Since v4.2.0 Source

Constructors

make

unstable
Since v4.2.0

Makes a fake source and its control. layer provides a new one per build.

Signature

declare function make(): GlobalEvents & TestGlobalEvents

Layers

layer

unstable
Since v4.2.0

Provides a fake HtmlSubscription.GlobalEvents with its control, TestGlobalEvents.

Signature

declare const layer: Layer.Layer<TestGlobalEvents>

Models

Targets

interface unstable
Since v4.2.0

The events fire takes for each target, with their payloads.

Signature

interface Targets {
readonly document: DocumentEvents;
readonly window: WindowEvents;
}

Services

Since v4.2.0

The TestGlobalEvents service.

Signature

declare const TestGlobalEvents: Service<TestGlobalEvents, TestGlobalEvents>

TestGlobalEvents

interface unstable
Since v4.2.0

The TestGlobalEvents service: the fake's control.

Signature

interface TestGlobalEvents {
readonly fire: <T extends keyof Targets, K extends string | number | symbol>(target: T, event: K, payload?: Partial<GlobalPayload<Targets[T][K]>>) => boolean;
readonly fireCustomEvent: (event: string, payload?: Partial<GlobalPayload<CustomEventPayload>>) => boolean;
}

Testing

fire

unstable
Since v4.2.0

Fires event on target through the fake, and returns whether a listener cancelled the default.

Signature

declare function fire<T extends keyof Targets, K extends string | number | symbol>(target: T, event: K, payload?: Partial<GlobalPayload<Targets[T][K]>>): Effect<boolean, never, TestGlobalEvents>
Since v4.2.0

Fires a custom event on the document through the fake, and returns whether a listener cancelled the default.

Signature

declare function fireCustomEvent(event: string, payload?: Partial<GlobalPayload<CustomEventPayload>>): Effect<boolean, never, TestGlobalEvents>