1
0
mirror of https://github.com/a-h/templ.git synced 2025-02-06 09:45:21 +00:00
templ/jsonstring_test.go
Adrian Hesketh 85a7b8b19a
feat: add JSONString and JSONScript functions, update docs, refer to templ script as legacy in docs (#745)
Co-authored-by: Joe Davidson <joe.davidson.21111@gmail.com>
2024-05-21 14:27:08 +01:00

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")
}
})
}