github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/enums_test.go (about)

     1  package followschema
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/operandinc/gqlgen/client"
     8  	"github.com/operandinc/gqlgen/graphql/handler"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestEnumsResolver(t *testing.T) {
    13  	resolvers := &Stub{}
    14  	resolvers.QueryResolver.EnumInInput = func(ctx context.Context, input *InputWithEnumValue) (EnumTest, error) {
    15  		return input.Enum, nil
    16  	}
    17  
    18  	c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))
    19  
    20  	t.Run("input with valid enum value", func(t *testing.T) {
    21  		var resp struct {
    22  			EnumInInput EnumTest
    23  		}
    24  		c.MustPost(`query {
    25  			enumInInput(input: {enum: OK})
    26  		}
    27  		`, &resp)
    28  		require.Equal(t, resp.EnumInInput, EnumTestOk)
    29  	})
    30  
    31  	t.Run("input with invalid enum value", func(t *testing.T) {
    32  		var resp struct {
    33  			EnumInInput EnumTest
    34  		}
    35  		err := c.Post(`query {
    36  			enumInInput(input: {enum: INVALID})
    37  		}
    38  		`, &resp)
    39  		require.EqualError(t, err, `http 422: {"errors":[{"message":"Expected type EnumTest!, found INVALID.","locations":[{"line":2,"column":30}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
    40  	})
    41  
    42  	t.Run("input with invalid enum value via vars", func(t *testing.T) {
    43  		var resp struct {
    44  			EnumInInput EnumTest
    45  		}
    46  		err := c.Post(`query ($input: InputWithEnumValue) {
    47  			enumInInput(input: $input)
    48  		}
    49  		`, &resp, client.Var("input", map[string]interface{}{"enum": "INVALID"}))
    50  		require.EqualError(t, err, `http 422: {"errors":[{"message":"INVALID is not a valid EnumTest","path":["variable","input","enum"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
    51  	})
    52  }