github.com/0chain/gosdk@v1.17.11/wasmsdk/jsbridge/func_test.go (about) 1 //go:build js && wasm 2 // +build js,wasm 3 4 package jsbridge 5 6 import ( 7 "syscall/js" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestBindAsyncFunc(t *testing.T) { 14 tests := []struct { 15 Name string 16 Func func() js.Func 17 Output func(outputs []js.Value) interface{} 18 Result interface{} 19 }{ 20 {Name: "ReturnString", Func: func() js.Func { 21 fn, _ := promise(func() string { 22 return "ReturnString" 23 }) 24 25 return fn 26 }, Output: func(outputs []js.Value) interface{} { 27 return outputs[0].String() 28 }, Result: "ReturnString"}, 29 {Name: "ReturnInt", Func: func() js.Func { 30 fn, _ := promise(func() int { 31 return 1 32 }) 33 34 return fn 35 }, Output: func(outputs []js.Value) interface{} { 36 return outputs[0].Int() 37 }, Result: 1}, 38 {Name: "ReturnInt32", Func: func() js.Func { 39 fn, _ := promise(func() int32 { 40 return int32(1) 41 }) 42 43 return fn 44 }, Output: func(outputs []js.Value) interface{} { 45 return int32(outputs[0].Int()) 46 }, Result: int32(1)}, 47 {Name: "ReturnInt64", Func: func() js.Func { 48 fn, _ := promise(func() int64 { 49 return int64(1) 50 }) 51 52 return fn 53 }, Output: func(outputs []js.Value) interface{} { 54 return int64(outputs[0].Int()) 55 }, Result: int64(1)}, 56 {Name: "ReturnFloat32", Func: func() js.Func { 57 fn, _ := promise(func() float32 { 58 return float32(1) 59 }) 60 61 return fn 62 }, Output: func(outputs []js.Value) interface{} { 63 return float32(outputs[0].Float()) 64 }, Result: float32(1)}, 65 {Name: "ReturnFloat64", Func: func() js.Func { 66 fn, _ := promise(func() float64 { 67 return float64(1) 68 }) 69 70 return fn 71 }, Output: func(outputs []js.Value) interface{} { 72 return outputs[0].Float() 73 }, Result: float64(1)}, 74 } 75 76 for _, it := range tests { 77 t.Run(it.Name, func(test *testing.T) { 78 79 jsFunc := it.Func() 80 81 outputs, _ := Await(jsFunc.Invoke()) 82 83 require.Equal(test, it.Result, it.Output(outputs)) 84 85 }) 86 } 87 88 }