template-nest-go/tests/01_render_test.go

212 lines
5.7 KiB
Go
Raw Normal View History

2024-11-16 04:13:48 +00:00
package tests
import (
"git.virtual.blue/tomgracey/template-nest-go"
"github.com/stretchr/testify/assert"
"testing"
)
func TestRenderSimplePage(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "00-simple-page",
"variable": "Simple Variable",
"simple_component": []templatenest.Hash{
templatenest.Hash{
"TEMPLATE": "01-simple-component",
"variable": "Simple Variable in Simple Component",
},
},
}
outputPage := templatenest.Hash{"TEMPLATE": "output/01-simple-page"}
render, err := nest.Render(page)
if err != nil {
t.Fatalf("Render failed for page: %+v", err)
}
outputRender := nest.MustRender(outputPage)
assert.Equal(t, outputRender, render, "Rendered output does not match expected output")
}
func TestRenderIncompletePage(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{TemplateDir: "templates"})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "00-simple-page",
"variable": "Simple Variable",
"simple_component": []templatenest.Hash{
templatenest.Hash{
"TEMPLATE": "01-simple-component",
},
},
}
outputPage := templatenest.Hash{"TEMPLATE": "output/03-incomplete-page"}
render := nest.MustRender(page)
outputRender := nest.MustRender(outputPage)
assert.Equal(t, outputRender, render, "Rendered output does not match expected output")
}
func TestRenderComplexPage(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{TemplateDir: "templates"})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "10-complex-page",
"title": "Complex Page",
"pre_body": templatenest.Hash{
"TEMPLATE": "18-styles",
},
"navigation": templatenest.Hash{
"TEMPLATE": "11-navigation",
"banner": templatenest.Hash{
"TEMPLATE": "12-navigation-banner",
},
"items": []templatenest.Hash{
templatenest.Hash{"TEMPLATE": "13-navigation-item-00-services"},
templatenest.Hash{"TEMPLATE": "13-navigation-item-01-resources"},
},
},
"hero_section": templatenest.Hash{
"TEMPLATE": "14-hero-section",
},
"main_content": []templatenest.Hash{
templatenest.Hash{"TEMPLATE": "15-isdc-card"},
templatenest.Hash{
"TEMPLATE": "16-vb-brand-cards",
"cards": []templatenest.Hash{
templatenest.Hash{
"TEMPLATE": "17-vb-brand-card-00",
"parent_classes": "p-card brand-card col-4",
},
templatenest.Hash{
"TEMPLATE": "17-vb-brand-card-01",
"parent_classes": "p-card brand-card col-4",
},
templatenest.Hash{
"TEMPLATE": "17-vb-brand-card-02",
"parent_classes": "p-card brand-card col-4",
},
},
},
},
"post_footer": templatenest.Hash{
"TEMPLATE": "19-scripts",
},
}
outputPage := templatenest.Hash{"TEMPLATE": "output/02-complex-page"}
render := nest.MustRender(page)
outputRender := nest.MustRender(outputPage)
assert.Equal(t, outputRender, render, "Rendered output does not match expected output")
}
func TestRenderArrayOfTemplateHash(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{TemplateDir: "templates"})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := []templatenest.Hash{
templatenest.Hash{
"TEMPLATE": "01-simple-component",
"variable": "This is a variable",
},
templatenest.Hash{
"TEMPLATE": "01-simple-component",
"variable": "This is another variable",
},
}
outputPage := templatenest.Hash{"TEMPLATE": "output/13-render-with-array-of-template-hash"}
render := nest.MustRender(page)
outputRender := nest.MustRender(outputPage)
assert.Equal(t, outputRender, render, "Rendered output does not match expected output")
}
2024-11-16 12:52:01 +00:00
func TestRenderNonExistentFile(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{TemplateDir: "templates"})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{"TEMPLATE": "this-template-file-does-not-exist"}
_, err = nest.Render(page)
if assert.NotNil(t, err) {
expected := "error getting file info:"
assert.Equal(t, expected, err.Error()[:len(expected)])
}
}
func TestRenderNoNameLabel(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{TemplateDir: "templates"})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{"variable": "Simple Variable"}
_, err = nest.Render(page)
if assert.NotNil(t, err) {
expected := "encountered hash with no name label"
assert.Equal(t, expected, err.Error()[:len(expected)])
}
}
func TestRenderSimplePageArrays(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",
NoEscapeInput: true,
})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{
"TEMPLATE": "00-simple-page",
"variable": "Simple Variable",
"simple_component": []interface{}{
templatenest.Hash{
"TEMPLATE": "01-simple-component",
"variable": "Simple Variable in Simple Component",
},
"<strong>Another test</strong>",
[]interface{}{
templatenest.Hash{
"TEMPLATE": "01-simple-component",
"variable": "Simple Variable in Simple Component",
},
"<strong>Another nested test 2</strong>",
},
},
}
outputPage := templatenest.Hash{"TEMPLATE": "output/12-simple-page-arrays"}
render := nest.MustRender(page)
outputRender := nest.MustRender(outputPage)
assert.Equal(t, outputRender, render, "Rendered output does not match expected output")
}