Skip to content

Commit dfd12ca

Browse files
authored
test: add tests for mkAllPublic (#51)
* test: add tests for mkAllPublic * chore: imports * chore: fix operation * chore: drop Authorization header for noAuth
1 parent 0445a6b commit dfd12ca

File tree

4 files changed

+73
-23
lines changed

4 files changed

+73
-23
lines changed
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
type Customer {
2-
name: String
3-
city: String
2+
name: String
3+
city: String
44
}
55
type Query {
6-
# An ecmascript generator of customer data.
7-
# Of course, in real life you will call an API or a database. You can do that by changing the `endpoint` argument on the `@rest` directive.
8-
# https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service
9-
customer (id: ID): Customer
10-
@rest (endpoint: "stepzen:empty"
11-
ecmascript: """
12-
function transformREST(s) {
13-
var id = get('id')
14-
if (id==1)
15-
return (JSON.stringify({"name":"John Doe","city":"Miami"}))
16-
else
17-
return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara"}))
18-
}
19-
"""
6+
# An ecmascript generator of customer data.
7+
# Of course, in real life you will call an API or a database. You can do that by changing the `endpoint` argument on the `@rest` directive.
8+
# https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service
9+
customer(id: ID!): Customer
10+
@rest(
11+
endpoint: "stepzen:empty"
12+
ecmascript: """
13+
function transformREST(s) {
14+
var id = get('id')
15+
if (id==1)
16+
return (JSON.stringify({"name":"John Doe","city":"Miami"}))
17+
else
18+
return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara"}))
19+
}
20+
"""
2021
)
21-
}
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
query Customer($id: ID!) {
2+
customer(id: $id) {
3+
name
4+
city
5+
}
6+
}
Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require("fs");
2+
const path = require("node:path");
13
const {
24
deployAndRun,
35
authTypes,
@@ -6,8 +8,42 @@ const {
68

79
testDescription = getTestDescription("snippets", __dirname);
810

11+
const requestsFile = path.join(path.dirname(__dirname), "operations.graphql");
12+
const requests = fs.readFileSync(requestsFile, "utf8").toString();
13+
914
describe(testDescription, function () {
15+
// note tests using no authorization since api is public
1016
const tests = [
11-
]
17+
{
18+
label: "customer-1",
19+
query: requests,
20+
operationName: "Customer",
21+
variables: {
22+
id: 1,
23+
},
24+
expected: {
25+
customer: {
26+
name: "John Doe",
27+
city: "Miami",
28+
},
29+
},
30+
authType: authTypes.noAuth,
31+
},
32+
{
33+
label: "customer-2",
34+
query: requests,
35+
operationName: "Customer",
36+
variables: {
37+
id: 2,
38+
},
39+
expected: {
40+
customer: {
41+
name: "Jane Smith",
42+
city: "Santa Clara",
43+
},
44+
},
45+
authType: authTypes.noAuth,
46+
},
47+
];
1248
return deployAndRun(__dirname, tests);
13-
});
49+
});

tests/gqltest.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function deployEndpoint(endpoint, dirname) {
4646
// The test will fail if the request does not
4747
// have status 200 or has any GraphQL errors.
4848
function runGqlOk(authType, endpoint, query, variables, operationName) {
49+
let authValue;
4950
switch (authType) {
5051
case authTypes.adminKey:
5152
authValue = adminKey;
@@ -59,12 +60,18 @@ function runGqlOk(authType, endpoint, query, variables, operationName) {
5960
default:
6061
authValue = "";
6162
}
63+
64+
let headers = {
65+
"Content-Type": "application/json",
66+
};
67+
68+
if (authValue) {
69+
headers.Authorization = authValue;
70+
}
71+
6272
return fetch(endpoint, {
6373
method: "POST",
64-
headers: {
65-
"Content-Type": "application/json",
66-
Authorization: authValue,
67-
},
74+
headers: headers,
6875
body: JSON.stringify({
6976
query: query,
7077
variables: variables,

0 commit comments

Comments
 (0)