Steps unstable
The steps every kind of test shares, and the data they work on.
A test kind's cursor, Story.Steps or Scene.Steps, extends
Steps.Steps: it starts from a Model, changes the props, settles Commands
and queries, and acknowledges what was emitted and sent. What each kind
adds on top is how a test drives the instance and what it can see. A helper
typed over Steps.Steps.Any works with any kind's cursor and keeps the
caller's type.
The acknowledgement rule
A test is the full account of what the component did, so a step throws when earlier work is still in flight or an earlier output hasn't been acknowledged:
- A message step (
subscription,layerFailed, Story'ssend, a Scene's interactions and child steps) throws while a Command or a load more is pending, or while an enabled query has no result at its args. A Command whose mode isswitchorqueuedoesn't block, nor does a stream Command, nor anythingkeepPendingnamed. - Every acting step (a message step, a settle step,
props,given) throws while an event or a send isn't acknowledged withemittedorsent, or, in a Scene, while an ignored interaction isn't acknowledged withexpectIgnored. make's end check, which runs when its scope closes, fails on anything pending, kept or exempt included, on a query with no result unless it was kept, and on any output not acknowledged. Astartcursor gets the checks before each step only.givenafter an acting step throws rather than discard pending work, a query with no result it would disable, or unacknowledged outputs. Before any acting step it replaces whatinitproduced.
Models
A query a Command.afterRefetch run waits for, by its name in
queries, at the args it returned when the run resolved. A queryMany
query has one per awaited key.
Signature
interface AwaitedQuery { readonly args: unknown; readonly name: string;}What expectBound expects of one children entry. A Component.child
entry is an object: key and props are checked when given. A
Component.childMany entry lists its keys in order, each a key or an
object that also checks its props.
Signature
type BoundExpected = { readonly key?: string; readonly props?: unknown;} | ReadonlyArray<string | { readonly key: string; readonly props?: unknown;}>One child a component binds with children, as a test records it: the
entry that binds it, the name of what it's bound to, its key, the path the
runtime starts it at and the props its binding passes.
Signature
interface BoundInfo { readonly child: string; readonly key: string; readonly name: string; readonly path: string; readonly props: unknown;}The query results given starts from, by query name: a Success value, a
infinite query's page array, or a keyed query's entries.
Signature
type Given<C> = { [K in keyof Component.Queries<C>]: Component.Queries<C>[K] extends Component.QueryMany<infer R> ? ReadonlyArray<KeyedLoad<R>> : Query.Query.Success<QueryOf<Component.Queries<C>[K]>> }One key of a queryMany query for loadedMany: its args and a Success
value or a typed failure.
Signature
type KeyedLoad<R extends Query.Query.Any> = { readonly args: Query.Query.Args<R>; readonly patched?: boolean; readonly value: Query.Query.Success<R>; readonly waiting?: boolean;} | { readonly args: Query.Query.Args<R>; readonly error: Query.Query.Error<R>; readonly waiting?: boolean;}A Command issued by the instance whose message hasn't been delivered.
Details
running is what the runtime would be executing; queued is waiting behind
a running run of the same group under mode: "queue". awaiting has
resolved or failed, and holds message: a Command.afterRefetch run
waits for the queries in awaiting, and under mode: "queue" a
message also waits for the earlier ones of its group.
Signature
interface Pending<Msg> { readonly awaiting?: readonly Array<AwaitedQuery>; readonly command: Issued<Noop | Msg, any, never, "Effect", never> | Issued<Noop | Msg, any, never, "Stream", never>; readonly message?: Noop | Msg; readonly status: "running" | "queued" | "awaiting";}One of a component's queries in a story: its name, its Query's name, and the args it returns
for the current Model and props, undefined when it is disabled. A
queryMany query's args are its unique args, in order.
Signature
interface QueryInfo { readonly args: unknown; readonly name: string; readonly query: string;}The state of one instance between steps that every kind of test records. Immutable: every step replaces the cursor's record with a new one. Each kind's record extends it with what that kind can see.
Signature
interface Record<Props, Model, Msg, Events, C = Component.Component<any, Props, Model, Msg, Events, any, any, any, never>> { readonly bound: () => readonly Array<BoundInfo>; readonly component: C; readonly emitted: readonly Array<Events>; readonly interrupted: readonly Array<Issued<Noop | Msg, any, never, "Effect", never> | Issued<Noop | Msg, any, never, "Stream", never>>; readonly loadingMore: readonly Array<string>; readonly pending: readonly Array<Pending<Msg>>; readonly props: Props; readonly queryInfo: () => readonly Array<QueryInfo>; readonly reports: { [name: string]: unknown; }; readonly sent: readonly Array<Send>; readonly subscriptions: () => readonly Array<string>;}The steps every kind of test shares. Each step runs as soon as it's
called, replaces current and returns the cursor, so steps chain. A step
that fails throws.
Details
The types come from the component: a cursor over Counter only takes
Counter's Model, Commands and queries. A step shared by any component,
and by any kind of test, is written <S extends Steps.Steps.Any>(s: S): S,
which keeps the caller's type, so the kind's own steps still chain after it.
Signature
interface Steps<C extends AnyComponent> extends Pipeable { readonly bound: readonly Array<BoundInfo>; readonly current: Record<Props<C>, Model<C>, Message<C>, Events<C>, RecordComponent<C>>; readonly interrupted: readonly Array<Issued<Noop | Message<C>, any, never, "Effect", never> | Issued<Noop | Message<C>, any, never, "Stream", never>>; readonly loadingMore: readonly Array<string>; readonly pending: readonly Array<Pending<Message<C>>>; readonly queryInfo: readonly Array<QueryInfo>; readonly subscriptions: readonly Array<string>; check(f: (current: Record<Props<C>, Model<C>, Message<C>, Events<C>, RecordComponent<C>>) => void): this; emitted(...events: readonly [Events<C>, Events<C>]): this; end<D extends StreamTarget>(target: D): this; expectBound(expected: { [name: string]: BoundExpected | undefined; }): this; expectExact(...targets: readonly Array<Target>): this; expectHas(target: Target): this; expectNone(): this; fail<D extends Target>(target: D, error: Failure<D>): this; given<O extends readonly Array<Owner> = []>(model: Model<C> | (model: Model<C>) => Model<C>, queries?: Given<C>, shared?: { [K in string | number | symbol]: readonly [O[K], Value<O[K]>] }): this; keepPending(target: Target | keyof Queries<C> & string): this; layerFailed(failure: Failure<never, any>): this; loaded<N extends string>(name: N, value: Success<QueryOf<Queries<C>[N]>>, options?: { readonly patched?: boolean; readonly waiting?: boolean; }): this; loaded<N extends string>(name: N, args: Args<QueryOf<Queries<C>[N]>>, value: Success<QueryOf<Queries<C>[N]>>, options?: { readonly patched?: boolean; readonly waiting?: boolean; }): this; loadedMany<N extends string>(name: N, entries: readonly Array<KeyedLoad<QueryOf<Queries<C>[N]>>>): this; loadedMore<N extends never>(name: N, page: Page<QueryOf<Queries<C>[N]>>): this; loadFailed<N extends string>(name: N, error: Error<QueryOf<Queries<C>[N]>>, options?: { readonly waiting?: boolean; }): this; loadFailed<N extends string>(name: N, args: Args<QueryOf<Queries<C>[N]>>, error: Error<QueryOf<Queries<C>[N]>>, options?: { readonly waiting?: boolean; }): this; loadMoreFailed<N extends never>(name: N, error: Error<QueryOf<Queries<C>[N]>>): this; next<D extends StreamTarget>(target: D, result: Result<D>): this; props(next: Props<C> | (props: Props<C>) => Props<C>): this; resolve<D extends Target>(target: D, result: Result<D>): this; sent<O extends Owner>(owner: O, message: NoInfer<SendMessage<Receives<O>>>): this; shared<O extends Owner>(owner: O, value: Value<O>): this; subscription(key: string, message: Message<C>): this; waitExpired(target: Target): this;}Example A check shared by every kind of test
import { Effect, Schema } from "effect"import { Component, UiSchema } from "effect/ui"import { Steps, Story } from "effect/ui/testing"
const Message = UiSchema.TaggedEnum({ ClickedIncrement: {} })const Counter = Component.define({ name: "Counter", model: Schema.Struct({ count: Schema.Number }), message: Message}).make({ init: () => ({ count: 0 }), update: ($) => ({ count: $.model.count + 1 }), view: (h, model) => h.el("button").on("click", Message.ClickedIncrement()).children(`+ ${model.count}`)})
const quiet = <S extends Steps.Steps.Any>(s: S): S => s.expectNone().check((current) => { if (current.emitted.length > 0) throw new Error("emitted") })
Effect.gen(function*() { const s = yield* Story.make(Counter, {}) s.pipe(quiet).send(Message.ClickedIncrement()) console.log(s.model.count) // 1}).pipe(Effect.scoped, Effect.runSync)A stream Command's definition, or an issued stream Command: what next and
end take.
Signature
type StreamTarget = Command.Definition<any, any, true, any, any> | Command.Issued<any, any, never, "Stream">A pending Command's definition, or an issued Command to also match the arguments.
Signature
type Target = Command.Definition.Any | Command.Issued<any, any, never, "Effect"> | Command.Issued<any, any, never, "Stream">Other
Utility Types
The type of current.component in a test. Preserves a headless component's
type so the record does not expose a view it cannot render.
Signature
type RecordComponent<C extends AnyComponent> = C extends Component.Headless.Any ? C : Record<Component.Props<C>, Component.Model<C>, Component.Message<C>, Component.Events<C>>["component"]