Skip to content
Snippets Groups Projects
ud_test.go 1.83 KiB
Newer Older
Jono Wenger's avatar
Jono Wenger committed
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation                                             //
Jono Wenger's avatar
Jono Wenger committed
//                                                                            //
// Use of this source code is governed by a license that can be found in the  //
// LICENSE file.                                                              //
Jono Wenger's avatar
Jono Wenger committed
////////////////////////////////////////////////////////////////////////////////

//go:build js && wasm

package wasm

import (
Jono Wenger's avatar
Jono Wenger committed
	"reflect"
	"testing"
)

// Tests that the map representing UserDiscovery returned by newUserDiscoveryJS
// contains all of the methods on UserDiscovery.
func Test_newUserDiscoveryJS(t *testing.T) {
	udType := reflect.TypeOf(&UserDiscovery{})

	ud := newUserDiscoveryJS(&bindings.UserDiscovery{})
	if len(ud) != udType.NumMethod() {
		t.Errorf("UserDiscovery JS object does not have all methods."+
			"\nexpected: %d\nreceived: %d", udType.NumMethod(), len(ud))
	}

	for i := 0; i < udType.NumMethod(); i++ {
		method := udType.Method(i)

		if _, exists := ud[method.Name]; !exists {
			t.Errorf("Method %s does not exist.", method.Name)
		}
	}
}
Jono Wenger's avatar
Jono Wenger committed

// Tests that UserDiscovery has all the methods that [bindings.UserDiscovery]
// has.
func Test_UserDiscoveryMethods(t *testing.T) {
	udType := reflect.TypeOf(&UserDiscovery{})
	binUdType := reflect.TypeOf(&bindings.UserDiscovery{})

	if binUdType.NumMethod() != udType.NumMethod() {
		t.Errorf("WASM UserDiscovery object does not have all methods from "+
			"bindings.\nexpected: %d\nreceived: %d",
			binUdType.NumMethod(), udType.NumMethod())
	}

	for i := 0; i < binUdType.NumMethod(); i++ {
		method := binUdType.Method(i)

		if _, exists := udType.MethodByName(method.Name); !exists {
			t.Errorf("Method %s does not exist.", method.Name)
		}
	}
}