viewtest

package
v2.16.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 19 Imported by: 0

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

func Context(ctx context.Context, vars ...any) context.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

func Pending() web.Option

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

func With(vars ...any) web.Option

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.

func (*Field) Error added in v2.8.0

func (f *Field) Error(key string)

Error checks that the field shows an error, its message key key ("validate.email", or the key a Clean method's forms.Errorf gave) or its text.

func (*Field) NoError added in v2.8.0

func (f *Field) NoError()

NoError checks that the field shows no error.

type Option added in v2.8.0

type Option func(*options)

Option configures Mount.

func Plain added in v2.8.0

func Plain() Option

Plain drives the page as a browser without script does: its form posts to the page on submit, the page answering 422 with the errors or 303.

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

func Mount[T any](t testing.TB, app *web.App, path string, opts ...Option) *Page[T]

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

func (p *Page[T]) Blur(field any) *Page[T]

Blur leaves the field, as moving focus out of it does: a field left with something in it shows its error.

func (*Page[T]) Expect added in v2.8.0

func (p *Page[T]) Expect(field any) *Field

Expect is what the page shows of the field, to check.

func (*Page[T]) ExpectRedirect added in v2.8.0

func (p *Page[T]) ExpectRedirect(url string)

ExpectRedirect checks that the last submit sent the visitor to url.

func (*Page[T]) Fill added in v2.8.0

func (p *Page[T]) Fill(field any, v string) *Page[T]

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.

func (*Page[T]) HTML added in v2.8.0

func (p *Page[T]) HTML() string

HTML is the page as it stands.

func (*Page[T]) Status added in v2.8.0

func (p *Page[T]) Status() int

Status is the status of the last submit's answer, as a plain post's: 422 when the form had errors, 303 when it was handled; 0 over a connection.

func (*Page[T]) Submit added in v2.8.0

func (p *Page[T]) Submit() *Page[T]

Submit submits the form.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL