move presentation to templates

This commit is contained in:
41666 2025-04-06 20:55:46 -07:00
parent 1c533926ca
commit d9146750ba
5 changed files with 70 additions and 69 deletions

View file

@ -0,0 +1,57 @@
package components
import (
"git.sapphic.engineer/roleypoly/v4/types"
"git.sapphic.engineer/roleypoly/v4/utils"
)
type RoleTemplateData struct {
ID string
CategoryID string
Name string
Selected bool
InputType InputType
Colors RoleColors
}
type RoleColors struct {
Main string
Alt string
}
type InputType string
const (
InputCheckbox InputType = "checkbox"
InputRadio InputType = "radio"
)
func Role(category *types.Category, role *types.Role, selected bool) RoleTemplateData {
inputType := InputCheckbox
if category.Type == types.CategorySingle {
inputType = InputRadio
}
colors := NewRoleColors(role.Color)
return RoleTemplateData{
ID: role.ID,
CategoryID: category.ID,
Name: role.Name,
Selected: selected,
InputType: inputType,
Colors: colors,
}
}
func NewRoleColors(roleColor uint32) RoleColors {
// TODO: no color
r, g, b := utils.IntToRgb(roleColor)
altR, altG, altB := utils.AltColor(r, g, b)
return RoleColors{
Main: utils.RgbToString(r, g, b),
Alt: utils.RgbToString(altR, altG, altB),
}
}

View file

@ -4,23 +4,18 @@ import (
"fmt"
"testing"
"git.sapphic.engineer/roleypoly/v4/presentation"
"git.sapphic.engineer/roleypoly/v4/templates/components"
"git.sapphic.engineer/roleypoly/v4/templates/templatetesting"
"git.sapphic.engineer/roleypoly/v4/types"
"git.sapphic.engineer/roleypoly/v4/types/fixtures"
"git.sapphic.engineer/roleypoly/v4/utils"
"github.com/stretchr/testify/assert"
)
func renderRole(t *testing.T, c *types.Category, r *types.Role, s bool) string {
data := presentation.Role(c, r, s)
return templatetesting.Template(t, "components/role", data)
}
func TestRole(t *testing.T) {
func TestRoleTemplate(t *testing.T) {
c := &fixtures.CategoryMulti
r := &fixtures.RoleWithDarkColor
html := renderRole(t, c, r, false)
data := components.Role(c, r, true)
html := templatetesting.Template(t, "components/role", data)
assert.Contains(t, html, "--role-color: #a20000;", "role color is set")
assert.Contains(t, html, `type="checkbox"`, "multi has input type=checkbox")
assert.Contains(t, html, fmt.Sprintf("--contrast-color: %s;", utils.RgbToString(utils.AltColor(162, 0, 0))), "contrast color is set")
@ -31,10 +26,31 @@ func TestRole(t *testing.T) {
c = &fixtures.CategorySingle
r = &fixtures.RoleWithLightColor
html = renderRole(t, c, r, true)
data = components.Role(c, r, false)
html = templatetesting.Template(t, "components/role", data)
assert.Contains(t, html, `type="radio"`, "single has input type=radio")
assert.Contains(t, html, fmt.Sprintf("--contrast-color: %s;", utils.RgbToString(utils.AltColor(0xff, 0xaa, 0x88))), "contrast color")
assert.Contains(t, html, fmt.Sprintf(`name="%s"`, utils.RoleInputName(c)), "single has name attr")
}
// TODO: these can probably be string utils that are injected as functions into template
func TestRole(t *testing.T) {
r := components.Role(&fixtures.CategoryMulti, &fixtures.RoleWithDarkColor, true)
assert.Equal(t, fixtures.RoleWithDarkColor.ID, r.ID)
assert.Equal(t, fixtures.RoleWithDarkColor.Name, r.Name)
assert.Equal(t, components.InputCheckbox, r.InputType)
assert.Equal(t, "#a20000", r.Colors.Main)
assert.True(t, r.Selected)
r = components.Role(&fixtures.CategorySingle, &fixtures.RoleWithDarkColor, false)
assert.Equal(t, components.InputRadio, r.InputType)
assert.False(t, r.Selected)
r = components.Role(&fixtures.CategorySingle, &fixtures.RoleWithLightColor, true)
assert.True(t, r.Selected)
}
func TestNewRoleColors(t *testing.T) {
c := components.NewRoleColors(0xa20000)
assert.Equal(t, "#a20000", c.Main)
assert.Equal(t, "#ffd8d8", c.Alt)
}

View file

@ -0,0 +1,16 @@
package layouts_test
import (
"testing"
"git.sapphic.engineer/roleypoly/v4/templates/templatetesting"
"github.com/stretchr/testify/assert"
)
func TestMainLayout(t *testing.T) {
r := templatetesting.Template(t, "layouts/main", struct{ HeadTitle string }{HeadTitle: "roleypoly"})
assert.Contains(t, r, "%%EMBED%%", "has {{embed}}")
assert.Contains(t, r, "<nav>", "loaded navigation (open)")
assert.Contains(t, r, "</nav>", "loaded navigation (close)")
assert.Contains(t, r, "<title>roleypoly</title>", "sets title")
}