about summary refs log tree commit diff stats
path: root/html/attributes_test.go
blob: 665dc9414acbb0d999e81c5fa51e9227a0b505b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package html_test

import (
	"fmt"
	"testing"

	g "github.com/maragudk/gomponents"
	. "github.com/maragudk/gomponents/html"
	"github.com/maragudk/gomponents/internal/assert"
)

func TestBooleanAttributes(t *testing.T) {
	cases := map[string]func() g.Node{
		"async":       Async,
		"autofocus":   AutoFocus,
		"autoplay":    AutoPlay,
		"controls":    Controls,
		"defer":       Defer,
		"disabled":    Disabled,
		"loop":        Loop,
		"multiple":    Multiple,
		"muted":       Muted,
		"playsinline": PlaysInline,
		"readonly":    ReadOnly,
		"required":    Required,
		"selected":    Selected,
	}

	for name, fn := range cases {
		t.Run(fmt.Sprintf("should output %v", name), func(t *testing.T) {
			n := g.El("div", fn())
			assert.Equal(t, fmt.Sprintf(`<div %v></div>`, name), n)
		})
	}
}

func TestSimpleAttributes(t *testing.T) {
	cases := map[string]func(string) g.Node{
		"accept":       Accept,
		"action":       Action,
		"alt":          Alt,
		"as":           As,
		"autocomplete": AutoComplete,
		"charset":      Charset,
		"class":        Class,
		"cols":         Cols,
		"content":      Content,
		"for":          For,
		"form":         FormAttr,
		"height":       Height,
		"href":         Href,
		"id":           ID,
		"lang":         Lang,
		"loading":      Loading,
		"max":          Max,
		"maxlength":    MaxLength,
		"method":       Method,
		"min":          Min,
		"minlength":    MinLength,
		"name":         Name,
		"pattern":      Pattern,
		"placeholder":  Placeholder,
		"poster":       Poster,
		"preload":      Preload,
		"rel":          Rel,
		"role":         Role,
		"rows":         Rows,
		"src":          Src,
		"srcset":       SrcSet,
		"style":        StyleAttr,
		"tabindex":     TabIndex,
		"target":       Target,
		"title":        TitleAttr,
		"type":         Type,
		"value":        Value,
		"width":        Width,
	}

	for name, fn := range cases {
		t.Run(fmt.Sprintf(`should output %v="hat"`, name), func(t *testing.T) {
			n := g.El("div", fn("hat"))
			assert.Equal(t, fmt.Sprintf(`<div %v="hat"></div>`, name), n)
		})
	}
}

func TestAria(t *testing.T) {
	t.Run("returns an attribute which name is prefixed with aria-", func(t *testing.T) {
		n := Aria("selected", "true")
		assert.Equal(t, ` aria-selected="true"`, n)
	})
}

func TestDataAttr(t *testing.T) {
	t.Run("returns an attribute which name is prefixed with data-", func(t *testing.T) {
		n := DataAttr("id", "partyhat")
		assert.Equal(t, ` data-id="partyhat"`, n)
	})
}