Case unstable
A case is one state of a component, written as data: its props, and
optionally a Model to start from, messages to send and a Layer to provide.
The same case runs in a test, with Play.fromCase or HtmlPlay.fromCase,
and in a browser, with BrowserCase.mount or the workbench page of
DevToolsVite.plugin.
Example Three states of a todo row
import { Command, Component, UiSchema } from "effect/ui"import { Case, Locator, Play, TestHost } 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 }) }), view: (h, model) => h.el("button").on("click", Message.ClickedSave()).children( model.saved ? "saved" : h.isRunning(SaveTodo) ? "saving" : "save" )})
const SavePending = SaveTodo.toLayer(() => Effect.never)
const TodoItemCases = Case.make(TodoItem, { empty: { props: { id: "t1" }, layer: SavePending }, saving: { props: { id: "t1" }, messages: [Message.ClickedSave()], layer: SavePending }, saved: { props: { id: "t1" }, model: { saved: true }, layer: SavePending }})
Effect.gen(function*() { const play = yield* Play.fromCase(TodoItemCases.saving) yield* play.expect(Locator.tag("button")).toHaveText("saving")}).pipe(Effect.scoped, Effect.provide(TestHost.layer()), Effect.runPromise)Constructors
Defines the cases of component, one per key.
Details
Every case is checked against the component: its props, Model and
messages. A case's name is the component's name and its key, such as
TodoItem/saved.
Signature
declare function make<C extends AnyComponent, Cases extends Readonly<Record<string, Input<NoInfer<C>>>>>(component: C, cases: Cases): { [K in string | number | symbol]: Case<C, Requirements<C, Cases[K]>> }Guards
Models
Any component a case can show.
Signature
type AnyComponent = Component.Members<string, any, any, any, any, any, any, any> | Component.Members<string, any, any, never, any, any, any, any>One state of component, named <component name>/<key>.
Details
R is what the mount still needs: the component's requirements and those
of an Effect props, less what layer provides, plus what layer
needs.
Signature
interface Case<C extends AnyComponent, R = never> { readonly "~effect/ui/testing/Case": { readonly _R: Covariant<R>; }; readonly component: C; readonly layer?: Layer<any, never, any>; readonly messages?: readonly Array<Message<C>>; readonly model?: Model<C>; readonly name: string; readonly props: PropsOf<C>;}Props and optional starting state, messages and services for a case.
Details
props may be an Effect, as for TestHost.mount, and may be left out
when every prop is optional.
model starts the root from that Model: what init returned is dropped,
its Commands, events and sends included. A field set to undefined
counts as left out.
messages are sent to the root in order right after it mounts, in one
synchronous pass, as root.send does. They go through the real update,
and the Commands they run reach the Layers, so their results arrive only
after the whole list has been sent. List what comes from outside the
component: with SaveTodo stubbed to succeed, [ClickedSave, CompletedSaveTodo] delivers CompletedSaveTodo twice. To show a state
past a Command, start from model instead.
layer is provided around the mount, for stubs such as a Command that
never returns, and for what an Effect props needs, such as
Router.memory.
Signature
type Input<C extends AnyComponent> = {} extends Component.Props<C> ? { readonly props?: PropsOf<C>;} : { readonly props: PropsOf<C>;} & { readonly layer?: AnyLayer; readonly messages?: ReadonlyArray<Component.Message<C>>; readonly model?: Component.Model<C>;}Type IDs
Utility Types
What a case built from input still needs.
Signature
type Requirements<C extends AnyComponent, I> = I extends unknown ? Exclude<Component.Requirements<C> | I extends { readonly props?: infer P;} ? Effect.Services<P> : never, I extends { readonly layer: Layer.Layer<infer A, any, any>;} ? A : never> | I extends { readonly layer?: infer L;} ? LayerRequirements<L> : never : never