udemy-go-web-1/internal/render/render_test.go

77 lines
1.3 KiB
Go
Raw Normal View History

2024-07-02 11:57:45 +00:00
package render
import (
"go-udemy-web-1/internal/models"
"net/http"
"testing"
)
func TestAddDefaultData(t *testing.T) {
var td models.TemplateData
r, err := getSession()
if err != nil {
t.Error(err)
}
session.Put(r.Context(), "flash", "123")
result := AddDefaultData(&td, r)
if result.Flash != "123" {
t.Error("flash value of 123 not found in session")
}
}
func TestRenderTemplate(t *testing.T) {
pathToTemplates = "../../templates"
tc, err := CreateTemplateCache()
if err != nil {
t.Error(err)
}
app.TemplateCahce = tc
r, err := getSession()
if err != nil {
t.Error(err)
}
var ww myWriter
2024-07-07 20:37:48 +00:00
err = Template(&ww, r, "home.page.tmpl", &models.TemplateData{})
2024-07-02 11:57:45 +00:00
if err != nil {
t.Error("error writiing template to browser")
}
2024-07-07 20:37:48 +00:00
err = Template(&ww, r, "non-existent.page.html", &models.TemplateData{})
2024-07-02 11:57:45 +00:00
if err == nil {
t.Error("rendered template that doesn't exist")
}
}
func getSession() (*http.Request, error) {
r, err := http.NewRequest("GET", "/some-url", nil)
if err != nil {
return nil, err
}
ctx := r.Context()
ctx, _ = session.Load(ctx, r.Header.Get("X-Session"))
r = r.WithContext(ctx)
return r, nil
}
func TestNewTemplate(t *testing.T) {
2024-07-07 20:37:48 +00:00
NewRenderer(app)
2024-07-02 11:57:45 +00:00
}
func TestCreateTemplateCache(t *testing.T) {
pathToTemplates = "../../templates"
_, err := CreateTemplateCache()
if err != nil {
t.Error(err)
}
}