Skip to content
Snippets Groups Projects
Commit b33d17d0 authored by Jono Wenger's avatar Jono Wenger
Browse files

Make JsToJson return valid JSON when the Javascript value is undefined

parent 568f72ae
No related branches found
No related tags found
2 merge requests!60Revert "Fail a test to be sure it works",!47Make JsToJson return valid JSON when the Javascript value is undefined
......@@ -32,6 +32,10 @@ func CopyBytesToJS(src []byte) js.Value {
// JsToJson converts the Javascript value to JSON.
func JsToJson(value js.Value) string {
if value.IsUndefined() {
return "null"
}
return JSON.Call("stringify", value).String()
}
......
......@@ -113,6 +113,22 @@ func TestJsToJson(t *testing.T) {
}
}
// Tests that JsToJson return a null object when the Javascript object is
// undefined.
func TestJsToJson_Undefined(t *testing.T) {
expected, err := json.Marshal(nil)
if err != nil {
t.Errorf("Failed to JSON marshal test object: %+v", err)
}
jsJson := JsToJson(js.Undefined())
if string(expected) != jsJson {
t.Errorf("Recieved incorrect JSON from Javascript object."+
"\nexpected: %s\nreceived: %s", expected, jsJson)
}
}
// Tests that JsonToJS can convert a JSON object with multiple types to a
// Javascript object and that all values match.
func TestJsonToJS(t *testing.T) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment