Documentation
¶
Index ¶
- func AssertRendersWithoutError(t *testing.T, component interface{})
- func ContainsClass(t *testing.T, html, class string)
- func ContainsElement(t *testing.T, html, tag string)
- func ContainsHTML(t *testing.T, html, expected string)
- func ContainsHTMLf(t *testing.T, html, expected, msgFormat string, args ...interface{})
- func HasAttribute(t *testing.T, html, attr, value string)
- func MustRender(t *testing.T, component templ.Component) string
- func NotContainsHTML(t *testing.T, html, unexpected string)
- func RenderToBytes(component templ.Component) ([]byte, error)
- func RenderToString(component templ.Component) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertRendersWithoutError ¶
AssertRendersWithoutError is a helper to ensure a component renders without error. This is useful for smoke testing components.
Example:
AssertRendersWithoutError(t, components.Modal(components.ModalProps{}, "Content"))
func ContainsClass ¶
ContainsClass asserts that the HTML contains an element with the specified CSS class. This is more robust than a simple substring search as it looks for the class attribute pattern.
Example:
html := MustRender(t, components.Badge(components.BadgeProps{Variant: "success"}, "Active"))
ContainsClass(t, html, "bg-success-100")
func ContainsElement ¶
ContainsElement asserts that the HTML contains at least one element with the specified tag name.
Example:
html := MustRender(t, components.Button(components.ButtonProps{}, "Click"))
ContainsElement(t, html, "button")
func ContainsHTML ¶
ContainsHTML asserts that the HTML string contains the expected substring. Fails the test if the substring is not found.
Example:
html := MustRender(t, components.Button(components.ButtonProps{Variant: "primary"}, "Click"))
ContainsHTML(t, html, "btn-primary")
func ContainsHTMLf ¶
ContainsHTMLf is like ContainsHTML but with formatted message support.
func HasAttribute ¶
HasAttribute asserts that the HTML contains an element with the specified attribute and value. The value can be empty string to just check for attribute presence.
Example:
html := MustRender(t, components.Button(components.ButtonProps{Type: "submit"}, "Submit"))
HasAttribute(t, html, "type", "submit")
html := MustRender(t, components.Button(components.ButtonProps{Disabled: true}, "Disabled"))
HasAttribute(t, html, "disabled", "") // checks for presence only
func MustRender ¶
MustRender renders a templ.Component to a string and fails the test if an error occurs. This is a convenience wrapper around RenderToString for test cases where errors are not expected.
Example:
html := MustRender(t, components.Alert(components.AlertProps{Variant: "info"}, "Information"))
// Test assertions on html...
func NotContainsHTML ¶
NotContainsHTML asserts that the HTML string does not contain the unexpected substring. Fails the test if the substring is found.
Example:
html := MustRender(t, components.Button(components.ButtonProps{Variant: "primary"}, "Click"))
NotContainsHTML(t, html, "btn-danger")
func RenderToBytes ¶
RenderToBytes renders a templ.Component to a byte slice. Returns the rendered HTML as bytes and any error encountered.
Example:
html, err := RenderToBytes(components.Badge(components.BadgeProps{Variant: "success"}, "Active"))
if err != nil {
t.Fatal(err)
}
func RenderToString ¶
RenderToString renders a templ.Component to a string. Returns the rendered HTML string and any error encountered.
Example:
html, err := RenderToString(components.Button(components.ButtonProps{Variant: "primary"}, "Click me"))
if err != nil {
t.Fatal(err)
}
Types ¶
This section is empty.