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)Constructors
Layers
Models
Services
TestGlobalEvents
unstableThe TestGlobalEvents service.
Signature
declare const TestGlobalEvents: Service<TestGlobalEvents, TestGlobalEvents>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
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>fireCustomEvent
unstableFires 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>