testkit API

testkit

package

API reference for the testkit package.

S
struct

renderComponent

testkit/testkit_test.go:11-11
type renderComponent struct

Methods

Render
Method

Returns

string
func (renderComponent) Render() string
{ return "<p>rendered</p>" }
GetName
Method

Returns

string
func (renderComponent) GetName() string
{ return "render" }
GetID
Method

Returns

string
func (renderComponent) GetID() string
{ return "render" }
F
function

TestRenderAndEventually

Parameters

testkit/testkit_test.go:17-25
func TestRenderAndEventually(t *testing.T)

{
	AssertContains(t, Render(renderComponent{}), "rendered")
	var ready atomic.Bool
	go func() {
		time.Sleep(time.Millisecond)
		ready.Store(true)
	}()
	Eventually(t, time.Second, ready.Load)
}
S
struct

Harness

Harness owns a component mounted into an isolated DOM container.

testkit/browser.go:12-15
type Harness struct

Methods

Root
Method

Root returns the mounted component root.

Returns

func (*Harness) Root() dom.Element
{
	if harness == nil || harness.component == nil {
		return dom.Element{}
	}
	return harness.container.Query(`[data-component-id="` + harness.component.GetID() + `"]`)
}
Query
Method

Query returns the first matching element below the harness container.

Parameters

selector string

Returns

func (*Harness) Query(selector string) dom.Element
{
	if harness == nil {
		return dom.Element{}
	}
	return harness.container.Query(selector)
}

AssertSameNode fails when selector no longer resolves to the captured DOM node. It is intended for reconciliation tests where equivalent HTML is not enough: browser state and third-party integrations depend on node identity.

Parameters

selector string
captured dom.Element
func (*Harness) AssertSameNode(t TestingT, selector string, captured dom.Element)
{
	t.Helper()
	current := harness.Query(selector)
	if current.IsNull() || current.IsUndefined() || !current.Equal(captured.Value) {
		t.Fatalf("selector %q did not preserve DOM node identity", selector)
	}
}
AssertActive
Method

AssertActive fails unless selector resolves to the document's active element.

Parameters

selector string
func (*Harness) AssertActive(t TestingT, selector string)
{
	t.Helper()
	current := harness.Query(selector)
	if current.IsNull() || current.IsUndefined() || !js.Doc().Get("activeElement").Equal(current.Value) {
		t.Fatalf("selector %q is not the active element", selector)
	}
}
LiveValue
Method

LiveValue returns the current value property of a form control. It does not read the value attribute, which represents only the rendered default.

Parameters

selector string

Returns

string
func (*Harness) LiveValue(selector string) string
{
	return harness.Query(selector).Val()
}
HTML
Method

HTML returns the isolated container markup.

Returns

string
func (*Harness) HTML() string
{
	if harness == nil {
		return ""
	}
	return harness.container.HTML()
}
Text
Method

Text returns the isolated container text content.

Returns

string
func (*Harness) Text() string
{
	if harness == nil {
		return ""
	}
	return harness.container.Text()
}
Click
Method

Click dispatches a browser click on the first matching element.

Parameters

selector string
func (*Harness) Click(t TestingT, selector string)
{
	t.Helper()
	element := harness.Query(selector)
	if element.IsNull() || element.IsUndefined() {
		t.Fatalf("selector %q did not match an element", selector)
	}
	element.Call("click")
}
Input
Method

Input sets a value and dispatches an input event.

Parameters

selector string
value string
func (*Harness) Input(t TestingT, selector, value string)
{
	t.Helper()
	element := harness.Query(selector)
	if element.IsNull() || element.IsUndefined() {
		t.Fatalf("selector %q did not match an element", selector)
	}
	element.SetValue(value)
	options := js.NewDict()
	options.Set("bubbles", true)
	element.Call("dispatchEvent", js.Get("Event").New("input", options.Value))
}
Unmount
Method

Unmount releases the component and removes its isolated container.

func (*Harness) Unmount()
{
	if harness == nil || harness.component == nil {
		return
	}
	harness.component.Unmount()
	if !harness.container.IsNull() && !harness.container.IsUndefined() {
		harness.container.Call("remove")
	}
	harness.component = nil
	harness.container = dom.Element{}
}

Fields

Name Type Description
component core.Component
container dom.Element
F
function

Mount

Mount renders and mounts a component into an isolated test container.

Parameters

component

Returns

testkit/browser.go:18-28
func Mount(t TestingT, component core.Component) *Harness

{
	t.Helper()
	container := dom.CreateElement("div")
	container.SetAttr("data-rfw-test", component.GetID())
	dom.Doc().Body().AppendChild(container)
	dom.UpdateDOMIn(container, component.GetID(), component.Render())
	component.Mount()
	harness := &Harness{component: component, container: container}
	t.Cleanup(harness.Unmount)
	return harness
}
F
function

TestBrowserHarnessMountsAndQueries

Parameters

testkit/browser_test.go:11-20
func TestBrowserHarnessMountsAndQueries(t *testing.T)

{
	component := core.NewHTMLComponent("Harness", []byte(`<root><button id="action">run</button></root>`), nil)
	component.SetComponent(component)
	component.Init(nil)

	harness := Mount(t, component)
	if harness.Query("#action").Text() != "run" {
		t.Fatalf("query returned wrong element: %s", harness.HTML())
	}
}
F
function

TestBrowserHarnessAssertsLiveNodeState

Parameters

testkit/browser_test.go:22-36
func TestBrowserHarnessAssertsLiveNodeState(t *testing.T)

{
	component := core.NewHTMLComponent("HarnessLive", []byte(`<root><input id="field" value="initial"></root>`), nil)
	component.SetComponent(component)
	component.Init(nil)

	harness := Mount(t, component)
	field := harness.Query("#field")
	field.SetValue("typed")
	field.Call("focus")
	harness.AssertSameNode(t, "#field", field)
	harness.AssertActive(t, "#field")
	if got := harness.LiveValue("#field"); got != "typed" {
		t.Fatalf("live value = %q, want typed", got)
	}
}
I
interface

TestingT

TestingT is the subset of testing.T used by this package.

testkit/testkit.go:12-16
type TestingT interface

Methods

Helper
Method
func Helper(...)
Fatalf
Method

Parameters

string
...any
func Fatalf(...)
Cleanup
Method

Parameters

func()
func Cleanup(...)
F
function

Render

Render returns a component’s rendered HTML without mounting it.

Parameters

component

Returns

string
testkit/testkit.go:19-24
func Render(component core.Component) string

{
	if component == nil {
		return ""
	}
	return component.Render()
}
F
function

AssertContains

AssertContains fails when text does not contain expected.

Parameters

text
string
expected
string
testkit/testkit.go:27-32
func AssertContains(t TestingT, text, expected string)

{
	t.Helper()
	if !strings.Contains(text, expected) {
		t.Fatalf("expected %q to contain %q", text, expected)
	}
}
F
function

Eventually

Eventually retries condition until it succeeds or timeout expires.

Parameters

timeout
condition
func() bool
testkit/testkit.go:35-47
func Eventually(t TestingT, timeout time.Duration, condition func() bool)

{
	t.Helper()
	if timeout <= 0 {
		timeout = time.Second
	}
	deadline := time.Now().Add(timeout)
	for !condition() {
		if time.Now().After(deadline) {
			t.Fatalf("condition was not met within %s", timeout)
		}
		time.Sleep(time.Millisecond)
	}
}