2021-12-25 08:21:26 -08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-06-15 12:34:24 -07:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-12-25 08:21:26 -08:00
|
|
|
"testing"
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2021-12-25 08:21:26 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/config"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/htmx"
|
2024-07-25 20:35:08 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/models"
|
2024-07-09 17:57:05 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/page"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/tests"
|
2024-07-25 20:35:08 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/templ/layouts"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/templ/pages"
|
2021-12-25 08:21:26 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTemplateRenderer(t *testing.T) {
|
|
|
|
group := "test"
|
|
|
|
id := "parse"
|
|
|
|
|
|
|
|
// Should not exist yet
|
|
|
|
_, err := c.TemplateRenderer.Load(group, id)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// Parse in to the cache
|
2022-01-19 06:14:18 -08:00
|
|
|
tpl, err := c.TemplateRenderer.
|
|
|
|
Parse().
|
|
|
|
Group(group).
|
|
|
|
Key(id).
|
2024-07-25 20:35:08 -07:00
|
|
|
Base("test").
|
|
|
|
Files("emails/test").
|
2022-01-19 06:14:18 -08:00
|
|
|
Store()
|
2021-12-25 08:21:26 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Should exist now
|
|
|
|
parsed, err := c.TemplateRenderer.Load(group, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that all expected templates are included
|
|
|
|
expectedTemplates := make(map[string]bool)
|
2024-07-25 20:35:08 -07:00
|
|
|
expectedTemplates["test"+config.TemplateExt] = true
|
2022-01-19 06:14:18 -08:00
|
|
|
for _, v := range parsed.Template.Templates() {
|
2021-12-25 08:21:26 -08:00
|
|
|
delete(expectedTemplates, v.Name())
|
|
|
|
}
|
|
|
|
assert.Empty(t, expectedTemplates)
|
|
|
|
|
|
|
|
data := struct {
|
|
|
|
StatusCode int
|
|
|
|
}{
|
|
|
|
StatusCode: 500,
|
|
|
|
}
|
2022-01-19 06:14:18 -08:00
|
|
|
buf, err := tpl.Execute(data)
|
2021-12-25 08:21:26 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, buf)
|
2024-07-25 20:35:08 -07:00
|
|
|
assert.Contains(t, buf.String(), "Test email template")
|
2021-12-25 08:21:26 -08:00
|
|
|
|
2022-01-19 06:14:18 -08:00
|
|
|
buf, err = c.TemplateRenderer.
|
|
|
|
Parse().
|
|
|
|
Group(group).
|
|
|
|
Key(id).
|
2024-07-25 20:35:08 -07:00
|
|
|
Base("test").
|
|
|
|
Files("email/test").
|
2022-01-19 06:14:18 -08:00
|
|
|
Execute(data)
|
|
|
|
|
2021-12-25 08:21:26 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, buf)
|
2024-07-25 20:35:08 -07:00
|
|
|
assert.Contains(t, buf.String(), "Test email template")
|
2021-12-25 08:21:26 -08:00
|
|
|
}
|
2024-06-15 12:34:24 -07:00
|
|
|
|
|
|
|
func TestTemplateRenderer_RenderPage(t *testing.T) {
|
|
|
|
setup := func() (echo.Context, *httptest.ResponseRecorder, page.Page) {
|
|
|
|
ctx, rec := tests.NewContext(c.Web, "/test/TestTemplateRenderer_RenderPage")
|
|
|
|
tests.InitSession(ctx)
|
|
|
|
|
|
|
|
p := page.New(ctx)
|
|
|
|
p.Cache.Enabled = false
|
|
|
|
p.Headers["A"] = "b"
|
|
|
|
p.Headers["C"] = "d"
|
|
|
|
p.StatusCode = http.StatusCreated
|
|
|
|
return ctx, rec, p
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("htmx rendering", func(t *testing.T) {
|
|
|
|
ctx, _, p := setup()
|
2024-07-27 12:28:45 -07:00
|
|
|
p.LayoutComponent = layouts.Main
|
2024-06-15 12:34:24 -07:00
|
|
|
p.HTMX.Request.Enabled = true
|
|
|
|
p.HTMX.Response = &htmx.Response{
|
|
|
|
Trigger: "trigger",
|
|
|
|
}
|
|
|
|
|
2024-07-25 20:35:08 -07:00
|
|
|
component := pages.Home(p, []models.Post{})
|
|
|
|
err := c.TemplateRenderer.RenderPageTempl(ctx, p, component)
|
2024-06-15 12:34:24 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-25 20:35:08 -07:00
|
|
|
// Check HTMX header
|
|
|
|
assert.Equal(t, "trigger", ctx.Response().Header().Get(htmx.HeaderTrigger))
|
2024-06-15 12:34:24 -07:00
|
|
|
})
|
|
|
|
}
|
2024-07-25 20:35:08 -07:00
|
|
|
|