internal/httpd/explorepage_test.go

v1.33.0
gitbay/internal/httpd/explorepage_test.go history · blame · raw

56 lines · 1771 bytes

 1package httpd
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gitbay.org/gitbay/internal/store"
 8	"gitbay.org/gitbay/internal/web"
 9)
10
11// A filtered explore showed a shorter list and nothing else: no count, no
12// restatement of the query, and no way back to the unfiltered view (#243).
13func TestExplorePageSummarisesTheFilter(t *testing.T) {
14	repos := []describedRepo{
15		{Repo: store.Repo{OwnerName: "krz", Name: "gitbay"}, Desc: "A CLI-first git forge."},
16	}
17	out := renderExplore(t, "forge", repos)
18	for _, want := range []string{"1 repository", "matching <strong>forge</strong>", `href="/explore"`, "clear filter"} {
19		if !strings.Contains(out, want) {
20			t.Errorf("explore.html missing %q:\n%s", want, out)
21		}
22	}
23
24	// Nothing to clear when nothing is filtered.
25	if out := renderExplore(t, "", repos); strings.Contains(out, "clear filter") {
26		t.Errorf("unfiltered explore offers a clear:\n%s", out)
27	} else if !strings.Contains(out, "1 repository") {
28		t.Errorf("unfiltered explore has no count:\n%s", out)
29	}
30
31	// A filter that matched nothing says so rather than claiming the
32	// instance has no public repositories.
33	out = renderExplore(t, "nothing", nil)
34	if !strings.Contains(out, "nothing matches that filter") {
35		t.Errorf("empty filtered list reads as an empty instance:\n%s", out)
36	}
37	if !strings.Contains(out, "0 repositories") {
38		t.Errorf("empty filtered list has no count:\n%s", out)
39	}
40}
41
42func renderExplore(t *testing.T, q string, repos []describedRepo) string {
43	t.Helper()
44	var sb strings.Builder
45	err := web.Render(&sb, "explore.html", struct {
46		basePage
47		Tab    string
48		Query  string
49		Facets []facetGroup
50		Repos  []describedRepo
51	}{basePage{Site: "gitbay"}, "explore", q, nil, repos})
52	if err != nil {
53		t.Fatalf("render: %v", err)
54	}
55	return sb.String()
56}