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 err = Template(&ww, r, "home.page.tmpl", &models.TemplateData{}) if err != nil { t.Error("error writiing template to browser") } err = Template(&ww, r, "non-existent.page.html", &models.TemplateData{}) 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) { NewRenderer(app) } func TestCreateTemplateCache(t *testing.T) { pathToTemplates = "../../templates" _, err := CreateTemplateCache() if err != nil { t.Error(err) } }