-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
gomponents_test.go
369 lines (310 loc) · 9.2 KB
/
gomponents_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package gomponents_test
import (
"errors"
"fmt"
"io"
"os"
"strings"
"testing"
g "maragu.dev/gomponents"
"maragu.dev/gomponents/internal/assert"
)
func TestNodeFunc(t *testing.T) {
t.Run("implements fmt.Stringer", func(t *testing.T) {
fn := g.NodeFunc(func(w io.Writer) error {
_, _ = w.Write([]byte("hat"))
return nil
})
if fn.String() != "hat" {
t.FailNow()
}
})
}
func TestAttr(t *testing.T) {
t.Run("renders just the local name with one argument", func(t *testing.T) {
a := g.Attr("required")
assert.Equal(t, " required", a)
})
t.Run("renders the name and value when given two arguments", func(t *testing.T) {
a := g.Attr("id", "hat")
assert.Equal(t, ` id="hat"`, a)
})
t.Run("panics with more than two arguments", func(t *testing.T) {
defer func() {
if rec := recover(); rec == nil {
t.FailNow()
}
}()
g.Attr("name", "value", "what is this?")
})
t.Run("implements fmt.Stringer", func(t *testing.T) {
a := g.Attr("required")
s := fmt.Sprintf("%v", a)
if s != " required" {
t.FailNow()
}
})
t.Run("escapes attribute values", func(t *testing.T) {
a := g.Attr(`id`, `hat"><script`)
assert.Equal(t, ` id="hat"><script"`, a)
})
}
func BenchmarkAttr(b *testing.B) {
b.Run("boolean attributes", func(b *testing.B) {
for i := 0; i < b.N; i++ {
a := g.Attr("hat")
_ = a.Render(&strings.Builder{})
}
})
b.Run("name-value attributes", func(b *testing.B) {
for i := 0; i < b.N; i++ {
a := g.Attr("hat", "party")
_ = a.Render(&strings.Builder{})
}
})
}
func ExampleAttr_bool() {
e := g.El("input", g.Attr("required"))
_ = e.Render(os.Stdout)
// Output: <input required>
}
func ExampleAttr_name_value() {
e := g.El("div", g.Attr("id", "hat"))
_ = e.Render(os.Stdout)
// Output: <div id="hat"></div>
}
type outsider struct{}
func (o outsider) String() string {
return "outsider"
}
func (o outsider) Render(w io.Writer) error {
_, _ = w.Write([]byte("outsider"))
return nil
}
func TestEl(t *testing.T) {
t.Run("renders an empty element if no children given", func(t *testing.T) {
e := g.El("div")
assert.Equal(t, "<div></div>", e)
})
t.Run("renders an empty element without closing tag if it's a void kind element", func(t *testing.T) {
e := g.El("hr")
assert.Equal(t, "<hr>", e)
e = g.El("br")
assert.Equal(t, "<br>", e)
e = g.El("img")
assert.Equal(t, "<img>", e)
})
t.Run("renders an empty element if only attributes given as children", func(t *testing.T) {
e := g.El("div", g.Attr("class", "hat"))
assert.Equal(t, `<div class="hat"></div>`, e)
})
t.Run("renders an element, attributes, and element children", func(t *testing.T) {
e := g.El("div", g.Attr("class", "hat"), g.El("br"))
assert.Equal(t, `<div class="hat"><br></div>`, e)
})
t.Run("renders attributes at the correct place regardless of placement in parameter list", func(t *testing.T) {
e := g.El("div", g.El("br"), g.Attr("class", "hat"))
assert.Equal(t, `<div class="hat"><br></div>`, e)
})
t.Run("renders outside if node does not implement nodeTypeDescriber", func(t *testing.T) {
e := g.El("div", outsider{})
assert.Equal(t, `<div>outsider</div>`, e)
})
t.Run("does not fail on nil node", func(t *testing.T) {
e := g.El("div", nil, g.El("br"), nil, g.El("br"))
assert.Equal(t, `<div><br><br></div>`, e)
})
t.Run("returns render error on cannot write", func(t *testing.T) {
e := g.El("div")
err := e.Render(&erroringWriter{})
assert.Error(t, err)
})
}
func BenchmarkEl(b *testing.B) {
b.Run("normal elements", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e := g.El("div")
_ = e.Render(&strings.Builder{})
}
})
}
func ExampleEl() {
e := g.El("div", g.El("span"))
_ = e.Render(os.Stdout)
// Output: <div><span></span></div>
}
type erroringWriter struct{}
func (w *erroringWriter) Write(p []byte) (n int, err error) {
return 0, errors.New("no thanks")
}
func TestText(t *testing.T) {
t.Run("renders escaped text", func(t *testing.T) {
e := g.Text("<div>")
assert.Equal(t, "<div>", e)
})
}
func ExampleText() {
e := g.El("span", g.Text("Party hats > normal hats."))
_ = e.Render(os.Stdout)
// Output: <span>Party hats > normal hats.</span>
}
func TestTextf(t *testing.T) {
t.Run("renders interpolated and escaped text", func(t *testing.T) {
e := g.Textf("<%v>", "div")
assert.Equal(t, "<div>", e)
})
}
func ExampleTextf() {
e := g.El("span", g.Textf("%v party hats > %v normal hats.", 2, 3))
_ = e.Render(os.Stdout)
// Output: <span>2 party hats > 3 normal hats.</span>
}
func TestRaw(t *testing.T) {
t.Run("renders raw text", func(t *testing.T) {
e := g.Raw("<div>")
assert.Equal(t, "<div>", e)
})
}
func ExampleRaw() {
e := g.El("span",
g.Raw(`<button onclick="javascript:alert('Party time!')">Party hats</button> > normal hats.`),
)
_ = e.Render(os.Stdout)
// Output: <span><button onclick="javascript:alert('Party time!')">Party hats</button> > normal hats.</span>
}
func TestRawf(t *testing.T) {
t.Run("renders interpolated and raw text", func(t *testing.T) {
e := g.Rawf("<%v>", "div")
assert.Equal(t, "<div>", e)
})
}
func ExampleRawf() {
e := g.El("span",
g.Rawf(`<button onclick="javascript:alert('%v')">Party hats</button> > normal hats.`, "Party time!"),
)
_ = e.Render(os.Stdout)
// Output: <span><button onclick="javascript:alert('Party time!')">Party hats</button> > normal hats.</span>
}
func TestMap(t *testing.T) {
t.Run("maps a slice to a group", func(t *testing.T) {
items := []string{"hat", "partyhat", "turtlehat"}
lis := g.Map(items, func(i string) g.Node {
return g.El("li", g.Text(i))
})
list := g.El("ul", lis...)
assert.Equal(t, `<ul><li>hat</li><li>partyhat</li><li>turtlehat</li></ul>`, list)
if len(lis) != 3 {
t.FailNow()
}
assert.Equal(t, `<li>hat</li>`, lis[0])
assert.Equal(t, `<li>partyhat</li>`, lis[1])
assert.Equal(t, `<li>turtlehat</li>`, lis[2])
})
}
func ExampleMap() {
items := []string{"party hat", "super hat"}
e := g.El("ul", g.Map(items, func(i string) g.Node {
return g.El("li", g.Text(i))
}))
_ = e.Render(os.Stdout)
// Output: <ul><li>party hat</li><li>super hat</li></ul>
}
func ExampleMap_index() {
items := []string{"party hat", "super hat"}
var index int
e := g.El("ul", g.Map(items, func(i string) g.Node {
e := g.El("li", g.Textf("%v: %v", index, i))
index++
return e
}))
_ = e.Render(os.Stdout)
// Output: <ul><li>0: party hat</li><li>1: super hat</li></ul>
}
func TestGroup(t *testing.T) {
t.Run("groups multiple nodes into one", func(t *testing.T) {
children := []g.Node{g.El("br", g.Attr("id", "hat")), g.El("hr")}
e := g.El("div", g.Attr("class", "foo"), g.El("img"), g.Group(children))
assert.Equal(t, `<div class="foo"><img><br id="hat"><hr></div>`, e)
})
t.Run("ignores attributes at the first level", func(t *testing.T) {
children := []g.Node{g.Attr("class", "hat"), g.El("div"), g.El("span")}
e := g.Group(children)
assert.Equal(t, "<div></div><span></span>", e)
})
t.Run("does not ignore attributes at the second level and below", func(t *testing.T) {
children := []g.Node{g.El("div", g.Attr("class", "hat"), g.El("hr", g.Attr("id", "partyhat"))), g.El("span")}
e := g.Group(children)
assert.Equal(t, `<div class="hat"><hr id="partyhat"></div><span></span>`, e)
})
t.Run("implements fmt.Stringer", func(t *testing.T) {
children := []g.Node{g.El("div"), g.El("span")}
e := g.Group(children)
if e, ok := any(e).(fmt.Stringer); !ok || e.String() != "<div></div><span></span>" {
t.FailNow()
}
})
t.Run("can be used like a regular slice", func(t *testing.T) {
e := g.Group{g.El("div"), g.El("span")}
assert.Equal(t, "<div></div><span></span>", e)
assert.Equal(t, "<div></div>", e[0])
assert.Equal(t, "<span></span>", e[1])
})
}
func ExampleGroup() {
children := []g.Node{g.El("div"), g.El("span")}
e := g.Group(children)
_ = e.Render(os.Stdout)
// Output: <div></div><span></span>
}
func ExampleGroup_slice() {
e := g.Group{g.El("div"), g.El("span")}
_ = e.Render(os.Stdout)
// Output: <div></div><span></span>
}
func TestIf(t *testing.T) {
t.Run("returns node if condition is true", func(t *testing.T) {
n := g.El("div", g.If(true, g.El("span")))
assert.Equal(t, "<div><span></span></div>", n)
})
t.Run("returns nil if condition is false", func(t *testing.T) {
n := g.El("div", g.If(false, g.El("span")))
assert.Equal(t, "<div></div>", n)
})
}
func ExampleIf() {
showMessage := true
e := g.El("div",
g.If(showMessage, g.El("span", g.Text("You lost your hat!"))),
g.If(!showMessage, g.El("span", g.Text("No messages."))),
)
_ = e.Render(os.Stdout)
// Output: <div><span>You lost your hat!</span></div>
}
func TestIff(t *testing.T) {
t.Run("returns node if condition is true", func(t *testing.T) {
n := g.El("div", g.Iff(true, func() g.Node {
return g.El("span")
}))
assert.Equal(t, "<div><span></span></div>", n)
})
t.Run("returns nil if condition is false", func(t *testing.T) {
n := g.El("div", g.Iff(false, func() g.Node {
return g.El("span")
}))
assert.Equal(t, "<div></div>", n)
})
}
func ExampleIff() {
type User struct {
Name string
}
var user *User
e := g.El("div",
// This would panic using just If
g.Iff(user != nil, func() g.Node {
return g.Text(user.Name)
}),
)
_ = e.Render(os.Stdout)
// Output: <div></div>
}