package web
import (
"bytes"
"strings"
"testing"
)
// A ticket's page says whether it is open right under its title. The
// state was only in the side panel, below the whole description on a
// phone, so a closed ticket read as a live one.
func TestATicketSaysItsStateUnderItsTitle(t *testing.T) {
for _, c := range []struct{ status, resolution, want string }{
{"Open", "", "open"},
{"Review", "", "review"},
{"Closed", "Fixed", "closed"},
{"Fixed", "", "closed"},
{"Deferred", "", "closed"},
{"", "", "open"},
} {
d := ticketDetail{ID: strings.Repeat("cd", 20), Fields: []ticketField{
{Name: "title", Value: "a crash"}, {Name: "status", Value: c.status}, {Name: "resolution", Value: c.resolution}}}
var buf bytes.Buffer
if err := noHooks.template(ticketPage).ExecuteTemplate(&buf, "content", pageData{Repo: "tk", Ticket: d}); err != nil {
t.Fatal(err)
}
html := buf.String()
hook := `data-hook="ticket-state" data-state="` + c.want + `"`
i := strings.Index(html, hook)
if i < 0 {
t.Errorf("FAIL: status %q shows no %s state", c.status, c.want)
continue
}
if title := strings.Index(html, `class="t-title"`); title < 0 || title > i {
t.Errorf("FAIL: status %q: the state is not under the title", c.status)
}
}
if !t.Failed() {
t.Log("ok: open, review and closed each show under the title")
}
}