fix template test renderer, add category

This commit is contained in:
41666 2025-04-06 22:27:42 -07:00
parent d9146750ba
commit e4317ec4fd
15 changed files with 171 additions and 103 deletions

View file

@ -0,0 +1 @@
<h1>hello world</h1>

View file

@ -0,0 +1,16 @@
package components
import (
"git.sapphic.engineer/roleypoly/v4/types"
)
type CategoryTemplateData struct {
ID string
Name string
Type types.CategoryType
Roles []RoleTemplateData
}
func Category(cat *types.Category, roles []*types.Role) CategoryTemplateData {
return CategoryTemplateData{}
}

View file

@ -0,0 +1,14 @@
<section class="category" data-testid="category-{{.ID}}">
<h3>{{.Name}}</h3>
<div class="role-container">
{{ range $_, $role := .Roles }} {{template "components/role" $role}} {{end}}
{{ if eq .Type "single" }}
<input
type="radio"
name="{{roleInputName .ID}}"
id="{{roleInputID .ID .ID}}"
/>
<label for="{{roleInputID .ID .ID}}">None</label>
{{ end }}
</div>
</section>

View file

@ -0,0 +1,43 @@
package components_test
import (
"fmt"
"testing"
"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 TestCategoryTemplate(t *testing.T) {
c := components.CategoryTemplateData{
ID: "123",
Name: "Multi",
Type: types.CategoryMultiple,
Roles: []components.RoleTemplateData{
components.Role(&fixtures.CategoryMulti, &fixtures.RoleWithDarkColor, true, true),
},
}
html := templatetesting.Template(t, "components/category", c)
assert.Contains(t, html, "<h3>Multi</h3>", "has header")
assert.Contains(t, html, `data-testid="category-123"`, "has testid")
assert.NotContains(t, html, `<input type="radio"`, "has no radios")
assert.NotContains(t, html, fmt.Sprintf(`id="%s"`, utils.RoleInputID("123", fixtures.RoleWithDarkColor.ID)), "has the role")
c = components.CategoryTemplateData{
ID: "456",
Name: "Single",
Roles: []components.RoleTemplateData{
components.Role(&fixtures.CategorySingle, &fixtures.RoleWithDarkColor, true, true),
},
}
html = templatetesting.Template(t, "components/category", c)
assert.Contains(t, html, "<h3>Single</h3>", "has header")
assert.Contains(t, html, `data-testid="category-456"`, "has testid")
assert.NotContains(t, html, `<input type="checkbox"`, "has no checkboxes")
assert.NotContains(t, html, fmt.Sprintf(`id="%s"`, utils.RoleInputID("456", "456")), "has no checkboxes")
assert.NotContains(t, html, fmt.Sprintf(`id="%s"`, utils.RoleInputID("456", fixtures.RoleWithDarkColor.ID)), "has the role")
}

View file

@ -10,6 +10,7 @@ type RoleTemplateData struct {
CategoryID string
Name string
Selected bool
Safe bool
InputType InputType
Colors RoleColors
}
@ -26,7 +27,7 @@ const (
InputRadio InputType = "radio"
)
func Role(category *types.Category, role *types.Role, selected bool) RoleTemplateData {
func Role(category *types.Category, role *types.Role, selected bool, safe bool) RoleTemplateData {
inputType := InputCheckbox
if category.Type == types.CategorySingle {
inputType = InputRadio
@ -41,6 +42,7 @@ func Role(category *types.Category, role *types.Role, selected bool) RoleTemplat
Selected: selected,
InputType: inputType,
Colors: colors,
Safe: safe,
}
}

View file

@ -5,6 +5,7 @@
data-testid="{{$for}}"
>
<input type="{{.InputType}}" id="{{$for}}" {{if eq .InputType
"radio"}}name="category_group_{{.CategoryID}}"{{end}} />
"radio"}}name="category_group_{{.CategoryID}}"{{end}} {{if not
.Safe}}disabled="disabled"{{end}}/>
<label for="{{$for}}">{{.Name}}</label>
</div>

View file

@ -14,39 +14,44 @@ import (
func TestRoleTemplate(t *testing.T) {
c := &fixtures.CategoryMulti
r := &fixtures.RoleWithDarkColor
data := components.Role(c, r, true)
data := components.Role(c, r, true, 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")
assert.Contains(t, html, fmt.Sprintf(`id="%s"`, utils.RoleInputID(c, r)), "input has ID attr")
assert.Contains(t, html, fmt.Sprintf(`for="%s"`, utils.RoleInputID(c, r)), "label has for attr")
assert.NotContains(t, html, fmt.Sprintf(`name="%s"`, utils.RoleInputName(c)), "multi has no name attr")
assert.Contains(t, html, fmt.Sprintf(`id="%s"`, utils.RoleInputID(c.ID, r.ID)), "input has ID attr")
assert.Contains(t, html, fmt.Sprintf(`for="%s"`, utils.RoleInputID(c.ID, r.ID)), "label has for attr")
assert.NotContains(t, html, fmt.Sprintf(`name="%s"`, utils.RoleInputName(c.ID)), "multi has no name attr")
// TODO: selected?
c = &fixtures.CategorySingle
r = &fixtures.RoleWithLightColor
data = components.Role(c, r, false)
data = components.Role(c, r, false, true)
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")
assert.Contains(t, html, fmt.Sprintf(`name="%s"`, utils.RoleInputName(c.ID)), "single has name attr")
data = components.Role(c, r, false, false)
html = templatetesting.Template(t, "components/role", data)
assert.Contains(t, html, `disabled="disabled"`)
}
func TestRole(t *testing.T) {
r := components.Role(&fixtures.CategoryMulti, &fixtures.RoleWithDarkColor, true)
r := components.Role(&fixtures.CategoryMulti, &fixtures.RoleWithDarkColor, true, 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)
r = components.Role(&fixtures.CategorySingle, &fixtures.RoleWithDarkColor, false, true)
assert.Equal(t, components.InputRadio, r.InputType)
assert.False(t, r.Selected)
r = components.Role(&fixtures.CategorySingle, &fixtures.RoleWithLightColor, true)
r = components.Role(&fixtures.CategorySingle, &fixtures.RoleWithLightColor, true, false)
assert.True(t, r.Selected)
assert.False(t, r.Safe)
}
func TestNewRoleColors(t *testing.T) {