package html
import (
"strings"
"testing"
i18n "dappco.re/go/core/i18n"
"slices"
)
func TestRawNode_Render(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 TestNewContext_Defaults(t *testing.T) {
ctx := NewContext()
if ctx == nil {
t.Fatal("NewContext() returned nil")
}
if ctx.Locale != "" {
t.Errorf("NewContext() Locale = %q, want empty string", ctx.Locale)
}
if ctx.Data == nil {
t.Fatal("NewContext() should initialise Data map")
}
}
func TestNewContext_Locale(t *testing.T) {
ctx := NewContext("en-GB")
if ctx.Locale != "en-GB" {
t.Errorf("NewContext(locale) Locale = %q, want %q", ctx.Locale, "en-GB")
}
}
func TestNewContextWithService_Locale(t *testing.T) {
svc, _ := i18n.New()
ctx := NewContextWithService(svc, "fr-FR")
if ctx.Locale != "fr-FR" {
t.Errorf("NewContextWithService(locale) Locale = %q, want %q", ctx.Locale, "fr-FR")
}
if ctx.service != svc {
t.Error("NewContextWithService should retain the provided service")
}
}
func TestElNode_Render(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(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 TestElNode_MultipleChildren(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_NilChild(t *testing.T) {
ctx := NewContext()
node := El("div", nil, Raw("content"))
got := node.Render(ctx)
want := "content
"
if got != want {
t.Errorf("El with nil child = %q, want %q", got, want)
}
}
func TestElNode_VoidElement(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(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_Escapes(t *testing.T) {
ctx := NewContext()
node := Text("")
got := node.Render(ctx)
if strings.Contains(got, "