mirror of
https://github.com/a-h/templ.git
synced 2025-02-06 09:45:21 +00:00
85a7b8b19a
Co-authored-by: Joe Davidson <joe.davidson.21111@gmail.com>
29 lines
660 B
Go
29 lines
660 B
Go
package templ_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/a-h/templ"
|
|
)
|
|
|
|
func TestJSONString(t *testing.T) {
|
|
t.Run("renders input data as a JSON string", func(t *testing.T) {
|
|
data := map[string]any{"foo": "bar"}
|
|
actual, err := templ.JSONString(data)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
expected := "{\"foo\":\"bar\"}"
|
|
if actual != expected {
|
|
t.Fatalf("unexpected output: want %q, got %q", expected, actual)
|
|
}
|
|
})
|
|
t.Run("returns an error if the data cannot be marshalled", func(t *testing.T) {
|
|
data := make(chan int)
|
|
_, err := templ.JSONString(data)
|
|
if err == nil {
|
|
t.Fatalf("expected an error, got nil")
|
|
}
|
|
})
|
|
}
|