Added test coverage for template renderer.
This commit is contained in:
parent
6501621136
commit
388718598e
6 changed files with 183 additions and 35 deletions
|
|
@ -46,12 +46,15 @@ func (c *Controller) RenderPage(ctx echo.Context, page Page) error {
|
|||
page.AppName = c.Container.Config.App.Name
|
||||
}
|
||||
|
||||
// Check if this is an HTMX request
|
||||
// Check if this is an HTMX non-boosted request which indicates that only partial
|
||||
// content should be rendered
|
||||
if page.HTMX.Request.Enabled && !page.HTMX.Request.Boosted {
|
||||
// Disable caching
|
||||
page.Cache.Enabled = false
|
||||
|
||||
// Parse and execute
|
||||
// Parse and execute the templates only for the content portion of the page
|
||||
// The templates used for this partial request will be:
|
||||
// 1. The base htmx template which omits the layout and only includes the content template
|
||||
// 2. The content template specified in Page.Name
|
||||
// 3. All templates within the components directory
|
||||
// Also included is the function map provided by the funcmap package
|
||||
buf, err = c.Container.TemplateRenderer.ParseAndExecute(
|
||||
"page:htmx",
|
||||
page.Name,
|
||||
|
|
@ -150,10 +153,10 @@ func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer)
|
|||
// Redirect redirects to a given route name with optional route parameters
|
||||
func (c *Controller) Redirect(ctx echo.Context, route string, routeParams ...interface{}) error {
|
||||
url := ctx.Echo().Reverse(route, routeParams)
|
||||
// TODO: HTMX redirect?
|
||||
return ctx.Redirect(http.StatusFound, url)
|
||||
}
|
||||
|
||||
// Fail is a helper to fail a request by returning a 500 error and logging the error
|
||||
func (c *Controller) Fail(ctx echo.Context, err error, log string) error {
|
||||
ctx.Logger().Errorf("%s: %v", log, err)
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"goweb/config"
|
||||
"goweb/htmx"
|
||||
"goweb/middleware"
|
||||
"goweb/services"
|
||||
"goweb/tests"
|
||||
|
|
@ -109,6 +110,39 @@ func TestController_RenderPage(t *testing.T) {
|
|||
assert.Empty(t, expectedTemplates)
|
||||
})
|
||||
|
||||
t.Run("htmx rendering", func(t *testing.T) {
|
||||
ctx, _, ctr, p := setup()
|
||||
p.HTMX.Request.Enabled = true
|
||||
p.HTMX.Response = &htmx.Response{
|
||||
Trigger: "trigger",
|
||||
}
|
||||
err := ctr.RenderPage(ctx, p)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Check HTMX header
|
||||
assert.Equal(t, "trigger", ctx.Response().Header().Get(htmx.HeaderTrigger))
|
||||
|
||||
// Check the template cache
|
||||
parsed, err := c.TemplateRenderer.Load("page:htmx", p.Name)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Check that all expected templates were parsed.
|
||||
// This includes the name, htmx and all components
|
||||
expectedTemplates := make(map[string]bool)
|
||||
expectedTemplates[p.Name+config.TemplateExt] = true
|
||||
expectedTemplates["htmx"+config.TemplateExt] = true
|
||||
components, err := ioutil.ReadDir(c.TemplateRenderer.GetTemplatesPath() + "/components")
|
||||
require.NoError(t, err)
|
||||
for _, f := range components {
|
||||
expectedTemplates[f.Name()] = true
|
||||
}
|
||||
|
||||
for _, v := range parsed.Templates() {
|
||||
delete(expectedTemplates, v.Name())
|
||||
}
|
||||
assert.Empty(t, expectedTemplates)
|
||||
})
|
||||
|
||||
t.Run("page cache", func(t *testing.T) {
|
||||
ctx, rec, ctr, p := setup()
|
||||
p.Cache.Enabled = true
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue