testkit
packageAPI reference for the testkit
package.
Imports
(7)renderComponent
type renderComponent struct
TestRenderAndEventually
Parameters
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)
}
Harness
Harness owns a component mounted into an isolated DOM container.
type Harness struct
Methods
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 returns the first matching element below the harness container.
Parameters
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
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 fails unless selector resolves to the document's active element.
Parameters
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 returns the current value property of a form control. It does not read the value attribute, which represents only the rendered default.
Parameters
Returns
func (*Harness) LiveValue(selector string) string
{
return harness.Query(selector).Val()
}
HTML returns the isolated container markup.
Returns
func (*Harness) HTML() string
{
if harness == nil {
return ""
}
return harness.container.HTML()
}
Text returns the isolated container text content.
Returns
func (*Harness) Text() string
{
if harness == nil {
return ""
}
return harness.container.Text()
}
Click dispatches a browser click on the first matching element.
Parameters
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 sets a value and dispatches an input event.
Parameters
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 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 |
Mount
Mount renders and mounts a component into an isolated test container.
Parameters
Returns
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
}
Uses
TestBrowserHarnessMountsAndQueries
Parameters
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())
}
}
TestBrowserHarnessAssertsLiveNodeState
Parameters
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)
}
}
Render
Render returns a component’s rendered HTML without mounting it.
Parameters
Returns
func Render(component core.Component) string
{
if component == nil {
return ""
}
return component.Render()
}
AssertContains
AssertContains fails when text does not contain expected.
Parameters
func AssertContains(t TestingT, text, expected string)
{
t.Helper()
if !strings.Contains(text, expected) {
t.Fatalf("expected %q to contain %q", text, expected)
}
}
Uses
Eventually
Eventually retries condition until it succeeds or timeout expires.
Parameters
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)
}
}