internal/httpd/facets_test.go

32a5f76e5b270097b63a5bba9a43557cf50ad63d
gitbay/internal/httpd/facets_test.go history · blame · raw

80 lines · 3305 bytes

 1package httpd
 2
 3import (
 4	"net/url"
 5	"testing"
 6
 7	"gitbay.org/gitbay/internal/store"
 8)
 9
10// A facet link keeps every other active filter, sets its own, and clears
11// its own when it is already active (desktop layout spec).
12func TestFacetHrefKeepsOtherFilters(t *testing.T) {
13	base := url.Values{"state": {"open"}, "label": {"bug"}, "q": {"crash"}}
14	if got := facetHref(base, "milestone", "v2"); got != "?label=bug&milestone=v2&q=crash&state=open" {
15		t.Errorf("set: %q", got)
16	}
17	if got := facetHref(base, "label", ""); got != "?q=crash&state=open" {
18		t.Errorf("clear: %q", got)
19	}
20	if got := facetHref(base, "state", "closed"); got != "?label=bug&q=crash&state=closed" {
21		t.Errorf("replace: %q", got)
22	}
23}
24
25func TestListFacetsGroups(t *testing.T) {
26	base := url.Values{"state": {"open"}, "label": {"bug"}}
27	labels := []store.Label{{Name: "bug", Issues: 2, MRs: 1}, {Name: "docs", Issues: 0, MRs: 3}}
28	ms := []store.Milestone{{Title: "v2", OpenItems: 4}}
29	groups := listFacets(base, []string{"open", "closed", "all"}, "open", labels, ms, false)
30	if len(groups) != 3 || groups[0].Title != "State" || groups[1].Title != "Labels" || groups[2].Title != "Milestones" {
31		t.Fatalf("groups: %+v", groups)
32	}
33	st := groups[0].Items
34	if !st[0].Active || st[0].Href != "?label=bug&state=open" || st[1].Active || st[1].Href != "?label=bug&state=closed" {
35		t.Errorf("state items: %+v", st)
36	}
37	lb := groups[1].Items
38	// docs has no issues, so the issue list does not offer it (#237)
39	if len(lb) != 1 {
40		t.Fatalf("labels: %+v", lb)
41	}
42	if lb[0].Label != "bug" || lb[0].Count != 2 || !lb[0].Active || lb[0].Href != "?state=open" {
43		t.Errorf("active label clears itself: %+v", lb[0])
44	}
45	if m := groups[2].Items[0]; m.Label != "v2" || m.Count != 4 || m.Href != "?label=bug&milestone=v2&state=open" {
46		t.Errorf("milestone: %+v", m)
47	}
48	// on the MR list a label's count is its MR count, and both labels
49	// have merge requests, so both are offered
50	mr := listFacets(base, []string{"open"}, "open", labels, nil, true)
51	if len(mr[1].Items) != 2 || mr[1].Items[1].Count != 3 {
52		t.Errorf("mr counts: %+v", mr[1].Items)
53	}
54}
55
56// A count of zero is a link to an empty list, so it is left out — unless
57// it is the filter in force, which has to stay clickable to clear (#237).
58func TestListFacetsDropsEmpty(t *testing.T) {
59	labels := []store.Label{{Name: "bug", Issues: 0}, {Name: "docs", Issues: 0}}
60	ms := []store.Milestone{{Title: "v2", OpenItems: 0}, {Title: "v3", OpenItems: 1}}
61
62	groups := listFacets(url.Values{"state": {"open"}}, []string{"open"}, "open", labels, ms, false)
63	if n := len(groups[1].Items); n != 0 {
64		t.Errorf("empty labels offered: %+v", groups[1].Items)
65	}
66	if n := len(groups[2].Items); n != 1 || groups[2].Items[0].Label != "v3" {
67		t.Errorf("milestones: %+v", groups[2].Items)
68	}
69
70	base := url.Values{"state": {"open"}, "label": {"bug"}, "milestone": {"v2"}}
71	groups = listFacets(base, []string{"open"}, "open", labels, ms, false)
72	lb := groups[1].Items
73	if len(lb) != 1 || lb[0].Label != "bug" || !lb[0].Active || lb[0].Href != "?milestone=v2&state=open" {
74		t.Errorf("active empty label dropped: %+v", lb)
75	}
76	mg := groups[2].Items
77	if len(mg) != 2 || mg[0].Label != "v2" || !mg[0].Active || mg[0].Href != "?label=bug&state=open" {
78		t.Errorf("active empty milestone dropped: %+v", mg)
79	}
80}