feat(html): harden recursive attr coverage
This commit is contained in:
parent
dd68c6d0d7
commit
57952b8a6a
2 changed files with 37 additions and 1 deletions
2
node.go
2
node.go
|
|
@ -96,7 +96,7 @@ func El(tag string, children ...Node) Node {
|
|||
// Attr sets an attribute on an El node. Returns the node for chaining.
|
||||
// Usage example: Attr(El("a", Text("docs")), "href", "/docs")
|
||||
// It recursively traverses through wrappers like If, Unless, Entitled, Each,
|
||||
// Layout, and Responsive when present.
|
||||
// EachSeq, Switch, Layout, and Responsive when present.
|
||||
func Attr(n Node, key, value string) Node {
|
||||
if n == nil {
|
||||
return n
|
||||
|
|
|
|||
36
node_test.go
36
node_test.go
|
|
@ -295,6 +295,42 @@ func TestElNode_Attr_Good(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestElNode_AttrRecursiveThroughEachSeq_Good(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
node := Attr(
|
||||
EachSeq(slices.Values([]string{"a", "b"}), func(item string) Node {
|
||||
return El("span", Raw(item))
|
||||
}),
|
||||
"data-kind",
|
||||
"item",
|
||||
)
|
||||
|
||||
got := NewLayout("C").C(node).Render(ctx)
|
||||
if count := countText(got, `data-kind="item"`); count != 2 {
|
||||
t.Fatalf("Attr through EachSeq should apply to every item, got %d in:\n%s", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestElNode_AttrRecursiveThroughSwitch_Good(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
node := Attr(
|
||||
Switch(
|
||||
func(*Context) string { return "match" },
|
||||
map[string]Node{
|
||||
"match": El("span", Raw("visible")),
|
||||
"miss": El("span", Raw("hidden")),
|
||||
},
|
||||
),
|
||||
"data-state",
|
||||
"selected",
|
||||
)
|
||||
|
||||
got := node.Render(ctx)
|
||||
if !containsText(got, `data-state="selected"`) {
|
||||
t.Fatalf("Attr through Switch should reach the selected case, got:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccessibilityHelpers_Good(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue