github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/nodejs/gen_lazyloads_test.go (about)

     1  // Copyright 2016-2022, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package nodejs
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestLazyLoadsGeneration(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	ll := newLazyLoadGen()
    28  
    29  	t.Run("resource", func(t *testing.T) { // nolint: paralleltest
    30  		var buf bytes.Buffer
    31  		ll.genReexport(&buf, fileInfo{
    32  			fileType: resourceFileType,
    33  			resourceFileInfo: resourceFileInfo{
    34  				resourceClassName:         "MyRes",
    35  				resourceArgsInterfaceName: "MyResArgs",
    36  			},
    37  		}, "./myResource")
    38  
    39  		assert.Equal(t, `export { MyResArgs } from "./myResource";
    40  export type MyRes = import("./myResource").MyRes;
    41  export const MyRes: typeof import("./myResource").MyRes = null as any;
    42  utilities.lazyLoad(exports, ["MyRes"], () => require("./myResource"));
    43  
    44  `,
    45  			buf.String())
    46  	})
    47  
    48  	t.Run("resource-with-state", func(t *testing.T) { // nolint: paralleltest
    49  		var buf bytes.Buffer
    50  		ll.genReexport(&buf, fileInfo{
    51  			fileType: resourceFileType,
    52  			resourceFileInfo: resourceFileInfo{
    53  				resourceClassName:         "MyRes1",
    54  				resourceArgsInterfaceName: "MyRes1Args",
    55  				stateInterfaceName:        "MyRes1State",
    56  			},
    57  		}, "./myResource1")
    58  
    59  		assert.Equal(t, `export { MyRes1Args, MyRes1State } from "./myResource1";
    60  export type MyRes1 = import("./myResource1").MyRes1;
    61  export const MyRes1: typeof import("./myResource1").MyRes1 = null as any;
    62  utilities.lazyLoad(exports, ["MyRes1"], () => require("./myResource1"));
    63  
    64  `,
    65  			buf.String())
    66  	})
    67  
    68  	t.Run("resource-with-methods", func(t *testing.T) { // nolint: paralleltest
    69  		var buf bytes.Buffer
    70  		ll.genReexport(&buf, fileInfo{
    71  			fileType: resourceFileType,
    72  			resourceFileInfo: resourceFileInfo{
    73  				resourceClassName:         "MyRes2",
    74  				resourceArgsInterfaceName: "MyRes2Args",
    75  				methodsNamespaceName:      "MyRes2",
    76  			},
    77  		}, "./myResource2")
    78  
    79  		assert.Equal(t, `export * from "./myResource2";
    80  import { MyRes2 } from "./myResource2";
    81  
    82  `, buf.String())
    83  	})
    84  
    85  	t.Run("function", func(t *testing.T) { // nolint: paralleltest
    86  		var buf bytes.Buffer
    87  		ll.genReexport(&buf, fileInfo{
    88  			fileType: functionFileType,
    89  			functionFileInfo: functionFileInfo{
    90  				functionName:                "myFunc",
    91  				functionArgsInterfaceName:   "MyFuncArgs",
    92  				functionResultInterfaceName: "MyFuncResult",
    93  			},
    94  		}, "./myFunc")
    95  
    96  		assert.Equal(t, `export { MyFuncArgs, MyFuncResult } from "./myFunc";
    97  export const myFunc: typeof import("./myFunc").myFunc = null as any;
    98  utilities.lazyLoad(exports, ["myFunc"], () => require("./myFunc"));
    99  
   100  `, buf.String())
   101  	})
   102  
   103  	t.Run("function-with-output-version", func(t *testing.T) { // nolint: paralleltest
   104  		var buf bytes.Buffer
   105  		ll.genReexport(&buf, fileInfo{
   106  			fileType: functionFileType,
   107  			functionFileInfo: functionFileInfo{
   108  				functionName:                           "myFunc1",
   109  				functionArgsInterfaceName:              "MyFunc1Args",
   110  				functionResultInterfaceName:            "MyFunc1Result",
   111  				functionOutputVersionName:              "myFunc1Output",
   112  				functionOutputVersionArgsInterfaceName: "MyFunc1OutputArgs",
   113  			},
   114  		}, "./myFunc1")
   115  
   116  		assert.Equal(t, `export { MyFunc1Args, MyFunc1Result, MyFunc1OutputArgs } from "./myFunc1";
   117  export const myFunc1: typeof import("./myFunc1").myFunc1 = null as any;
   118  export const myFunc1Output: typeof import("./myFunc1").myFunc1Output = null as any;
   119  utilities.lazyLoad(exports, ["myFunc1","myFunc1Output"], () => require("./myFunc1"));
   120  
   121  `, buf.String())
   122  	})
   123  
   124  	t.Run("fallthrough-reexport", func(t *testing.T) { // nolint: paralleltest
   125  		var buf bytes.Buffer
   126  		ll.genReexport(&buf, fileInfo{
   127  			fileType: otherFileType,
   128  		}, "./myOtherFile")
   129  
   130  		assert.Equal(t, `export * from "./myOtherFile";
   131  `, buf.String())
   132  	})
   133  }