Skip to content

Commit

Permalink
tests for shared template data & funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Sarvarov committed Jun 15, 2024
1 parent f64433a commit a8ed26a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 82 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ i.ShareTemplateData("title", "Home page")
#### Share template func

```go
i.ShareTemplateFunc("trim", strings.Trim)
i.ShareTemplateFunc("trim", strings.TrimSpace)
```

```html
<title>{{ trim "foo bar" }}</title>
<title>{{ trim " foo bar " }}</title>
```

#### Pass template data via context (in middleware)
Expand Down
2 changes: 2 additions & 0 deletions examples/vue3_tailwind/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ssr:
npm run build && node bootstrap/ssr/ssr.js
25 changes: 23 additions & 2 deletions inertia.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,41 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin
}

if IsInertiaRequest(r) {
if err := i.doInertiaResponse(w, page); err != nil {
if err = i.doInertiaResponse(w, page); err != nil {
return fmt.Errorf("inertia response: %w", err)
}

return
}

if err := i.doHTMLResponse(w, r, page); err != nil {
if err = i.doHTMLResponse(w, r, page); err != nil {
return fmt.Errorf("html response: %w", err)
}

return nil
}

type page struct {
Component string `json:"component"`
Props Props `json:"props"`
URL string `json:"url"`
Version string `json:"version"`
}

func (i *Inertia) buildPage(r *http.Request, component string, props Props) (*page, error) {
props, err := i.prepareProps(r, component, props)
if err != nil {
return nil, fmt.Errorf("prepare props: %w", err)
}

return &page{
Component: component,
Props: props,
URL: r.RequestURI,
Version: i.version,
}, nil
}

func (i *Inertia) doInertiaResponse(w http.ResponseWriter, page *page) error {
pageJSON, err := i.marshallJSON(page)
if err != nil {
Expand Down
25 changes: 0 additions & 25 deletions inertia_bench_test.go

This file was deleted.

53 changes: 53 additions & 0 deletions inertia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gonertia

import (
"net/http"
"strings"
"testing"
"testing/fstest"
)
Expand Down Expand Up @@ -150,6 +151,58 @@ func TestInertia_Render(t *testing.T) {

assertRootTemplateSuccess(t, i)
})

t.Run("shared funcs", func(t *testing.T) {
t.Parallel()

f := tmpFile(t, `{{ trim " foo bar " }}`)
w, r := requestMock(http.MethodGet, "/")

i := I(func(i *Inertia) {
i.rootTemplatePath = f.Name()
i.sharedTemplateFuncs = TemplateFuncs{
"trim": strings.TrimSpace,
}
})

err := i.Render(w, r, "Some/Component")
if err != nil {
t.Fatalf("unexpected error: %#v", err)
}

got := w.Body.String()
want := "foo bar"

if got != want {
t.Fatalf("got=%s, want=%s", got, want)
}
})

t.Run("shared template data", func(t *testing.T) {
t.Parallel()

f := tmpFile(t, `Hello, {{ .text }}!`)
w, r := requestMock(http.MethodGet, "/")

i := I(func(i *Inertia) {
i.rootTemplatePath = f.Name()
i.sharedTemplateData = TemplateData{
"text": "world",
}
})

err := i.Render(w, r, "Some/Component")
if err != nil {
t.Fatalf("unexpected error: %#v", err)
}

got := w.Body.String()
want := "foo bar"

if got != want {
t.Fatalf("got=%s, want=%s", got, want)
}
})
})

t.Run("inertia request", func(t *testing.T) {
Expand Down
27 changes: 0 additions & 27 deletions page.go

This file was deleted.

26 changes: 0 additions & 26 deletions page_bench_test.go

This file was deleted.

0 comments on commit a8ed26a

Please sign in to comment.