internal/httpd/explorepage_test.go
93 lines · 3328 bytes
1package httpd
2
3import (
4 "fmt"
5 "strings"
6 "testing"
7
8 "gitbay.org/gitbay/internal/store"
9 "gitbay.org/gitbay/internal/web"
10)
11
12// A filtered explore showed a shorter list and nothing else: no count, no
13// restatement of the query, and no way back to the unfiltered view (#243).
14func TestExplorePageSummarisesTheFilter(t *testing.T) {
15 repos := []describedRepo{
16 {Repo: store.Repo{OwnerName: "krz", Name: "gitbay"}, Desc: "A CLI-first git forge."},
17 }
18 out := renderExplore(t, "forge", repos)
19 for _, want := range []string{"1 repository", "matching <strong>forge</strong>", `href="/explore"`, "clear filter"} {
20 if !strings.Contains(out, want) {
21 t.Errorf("explore.html missing %q:\n%s", want, out)
22 }
23 }
24
25 // Nothing to clear when nothing is filtered.
26 if out := renderExplore(t, "", repos); strings.Contains(out, "clear filter") {
27 t.Errorf("unfiltered explore offers a clear:\n%s", out)
28 } else if !strings.Contains(out, "1 repository") {
29 t.Errorf("unfiltered explore has no count:\n%s", out)
30 }
31
32 // A filter that matched nothing says so rather than claiming the
33 // instance has no public repositories.
34 out = renderExplore(t, "nothing", nil)
35 if !strings.Contains(out, "nothing matches that filter") {
36 t.Errorf("empty filtered list reads as an empty instance:\n%s", out)
37 }
38 if !strings.Contains(out, "0 repositories") {
39 t.Errorf("empty filtered list has no count:\n%s", out)
40 }
41}
42
43func renderExplore(t *testing.T, q string, repos []describedRepo) string {
44 t.Helper()
45 p := explorePage{basePage: basePage{Site: "gitbay"}, Tab: "explore", Query: q}
46 p.Repos, p.Total, p.Page, p.Pages = pageOf(repos, "")
47 return renderExplorePage(t, p)
48}
49
50func renderExplorePage(t *testing.T, p explorePage) string {
51 t.Helper()
52 var sb strings.Builder
53 if err := web.Render(&sb, "explore.html", p); err != nil {
54 t.Fatalf("render: %v", err)
55 }
56 return sb.String()
57}
58
59func TestExplorePages(t *testing.T) {
60 var repos []describedRepo
61 for i := range 45 {
62 repos = append(repos, describedRepo{Repo: store.Repo{OwnerName: "krz", Name: fmt.Sprintf("r%02d", i)}})
63 }
64 for _, tc := range []struct {
65 in string
66 page, len int
67 first string
68 }{
69 {"", 1, 20, "r00"}, {"2", 2, 20, "r20"}, {"3", 3, 5, "r40"},
70 {"9", 3, 5, "r40"}, {"0", 1, 20, "r00"}, {"x", 1, 20, "r00"},
71 } {
72 got, total, page, pages := pageOf(repos, tc.in)
73 if total != 45 || pages != 3 || page != tc.page || len(got) != tc.len || got[0].Name != tc.first {
74 t.Errorf("page %q: total %d, page %d of %d, %d rows from %s", tc.in, total, page, pages, len(got), got[0].Name)
75 }
76 }
77 if got, _, page, pages := pageOf(nil, "2"); len(got) != 0 || page != 1 || pages != 1 {
78 t.Errorf("empty listing: %d rows, page %d of %d", len(got), page, pages)
79 }
80
81 p := explorePage{basePage: basePage{Site: "gitbay"}, Tab: "explore", Query: "go"}
82 p.Repos, p.Total, p.Page, p.Pages = pageOf(repos, "2")
83 p.Prev, p.Next = explorePageURL("go", 1), explorePageURL("go", 3)
84 out := renderExplorePage(t, p)
85 for _, want := range []string{"45 repositories", "page 2 of 3", `href="/explore?q=go"`, `href="/explore?page=3&q=go"`} {
86 if !strings.Contains(out, want) {
87 t.Errorf("explore.html missing %q:\n%s", want, out)
88 }
89 }
90 if out := renderExplore(t, "", repos[:3]); strings.Contains(out, `class="pager"`) {
91 t.Errorf("a single page has a pager:\n%s", out)
92 }
93}