internal/httpd/mrsrow_test.go

v1.38.0
gitbay/internal/httpd/mrsrow_test.go history · blame · raw

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