@@ -7,10 +7,8 @@ import { TestConfig, TestResource } from './resource.test.js';
77import { TransformParameter } from './transform-parameter.js' ;
88
99class TestParameter extends StatefulParameter < TestConfig , string > {
10- constructor ( options ?: StatefulParameterOptions < TestConfig > ) {
11- super ( options ?? {
12- name : 'propA'
13- } )
10+ constructor ( options ?: StatefulParameterOptions < string > ) {
11+ super ( options ?? { } )
1412 }
1513
1614 applyAdd ( valueToAdd : string , plan : Plan < TestConfig > ) : Promise < void > {
@@ -140,6 +138,51 @@ describe('Resource parameter tests', () => {
140138 expect ( resourceSpy . applyModify . calledOnce ) . to . be . true ;
141139 } )
142140
141+ it ( 'Allows stateful parameters to have default values' , async ( ) => {
142+ const statefulParameter = spy ( new class extends TestParameter {
143+ constructor ( ) {
144+ super ( {
145+ default : 'abc'
146+ } ) ;
147+ }
148+
149+ async refresh ( ) : Promise < string | null > {
150+ return null ;
151+ }
152+ } ) ;
153+
154+ const resource = new class extends TestResource {
155+ constructor ( ) {
156+ super ( {
157+ type : 'resource' ,
158+ parameterOptions : {
159+ propA : { statefulParameter }
160+ } ,
161+ } ) ;
162+ }
163+
164+ async refresh ( ) : Promise < any > {
165+ return null ;
166+ }
167+ }
168+
169+ const plan = await resource . plan ( {
170+ type : 'resource' ,
171+ } )
172+
173+ expect ( statefulParameter . refresh . notCalled ) . to . be . true ;
174+ expect ( plan . currentConfig ) . toMatchObject ( {
175+ type : 'resource' ,
176+ propA : null ,
177+ } )
178+ expect ( plan . desiredConfig ) . toMatchObject ( {
179+ type : 'resource' ,
180+ propA : 'abc' ,
181+ } )
182+ expect ( plan . changeSet . operation ) . to . eq ( ResourceOperation . CREATE ) ;
183+ } )
184+
185+
143186 it ( 'Filters array results in stateless mode to prevent modify from being called' , async ( ) => {
144187 const statefulParameter = new class extends TestParameter {
145188 async refresh ( ) : Promise < any | null > {
@@ -210,7 +253,6 @@ describe('Resource parameter tests', () => {
210253 const statefulParameter = new class extends ArrayStatefulParameter < TestConfig , string > {
211254 constructor ( ) {
212255 super ( {
213- name : 'propA' ,
214256 isElementEqual : ( desired , current ) => current . includes ( desired ) ,
215257 } ) ;
216258 }
@@ -531,4 +573,44 @@ describe('Resource parameter tests', () => {
531573 expect ( plan . desiredConfig . propE ) . to . be . lessThan ( plan . desiredConfig . propF as any ) ;
532574 expect ( plan . desiredConfig . propF ) . to . be . lessThan ( plan . desiredConfig . propD as any ) ;
533575 } )
576+
577+ it ( 'Plans transform even for creating new resources' , async ( ) => {
578+ const transformParameterA = spy ( new class extends TransformParameter < TestConfig > {
579+ async transform ( value : any ) : Promise < Partial < TestConfig > > {
580+ return { propD : 'abc' , propE : 10 }
581+ }
582+ } ) ;
583+
584+ const resource = spy ( new class extends TestResource {
585+ constructor ( ) {
586+ super ( {
587+ type : 'resourceType' ,
588+ parameterOptions : {
589+ propA : { transformParameter : transformParameterA } ,
590+ } ,
591+ } ) ;
592+ }
593+
594+ async refresh ( ) : Promise < Partial < TestConfig > | null > {
595+ return null ;
596+ }
597+ } ) ;
598+
599+ const plan = await resource . plan ( {
600+ type : 'resourceType' ,
601+ propA : 'propA' ,
602+ propB : 10 ,
603+ propC : 'propC' ,
604+ } ) ;
605+ expect ( plan . currentConfig ) . toMatchObject ( {
606+ type : 'resourceType' ,
607+ propD : null ,
608+ propE : null ,
609+ } )
610+ expect ( plan . desiredConfig ) . toMatchObject ( {
611+ type : 'resourceType' ,
612+ propD : 'abc' ,
613+ propE : 10 ,
614+ } )
615+ } )
534616} )
0 commit comments