asd
This commit is contained in:
51
internal/standard/fetch_promise_test.go
Normal file
51
internal/standard/fetch_promise_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package standard
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
|
||||
"reichard.io/poiesis/internal/builtin"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFetchReturnsPromise(t *testing.T) {
|
||||
vm := goja.New()
|
||||
builtin.RegisterBuiltins(vm)
|
||||
|
||||
result, err := vm.RunString(`fetch({input: "https://example.com"})`)
|
||||
require.NoError(t, err)
|
||||
|
||||
promise, ok := result.Export().(*goja.Promise)
|
||||
require.True(t, ok, "fetch should return a Promise")
|
||||
assert.NotNil(t, promise)
|
||||
}
|
||||
|
||||
func TestFetchAsyncAwait(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"status":"ok"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
vm := goja.New()
|
||||
builtin.RegisterBuiltins(vm)
|
||||
|
||||
result, err := vm.RunString(`
|
||||
async function testFetch() {
|
||||
const response = await fetch({input: "` + server.URL + `"});
|
||||
return response.ok;
|
||||
}
|
||||
testFetch();
|
||||
`)
|
||||
require.NoError(t, err)
|
||||
|
||||
promise, ok := result.Export().(*goja.Promise)
|
||||
require.True(t, ok, "async function should return a Promise")
|
||||
assert.NotNil(t, promise)
|
||||
}
|
||||
Reference in New Issue
Block a user