-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_test.go
183 lines (156 loc) · 5.39 KB
/
render_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package gorgeous
import "testing"
func TestRenderElementId(t *testing.T) {
output := RenderElement(Div(EB{Id: "test"}), "")
if output.Document.String() != "<div id=\"test\"></div>" {
t.Errorf("RenderElement(Empty(), \"test\") did not render \"<div id=\"test\"></div>\", got: \"%s\"", output.Document.String())
}
}
func TestRenderStylesTableDriven(t *testing.T) {
tests := []struct {
input CSSProps
expected string
}{
{CSSProps{}, ""},
{CSSProps{"color": "red"}, "style=\"color: red;\""},
{CSSProps{"color": "red", "font-size": "12px"}, "style=\"color: red;font-size: 12px;\""},
}
for _, test := range tests {
output := renderStyles(test.input)
if output != test.expected {
t.Errorf("renderStyles(%v) did not render \"%s\", got: \"%s\"", test.input, test.expected, output)
}
}
}
func TestRenderClassListTableDriven(t *testing.T) {
tests := []struct {
input []string
expected string
}{
{[]string{}, ""},
{[]string{"test"}, "class=\"test\""},
{[]string{"test", "test2"}, "class=\"test test2\""},
}
for _, test := range tests {
output := renderClassList(test.input)
if output != test.expected {
t.Errorf("renderClassList(%v) did not render \"%s\", got: \"%s\"", test.input, test.expected, output)
}
}
}
func TestRenderTextPropsTableDriven(t *testing.T) {
tests := []struct {
input Props
expected string
}{
{Props{}, ""},
{Props{"test": "test"}, "test=\"test\""},
{Props{"test": "test", "test2": "test2"}, "test=\"test\" test2=\"test2\""},
// test that the rendered props are sorted alphabetically
{Props{"zzz": "zzz", "aaa": "aaa"}, "aaa=\"aaa\" zzz=\"zzz\""},
// test that empty keys and values are ignored
{Props{"": "nope", "test": "test"}, "test=\"test\""},
{Props{"nope": "", "test": "test"}, "test=\"test\""},
}
for _, test := range tests {
output := renderTextProps(test.input)
if output != test.expected {
t.Errorf("renderTextProps(%v) did not render \"%s\", got: \"%s\"", test.input, test.expected, output)
}
}
}
func TestRenderCSSPropsTableDriven(t *testing.T) {
tests := []struct {
name string
input CSSProps
expected CSS
}{
{"", CSSProps{}, CSS("")},
{"test", CSSProps{"color": "red"}, CSS("test {\n\tcolor: red;\n}\n")},
// tests that the rendered CSS is sorted alphabetically
{"test", CSSProps{"font-size": "12px", "color": "red"}, CSS("test {\n\tcolor: red;\n\tfont-size: 12px;\n}\n")},
}
for _, test := range tests {
output := renderCSSProps(test.name, test.input)
if output != test.expected {
t.Errorf("renderCSSProps(%s, %v) did not render \"%s\", got: \"%s\"", test.name, test.input, test.expected, output)
}
}
}
func TestRenderElementScript(t *testing.T) {
output := RenderElement(Script(EB{
Id: "test",
Children: CE{
Text("test"),
},
}), "")
if output.Document.String() != "<script id=\"test\">test</script>" {
t.Errorf("RenderElement(Script()) did not render \"<script id=\"test\">test</script>\", got: \"%s\"", output.Document.String())
}
}
func TestRenderElementTableDriven(t *testing.T) {
tests := []struct {
input *HTMLElement
expected string
}{
{Empty(), ""},
{Div(EB{Id: "a"}), "<div id=\"a\"></div>"},
{P(EB{Id: "a", Style: CSSProps{"color": "red"}}), "<p id=\"a\" style=\"color: red;\"></p>"},
{P(EB{Id: "a", ClassList: []string{"test"}}), "<p id=\"a\" class=\"test\"></p>"},
{P(EB{Id: "a", Props: Props{"test": "test"}}), "<p id=\"a\" test=\"test\"></p>"},
{P(EB{Id: "a", Script: "test"}), "<p id=\"a\"></p><script id=\"script-a\">\n\t\t\t((thisElement) => { test })(document.getElementById('a'));\n\t\t\tdocument.getElementById('script-a').remove();\n\t\t</script>"},
}
for _, test := range tests {
output := RenderElement(test.input, "")
if output.Document.String() != test.expected {
t.Errorf("RenderElement(%v) did not render \"%s\", got: \"%s\"", test.input, test.expected, output.Document.String())
}
}
}
func TestRemoveEmptyStringsTableDriven(t *testing.T) {
tests := []struct {
input []string
expected []string
}{
{[]string{}, []string{}},
{[]string{"test"}, []string{"test"}},
{[]string{"test", ""}, []string{"test"}},
{[]string{"", "test"}, []string{"test"}},
{[]string{"", "test", ""}, []string{"test"}},
{[]string{"test", "", "test"}, []string{"test", "test"}},
{[]string{"", "test", "", "test", ""}, []string{"test", "test"}},
}
for _, test := range tests {
output := removeEmptyStrings(test.input)
if len(output) != len(test.expected) {
t.Errorf("removeEmptyStrings(%v) did not return %v, got: %v", test.input, test.expected, output)
}
for i, str := range output {
if str != test.expected[i] {
t.Errorf("removeEmptyStrings(%v) did not return %v, got: %v", test.input, test.expected, output)
}
}
}
}
func TestRenderOnClickTableDriven(t *testing.T) {
tests := []struct {
OnClick string
PropsOnClick string
Expected string
}{
{"", "", `<div id="a"></div>`},
{"clip()", "", `<div id="a" onclick="clip()"></div>`},
{"", "clip()", `<div id="a" onclick="clip()"></div>`},
{"clip()", "clip()", `<div id="a" onclick="clip();clip()"></div>`},
}
for _, test := range tests {
ele := Div(EB{
Id: "a",
OnClick: JavaScript(test.OnClick),
Props: Props{"onclick": test.PropsOnClick}})
output := RenderElement(ele, "")
if output.Document.String() != test.Expected {
t.Errorf("RenderElement(%v) did not render \"%s\", got: \"%s\"", ele, test.Expected, output.Document.String())
}
}
}