internal/httpd/mrsrow_test.go

3bcdce33fb9a2309312854331359d376171c7368
gitbay/internal/httpd/mrsrow_test.go history · blame · raw

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