149 lines
4.7 KiB
Go
149 lines
4.7 KiB
Go
package templates
|
||
|
||
import (
|
||
"bytes"
|
||
gotemplate "html/template"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/flamego/template"
|
||
|
||
"github.com/tamakyi/TamaBox/internal/db"
|
||
"github.com/tamakyi/TamaBox/internal/dbutil"
|
||
templatepkg "github.com/tamakyi/TamaBox/internal/template"
|
||
)
|
||
|
||
// TestPercentRender 渲染投票结果页并检查百分比与进度条是否真实输出。
|
||
func TestPercentRender(t *testing.T) {
|
||
fs, err := template.EmbedFS(FS, ".", []string{".html"})
|
||
if err != nil {
|
||
t.Fatalf("EmbedFS: %v", err)
|
||
}
|
||
tpl := gotemplate.New("Flamego.Template")
|
||
for _, f := range fs.Files() {
|
||
tt := tpl.New(f.Name())
|
||
for _, funcMap := range templatepkg.FuncMap() {
|
||
tt.Funcs(funcMap)
|
||
}
|
||
dataBytes, err := f.Data()
|
||
if err != nil {
|
||
t.Fatalf("get data of %q: %v", f.Name(), err)
|
||
}
|
||
if _, err := tt.Parse(string(dataBytes)); err != nil {
|
||
t.Fatalf("parse %q: %v", f.Name(), err)
|
||
}
|
||
}
|
||
|
||
data := map[string]interface{}{
|
||
"LangCode": "zh-CN",
|
||
"IsUserPage": true,
|
||
"Vote": &db.Vote{Model: dbutil.Model{ID: 1}, Title: "表单结果", Type: db.VoteTypeForm},
|
||
"Total": int64(1),
|
||
"HasTextFields": true,
|
||
"FormFields": []*db.VoteFormField{
|
||
{Label: "满意度", FieldType: db.VoteFormFieldTypeRadio, Options: `["满意","一般"]`},
|
||
},
|
||
"Results": map[string]interface{}{
|
||
"Type": "form", "FormCount": int64(1), "FormFieldStats": []interface{}{
|
||
map[string]interface{}{
|
||
"Field": map[string]interface{}{"Label": "满意度", "FieldType": "radio"},
|
||
"Total": int64(3),
|
||
"OptionStats": []interface{}{
|
||
map[string]interface{}{"Option": map[string]interface{}{"Content": "满意"}, "Count": int64(2)},
|
||
map[string]interface{}{"Option": map[string]interface{}{"Content": "一般"}, "Count": int64(1)},
|
||
},
|
||
},
|
||
},
|
||
},
|
||
"Submissions": []interface{}{
|
||
map[string]interface{}{"CreatedAt": time.Now(), "Fields": map[string]interface{}{"满意度": "满意"}},
|
||
},
|
||
"CSRFTokenHTML": gotemplate.HTML(""),
|
||
}
|
||
|
||
var buf bytes.Buffer
|
||
if err := tpl.ExecuteTemplate(&buf, "user/vote-results", data); err != nil {
|
||
t.Fatalf("execute: %v", err)
|
||
}
|
||
html := buf.String()
|
||
|
||
// 2/3 取整 = 66%,1/3 取整 = 33%;进度条宽度同步渲染
|
||
for _, want := range []string{"(66%)", "(33%)", "width: 66%", "width: 33%", "uk-progressbar"} {
|
||
if !bytes.Contains([]byte(html), []byte(want)) {
|
||
t.Errorf("HTML missing %q", want)
|
||
}
|
||
}
|
||
}
|
||
|
||
// TestInlineTextRepliesRender 验证表单结果页:文本类字段的逐条提交内容内联在题目
|
||
// 下方、默认收起(含 hidden 属性),点击"查看文本提交(N)"展开。
|
||
func TestInlineTextRepliesRender(t *testing.T) {
|
||
fs, err := template.EmbedFS(FS, ".", []string{".html"})
|
||
if err != nil {
|
||
t.Fatalf("EmbedFS: %v", err)
|
||
}
|
||
tpl := gotemplate.New("Flamego.Template")
|
||
for _, f := range fs.Files() {
|
||
tt := tpl.New(f.Name())
|
||
for _, funcMap := range templatepkg.FuncMap() {
|
||
tt.Funcs(funcMap)
|
||
}
|
||
dataBytes, err := f.Data()
|
||
if err != nil {
|
||
t.Fatalf("get data of %q: %v", f.Name(), err)
|
||
}
|
||
if _, err := tt.Parse(string(dataBytes)); err != nil {
|
||
t.Fatalf("parse %q: %v", f.Name(), err)
|
||
}
|
||
}
|
||
|
||
data := map[string]interface{}{
|
||
"LangCode": "zh-CN",
|
||
"IsUserPage": true,
|
||
"Vote": &db.Vote{Model: dbutil.Model{ID: 1}, Title: "表单结果", Type: db.VoteTypeForm},
|
||
"Total": int64(1),
|
||
"FormFields": []*db.VoteFormField{
|
||
{Model: dbutil.Model{ID: 10}, Label: "建议", FieldType: db.VoteFormFieldTypeTextarea},
|
||
},
|
||
"Results": map[string]interface{}{
|
||
"Type": "form", "FormCount": int64(1), "FormFieldStats": []interface{}{
|
||
map[string]interface{}{
|
||
"Field": map[string]interface{}{"ID": int64(10), "Label": "建议", "FieldType": "textarea"},
|
||
"Total": int64(2),
|
||
"TextCount": int64(2),
|
||
},
|
||
},
|
||
},
|
||
"TextReplies": map[string]interface{}{
|
||
"建议": []interface{}{
|
||
map[string]interface{}{"CreatedAt": time.Now(), "Text": "希望增加夜间模式"},
|
||
map[string]interface{}{"CreatedAt": time.Now(), "Text": "页面很清爽"},
|
||
},
|
||
},
|
||
"CSRFTokenHTML": gotemplate.HTML(""),
|
||
}
|
||
|
||
var buf bytes.Buffer
|
||
if err := tpl.ExecuteTemplate(&buf, "user/vote-results", data); err != nil {
|
||
t.Fatalf("execute: %v", err)
|
||
}
|
||
html := buf.String()
|
||
|
||
// 内联折叠入口 + 两则文本均存在 + 默认收起(hidden)
|
||
for _, want := range []string{
|
||
"查看文本提交(2)",
|
||
"希望增加夜间模式",
|
||
"页面很清爽",
|
||
`id="replies-10"`,
|
||
`id="caret-10"`,
|
||
} {
|
||
if !bytes.Contains([]byte(html), []byte(want)) {
|
||
t.Errorf("HTML missing %q", want)
|
||
}
|
||
}
|
||
// 折叠内容默认 hidden,不随页面始终铺在底部
|
||
if !bytes.Contains([]byte(html), []byte(`id="replies-10" hidden`)) {
|
||
t.Errorf("inline text replies should be hidden by default")
|
||
}
|
||
}
|