package html
import (
"testing"
i18n "dappco.re/go/core/i18n"
"slices"
)
func TestRawNode_Render_Good(t *testing.T) {
ctx := NewContext()
node := Raw("hello")
got := node.Render(ctx)
if got != "hello" {
t.Errorf("Raw(\"hello\").Render() = %q, want %q", got, "hello")
}
}
func TestElNode_Render_Good(t *testing.T) {
ctx := NewContext()
node := El("div", Raw("content"))
got := node.Render(ctx)
want := "
content
"
if got != want {
t.Errorf("El(\"div\", Raw(\"content\")).Render() = %q, want %q", got, want)
}
}
func TestElNode_Nested_Good(t *testing.T) {
ctx := NewContext()
node := El("div", El("span", Raw("inner")))
got := node.Render(ctx)
want := "inner
"
if got != want {
t.Errorf("nested El().Render() = %q, want %q", got, want)
}
}
func TestLayout_DirectElementBlockPath_Good(t *testing.T) {
ctx := NewContext()
got := NewLayout("C").C(El("div", Raw("content"))).Render(ctx)
if !containsText(got, `data-block="C-0.0"`) {
t.Fatalf("direct element inside layout should receive a block path, got:\n%s", got)
}
}
func TestLayout_EachElementBlockPaths_Good(t *testing.T) {
ctx := NewContext()
got := NewLayout("C").C(
Each([]string{"a", "b"}, func(item string) Node {
return El("span", Raw(item))
}),
).Render(ctx)
if !containsText(got, `data-block="C-0.0.0"`) {
t.Fatalf("first Each item should receive a block path, got:\n%s", got)
}
if !containsText(got, `data-block="C-0.0.1"`) {
t.Fatalf("second Each item should receive a block path, got:\n%s", got)
}
}
func TestElNode_MultipleChildren_Good(t *testing.T) {
ctx := NewContext()
node := El("div", Raw("a"), Raw("b"))
got := node.Render(ctx)
want := "ab
"
if got != want {
t.Errorf("El with multiple children = %q, want %q", got, want)
}
}
func TestElNode_VoidElement_Good(t *testing.T) {
ctx := NewContext()
node := El("br")
got := node.Render(ctx)
want := "
"
if got != want {
t.Errorf("El(\"br\").Render() = %q, want %q", got, want)
}
}
func TestTextNode_Render_Good(t *testing.T) {
ctx := NewContext()
node := Text("hello")
got := node.Render(ctx)
if got != "hello" {
t.Errorf("Text(\"hello\").Render() = %q, want %q", got, "hello")
}
}
func TestTextNode_UsesContextDataForCount_Good(t *testing.T) {
svc, _ := i18n.New()
i18n.SetDefault(svc)
tests := []struct {
name string
key string
data map[string]any
want string
}{
{
name: "capitalised count",
key: "i18n.count.file",
data: map[string]any{"Count": 5},
want: "5 files",
},
{
name: "lowercase count",
key: "i18n.count.file",
data: map[string]any{"count": 1},
want: "1 file",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := NewContext()
for k, v := range tt.data {
ctx.Metadata[k] = v
}
got := Text(tt.key).Render(ctx)
if got != tt.want {
t.Fatalf("Text(%q).Render() = %q, want %q", tt.key, got, tt.want)
}
})
}
}
func TestTextNode_Escapes_Good(t *testing.T) {
ctx := NewContext()
node := Text("")
got := node.Render(ctx)
if containsText(got, "