internal/httpd/sidecol_test.go

v1.36.0
gitbay/internal/httpd/sidecol_test.go history · blame · raw

74 lines · 2612 bytes

 1package httpd
 2
 3import (
 4	"html/template"
 5	"net/url"
 6	"strings"
 7	"testing"
 8
 9	"gitbay.org/gitbay/internal/store"
10	"gitbay.org/gitbay/internal/web"
11)
12
13// renderIssues renders issues.html around the facets given, which is the
14// shared sidecol partial every list page uses.
15func renderIssues(t *testing.T, facets []facetGroup) string {
16	t.Helper()
17	var sb strings.Builder
18	err := web.Render(&sb, "issues.html", struct {
19		repoPage
20		State       string
21		Label       string
22		Query       string
23		Filters     []listFilter
24		Facets      []facetGroup
25		Issues      []store.Issue
26		LabelColors map[string]template.CSS
27		Older       string
28	}{repoPage: testRepoPage(), State: "open", Facets: facets})
29	if err != nil {
30		t.Fatalf("render: %v", err)
31	}
32	return sb.String()
33}
34
35// The column's groups sit inside a details.sidedrop whose summary names
36// the filters in force. Below 62rem that disclosure is the column; above
37// it style.css hides the summary and shows the content, so the markup is
38// the same either way (#237).
39func TestSidecolIsADisclosure(t *testing.T) {
40	base := url.Values{"state": {"open"}, "label": {"bug"}}
41	labels := []store.Label{{Name: "bug", Issues: 2}}
42	facets := listFacets(base, []string{"open", "closed"}, "open", labels, nil, false)
43
44	out := renderIssues(t, facets)
45	nav := strings.Index(out, `<nav class="sidecol" aria-label="Filters">`)
46	drop := strings.Index(out, `<details class="sidedrop">`)
47	sum := strings.Index(out, "<summary>")
48	grp := strings.Index(out, `<div class="grp">`)
49	if nav < 0 || drop < nav || sum < drop || grp < sum {
50		t.Fatalf("nav=%d details=%d summary=%d grp=%d\n%s", nav, drop, sum, grp, out)
51	}
52	if !strings.Contains(out, `<span class="sideword">Filters</span>`) {
53		t.Errorf("summary does not head with its word:\n%s", out)
54	}
55	// state and label are both in force, so both are named
56	for _, want := range []string{`<span class="sideon">open</span>`, `<span class="sideon">bug</span>`} {
57		if !strings.Contains(out, want) {
58			t.Errorf("summary does not name %q:\n%s", want, out)
59		}
60	}
61	if n := strings.Count(out, "</details>"); n != 1 {
62		t.Errorf("details not closed once: %d", n)
63	}
64}
65
66// The column leads the list in the markup, because below 62rem it is a
67// control above what it narrows rather than a note after it (#237).
68func TestSidecolPrecedesTheList(t *testing.T) {
69	facets := listFacets(url.Values{"state": {"open"}}, []string{"open"}, "open", nil, nil, false)
70	out := renderIssues(t, facets)
71	if i, j := strings.Index(out, `class="sidecol"`), strings.Index(out, `class="colmain"`); i < 0 || j < i {
72		t.Errorf("sidecol=%d colmain=%d", i, j)
73	}
74}