diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2025-12-21 13:23:16 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2025-12-21 13:23:16 +0100 |
| commit | b8b0189bab9e282624fcf64d622cf100f3ddec11 (patch) | |
| tree | 4f3915864d75b9deb83aeafaab8b17370085a18f | |
| parent | 73c4a664b03f8afa8edb527fb6a8cc310bdb3380 (diff) | |
test(dom): create
| -rw-r--r-- | dom/css_class_test.go | 36 | ||||
| -rw-r--r-- | dom/html_test.go | 120 |
2 files changed, 156 insertions, 0 deletions
diff --git a/dom/css_class_test.go b/dom/css_class_test.go new file mode 100644 index 0000000..1365b34 --- /dev/null +++ b/dom/css_class_test.go @@ -0,0 +1,36 @@ +package dom + +import "testing" + +func TestClassList(t *testing.T) { + cl := NewClassList() + if cl.Has("foo") { + t.Errorf("class foo was never added") + } + cl.Add("foo") + if !cl.Has("foo") { + t.Errorf("class foo was added") + } + cl.Add("bar") + if !cl.Has("bar") { + t.Errorf("class bar was added") + } + if !cl.Has("foo") { + t.Errorf("class foo was not removed") + } + cl.Remove("foo") + if cl.Has("foo") { + t.Errorf("class foo was removed") + } + if !cl.Has("bar") { + t.Errorf("class bar was not removed") + } + cl.Toggle("foo") + if !cl.Has("foo") { + t.Errorf("class foo was toggled (added)") + } + cl.Toggle("foo") + if cl.Has("foo") { + t.Errorf("class foo was toggled (removed)") + } +} diff --git a/dom/html_test.go b/dom/html_test.go new file mode 100644 index 0000000..39f6a1f --- /dev/null +++ b/dom/html_test.go @@ -0,0 +1,120 @@ +package dom + +import ( + "html/template" + "testing" +) + +func TestRender(t *testing.T) { + fn := func(tag string, attributes map[string]string, endSlash bool, expected string) func(*testing.T) { + return func(t *testing.T) { + t.Parallel() + got := string(render(tag, attributes, endSlash)) + if got != expected { + t.Errorf("invalid value, got %s", got) + } + } + } + t.Run("render", func(t *testing.T) { + t.Run("simple", fn("p", map[string]string{}, false, "<p>")) + t.Run("endslash", fn("img", map[string]string{}, true, "<img />")) + t.Run("attributes", fn("a", map[string]string{"href": "link"}, false, `<a href="link">`)) + }) +} + +func TestLiteralElement(t *testing.T) { + fn := func(raw, expected string) func(*testing.T) { + return func(t *testing.T) { + t.Parallel() + got := NewLiteralElement(template.HTML(raw)) + if string(got.Render()) != expected { + t.Errorf("invalid value, got %s", got) + } + } + } + t.Run("render", func(t *testing.T) { + t.Run("simple", fn("hello world", "hello world")) + }) +} + +func TestVoidElement(t *testing.T) { + fn := func(tag string, attributes map[string]string, expected string) func(*testing.T) { + return func(t *testing.T) { + t.Parallel() + e := NewVoidElement(tag) + for k, v := range attributes { + e.SetAttribute(k, v) + if !e.HasAttribute(k) { + t.Errorf("doesn't not have attribute %s after being added", k) + } + } + got := string(e.Render()) + if got != expected { + t.Errorf("invalid value, got %s", got) + } + } + } + t.Run("no_attributes", func(t *testing.T) { + t.Run("simple1", fn("br", nil, "<br />")) + t.Run("simple2", fn("img", nil, "<img />")) + }) + t.Run("attributes", func(t *testing.T) { + t.Run("one", fn("img", map[string]string{"src": "link"}, `<img src="link" />`)) + t.Run("two", fn("img", map[string]string{"src": "link", "alt": "well"}, `<img src="link" alt="well" />`)) + }) +} + +func TestContentElement(t *testing.T) { + fn := func(tag string, els []Element, attributes map[string]string, expected string) func(*testing.T) { + return func(t *testing.T) { + t.Parallel() + e := NewContentElement(tag, els) + for k, v := range attributes { + e.SetAttribute(k, v) + if !e.HasAttribute(k) { + t.Errorf("doesn't not have attribute %s after being added", k) + } + } + got := string(e.Render()) + if got != expected { + t.Errorf("invalid value, got %s", got) + } + } + } + fnLiteral := func(tag string, lit string, attributes map[string]string, expected string) func(*testing.T) { + return fn(tag, []Element{NewLiteralElement(template.HTML(lit))}, attributes, expected) + } + t.Run("no_attributes", func(t *testing.T) { + t.Run("simple", fnLiteral("p", "", nil, `<p></p>`)) + t.Run("literal", fnLiteral("p", "content", nil, `<p>content</p>`)) + t.Run("elements", fn("div", []Element{NewVoidElement("img"), NewVoidElement("br")}, nil, `<div><img /><br /></div>`)) + }) + t.Run("attributes", func(t *testing.T) { + t.Run("simple_one", fnLiteral("script", "", map[string]string{"src": "link"}, `<script src="link"></script>`)) + t.Run("literal_one", fnLiteral("p", "content", map[string]string{"class": "hey"}, `<p class="hey">content</p>`)) + }) +} + +func TestElement_ClassList(t *testing.T) { + fn := func(e Element, expected string, clazz ...string) func(*testing.T) { + return func(t *testing.T) { + t.Parallel() + cl := e.ClassList() + for _, s := range clazz { + cl.Add(s) + if !cl.Has(s) { + t.Errorf("doesn't have class %s after being added", s) + } + } + got := string(e.Render()) + if got != expected { + t.Errorf("invalid value, got %s", got) + } + } + } + t.Run("add", func(t *testing.T) { + t.Run("empty", fn(NewVoidElement("img"), `<img />`)) + t.Run("one", fn(NewVoidElement("img"), `<img class="bg" />`, "bg")) + t.Run("two", fn(NewVoidElement("img"), `<img class="bg large" />`, "bg", "large")) + }) +} |
