-
Notifications
You must be signed in to change notification settings - Fork 43
/
element_test.go
92 lines (83 loc) · 2.49 KB
/
element_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gowd
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestElement_SetAttributes(t *testing.T) {
elem := NewElement("div")
event := EventElement{}
event.Properties = make(map[string]string)
event.Properties["value"] = "123"
event.Properties["id"] = "myID"
elem.SetAttributes(&event)
assert.EqualValues(t, event.GetID(), "myID")
assert.EqualValues(t, event.GetValue(), "123")
assert.EqualValues(t, event.GetID(), elem.GetID())
assert.EqualValues(t, event.GetValue(), elem.GetValue())
}
func TestElement_Enable(t *testing.T) {
elem := NewElement("div")
elem.Disable()
val, found := elem.GetAttribute("disabled")
assert.EqualValues(t, val, "true")
assert.True(t, found)
elem.Enable()
val, found = elem.GetAttribute("disabled")
assert.EqualValues(t, val, "")
assert.False(t, found)
}
func TestElement_SetClass(t *testing.T) {
elem := NewElement("div")
elem.SetClass("well sunken")
class, _ := elem.GetAttribute("class")
assert.EqualValues(t, strings.TrimSpace(class), "well sunken")
elem.SetClass("upper")
class, _ = elem.GetAttribute("class")
assert.EqualValues(t, strings.TrimSpace(class), "well sunken upper")
elem.UnsetClass("sunken")
class, _ = elem.GetAttribute("class")
assert.EqualValues(t, strings.TrimSpace(class), "well upper")
}
func TestElement_Hide(t *testing.T) {
em := NewElementMap()
elem, err := ParseElement(`<div><p id="text">text</p></div>`, em)
if err != nil {
t.Fatal(err)
}
p := em["text"]
assert.False(t, p.Hidden)
testOuput(t, elem, "<div><p id=\"text\">text</p></div>")
p.Hide()
assert.True(t, p.Hidden)
testOuput(t, elem, "<div><!--p--></div>")
p.SetText("Show me!!")
p.Show()
testOuput(t, elem, "<div><p id=\"text\">Show me!!</p></div>")
elem.RemoveElement(p)
testOuput(t, elem, "<div></div>")
}
func TestElement_SetValue(t *testing.T) {
elem := NewElement("div")
elem.SetValue("hoho")
assert.Equal(t, "hoho", elem.GetValue())
testOuput(t, elem, "<div id=\"_div1\" value=\"hoho\"></div>")
}
func TestElement_AutoFocus(t *testing.T) {
elem := NewElement("div")
_, exists := elem.GetAttribute("autofocus")
assert.False(t, exists)
elem.AutoFocus()
testOuput(t, elem, "<div id=\"_div1\" autofocus=\"\"></div>")
}
func TestElement_SetElement(t *testing.T) {
elem := NewElement("div")
assert.Empty(t, elem.Kids)
for i := 0; i < 10; i++ {
elem.AddElement(NewElement("p"))
}
assert.Equal(t, 10, len(elem.Kids))
elem.SetElement(NewElement("a"))
assert.Equal(t, 1, len(elem.Kids))
assert.Equal(t, "a", elem.Kids[0].data)
}