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

@ -1,31 +0,0 @@
package presentation_test
import (
"testing"
"git.sapphic.engineer/roleypoly/v4/presentation"
"git.sapphic.engineer/roleypoly/v4/types/fixtures"
"github.com/stretchr/testify/assert"
)
func TestRole(t *testing.T) {
r := presentation.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, presentation.InputCheckbox, r.InputType)
assert.Equal(t, "#a20000", r.Colors.Main)
assert.True(t, r.Selected)
r = presentation.Role(&fixtures.CategorySingle, &fixtures.RoleWithDarkColor, false)
assert.Equal(t, presentation.InputRadio, r.InputType)
assert.False(t, r.Selected)
r = presentation.Role(&fixtures.CategorySingle, &fixtures.RoleWithLightColor, true)
assert.True(t, r.Selected)
}
func TestGetColors(t *testing.T) {
c := presentation.GetColors(0xa20000)
assert.Equal(t, "#a20000", c.Main)
assert.Equal(t, "#ffd8d8", c.Alt)
}

View file

@ -1,10 +1,24 @@
package presentation
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 (
@ -12,24 +26,15 @@ const (
InputRadio InputType = "radio"
)
type PresentableRole struct {
ID string
CategoryID string
Name string
Selected bool
InputType InputType
Colors PresentableRoleColors
}
func Role(category *types.Category, role *types.Role, selected bool) PresentableRole {
func Role(category *types.Category, role *types.Role, selected bool) RoleTemplateData {
inputType := InputCheckbox
if category.Type == types.CategorySingle {
inputType = InputRadio
}
colors := GetColors(role.Color)
colors := NewRoleColors(role.Color)
return PresentableRole{
return RoleTemplateData{
ID: role.ID,
CategoryID: category.ID,
Name: role.Name,
@ -39,18 +44,13 @@ func Role(category *types.Category, role *types.Role, selected bool) Presentable
}
}
type PresentableRoleColors struct {
Main string
Alt string
}
func GetColors(roleColor uint32) PresentableRoleColors {
func NewRoleColors(roleColor uint32) RoleColors {
// TODO: no color
r, g, b := utils.IntToRgb(roleColor)
altR, altG, altB := utils.AltColor(r, g, b)
return PresentableRoleColors{
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")
}

View file

@ -7,7 +7,7 @@ import (
"git.sapphic.engineer/roleypoly/v4/auth/authmiddleware"
"git.sapphic.engineer/roleypoly/v4/discord"
"git.sapphic.engineer/roleypoly/v4/presentation"
"git.sapphic.engineer/roleypoly/v4/templates/components"
"git.sapphic.engineer/roleypoly/v4/types"
"git.sapphic.engineer/roleypoly/v4/types/fixtures"
)
@ -39,12 +39,12 @@ func (t *TestingController) TestTemplate(c fiber.Ctx) error {
cat1 := fixtures.Category(fixtures.CategoryMulti)
cat2 := fixtures.Category(fixtures.CategorySingle)
return c.Render("tests/"+which, fiber.Map{
"TestRole": presentation.Role(cat1, &fixtures.RoleWithDarkColor, false),
"TestRole2": presentation.Role(cat1, &fixtures.RoleWithDarkMediumColor, false),
"TestRole3": presentation.Role(cat1, &fixtures.RoleWithLightColor, true),
"TestRole4": presentation.Role(cat1, &fixtures.RoleWithDarkColor, false),
"TestRole5": presentation.Role(cat2, &fixtures.RoleWithLightColor, true),
"TestRole6": presentation.Role(cat1, &fixtures.RoleWithLightMediumColor, false),
"TestRole": components.Role(cat1, &fixtures.RoleWithDarkColor, false),
"TestRole2": components.Role(cat1, &fixtures.RoleWithDarkMediumColor, false),
"TestRole3": components.Role(cat1, &fixtures.RoleWithLightColor, true),
"TestRole4": components.Role(cat1, &fixtures.RoleWithDarkColor, false),
"TestRole5": components.Role(cat2, &fixtures.RoleWithLightColor, true),
"TestRole6": components.Role(cat1, &fixtures.RoleWithLightMediumColor, false),
}, "layouts/main")
}