-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgodom_test.go
129 lines (111 loc) · 2.95 KB
/
godom_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
package godom_test
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
. "github.com/tbe/godom"
"github.com/tbe/godom/types"
"github.com/tbe/godom/util"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/html"
)
func ExampleElement_Render() {
isFeatured := true // this can be dynamically set based on data
showAuthorBio := true
// Conditional class for featured post
featuredClass := util.IfAttr(isFeatured, Class("featured"))
// Conditional author bio
authorBio := util.IfElem(showAuthorBio, Div(Class("author-bio"))(
P()(Content("This is the author's bio. It provides information about the author.")),
))
// Delayed attribute example: A function that decides the attribute based on some condition
dynamicAttribute := util.DelayedAttribute(func(attrs map[string]string, _ *[]string, _ *[]types.Attribute) {
if isFeatured {
attrs["data-highlight"] = "true"
}
})
// Delayed element: Maybe we decide to render a special note for featured articles
featuredNote := util.DelayedElement(func() types.Element {
if isFeatured {
return P(Class("featured-note"))(Content("This is a featured article!"))
}
return util.IfElem(false, nil) // returns an empty group if not featured
})
// Construct the full document
doc := Group(
Doctype(),
HTML()(
Header()(
Meta(Charset("utf-8")),
Title()(Content("Blog Post Title")),
),
Body()(
Div(Class("blog-post"), featuredClass, dynamicAttribute)(
H1()(Content("Blog Post Title")),
P()(Content("This is the introduction of the blog post.")),
featuredNote,
authorBio,
),
),
),
)
var buf bytes.Buffer
doc.Render(&buf)
fmt.Println(buf.String())
}
func TestDocRender(t *testing.T) {
var buf bytes.Buffer
// we create a full example document and render it
doc := Group(
Doctype(),
HTML()(
Header()(
Meta(Charset("utf-8")),
Title()(Content("Testdocument")),
),
Body(Class("someclass"))(
Div(Class("someotherclass"), ID("mydiv"))(
H1()(Content("Some Heading here")),
Form()(
Button(Type("abort"), Disabled())(Content("My Button")),
),
),
),
),
)
assert.NoError(t, doc.Render(&buf))
// to keep this readable, we minify everything
expected := `
<!DOCTYPE html>
<html>
<header>
<meta charset="utf-8">
<title>Testdocument</title>
</header>
<body class="someclass">
<div class="someotherclass" id="mydiv">
<h1>Some Heading here</h1>
<form>
<button disabled type="abort">My Button</button>
</form>
</div>
</body>
</html>`
m := minify.New()
m.AddFunc("text/html", html.Minify)
minifedExpected, err := m.String("text/html", expected)
if err != nil {
panic(err)
}
minifiedRendered, err := m.String("text/html", buf.String())
if err != nil {
panic(err)
}
assert.Equal(t, minifedExpected, minifiedRendered)
}
func TestDuplicateAttribute(t *testing.T) {
attrs := make(map[string]string)
attrs["href"] = "somelink"
assert.Panics(t, func() { HRef("abcd")(attrs, nil, nil) })
}