HtmlPlay unstable
Play with the HTML vocabulary: steps that send what the browser host
sends for each user action, and assertions on fields.
Details
HtmlPlay.HtmlPlay extends Play.Play, so every Play step works here.
Each step is the HtmlScene step of the same name, sent to the tree on
TestHost. click sends pointerdown, mousedown, pointerup, mouseup and click,
then runs the browser's defaults as HtmlScene.click does: it throws on a
disabled control, a submit button submits its form with the fields read from
the tree, a checkbox or radio button fires input and change, a
popovertarget button toggles its popover, and a label clicks its control.
Some things differ from a Scene, as they do in a browser. type and
change write the field's value, and a click on a checkbox or radio
button its checked, unchecking the rest of a radio group, before the
event, so an uncontrolled field keeps what the user entered. A controlled
field goes back to what the view set once the step settles, unless the
Model took the edit. A popovertarget click opens or closes the popover,
so the next click sees it. click settles between the press (pointerdown,
mousedown) and the release (pointerup, mouseup, click, defaults), while the
release events run with no render between them, so a submit reads the fields
as they were before the click's own message. And focus, blur and the
browser's other events that don't bubble go to the matched element only.
Example Signing in
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import { HtmlLocator, HtmlPlay } from "effect/ui/html/testing"import { Locator, TestHost } from "effect/ui/testing"import { Effect, Schema } from "effect"
const Message = UiSchema.TaggedEnum({ ChangedEmail: { value: Schema.String }, SubmittedForm: {}})
const SignIn = Html.component({ name: "SignIn", model: Schema.Struct({ email: Schema.String, signedIn: Schema.Boolean }), message: Message}).make({ init: () => ({ email: "", signedIn: false }), update: ($, message) => Message.match(message, { ChangedEmail: ({ value }) => $.evolve({ email: value }), SubmittedForm: () => $.evolve({ signedIn: true }) }), view: (h, model) => model.signedIn ? h.p(`Welcome, ${model.email}`) : h.form.on("submit", Message.SubmittedForm()).children( h.label.attr("for", "email").children("Email"), h.input.attr("id", "email").value(model.email).on("input", ({ value }) => Message.ChangedEmail({ value })), h.button.children("Sign In") )})
Effect.gen(function*() { const play = yield* HtmlPlay.make(SignIn, {}) yield* play.type(HtmlLocator.label("Email"), "alice@example.com") yield* play.click(HtmlLocator.role("button", { name: "Sign In" })) yield* play.expect(Locator.text("Welcome, alice@example.com")).toExist()}).pipe(Effect.scoped, Effect.provide(TestHost.layer()), Effect.runPromise)Constructors
Mounts a Case on TestHost, sends its messages, settles, and returns
an HtmlPlay cursor over it, as Play.fromCase does.
Signature
declare function fromCase<C extends AnyComponent, R>(c: Case<C, R>): Effect<HtmlPlay<Model<C>, Message<C>, Events<C>, never>, StepError, Scope | TestHost | R>Mounts component on TestHost with props, settles, and returns an
HTML cursor over it. It takes what Play.make takes, and runs the same
end check when the scope closes.
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<HtmlPlay<Model, Msg, Events, Props>, StepError, Scope | R | RP | TestHost>Models
The assertions expect(locator) offers in an HtmlPlay: the Play's,
plus checks on fields. The checks on fields want exactly one match.
Signature
interface Expect extends Expect<Effect.Effect<void, Play.StepError>> { toBeFocused(focused?: boolean): Effect<void, StepError>;}A cursor over an HTML tree on TestHost: the Play's steps, plus a step
per user action the browser host turns into an event. Each one is an
interaction.
Details
Simulated user actions (click, type, change, keydown, submit,
focus and drop) throw on inert targets, regardless of how they were
found. Raw events (fire, fireElement) and blur still reach inert
targets.
Focus is tracked by the TestHtmlHost provided, or one the Play makes
when no Html.Host is provided. With another Html.Host, focus, blur
and toBeFocused fail and press goes to the document.
Signature
interface HtmlPlay<Model, Msg, Events, Props = never> extends Play<Model, Msg, Events, Props> { blur(locator: Locator): Effect<void, StepError>; change(locator: Locator, value: string | Partial<InputPayload>): Effect<void, StepError>; click(target: string | Locator): Effect<void, StepError>; drop(locator: Locator, payload?: Partial<DropPayload>): Effect<void, StepError>; expect(locator: Locator): Expect; fireElement<E extends { [event: string]: Decoder<unknown, never>; }, K extends string>(element: CustomElementBinding<any, any, E>, locator: Locator, event: K, detail: E[K]["Encoded"]): Effect<void, StepError>; focus(locator: Locator): Effect<void, StepError>; keydown(locator: Locator, key: string, details?: Partial<Omit<KeyPayload, "key">>): Effect<void, StepError>; press(key: string, details?: Partial<Omit<GlobalPayload<KeyPayload>, "key" | "defaultPrevented">>): Effect<void, StepError>; submit(locator: Locator): Effect<void, StepError>; type(locator: Locator, value: string): Effect<void, StepError>;}