@@ -3,8 +3,7 @@ import { Plugin } from './plugin.js';
33import { ParameterOperation , ResourceOperation , StringIndexedObject } from 'codify-schemas' ;
44import { Resource } from './resource.js' ;
55import { Plan } from './plan.js' ;
6- import { ValidationResult } from './resource-types.js' ;
7- import { ApplyValidationError } from './errors.js' ;
6+ import { spy } from 'sinon' ;
87
98interface TestConfig extends StringIndexedObject {
109 propA : string ;
@@ -27,36 +26,19 @@ class TestResource extends Resource<TestConfig> {
2726 return Promise . resolve ( undefined ) ;
2827 }
2928
30- async refresh ( keys : Map < string , unknown > ) : Promise < Partial < TestConfig > | null > {
29+ async refresh ( ) : Promise < Partial < TestConfig > | null > {
3130 return {
3231 propA : 'a' ,
3332 propB : 10 ,
3433 propC : 'c' ,
3534 } ;
3635 }
37-
38- async validateResource ( config : unknown ) : Promise < ValidationResult > {
39- return {
40- isValid : true
41- }
42- }
4336}
4437
4538describe ( 'Plugin tests' , ( ) => {
46- it ( 'Validates that applies were successfully applied' , async ( ) => {
47- const resource = new class extends TestResource {
48- async applyCreate ( plan : Plan < TestConfig > ) : Promise < void > {
49- }
50-
51- // Refresh has to line up with desired for the apply to go through
52- async refresh ( keys : Map < string , unknown > ) : Promise < Partial < TestConfig > | null > {
53- return {
54- propA : 'abc'
55- }
56- }
57- }
58-
59- const plugin = Plugin . create ( 'testPlugin' , [ resource ] )
39+ it ( 'Can apply resource' , async ( ) => {
40+ const resource = spy ( new TestResource ( ) )
41+ const plugin = Plugin . create ( 'testPlugin' , [ resource as any ] )
6042
6143 const plan = {
6244 operation : ResourceOperation . CREATE ,
@@ -66,45 +48,13 @@ describe('Plugin tests', () => {
6648 ]
6749 } ;
6850
69- // If this doesn't throw then it passes the test
7051 await plugin . apply ( { plan } ) ;
52+ expect ( resource . applyCreate . calledOnce ) . to . be . true ;
7153 } ) ;
7254
73- it ( 'Validates that applies were successfully applied (error)' , async ( ) => {
74- const resource = new class extends TestResource {
75- async applyCreate ( plan : Plan < TestConfig > ) : Promise < void > {
76- }
77-
78- // Return null to indicate that the resource was not created
79- async refresh ( keys : Map < string , unknown > ) : Promise < Partial < TestConfig > | null > {
80- return null ;
81- }
82- }
83- const plugin = Plugin . create ( 'testPlugin' , [ resource ] )
84-
85- const plan = {
86- operation : ResourceOperation . CREATE ,
87- resourceType : 'testResource' ,
88- parameters : [
89- { name : 'propA' , operation : ParameterOperation . ADD , newValue : 'abc' , previousValue : null } ,
90- ]
91- } ;
92-
93- await expect ( async ( ) => plugin . apply ( { plan } ) ) . rejects . toThrowError ( expect . any ( ApplyValidationError ) ) ;
94- } ) ;
95-
96- it ( 'Validates that deletes were successfully applied' , async ( ) => {
97- const resource = new class extends TestResource {
98- async applyCreate ( plan : Plan < TestConfig > ) : Promise < void > {
99- }
100-
101- // Return null to indicate that the resource was deleted
102- async refresh ( keys : Map < string , unknown > ) : Promise < Partial < TestConfig > | null > {
103- return null ;
104- }
105- }
106-
107- const testPlugin = Plugin . create ( 'testPlugin' , [ resource ] )
55+ it ( 'Can destroy resource' , async ( ) => {
56+ const resource = spy ( new TestResource ( ) ) ;
57+ const testPlugin = Plugin . create ( 'testPlugin' , [ resource as any ] )
10858
10959 const plan = {
11060 operation : ResourceOperation . DESTROY ,
@@ -114,46 +64,13 @@ describe('Plugin tests', () => {
11464 ]
11565 } ;
11666
117- // If this doesn't throw then it passes the test
11867 await testPlugin . apply ( { plan } )
68+ expect ( resource . applyDestroy . calledOnce ) . to . be . true ;
11969 } ) ;
12070
121- it ( 'Validates that deletes were successfully applied (error)' , async ( ) => {
122- const resource = new class extends TestResource {
123- async applyCreate ( plan : Plan < TestConfig > ) : Promise < void > {
124- }
125-
126- // Return a value to indicate that the resource still exists
127- async refresh ( keys : Map < string , unknown > ) : Promise < Partial < TestConfig > | null > {
128- return { propA : 'abc' } ;
129- }
130- }
131-
132- const testPlugin = Plugin . create ( 'testPlugin' , [ resource ] )
133-
134- const plan = {
135- operation : ResourceOperation . DESTROY ,
136- resourceType : 'testResource' ,
137- parameters : [
138- { name : 'propA' , operation : ParameterOperation . REMOVE , newValue : null , previousValue : 'abc' } ,
139- ]
140- } ;
141-
142- // If this doesn't throw then it passes the test
143- expect ( async ( ) => await testPlugin . apply ( { plan } ) ) . rejects . toThrowError ( expect . any ( ApplyValidationError ) ) ;
144- } ) ;
145-
146- it ( 'Validates that re-create was successfully applied' , async ( ) => {
147- const resource = new class extends TestResource {
148- async applyCreate ( plan : Plan < TestConfig > ) : Promise < void > {
149- }
150-
151- async refresh ( keys : Map < string , unknown > ) : Promise < Partial < TestConfig > | null > {
152- return { propA : 'def' } ;
153- }
154- }
155-
156- const testPlugin = Plugin . create ( 'testPlugin' , [ resource ] )
71+ it ( 'Can re-create resource' , async ( ) => {
72+ const resource = spy ( new TestResource ( ) )
73+ const testPlugin = Plugin . create ( 'testPlugin' , [ resource as any ] )
15774
15875 const plan = {
15976 operation : ResourceOperation . RECREATE ,
@@ -163,31 +80,24 @@ describe('Plugin tests', () => {
16380 ]
16481 } ;
16582
166- // If this doesn't throw then it passes the test
16783 await testPlugin . apply ( { plan } )
84+ expect ( resource . applyDestroy . calledOnce ) . to . be . true ;
85+ expect ( resource . applyCreate . calledOnce ) . to . be . true ;
16886 } ) ;
16987
170- it ( 'Validates that modify was successfully applied (error)' , async ( ) => {
171- const resource = new class extends TestResource {
172- async applyCreate ( plan : Plan < TestConfig > ) : Promise < void > {
173- }
174-
175- async refresh ( keys : Map < string , unknown > ) : Promise < Partial < TestConfig > | null > {
176- return { propA : 'abc' } ;
177- }
178- }
179-
180- const testPlugin = Plugin . create ( 'testPlugin' , [ resource ] )
88+ it ( 'Can modify resource' , async ( ) => {
89+ const resource = spy ( new TestResource ( ) )
90+ const testPlugin = Plugin . create ( 'testPlugin' , [ resource as any ] )
18191
18292 const plan = {
183- operation : ResourceOperation . DESTROY ,
93+ operation : ResourceOperation . MODIFY ,
18494 resourceType : 'testResource' ,
18595 parameters : [
186- { name : 'propA' , operation : ParameterOperation . REMOVE , newValue : 'def' , previousValue : 'abc' } ,
96+ { name : 'propA' , operation : ParameterOperation . MODIFY , newValue : 'def' , previousValue : 'abc' } ,
18797 ]
18898 } ;
18999
190- // If this doesn't throw then it passes the test
191- expect ( async ( ) => await testPlugin . apply ( { plan } ) ) . rejects . toThrowError ( expect . any ( ApplyValidationError ) ) ;
100+ await testPlugin . apply ( { plan } )
101+ expect ( resource . applyModify . calledOnce ) . to . be . true ;
192102 } ) ;
193103} ) ;
0 commit comments