github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/api/option_test.go (about)

     1  package api
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/niko0xdev/gqlgen/codegen/config"
     9  	"github.com/niko0xdev/gqlgen/plugin"
    10  	"github.com/niko0xdev/gqlgen/plugin/federation"
    11  	"github.com/niko0xdev/gqlgen/plugin/modelgen"
    12  	"github.com/niko0xdev/gqlgen/plugin/resolvergen"
    13  )
    14  
    15  type testPlugin struct{}
    16  
    17  // Name returns the plugin name
    18  func (t *testPlugin) Name() string {
    19  	return "modelgen"
    20  }
    21  
    22  // MutateConfig mutates the configuration
    23  func (t *testPlugin) MutateConfig(_ *config.Config) error {
    24  	return nil
    25  }
    26  
    27  func TestReplacePlugin(t *testing.T) {
    28  	t.Run("replace plugin if exists", func(t *testing.T) {
    29  		pg := []plugin.Plugin{
    30  			federation.New(1),
    31  			modelgen.New(),
    32  			resolvergen.New(),
    33  		}
    34  
    35  		expectedPlugin := &testPlugin{}
    36  		ReplacePlugin(expectedPlugin)(config.DefaultConfig(), &pg)
    37  
    38  		require.EqualValues(t, federation.New(1), pg[0])
    39  		require.EqualValues(t, expectedPlugin, pg[1])
    40  		require.EqualValues(t, resolvergen.New(), pg[2])
    41  	})
    42  
    43  	t.Run("add plugin if doesn't exist", func(t *testing.T) {
    44  		pg := []plugin.Plugin{
    45  			federation.New(1),
    46  			resolvergen.New(),
    47  		}
    48  
    49  		expectedPlugin := &testPlugin{}
    50  		ReplacePlugin(expectedPlugin)(config.DefaultConfig(), &pg)
    51  
    52  		require.EqualValues(t, federation.New(1), pg[0])
    53  		require.EqualValues(t, resolvergen.New(), pg[1])
    54  		require.EqualValues(t, expectedPlugin, pg[2])
    55  	})
    56  }
    57  
    58  func TestPrependPlugin(t *testing.T) {
    59  	modelgenPlugin := modelgen.New()
    60  	pg := []plugin.Plugin{
    61  		modelgenPlugin,
    62  	}
    63  
    64  	expectedPlugin := &testPlugin{}
    65  	PrependPlugin(expectedPlugin)(config.DefaultConfig(), &pg)
    66  
    67  	require.EqualValues(t, expectedPlugin, pg[0])
    68  	require.EqualValues(t, modelgenPlugin, pg[1])
    69  }