xonecas.io
5011
Sp

scrap-paper / commit ec57618

test: explore how to test html template renders

Sean2025-03-25 ec57618c64aeabcec96c491af600024911196aaf parent d73390f

6 files changed

lib/render_template.go+16−0
@@ -0,0 +1,16 @@
1+package lib
2+
3+import (
4+ "bytes"
5+ "html/template"
6+)
7+
8+func RenderTemplate(tmpl string, data interface{}) (string, error) {
9+ t := template.Must(template.New("index").Parse(tmpl))
10+
11+ var buf bytes.Buffer
12+ if err := t.Execute(&buf, data); err != nil {
13+ return "", err
14+ }
15+ return buf.String(), nil
16+}
scrap-paper/assets.go+14−0
@@ -0,0 +1,14 @@
1+package scrap_paper
2+
3+import (
4+ "net/http"
5+
6+ "encore.dev"
7+)
8+
9+//encore:api public raw path=/assets/:filename
10+func Assets(w http.ResponseWriter, r *http.Request) {
11+ // This path is relative to the root of the project
12+ path := "." + encore.CurrentRequest().Path
13+ http.ServeFile(w, r, path)
14+}
scrap-paper/assets/favicon.svg+0−0
scrap-paper/index.go+4−29
@@ -1,37 +1,24 @@
11 package scrap_paper
22
3-// We are building a service that uses htmx to return html from a url
43 import (
5- "bytes"
64 _ "embed"
75 "fmt"
86 "net/http"
97
10- "html/template"
11-
8+ "encore.app/lib"
129 "encore.dev"
1310 )
1411
1512 //go:embed index.html
16-var indexRawTemplate string
13+var IndexRawTemplate string
1714
1815 type IndexTemplateData struct {
1916 Title string
2017 Env string
2118 }
2219
23-func IndexHtml(data IndexTemplateData) (string, error) {
24- tmpl := template.Must(template.New("index").Parse(indexRawTemplate))
25-
26- var buf bytes.Buffer
27- if err := tmpl.Execute(&buf, data); err != nil {
28- return "", err
29- }
30- return buf.String(), nil
31-}
32-
3320 //encore:api public raw path=/!fallback
34-func Index(w http.ResponseWriter, r *http.Request) {
21+func (svc *ScrapPaperService) Index(w http.ResponseWriter, r *http.Request) {
3522 req := encore.CurrentRequest()
3623 env := encore.Meta().Environment.Name
3724 title := "Scrap Paper - Home"
@@ -41,7 +28,7 @@ func Index(w http.ResponseWriter, r *http.Request) {
4128 }
4229
4330 if req.Path == "/" {
44- html, err := IndexHtml(IndexTemplateData{
31+ html, err := lib.RenderTemplate(IndexRawTemplate, IndexTemplateData{
4532 Title: title,
4633 Env: env,
4734 })
@@ -55,17 +42,5 @@ func Index(w http.ResponseWriter, r *http.Request) {
5542
5643 // Since we are abusing fallback handler to mount a page on "/"
5744 // we need to return a 404 error if the path is not "/"
58- // TODO: Return an error page
5945 http.NotFound(w, r)
6046 }
61-
62-//
63-// Handle static assets
64-//
65-
66-//encore:api public raw path=/assets/:filename
67-func Assets(w http.ResponseWriter, r *http.Request) {
68- // This path is relative to the root of the project
69- path := "." + encore.CurrentRequest().Path
70- http.ServeFile(w, r, path)
71-}
scrap-paper/index_test.go+40−0
@@ -0,0 +1,40 @@
1+package scrap_paper_test
2+
3+import (
4+ "strings"
5+ "testing"
6+
7+ "encore.app/lib"
8+ scrap_paper "encore.app/scrap-paper"
9+)
10+
11+// request and check the html for the htmx script tag and then for the tailwind css link
12+// and finally for the title.
13+func TestIndex(t *testing.T) {
14+ // We can't use requests to test a raw endpoint so we just check the html
15+ body, err := lib.RenderTemplate(scrap_paper.IndexRawTemplate, scrap_paper.IndexTemplateData{
16+ Title: "Scrap Paper",
17+ Env: "test",
18+ })
19+ if err != nil {
20+ t.Fatal(err)
21+ }
22+
23+ t.Run("htmx script tag", func(t *testing.T) {
24+ if !strings.Contains(string(body), "htmx.org") {
25+ t.Fatal("htmx script tag not found")
26+ }
27+ })
28+
29+ t.Run("tailwind css link", func(t *testing.T) {
30+ if !strings.Contains(string(body), "@tailwindcss") {
31+ t.Fatal("tailwind css link not found")
32+ }
33+ })
34+
35+ t.Run("title", func(t *testing.T) {
36+ if !strings.Contains(string(body), "Scrap Paper") {
37+ t.Fatal("title not found")
38+ }
39+ })
40+}
scrap-paper/storage_test.go+0−1
@@ -83,6 +83,5 @@ func TestScrapPaperCRUD(t *testing.T) {
8383 if err != sql.ErrNoRows {
8484 t.Fatalf("Row should not be found: %v", err)
8585 }
86-
8786 })
8887 }