package web
import (
"bytes"
"html/template"
"strings"
"testing"
)
// #376: the live poll goes where its fence was, and each option is a
// button holding its own text.
func TestAPollDrawsInPlaceWithLabelledVotes(t *testing.T) {
var p forumPostRow
p.Hash = "abc"
p.HTML = template.HTML("<p>BEFORE.</p>" + pollSlot + "<p>AFTER.</p>")
p.Poll.Present = true
p.Poll.Question = "Lunch?"
p.Poll.Options = []string{"ramen", "salad"}
p.Poll.Counts = []int{1, 0}
p.Poll.Percents = []int{100, 0}
p.Poll.YourChoice = -1
if !p.PollInline() || string(p.HTMLBefore()) != "<p>BEFORE.</p>" || string(p.HTMLAfter()) != "<p>AFTER.</p>" {
t.Fatalf("FAIL: the body does not split at the poll: %q | %q", p.HTMLBefore(), p.HTMLAfter())
}
var b bytes.Buffer
page := pageData{Repo: "r", ForumRoot: "root", CanWrite: true}
if err := noHooks.template(forumThreadPage).ExecuteTemplate(&b, "f-poll", map[string]any{"P": p, "Page": page}); err != nil {
t.Fatal(err)
}
out := b.String()
if strings.Count(out, `data-hook="poll-vote"`) != 2 {
t.Fatalf("FAIL: want two vote buttons:\n%s", out)
}
parts := strings.Split(out, `data-hook="poll-vote"`)[1:]
for k, o := range []string{"ramen", "salad"} {
button, _, _ := strings.Cut(parts[k], "</button>")
if !strings.Contains(button, `<span class="f-poll-t">`+o+`</span>`) {
t.Errorf("FAIL: option %q is not inside its button: %s", o, button)
}
}
}