Play unstable
Play tests mount a component on TestHost and drive it as a user does,
with the Scene's steps, while the runtime runs: children are real,
Commands and Subscriptions run against the Layers the test provides, and
the test resolves nothing by hand.
Details
A Play is any test where effects run and aren't resolved by the test,
one component included. Write one for how components work together: a
child's real view, a Command's real implementation, a Subscription on the
clock, the Router, a Layer. Its file is X.play.test.ts. One instance on
its own is a Story or a Scene, which fail closer to the cause.
Every step is an Effect, one yield* per step. A step settles before it
acts, so it picks up a TestClock.adjust, a navigation or a
TestHost.setOnline the test made since the last one, and settles again
after, so the work it caused has run before the test moves on. expect
settles, then checks. A failed check is a defect, with the stack of the
step's call site. A crashed root, an unhandled child failure or query
problem fails the step with settle's typed error. Take an expected one
with TestHost.expectFailure or TestHost.expectProblem.
Of the Scene's acknowledgement rule only one part carries over: an
interaction no handler turned into a message must be followed by
expectIgnored(). Commands, events and sends need no acknowledgement,
since Layers and real components answered them.
TestHost never runs a mount's body, so mounted sends what it would
have, to any mount in the tree, a child's included. A mount nothing
answers doesn't fail the test.
Example Saving through a real Command
import { Command, Component, UiSchema } from "effect/ui"import { Locator, Play, TestHost } from "effect/ui/testing"import { Effect, Schema } from "effect"
const Message = UiSchema.TaggedEnum({ ClickedSave: {}, CompletedSave: {} })
class Save extends Command.Service<Save>()( "todos/Save", { args: {}, success: Message }) { static readonly layer = this.toLayer(() => Effect.succeed(Message.CompletedSave()))}
const Todo = Component.define({ name: "Todo", model: Schema.Boolean, message: Message}).make({ init: () => false, update: ($, message) => Message.match(message, { ClickedSave: () => $.run(Save.make({})), CompletedSave: () => true }), view: (h, saved) => h.el("button").on("click", Message.ClickedSave()).children(saved ? "saved" : "save")}).pipe(Component.provide(Save.layer))
Effect.gen(function*() { const play = yield* Play.make(Todo, {}) yield* play.click("save") yield* play.expect(Locator.tag("button")).toHaveText("saved")}).pipe(Effect.scoped, Effect.provide(TestHost.layer()), Effect.runPromise)Constructors
Returns an Effect that mounts start on TestHost and creates a Play
with custom host steps. Methods returned by steps replace any built-in
methods with the same name and should not rely on this.
Details
Build interactions with driver.step to settle between events, track
ignored interactions and report failures at the test's call site.
Call custom directly in the host module's constructor so the scope's
final ignored-interaction check also points to the test.
Use around to allocate state and provide host services for each run.
It wraps mounting and step creation inside the Case's layer, so both
see that layer's services. Have steps read state from those services
to keep repeated executions independent.
Signature
declare const custom: { <Steps extends object, S extends Start, RS = never, RA = never>(options: AroundOptions<Steps, S, RS, RA>): Effect<Omit<Play<any, any, any, any>, keyof Steps> & Steps, StepError, Scope | TestHost | RA | StartRequirements<S>>; <Steps extends object, S extends Start, RS = never>(options: CustomOptions<Steps, S, RS> & { readonly around?: never; }): Effect<Omit<Play<any, any, any, any>, keyof Steps> & Steps, StepError, Scope | TestHost | RS | StartRequirements<S>>;}Mounts a Case on TestHost, sends its messages, settles, and returns
a cursor over it, for the life of the current Scope.
Details
The root starts from the case's model when it has one, and the case's
layer is built in the current Scope and provided around the mount.
What the case still needs comes from the test's environment. Otherwise it
is make, end check included.
Signature
declare function fromCase<C extends AnyComponent, R>(c: Case<C, R>): Effect<Play<Model<C>, Message<C>, Events<C>, never>, StepError, Scope | TestHost | R>Mounts component on TestHost with props, settles, and returns a
cursor over it, for the life of the current Scope.
Details
props may be an Effect, such as Router.props(AppRoute) under
Router.memory, as with TestHost.mount. The Layers come from the
test's environment, TestHost.layer() included.
When the scope closes with a success, the end check settles once more, so
a crash, an unhandled child failure or a query problem after the last step
still fails the test, and so does an ignored interaction nothing
acknowledged. Work still asleep on
the clock or waiting outside the app doesn't fail it: read
play.root.pending() to assert on that. When the scope closes with a
failure the end check doesn't run.
Gotchas
Provide TestHost.layer() beneath the app's Layers, or a step can settle
before their fibers have run: see TestHost.layer.
Signature
declare function make<Name extends string, Props, Model, Msg, Events, R, V extends Elements, RP = never>(component: Members<Name, Props, Model, Msg, Events, R, V, any>, props: PropsInput<Props, never, RP>): Effect<Play<Model, Msg, Events, Props>, StepError, Scope | TestHost | R | RP>Errors
Models
What a step's body uses to act on the tree. The step counts as reached
once a send finds a handler or heard is called, and as handled once
one of them produced a message.
Signature
interface Action { find(locator: Locator): TestElement; heard(handled: boolean): void; send(node: TestNode, event: string, payload: unknown): Sent | undefined;}Options for wrapping each execution of custom. The wrapper
replaces step requirements RS with its own requirements RA.
Signature
interface AroundOptions<Steps extends object, S extends Start = Start, RS = never, RA = never> extends CustomOptions<Steps, S, RS> { readonly around: (run: Effect<Omit<Play<any, any, any, any>, keyof Steps> & Steps, StepError, RS>) => Effect<Omit<Play<any, any, any, any>, keyof Steps> & Steps, StepError, RA>;}The assertions Driver.expect makes from a host's matchers: one per
matcher, taking its arguments.
Signature
type Assertions<Matchers extends { [name: string]: (...args: Array<any>) => Matcher;}> = { [K in keyof Matchers]: (...args: Parameters<Matchers[K]>) => Effect.Effect<void, StepError> }The options of custom.
Signature
interface CustomOptions<Steps extends object, S extends Start = Start, RS = never> { readonly bubbles?: (event: string) => boolean; readonly caller: Function; readonly module: string; readonly refuse?: (element: TestElement, step: string) => string | undefined; readonly start: S & CheckStart<S>; readonly steps: (driver: Driver) => Steps | Effect<Steps, never, RS>;}Tree access, settling and assertion builders for custom host steps.
Signature
interface Driver { readonly root: TestRoot<any, any, any, any>; readonly settle: Effect<void, StepError>; expect<Matchers extends { [name: string]: (...args: Array<any>) => Matcher; }>(locator: Locator, matchers: Matchers): Expect & Assertions<Matchers>; step(name: string, body: (action: Action) => void | Iterable<void, any, any>): Effect<void, StepError>;}The assertions expect(locator) offers: the Scene's, each an Effect that
settles, then checks the elements locator matches on the tree. A
failure lists the tree's elements.
Signature
type Expect = Scene.Expect<Effect.Effect<void, StepError>>Checks matched elements and returns a failure reason or undefined on
success. root gives access to the whole tree.
Signature
type Matcher = (nodes: ReadonlyArray<Locator.Node>, root: Locator.Node) => string | undefinedA component with props supplied directly or by an Effect that cannot fail.
Signature
interface PlainStart<C extends Case.AnyComponent, RP = never> { readonly component: C; readonly props: PropsInput<Props<C>, never, RP>;}A cursor over a tree mounted on TestHost.
Details
An interaction settles, sends its events to the live tree as the host would, then settles again. It throws when its locator matches nothing or several elements, or when nothing handles its event, and fails when the interaction before it produced no message and nothing acknowledged that.
root is the TestRoot under the cursor, for model(), log(),
head(), emitted and the rest. TestHost.settle, setOnline and
expectProblem work beside a Play, since it's the same host.
Signature
interface Play<Model, Msg, Events, Props = never> { readonly root: TestRoot<Model, Msg, Events, Props>; click(target: string | Locator): Effect<void, StepError>; expect(locator: Locator): Expect; expectIgnored(): Effect<void>; expectMount(mount: Any): Effect<void, StepError>; expectMount(locator: Locator, mount: Any): Effect<void, StepError>; fire(locator: Locator, event: string, payload?: unknown): Effect<void, StepError>; mounted<D extends MountTarget>(target: D, ...result: MountedResult<D>): Effect<void, StepError>; mounted<D extends MountTarget>(locator: Locator, target: D, ...result: MountedResult<D>): Effect<void, StepError>;}What Action.send did: whether a handler produced a message, whether the
event's default was cancelled, and the element a pointerdown handler
captured the pointer on.
Signature
interface Sent { readonly captured: TestElement | undefined; readonly handled: boolean; readonly prevented: boolean;}What custom mounts: a Case, or a component and its props, as
make takes them. A starting Model, messages or a layer come with a
Case.
Signature
type Start = Case.Case<Case.AnyComponent, any> | PlainStart<Case.AnyComponent, any>Utility Types
The services mounting S needs: a Case's, or the component's and
those of an Effect for its props.
Signature
type StartRequirements<S> = S extends Case.Case<any, infer R> ? R : S extends { readonly component: infer C; readonly props: infer P;} ? Component.Requirements<C> | P extends Effect.Effect<any, any, infer RP> ? RP : never : never