Skip to content

Commit 15f87dc

Browse files
committed
Add tests
1 parent b42d7b5 commit 15f87dc

File tree

11 files changed

+2374
-49
lines changed

11 files changed

+2374
-49
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ jobs:
1414
- name: Install dependencies
1515
run: |
1616
yarn install --frozen-lockfile
17-
- name: Run tests
17+
- name: Run lint
1818
run: yarn run lint
19+
- name: Run tests
20+
run: yarn run tests

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"scripts": {
1414
"lint": "eslint",
15-
"build": "babel src/ -d lib/ --delete-dir-on-start",
15+
"tests": "jest",
16+
"build": "babel src/ -d lib/ --delete-dir-on-start --ignore '**/__tests__'",
1617
"prepare": "yarn run build",
1718
"start-dev": "yarn run build -w --verbose"
1819
},
@@ -24,9 +25,11 @@
2425
"amplify-nodejs-function-runtime-provider": "^1.1.6",
2526
"aws-sdk": "^2.792.0",
2627
"axios": "^0.21.0",
28+
"babel-jest": "^26.6.3",
2729
"cfn-resolver-lib": "^1.1.7",
2830
"dataloader": "^2.0.0",
2931
"fb-watchman": "^2.0.1",
32+
"jest": "^26.6.3",
3033
"lodash": "^4.17.20",
3134
"merge-graphql-schemas": "^1.5.8"
3235
},
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`getAppSyncConfig should generate a valid config 1`] = `
4+
Object {
5+
"additionalAuthenticationProviders": Array [],
6+
"apiKey": "123456789",
7+
"defaultAuthenticationType": Object {
8+
"authenticationType": "API_KEY",
9+
},
10+
"name": "myAPI",
11+
}
12+
`;
13+
14+
exports[`getAppSyncConfig should generate a valid config 2`] = `
15+
Object {
16+
"content": "type Post {
17+
userId: Int!
18+
id: Int!
19+
title: String!
20+
body: String!
21+
}
22+
23+
type Query {
24+
getPost: Post
25+
getPosts: [Post]!
26+
}
27+
28+
schema {
29+
query: Query
30+
}
31+
",
32+
"path": "schema.graphql",
33+
}
34+
`;
35+
36+
exports[`getAppSyncConfig should generate a valid config 3`] = `
37+
Array [
38+
Object {
39+
"dataSourceName": "lambda",
40+
"fieldName": "templates",
41+
"functions": undefined,
42+
"kind": "UNIT",
43+
"requestMappingTemplate": "{
44+
\\"version\\": \\"2018-05-29\\",
45+
\\"operation\\": \\"Invoke\\",
46+
\\"payload\\": {
47+
\\"substitution\\": \\"lambda\\",
48+
\\"args\\": $utils.toJson($context.arguments)
49+
}
50+
}
51+
",
52+
"responseMappingTemplate": "$utils.toJson($context.result.lambda)
53+
",
54+
"typeName": "Query",
55+
},
56+
Object {
57+
"dataSourceName": "lambda",
58+
"fieldName": "default",
59+
"functions": undefined,
60+
"kind": "UNIT",
61+
"requestMappingTemplate": "{
62+
\\"version\\": \\"2018-05-29\\",
63+
\\"operation\\": \\"Invoke\\",
64+
\\"payload\\": {
65+
\\"substitution\\": \\"default\\",
66+
\\"type\\": \\"default\\"
67+
}
68+
}
69+
",
70+
"responseMappingTemplate": "$utils.toJson($context.result.default)
71+
",
72+
"typeName": "Query",
73+
},
74+
Object {
75+
"dataSourceName": "lambda",
76+
"fieldName": "directLambda",
77+
"functions": undefined,
78+
"kind": "UNIT",
79+
"requestMappingTemplate": "## Direct lambda request
80+
{
81+
\\"version\\": \\"2018-05-29\\",
82+
\\"operation\\": \\"Invoke\\",
83+
\\"payload\\": $utils.toJson($context)
84+
}
85+
",
86+
"responseMappingTemplate": "## Direct lambda response
87+
#if($ctx.error)
88+
$util.error($ctx.error.message, $ctx.error.type, $ctx.result)
89+
#end
90+
$util.toJson($ctx.result)
91+
",
92+
"typeName": "Query",
93+
},
94+
Object {
95+
"dataSourceName": undefined,
96+
"fieldName": "pipeline",
97+
"functions": Array [
98+
"func",
99+
"func-default",
100+
],
101+
"kind": "PIPELINE",
102+
"requestMappingTemplate": "{
103+
\\"version\\": \\"2018-05-29\\",
104+
\\"operation\\": \\"Invoke\\",
105+
\\"payload\\": {
106+
\\"substitution\\": \\"pipeline\\",
107+
\\"type\\": \\"default\\"
108+
}
109+
}
110+
",
111+
"responseMappingTemplate": "$utils.toJson($context.result.default)
112+
",
113+
"typeName": "Query",
114+
},
115+
]
116+
`;
117+
118+
exports[`getAppSyncConfig should generate a valid config 4`] = `
119+
Array [
120+
Object {
121+
"invoke": [Function],
122+
"name": "lambda",
123+
"type": "AWS_LAMBDA",
124+
},
125+
Object {
126+
"config": Object {
127+
"accessKeyId": "DEFAULT_ACCESS_KEY",
128+
"endpoint": "http://localhost:8000",
129+
"region": "localhost",
130+
"secretAccessKey": "DEFAULT_SECRET",
131+
"tableName": "myTable",
132+
},
133+
"name": "dynamodb",
134+
"type": "AMAZON_DYNAMODB",
135+
},
136+
Object {
137+
"endpoint": "http://127.0.0.1",
138+
"name": "http",
139+
"type": "HTTP",
140+
},
141+
]
142+
`;
143+
144+
exports[`getAppSyncConfig should generate a valid config 5`] = `
145+
Array [
146+
Object {
147+
"dataSourceName": "lambda",
148+
"name": "func",
149+
"requestMappingTemplate": "{
150+
\\"version\\": \\"2018-05-29\\",
151+
\\"operation\\": \\"Invoke\\",
152+
\\"payload\\": {
153+
\\"substitution\\": \\"template-function\\",
154+
\\"args\\": $utils.toJson($context.arguments)
155+
}
156+
}
157+
",
158+
"responseMappingTemplate": "$utils.toJson($context.result.lambda)
159+
",
160+
},
161+
Object {
162+
"dataSourceName": "lambda",
163+
"name": "func-default",
164+
"requestMappingTemplate": "{
165+
\\"version\\": \\"2018-05-29\\",
166+
\\"operation\\": \\"Invoke\\",
167+
\\"payload\\": {
168+
\\"substitution\\": \\"default-function\\",
169+
\\"type\\": \\"default\\"
170+
}
171+
}
172+
",
173+
"responseMappingTemplate": "$utils.toJson($context.result.default)
174+
",
175+
},
176+
Object {
177+
"dataSourceName": "lambda",
178+
"name": "func-direct",
179+
"requestMappingTemplate": "## Direct lambda request
180+
{
181+
\\"version\\": \\"2018-05-29\\",
182+
\\"operation\\": \\"Invoke\\",
183+
\\"payload\\": $utils.toJson($context)
184+
}
185+
",
186+
"responseMappingTemplate": "## Direct lambda response
187+
#if($ctx.error)
188+
$util.error($ctx.error.message, $ctx.error.type, $ctx.result)
189+
#end
190+
$util.toJson($ctx.result)
191+
",
192+
},
193+
]
194+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"version": "2018-05-29",
3+
"operation": "Invoke",
4+
"payload": {
5+
"substitution": "$mySubVar",
6+
"type": "default"
7+
}
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$utils.toJson($context.result.default)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"version": "2018-05-29",
3+
"operation": "Invoke",
4+
"payload": {
5+
"substitution": "$mySubVar",
6+
"args": $utils.toJson($context.arguments)
7+
}
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$utils.toJson($context.result.lambda)

src/__tests__/files/schema.graphql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type Post {
2+
userId: Int!
3+
id: Int!
4+
title: String!
5+
body: String!
6+
}
7+
8+
type Query {
9+
getPost: Post
10+
getPosts: [Post]!
11+
}
12+
13+
schema {
14+
query: Query
15+
}

src/__tests__/getAppSyncConfig.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import path from 'path';
2+
import getAppSyncConfig from '../getAppSyncConfig';
3+
4+
describe('getAppSyncConfig', () => {
5+
it('should generate a valid config', () => {
6+
const config = {
7+
name: 'myAPI',
8+
authenticationType: 'API_KEY',
9+
defaultMappingTemplates: {
10+
request: 'default.request.vtl',
11+
response: 'default.response.vtl',
12+
},
13+
mappingTemplates: [
14+
{
15+
dataSource: 'lambda',
16+
type: 'Query',
17+
field: 'templates',
18+
request: 'lambda.request.vtl',
19+
response: 'lambda.response.vtl',
20+
substitutions: {
21+
mySubVar: 'lambda',
22+
},
23+
},
24+
{
25+
dataSource: 'lambda',
26+
type: 'Query',
27+
field: 'default',
28+
substitutions: {
29+
mySubVar: 'default',
30+
},
31+
},
32+
{
33+
dataSource: 'lambda',
34+
type: 'Query',
35+
field: 'directLambda',
36+
request: false,
37+
response: false,
38+
},
39+
{
40+
type: 'Query',
41+
kind: 'PIPELINE',
42+
field: 'pipeline',
43+
functions: ['func', 'func-default'],
44+
substitutions: {
45+
mySubVar: 'pipeline',
46+
},
47+
},
48+
],
49+
functionConfigurations: [
50+
{
51+
dataSource: 'lambda',
52+
name: 'func',
53+
request: 'lambda.request.vtl',
54+
response: 'lambda.response.vtl',
55+
substitutions: {
56+
mySubVar: 'template-function',
57+
},
58+
},
59+
{
60+
dataSource: 'lambda',
61+
name: 'func-default',
62+
substitutions: {
63+
mySubVar: 'default-function',
64+
},
65+
},
66+
{
67+
dataSource: 'lambda',
68+
name: 'func-direct',
69+
request: false,
70+
response: false,
71+
},
72+
],
73+
dataSources: [
74+
{
75+
type: 'AWS_LAMBDA',
76+
name: 'lambda',
77+
config: {
78+
functionName: 'getPosts',
79+
},
80+
},
81+
{
82+
type: 'AMAZON_DYNAMODB',
83+
name: 'dynamodb',
84+
config: {
85+
tableName: 'myTable',
86+
},
87+
},
88+
{
89+
type: 'HTTP',
90+
name: 'http',
91+
config: {
92+
endpoint: 'http://127.0.0.1',
93+
},
94+
},
95+
],
96+
};
97+
98+
const result = getAppSyncConfig(
99+
{
100+
options: {
101+
apiKey: '123456789',
102+
dynamoDb: {
103+
endpoint: `http://localhost:8000`,
104+
region: 'localhost',
105+
accessKeyId: 'DEFAULT_ACCESS_KEY',
106+
secretAccessKey: 'DEFAULT_SECRET',
107+
},
108+
},
109+
serverless: {
110+
config: { servicePath: path.join(__dirname, 'files') },
111+
service: {
112+
functions: {
113+
getPost: {
114+
hndler: 'index.handler',
115+
},
116+
getPosts: {
117+
hndler: 'index.handler',
118+
},
119+
},
120+
},
121+
},
122+
},
123+
config,
124+
);
125+
expect(result.appSync).toMatchSnapshot();
126+
expect(result.schema).toMatchSnapshot();
127+
expect(result.resolvers).toMatchSnapshot();
128+
expect(result.dataSources).toMatchSnapshot();
129+
expect(result.functions).toMatchSnapshot();
130+
});
131+
});

0 commit comments

Comments
 (0)