internal/httpd/rendered_test.go

bd49b87fce895e9f0a7588152548fb6e1821ac7d
gitbay/internal/httpd/rendered_test.go history · blame · raw

44 lines · 1734 bytes

 1package httpd
 2
 3import (
 4	"html/template"
 5	"strings"
 6	"testing"
 7)
 8
 9func TestFocusableBlocks(t *testing.T) {
10	got := string(focusableBlocks(`<pre class="chroma">x</pre><pre>y</pre><pre tabindex="-1">z</pre>`))
11	want := `<pre tabindex="0" class="chroma">x</pre><pre tabindex="0">y</pre><pre tabindex="-1">z</pre>`
12	if got != want {
13		t.Fatalf("got  %s\nwant %s", got, want)
14	}
15}
16
17func TestImageAlt(t *testing.T) {
18	cases := map[string]string{
19		`<a href="/b"><img src="https://forge.test/krz/gitbay/badge/build.svg"/></a>`: `<a href="/b"><img alt="build" src="https://forge.test/krz/gitbay/badge/build.svg"/></a>`,
20		`<img src="pic.png?v=2">`:       `<img alt="pic" src="pic.png?v=2">`,
21		`<img alt="" src="pic.png">`:    `<img alt="" src="pic.png">`,
22		`<img alt="a chart" src="c.png">`: `<img alt="a chart" src="c.png">`,
23		`<img>`:                         `<img alt="">`,
24	}
25	for in, want := range cases {
26		if got := string(imageAlt(template.HTML(in))); got != want {
27			t.Errorf("%s\n got %s\nwant %s", in, got, want)
28		}
29	}
30}
31
32// A README badge written as a bare org image link renders as a named
33// image, and an org source block takes keyboard focus.
34func TestOrgReadmeBadgeAndBlocks(t *testing.T) {
35	out := string(renderReadme("README.org", []byte("[[https://forge.test/krz/gitbay/builds][https://forge.test/krz/gitbay/badge/build.svg]]\n\n#+begin_src go\npackage main\n#+end_src\n")))
36	for _, want := range []string{`alt="build"`, `<pre tabindex="0"`} {
37		if !strings.Contains(out, want) {
38			t.Errorf("lacks %s:\n%s", want, out)
39		}
40	}
41	if got := string(renderReadme("README.md", []byte("```go\npackage main\n```\n"))); !strings.Contains(got, `<pre tabindex="0"`) {
42		t.Errorf("markdown fence has no tab stop:\n%s", got)
43	}
44}