@@ -6,10 +6,15 @@ import {
6
6
AmplifyData ,
7
7
AmplifyDataDefinition ,
8
8
} from '@aws-amplify/data-construct' ;
9
- import { resolve } from 'path' ;
9
+ import { join , resolve } from 'path' ;
10
+ import { tmpdir } from 'os' ;
10
11
import { fileURLToPath } from 'url' ;
11
- import { convertJsResolverDefinition } from './convert_js_resolvers.js' ;
12
+ import {
13
+ convertJsResolverDefinition ,
14
+ defaultJsResolverCode ,
15
+ } from './convert_js_resolvers.js' ;
12
16
import { a } from '@aws-amplify/data-schema' ;
17
+ import { writeFileSync } from 'node:fs' ;
13
18
14
19
// stub schema for the AmplifyApi construct
15
20
// not relevant to this test suite
@@ -28,6 +33,29 @@ const createStackAndSetContext = (): Stack => {
28
33
return stack ;
29
34
} ;
30
35
36
+ void describe ( 'defaultJsResolverCode' , ( ) => {
37
+ void it ( 'returns the default JS resolver code with api id and env name in valid JS' , async ( ) => {
38
+ const code = defaultJsResolverCode ( 'testApiId' , 'testEnvName' ) ;
39
+ assert ( code . includes ( "ctx.stash.awsAppsyncApiId = 'testApiId';" ) ) ;
40
+ assert ( code . includes ( "ctx.stash.amplifyBranchName = 'testEnvName';" ) ) ;
41
+
42
+ const tempDir = tmpdir ( ) ;
43
+ const filename = join ( tempDir , 'js_resolver_handler.js' ) ;
44
+ writeFileSync ( filename , code ) ;
45
+
46
+ const resolver = await import ( filename ) ;
47
+ const context = { stash : { } , prev : { result : 'result' } } ;
48
+ assert . deepEqual ( resolver . request ( context ) , { } ) ;
49
+
50
+ // assert api id and env name are added to the context stash
51
+ assert . deepEqual ( context . stash , {
52
+ awsAppsyncApiId : 'testApiId' ,
53
+ amplifyBranchName : 'testEnvName' ,
54
+ } ) ;
55
+ assert . equal ( resolver . response ( context ) , 'result' ) ;
56
+ } ) ;
57
+ } ) ;
58
+
31
59
void describe ( 'convertJsResolverDefinition' , ( ) => {
32
60
let stack : Stack ;
33
61
let amplifyApi : AmplifyData ;
0 commit comments