2020-10-29 04:03:43 -07:00
|
|
|
package components_test
|
|
|
|
|
|
|
|
import (
|
2022-11-03 03:32:16 -07:00
|
|
|
"os"
|
2020-10-29 04:03:43 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
g "github.com/maragudk/gomponents"
|
2020-12-10 04:00:23 -08:00
|
|
|
. "github.com/maragudk/gomponents/components"
|
|
|
|
. "github.com/maragudk/gomponents/html"
|
2021-10-06 11:49:43 -07:00
|
|
|
"github.com/maragudk/gomponents/internal/assert"
|
2020-10-29 04:03:43 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTML5(t *testing.T) {
|
|
|
|
t.Run("returns an html5 document template", func(t *testing.T) {
|
2020-12-10 04:00:23 -08:00
|
|
|
e := HTML5(HTML5Props{
|
2020-10-29 04:03:43 -07:00
|
|
|
Title: "Hat",
|
|
|
|
Description: "Love hats.",
|
|
|
|
Language: "en",
|
2020-12-10 04:00:23 -08:00
|
|
|
Head: []g.Node{Link(Rel("stylesheet"), Href("/hat.css"))},
|
|
|
|
Body: []g.Node{Div()},
|
2020-10-29 04:03:43 -07:00
|
|
|
})
|
|
|
|
|
2020-11-16 03:38:24 -08:00
|
|
|
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)
|
2020-10-29 04:03:43 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("returns no language, description, and extra head/body elements if empty", func(t *testing.T) {
|
2020-12-10 04:00:23 -08:00
|
|
|
e := HTML5(HTML5Props{
|
2020-10-29 04:03:43 -07:00
|
|
|
Title: "Hat",
|
|
|
|
})
|
|
|
|
|
2020-11-16 03:38:24 -08:00
|
|
|
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)
|
2020-10-29 04:03:43 -07:00
|
|
|
})
|
|
|
|
}
|
2022-11-03 03:32:16 -07:00
|
|
|
|
|
|
|
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>
|
|
|
|
}
|