Scene unstable
Scene tests drive one component through its rendered view and assert on what it renders: pure, with no runtime, no Layers and no host.
A scene can only do what the view allows: a click reaches update only
through the handler the view put on the element. That makes a scene the
evidence for what the app does, where a Story is evidence about
update. A scene has no send and doesn't show the Model; it asserts
with expect(locator) on the view.
Commands stay pending as data until a step resolves them, as in a story,
and every step follows the acknowledgement rule described in Steps. An
interaction that no handler turned into a message must be followed by
expectIgnored(). A child is a leaf: childEmit stands in for what it
emits, and anything that crosses into one is a Play. A headless child's
parts render, but its update doesn't run, so a click on a part only the
child handles throws: test what the component does with the child's events
with childEmit, and that the part emits them in a Play. A child the
component binds with children takes child steps whether or not the
view places it. A child step
names the child by its component, then by its key when it has one:
childEmit(Listbox, event), childEmit(TodoItem, "t1", event).
Example Save, resolve its Command, see the result
import { Command, Component, UiSchema } from "effect/ui"import { Locator, Scene } from "effect/ui/testing"import { Effect, Schema } from "effect"
const Message = UiSchema.TaggedEnum({ ClickedSave: {}, CompletedSaveTodo: {} })
class SaveTodo extends Command.Service<SaveTodo>()( "todos/SaveTodo", { args: { id: Schema.String }, success: Message }) {}
const TodoItem = Component.define({ name: "TodoItem", props: Schema.Struct({ id: Schema.String }), model: Schema.Struct({ saved: Schema.Boolean }), message: Message}).make({ init: () => ({ saved: false }), update: ($, message) => Message.match(message, { ClickedSave: () => $.run(SaveTodo.make({ id: $.props.id })), CompletedSaveTodo: () => ({ saved: true }) }), // In a scene, `h.isRunning` is derived from the pending Commands. view: (h, model) => h.el("button").on("click", Message.ClickedSave()).children( model.saved ? "saved" : h.isRunning(SaveTodo) ? "saving" : "save" )})
Effect.gen(function*() { const s = yield* Scene.make(TodoItem, { id: "t1" }) s.click("save") .expect(Locator.text("saving")).toExist() .expectExact(SaveTodo.make({ id: "t1" })) .resolve(SaveTodo, Message.CompletedSaveTodo()) .expect(Locator.tag("button")).toHaveText("saved")}).pipe(Effect.scoped, Effect.runSync)Constructors
Starts a scene: init with props, as a cursor in a scope. The test
calls steps on the cursor and reads the view from it.
Details
When the scope closes with a success, the end check runs: it fails on
anything pending, kept or exempt included, on a query with no result
unless keepPending named it, on a mount not acknowledged, and on any
event, send or ignored interaction not acknowledged. When the scope closes
with a failure the end check doesn't run, so a failed test reports only
its own failure. it.effect from @effect/vitest provides the scope,
which closes at the end of the test.
Signature
declare const make: { <C extends AnyComponent>(component: C, props: Props<C>): Effect<Steps<C, never, never>, never, Scope>; <C extends AnyComponent, V extends Elements = never>(component: C, props: Props<C>, options: { readonly elements?: View.Vocabulary<V>; readonly slots: (h: View.WithTags<never, unknown, [V] extends [never] ? VocabularyOf<C> : V>) => { [K in Component.Slots<C>]: View.Input<never, any> }; }): Effect<Steps<C, never, never>, never, Scope>; <Props, Events, Parts, R, Rcv, V extends Elements = never, Rp = never>(component: Headless<Props, Events, Parts, R, Rcv, never, never, Rp>, props: Props, options: { readonly elements?: View.Vocabulary<V>; readonly view: (parts: Parts, h: View.WithTags<never, Props, V>) => View.Content<never, any>; }): Effect<Steps<Component<string, Props, any, any, Events, R, V, {}, never>, Parts, Rp>, never, Scope>;}The cursor after init. Its steps follow the acknowledgement rule, but
nothing checks what's pending or unacknowledged at the end.
Signature
declare const start: { <C extends AnyComponent>(component: C, props: Props<C>): Steps<C>; <Props, Events, Parts, R, Rcv, V extends Elements = never>(component: Headless<Props, Events, Parts, R, Rcv>, props: Props, options: { readonly elements?: View.Vocabulary<V>; readonly view: (parts: Parts, h: View.WithTags<never, Props, V>) => View.Content<never, any>; }): Steps<Component<string, Props, any, any, Events, R, V, {}, never>, Parts>;}Models
The assertions expect(locator) offers. Each one checks the elements
locator matches in the current view and returns the cursor. A failure
lists the view's elements.
Signature
interface Expect<S> { toBeAbsent(): S; toExist(): S; toHaveAttribute(name: string, value?: string | false | RegExp): S; toHaveAttributes(attributes: { [name: string]: string | false | RegExp; }): S; toHaveCount(count: number): S; toHaveText(text: string | RegExp): S;}A mount's definition, or an issued mount to also match the args.
Signature
type MountTarget = Mount.Definition.Any | Mount.AnyThe state of one instance between steps, as a scene records it: what the view shows, and no Model. Immutable: every step replaces the cursor's record with a new one.
Details
get, query and all run locators over the current view, as
TestHost's roots do.
Signature
interface Scene<Props, Model, Msg, Events, C = Shared.Record<Props, Model, Msg, Events>["component"]> extends Record<Props, Model, Msg, Events, C> { readonly all: (locator: Locator) => readonly Array<Node>; readonly defaultPrevented: boolean; readonly get: (locator: Locator) => Node; readonly query: (locator: Locator) => Node | undefined; readonly root: Node; readonly view: () => Node<Msg, any>;}A cursor over one scene. It has the steps every kind of test shares, from
Steps.Steps, and drives the instance through its view.
Details
Parts is a headless component's parts, in a scene that stands in a
view for one. A step shared by any component is written
<S extends Scene.Steps.Any>(s: S): S, which keeps the caller's type.
Signature
interface Steps<C extends AnyComponent, Parts = never, Report = never> extends Steps<C> { readonly current: Scene<Props<C>, Model<C>, Message<C>, Events<C>, RecordComponent<C>>; readonly defaultPrevented: boolean; readonly root: Node; readonly view: Node<Message<C>, any>; all(locator: Locator): readonly Array<Node>; childEmit<Child extends ChildTarget>(child: Child, event: EventsOf<Child>): this; childEmit<Child extends ChildTarget>(child: Child, key: string, event: EventsOf<Child>): this; childFailure(child: ChildTarget, cause: Cause<unknown>): this; childFailure(child: ChildTarget, key: string, cause: Cause<unknown>): this; childPending(child: ChildTarget): this; childPending(child: ChildTarget, key: string): this; click(target: string | Locator): this; collected(reports: readonly Array<Report>): this; expect(locator: Locator): Expect<Steps<C, Parts, Report>>; expectDefaultPrevented(expected?: boolean): this; expectHandled(): this; expectIgnored(): this; expectLazyReused(): this; expectMount(mount: Any): this; expectMount(locator: Locator, mount: Any): this; fire(locator: Locator, event: string, payload?: unknown): this; get(locator: Locator): Node; givenChild(child: Any, model: unknown): this; givenChild(child: Any, key: string, model: unknown): this; mounted<D extends MountTarget>(target: D, ...result: MountedResult<D>): this; mounted<D extends MountTarget>(locator: Locator, target: D, ...result: MountedResult<D>): this; parts(f: (parts: Parts) => void): this; query(locator: Locator): Node | undefined;}Other
Queries
The children declared in a view, optionally filtered by component or feature service.
Signature
declare function children<Msg>(view: Node<Msg, any>, component?: Any | { readonly key: string;}): readonly Array<Child<Msg, any>>What a child of child shows for result: its fallback evaluated with an
AsyncResult.Initial or AsyncResult.Failure, or undefined without one.
The child is the one without a key, or the one at key.
Throws, as the runtime fails the parent, if the fallback renders two
children at one path, or a child at the path of another child of
the view, closure or slot content that declared the child. It sees one fallback, so it can't check two shown at once, as the
runtime does.
Signature
declare const fallback: { <Msg>(view: Node<Msg, any>, child: ChildTarget, result: Failure<never, unknown> | Initial<never, unknown>): Node<Msg, any> | undefined; <Msg>(view: Node<Msg, any>, child: ChildTarget, key: string, result: Failure<never, unknown> | Initial<never, unknown>): Node<Msg, any> | undefined;}Utility Types
The result argument of the mounted step: the definition's own result,
and none for a mount that ends with Component.noop.
Signature
type MountedResult<D> = [Mount.Result<D>] extends [Component.Noop] ? [] : [result: Mount.Result<D>]