internal/httpd/mrsrow_test.go

da232bfde4952959944a074ccec455f38eab3b66
gitbay/internal/httpd/mrsrow_test.go history · blame · raw

72 lines · 2198 bytes

 1package httpd
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gitbay.org/gitbay/internal/web"
 8)
 9
10// mrsPageData mirrors the anonymous struct the mrs handler renders with.
11type mrsPageData struct {
12	repoPage
13	State   string
14	Query   string
15	Filters []listFilter
16	MRs     []mrRow
17	Older   string
18}
19
20func renderMRs(t *testing.T, rows []mrRow, state string) string {
21	t.Helper()
22	var sb strings.Builder
23	if err := web.Render(&sb, "mrs.html", mrsPageData{
24		repoPage: testRepoPage(), State: state, MRs: rows,
25	}); err != nil {
26		t.Fatalf("render: %v", err)
27	}
28	return sb.String()
29}
30
31// Each row carries its head's combined check state as a chip linking to
32// the diff, so a reviewer can tell what needs attention without opening
33// every request (#230).
34func TestMRListRowShowsCheck(t *testing.T) {
35	out := renderMRs(t, []mrRow{{MR: testMR("open"), Check: "success"}}, "open")
36	if !strings.Contains(out, `class="chip check-success"`) {
37		t.Errorf("no check chip:\n%s", out)
38	}
39	if !strings.Contains(out, `/krz/gitbay/mrs/42?view=diff`) {
40		t.Errorf("check chip does not link to the diff:\n%s", out)
41	}
42}
43
44// No checks have reported yet: no chip, not an empty one.
45func TestMRListRowHidesEmptyCheck(t *testing.T) {
46	out := renderMRs(t, []mrRow{{MR: testMR("open")}}, "open")
47	if strings.Contains(out, "check-") {
48		t.Errorf("empty check rendered a chip:\n%s", out)
49	}
50}
51
52// The comment count folds conversation and diff-thread comments (see
53// store.MRCommentCounts) and is hidden, not zero, when there are none.
54func TestMRListRowShowsCommentCount(t *testing.T) {
55	out := renderMRs(t, []mrRow{{MR: testMR("open"), Comments: 3}}, "open")
56	if !strings.Contains(out, `title="3 comments"`) {
57		t.Errorf("no plural comment count:\n%s", out)
58	}
59	if !strings.Contains(out, `>3 <span class="vh">comments</span>`) {
60		t.Errorf("no comment count text:\n%s", out)
61	}
62
63	one := renderMRs(t, []mrRow{{MR: testMR("open"), Comments: 1}}, "open")
64	if !strings.Contains(one, `title="1 comment"`) {
65		t.Errorf("comment count not singular for 1:\n%s", one)
66	}
67
68	none := renderMRs(t, []mrRow{{MR: testMR("open")}}, "open")
69	if strings.Contains(none, "vh\">comments</span>") {
70		t.Errorf("zero comments rendered a count:\n%s", none)
71	}
72}