Skip to content

Commit 40fb489

Browse files
committed
test for v1 of composition
1 parent 23ca88b commit 40fb489

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

render_container_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package veun_test
2+
3+
import (
4+
"html/template"
5+
"testing"
6+
7+
"github.com/alecthomas/assert/v2"
8+
9+
. "github.com/stanistan/veun"
10+
)
11+
12+
type ContainerView struct {
13+
Heading Renderable
14+
Body Renderable
15+
}
16+
17+
func (v ContainerView) Template() (*template.Template, error) {
18+
return template.New("containerView").Funcs(template.FuncMap{
19+
"slot": func(name string) (template.HTML, error) {
20+
switch name {
21+
case "heading":
22+
return Render(v.Heading)
23+
case "body":
24+
return Render(v.Body)
25+
default:
26+
return template.HTML(""), nil
27+
}
28+
},
29+
}).Parse(`<div>
30+
<div class="heading">{{ slot "heading" }}</div>
31+
<div class="body">{{ slot "body" }}</div>
32+
</div>`)
33+
}
34+
35+
func (v ContainerView) TemplateData() (any, error) {
36+
return nil, nil
37+
}
38+
39+
var childViewTemplate = template.Must(
40+
template.New("childView").Parse(`{{ . }}`),
41+
)
42+
43+
type ChildView1 struct{}
44+
45+
func (v ChildView1) Template() (*template.Template, error) {
46+
return childViewTemplate, nil
47+
}
48+
49+
func (v ChildView1) TemplateData() (any, error) {
50+
return "HEADING", nil
51+
}
52+
53+
type ChildView2 struct{}
54+
55+
func (v ChildView2) Template() (*template.Template, error) {
56+
return childViewTemplate, nil
57+
}
58+
59+
func (v ChildView2) TemplateData() (any, error) {
60+
return "BODY", nil
61+
}
62+
63+
func TestRenderContainer(t *testing.T) {
64+
html, err := Render(&ContainerView{
65+
Heading: ChildView1{},
66+
Body: ChildView2{},
67+
})
68+
assert.NoError(t, err)
69+
assert.Equal(t, template.HTML(`<div>
70+
<div class="heading">HEADING</div>
71+
<div class="body">BODY</div>
72+
</div>`), html)
73+
}

0 commit comments

Comments
 (0)