github.com/ManabuSeki/goa-v1@v1.4.3/goagen/gen_js/generator_test.go (about)

     1  package genjs_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"time"
    10  
    11  	"github.com/goadesign/goa/design"
    12  	genjs "github.com/goadesign/goa/goagen/gen_js"
    13  	"github.com/goadesign/goa/version"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Generate", func() {
    19  	const testgenPackagePath = "github.com/goadesign/goa/goagen/gen_js/test_"
    20  
    21  	var outDir string
    22  	var files []string
    23  	var genErr error
    24  
    25  	BeforeEach(func() {
    26  		gopath := filepath.SplitList(os.Getenv("GOPATH"))[0]
    27  		outDir = filepath.Join(gopath, "src", testgenPackagePath)
    28  		err := os.MkdirAll(outDir, 0777)
    29  		Ω(err).ShouldNot(HaveOccurred())
    30  		os.Args = []string{"goagen", "--out=" + outDir, "--design=foo", "--host=baz", "--version=" + version.String()}
    31  	})
    32  
    33  	JustBeforeEach(func() {
    34  		files, genErr = genjs.Generate()
    35  	})
    36  
    37  	AfterEach(func() {
    38  		os.RemoveAll(outDir)
    39  	})
    40  
    41  	Context("with a dummy API", func() {
    42  		BeforeEach(func() {
    43  			design.Design = &design.APIDefinition{
    44  				Name:        "testapi",
    45  				Title:       "dummy API with no resource",
    46  				Description: "I told you it's dummy",
    47  			}
    48  		})
    49  
    50  		It("generates a dummy js", func() {
    51  			Ω(genErr).Should(BeNil())
    52  			Ω(files).Should(HaveLen(3))
    53  			content, err := ioutil.ReadFile(filepath.Join(outDir, "js", "client.js"))
    54  			Ω(err).ShouldNot(HaveOccurred())
    55  			Ω(len(strings.Split(string(content), "\n"))).Should(BeNumerically(">=", 13))
    56  		})
    57  	})
    58  
    59  	Context("with an example action with query parameters", func() {
    60  		BeforeEach(func() {
    61  			action := &design.ActionDefinition{
    62  				Name: "show",
    63  				Routes: []*design.RouteDefinition{{
    64  					Verb: "GET",
    65  					Path: "/",
    66  				}},
    67  				Params: &design.AttributeDefinition{
    68  					Type: design.Object{
    69  						"query": {Type: design.String},
    70  					},
    71  				},
    72  				QueryParams: &design.AttributeDefinition{
    73  					Type: design.Object{
    74  						"query": {Type: design.String},
    75  					},
    76  				},
    77  			}
    78  			design.Design = &design.APIDefinition{
    79  				Name:        "testapi",
    80  				Title:       "dummy API with no resource",
    81  				Description: "I told you it's dummy",
    82  				Resources: map[string]*design.ResourceDefinition{
    83  					"bottle": {
    84  						Name: "bottle",
    85  						Actions: map[string]*design.ActionDefinition{
    86  							"show": action,
    87  						},
    88  					},
    89  				},
    90  			}
    91  			action.Parent = design.Design.Resources["bottle"]
    92  		})
    93  
    94  		It("generates an example HTML", func() {
    95  			Ω(genErr).Should(BeNil())
    96  			Ω(files).Should(HaveLen(5))
    97  			content, err := ioutil.ReadFile(filepath.Join(outDir, "js", "index.html"))
    98  			Ω(err).ShouldNot(HaveOccurred())
    99  			Ω(len(strings.Split(string(content), "\n"))).Should(BeNumerically(">=", 13))
   100  		})
   101  	})
   102  })
   103  
   104  var _ = Describe("NewGenerator", func() {
   105  	var generator *genjs.Generator
   106  
   107  	var args = struct {
   108  		api       *design.APIDefinition
   109  		outDir    string
   110  		timeout   time.Duration
   111  		scheme    string
   112  		host      string
   113  		noExample bool
   114  	}{
   115  		api: &design.APIDefinition{
   116  			Name: "test api",
   117  		},
   118  		outDir:    "out_dir",
   119  		timeout:   time.Millisecond * 500,
   120  		scheme:    "http",
   121  		host:      "localhost",
   122  		noExample: true,
   123  	}
   124  
   125  	Context("with options all options set", func() {
   126  		BeforeEach(func() {
   127  
   128  			generator = genjs.NewGenerator(
   129  				genjs.API(args.api),
   130  				genjs.OutDir(args.outDir),
   131  				genjs.Timeout(args.timeout),
   132  				genjs.Scheme(args.scheme),
   133  				genjs.Host(args.host),
   134  				genjs.NoExample(args.noExample),
   135  			)
   136  		})
   137  
   138  		It("has all public properties set with expected value", func() {
   139  			Ω(generator).ShouldNot(BeNil())
   140  			Ω(generator.API.Name).Should(Equal(args.api.Name))
   141  			Ω(generator.OutDir).Should(Equal(args.outDir))
   142  			Ω(generator.Timeout).Should(Equal(args.timeout))
   143  			Ω(generator.Scheme).Should(Equal(args.scheme))
   144  			Ω(generator.Host).Should(Equal(args.host))
   145  			Ω(generator.NoExample).Should(Equal(args.noExample))
   146  		})
   147  
   148  	})
   149  })