components/components_test.go (view raw)
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | package components_test
import (
"errors"
"io"
"os"
"testing"
g "alin.ovh/gomponents"
. "alin.ovh/gomponents/components"
. "alin.ovh/gomponents/html"
"alin.ovh/gomponents/internal/assert"
)
func TestHTML5(t *testing.T) {
t.Run("returns an html5 document template", func(t *testing.T) {
e := HTML5(HTML5Props{
Title: "Hat",
Description: "Love hats.",
Language: "en",
Head: []g.Node{Link(Rel("stylesheet"), Href("/hat.css"))},
Body: []g.Node{Div()},
})
assert.Equal(
t,
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Hat</title><meta name="description" content="Love hats."><link rel="stylesheet" href="/hat.css"></head><body><div></div></body></html>`,
e,
)
})
t.Run(
"returns no language, description, and extra head/body elements if empty",
func(t *testing.T) {
e := HTML5(HTML5Props{
Title: "Hat",
})
assert.Equal(
t,
`<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Hat</title></head><body></body></html>`,
e,
)
},
)
t.Run("returns an html5 document template with additional HTML attributes", func(t *testing.T) {
e := HTML5(HTML5Props{
Title: "Hat",
Description: "Love hats.",
Language: "en",
Head: []g.Node{Link(Rel("stylesheet"), Href("/hat.css"))},
Body: []g.Node{Div()},
HTMLAttrs: []g.Node{Class("h-full"), ID("htmlid")},
})
assert.Equal(
t,
`<!doctype html><html lang="en" class="h-full" id="htmlid"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Hat</title><meta name="description" content="Love hats."><link rel="stylesheet" href="/hat.css"></head><body><div></div></body></html>`,
e,
)
})
}
func TestClasses(t *testing.T) {
t.Run("given a map, returns sorted keys from the map with value true", func(t *testing.T) {
assert.Equal(t, ` class="boheme-hat hat partyhat"`, Classes{
"boheme-hat": true,
"hat": true,
"partyhat": true,
"turtlehat": false,
})
})
t.Run("renders as attribute in an element", func(t *testing.T) {
e := g.El("div", Classes{"hat": true})
assert.Equal(t, `<div class="hat"></div>`, e)
})
t.Run("also works with fmt", func(t *testing.T) {
a := Classes{"hat": true}
if a.String() != ` class="hat"` {
t.FailNow()
}
})
}
func ExampleClasses() {
e := g.El("div", Classes{"party-hat": true, "boring-hat": false})
_ = e.Render(os.Stdout)
// Output: <div class="party-hat"></div>
}
func hat(children ...g.Node) g.Node {
return Div(JoinAttrs("class", g.Group(children), Class("hat")))
}
func partyHat(children ...g.Node) g.Node {
return hat(ID("party-hat"), Class("party"), g.Group(children))
}
type brokenNode struct {
first bool
}
func (b *brokenNode) WriteTo(io.Writer) (int64, error) {
if !b.first {
return 0, nil
}
b.first = false
return 0, errors.New("oh no")
}
func (b *brokenNode) Render(w io.Writer) error {
_, err := b.WriteTo(w)
return err
}
func (b *brokenNode) Type() g.NodeType {
return g.AttributeType
}
func TestJoinAttrs(t *testing.T) {
t.Run("joins classes", func(t *testing.T) {
n := Div(JoinAttrs("class", Class("party"), ID("hey"), Class("hat")))
assert.Equal(t, `<div class="party hat" id="hey"></div>`, n)
})
t.Run("joins classes in groups", func(t *testing.T) {
n := partyHat(Span(ID("party-hat-text"), Class("solid"), Class("gold"), g.Text("Yo.")))
assert.Equal(t, `<div id="party-hat" class="party hat"><span id="party-hat-text" class="solid" class="gold">Yo.</span></div>`, n)
})
t.Run("does nothing if attribute not found", func(t *testing.T) {
n := Div(JoinAttrs("style", Class("party"), ID("hey"), Class("hat")))
assert.Equal(t, `<div class="party" id="hey" class="hat"></div>`, n)
})
t.Run("ignores nodes that can't render", func(t *testing.T) {
n := Div(JoinAttrs("class", Class("party"), ID("hey"), &brokenNode{first: true}, Class("hat")))
assert.Equal(t, `<div class="party hat" id="hey"></div>`, n)
})
}
func myButton(children ...g.Node) g.Node {
return Div(JoinAttrs("class", g.Group(children), Class("button")))
}
func myPrimaryButton(text string) g.Node {
return myButton(Class("primary"), g.Text(text))
}
func ExampleJoinAttrs() {
danceButton := myPrimaryButton("Dance")
_ = danceButton.Render(os.Stdout)
// Output: <div class="primary button">Dance</div>
}
|