github.com/dop251/goja_nodejs@v0.0.0-20240418154818-2aae10d4cbcf/url/urlsearchparams_test.go (about)

     1  package url
     2  
     3  import (
     4  	_ "embed"
     5  	"testing"
     6  
     7  	"github.com/dop251/goja"
     8  	"github.com/dop251/goja_nodejs/console"
     9  	"github.com/dop251/goja_nodejs/require"
    10  )
    11  
    12  func createVM() *goja.Runtime {
    13  	vm := goja.New()
    14  	new(require.Registry).Enable(vm)
    15  	console.Enable(vm)
    16  	Enable(vm)
    17  	return vm
    18  }
    19  
    20  func TestURLSearchParams(t *testing.T) {
    21  	vm := createVM()
    22  
    23  	if c := vm.Get("URLSearchParams"); c == nil {
    24  		t.Fatal("URLSearchParams not found")
    25  	}
    26  
    27  	script := `const params = new URLSearchParams();`
    28  
    29  	if _, err := vm.RunString(script); err != nil {
    30  		t.Fatal("Failed to process url script.", err)
    31  	}
    32  }
    33  
    34  //go:embed testdata/url_search_params.js
    35  var url_search_params string
    36  
    37  func TestURLSearchParameters(t *testing.T) {
    38  	vm := createVM()
    39  
    40  	if c := vm.Get("URLSearchParams"); c == nil {
    41  		t.Fatal("URLSearchParams not found")
    42  	}
    43  
    44  	// Script will throw an error on failed validation
    45  
    46  	_, err := vm.RunScript("testdata/url_search_params.js", url_search_params)
    47  	if err != nil {
    48  		if ex, ok := err.(*goja.Exception); ok {
    49  			t.Fatal(ex.String())
    50  		}
    51  		t.Fatal("Failed to process url script.", err)
    52  	}
    53  }