pagination_test.go
⎇
Raw
1package e2e
2
3import (
4 "net/url"
5 "strconv"
6 "strings"
7 "testing"
8)
9
10// pageValidPatch is a minimal but well-formed format-patch file.
11var pageValidPatch = strings.Join([]string{
12 "From a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2 Mon Sep 17 00:00:00 2001",
13 "From: Test User <test@example.com>",
14 "Date: Mon, 01 Jan 2024 12:00:00 +0000",
15 "Subject: [PATCH] Add f.txt",
16 "",
17 "---",
18 "diff --git a/f.txt b/f.txt",
19 "new file mode 100644",
20 "index 0000000..9daeafb",
21 "--- /dev/null",
22 "+++ b/f.txt",
23 "@@ -0,0 +1 @@",
24 "+x",
25 "",
26}, "\n")
27
28func TestPagination(t *testing.T) {
29 e := newEnv(t)
30 admin := e.admin()
31 e.createRepo(admin, "paged-repo")
32
33 // 21 issues and 21 patches each spill onto a second page at 20 per page.
34 for i := 1; i <= 21; i++ {
35 admin.post("/paged-repo/issues", url.Values{
36 "title": {"Issue number " + strconv.Itoa(i)}, "body": {""},
37 }).mustRedirect("/paged-repo/issues/")
38 }
39 for i := 1; i <= 21; i++ {
40 admin.postMultipart("/paged-repo/patches",
41 url.Values{"title": {"Patch number " + strconv.Itoa(i)}},
42 file{Field: "patch_file", Name: "bulk.patch", Content: []byte(pageValidPatch)},
43 ).mustRedirect("/paged-repo/patches/")
44 }
45
46 t.Run("repo list page 1 shows repos and no pagination when few repos", func(t *testing.T) {
47 if n := admin.get("/").Count(".repo-name"); n == 0 {
48 t.Error("no repos listed")
49 }
50 })
51
52 t.Run("issue list page 1 shows at most 20 items", func(t *testing.T) {
53 if n := admin.get("/paged-repo/issues").Count(".issue-item"); n > 20 {
54 t.Errorf("issues on page 1 = %d", n)
55 }
56 })
57
58 t.Run("issue list pagination nav appears when more than 20 issues", func(t *testing.T) {
59 if !admin.get("/paged-repo/issues").Has(".pagination") {
60 t.Error("pagination missing")
61 }
62 })
63
64 t.Run("issue list page 2 shows remaining issues", func(t *testing.T) {
65 n := admin.get("/paged-repo/issues?page=2").Count(".issue-item")
66 if n == 0 || n > 20 {
67 t.Errorf("issues on page 2 = %d", n)
68 }
69 })
70
71 t.Run("issue list page 2 prev link goes to page 1", func(t *testing.T) {
72 href := admin.get("/paged-repo/issues?page=2").Attr(".pagination-prev .pagination-btn", "href")
73 if !strings.Contains(href, "page=1") {
74 t.Errorf("prev href = %q", href)
75 }
76 })
77
78 t.Run("issue list page 1 next link goes to page 2", func(t *testing.T) {
79 href := admin.get("/paged-repo/issues").Attr(".pagination-next .pagination-btn", "href")
80 if !strings.Contains(href, "page=2") {
81 t.Errorf("next href = %q", href)
82 }
83 })
84
85 t.Run("patch list page 1 shows at most 20 items", func(t *testing.T) {
86 if n := admin.get("/paged-repo/patches").Count(".issue-item"); n > 20 {
87 t.Errorf("patches on page 1 = %d", n)
88 }
89 })
90
91 t.Run("patch list pagination nav appears when more than 20 patches", func(t *testing.T) {
92 if !admin.get("/paged-repo/patches").Has(".pagination") {
93 t.Error("pagination missing")
94 }
95 })
96
97 t.Run("patch list page 2 shows remaining patches", func(t *testing.T) {
98 if n := admin.get("/paged-repo/patches?page=2").Count(".issue-item"); n == 0 {
99 t.Error("no patches on page 2")
100 }
101 })
102
103 t.Run("commit log with few commits shows no cursor nav", func(t *testing.T) {
104 // paged-repo has no commits, so there is nothing to paginate.
105 if n := admin.get("/paged-repo").Count(".commit-cursor-nav"); n != 0 {
106 t.Errorf("commit-cursor-nav count = %d", n)
107 }
108 })
109}
110