Documentation
¶
Overview ¶
Package viewtest helps test pages: it gives pages that read template variables the values a test chooses instead of what the app's context processors (web.ContextProcessors) compute, and sets how a page's deferred parts render.
In a test binary a page's deferred parts (@ui.Defer, ui.Part) render in place: the test reads the page whole, as it is once every part has arrived. Pending renders the page as it first shows instead.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Context ¶
Context is ctx reading the variables of each of vars from it, as web.WithVars does: for a live session a test builds itself (live.NewSession, livetest), a component rendered alone, or Go code calling web.Var.
func Pending ¶ added in v2.9.0
Pending makes the app's pages render as they first show: each deferred part's fallback in its place, its method never run. For a test of what a page shows before its parts arrive:
app := web.New(pages.Routes(), viewtest.Pending())
Outside a test binary it panics.
func With ¶
With makes every request of the app — pages, live pages and their events — read the variables of each of vars (a context processor's result struct, such as users.Vars{Me: u}) from it, the processor returning its type not running:
app := web.New(web.ContextProcessors(users.Context), viewtest.With(users.Vars{Me: ada}))
Outside a test binary it panics.
Types ¶
type Field ¶ added in v2.8.0
type Field struct {
// contains filtered or unexported fields
}
Field is what the page shows of one field.
type Page ¶ added in v2.8.0
type Page[T any] struct { // Page is a value of the page's type, for its fields' addresses: what // Fill, Blur and Expect take. Page T // contains filtered or unexported fields }
Page drives a page's struct form (<ui.Form for>) as a visitor does: it fills fields, leaves them and submits the form, and checks what the page shows of each field and where a submit sends the visitor.
p := viewtest.Mount[*pages.Join](t, app, "/join")
p.Fill(&p.Page.Form.Email, "pa@").Blur(&p.Page.Form.Email)
p.Expect(&p.Page.Form.Email).Error("validate.email")
p.Fill(&p.Page.Form.Email, "pa@example.com").Submit()
p.ExpectRedirect("/welcome")
A field is named by a pointer into Page — a value of the page's type the test holds only to point at its fields — or by its path ("email", "address.city"). Filling a field the form doesn't render fails the test.
By default the page runs live, over its connection, as in a browser with script; Plain drives it as a browser without script, each submit a plain POST. A form test reads the same either way, but for the live checks a Blur runs: without script they happen on submit.
func Mount ¶ added in v2.8.0
Mount opens the page app serves at path, a live page of type T (a pointer, *pages.Join), with a fresh visitor.
func (*Page[T]) Blur ¶ added in v2.8.0
Blur leaves the field, as moving focus out of it does: a field left with something in it shows its error.
func (*Page[T]) ExpectRedirect ¶ added in v2.8.0
ExpectRedirect checks that the last submit sent the visitor to url.
func (*Page[T]) Fill ¶ added in v2.8.0
Fill sets the field to v, as typing it (or choosing it) does: over the connection the form's checks run on it, showing errors only of fields already showing one.