/
index.test.js
88 lines (86 loc) · 1.65 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {
graphql,
GraphQLObjectType,
GraphQLSchema,
Kind,
GraphQLError,
} from "graphql";
import { makeExecutableSchema } from "graphql-tools";
import JSON5 from "../";
import json5 from "json5";
const schema1 = makeExecutableSchema({
typeDefs: /* GraphQL */ `
scalar JSON5
type Query {
value(params: JSON5): JSON5
}
`,
resolvers: {
JSON5,
Query: {
value(parent, args, context, info) {
return args.params;
},
},
},
});
const schema2 = new GraphQLSchema({
types: [JSON5],
query: new GraphQLObjectType({
name: "Query",
fields: {
value: {
type: JSON5,
args: {
params: {
type: JSON5,
},
},
resolve(source, args, context, info) {
return args.params;
},
},
},
}),
});
describe("JSON", () => {
it("works #1", async () => {
expect(
await graphql(
schema1,
/* GraphQL */ `
query {
value(params: "{ x: 'y'}")
}
`,
),
).toMatchObject({
data: { value: json5.stringify({ x: "y" }) },
});
});
it("works #2", async () => {
expect(
await graphql(
schema2,
/* GraphQL */ `
query {
value(params: "{ x: 'y'}")
}
`,
),
).toMatchObject({
data: { value: json5.stringify({ x: "y" }) },
});
});
it("rejects what json5 can't parse", async () => {
const { errors } = await graphql(
schema1,
/* GraphQL */ `
query {
value(params: {})
}
`,
);
expect(errors[0]).toBeInstanceOf(GraphQLError);
});
});