This repository was archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathTypeScriptGenerator-test.ts
190 lines (182 loc) · 5.42 KB
/
TypeScriptGenerator-test.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { CompilerContext, IRTransforms, Root } from "relay-compiler";
import { TypeGeneratorOptions } from "relay-compiler/lib/language/RelayLanguagePluginInterface";
import { generateTestsFromFixtures } from "relay-test-utils-internal/lib/generateTestsFromFixtures";
import parseGraphQLText = require("relay-test-utils-internal/lib/parseGraphQLText");
import { TestSchema } from "relay-test-utils-internal/lib/TestSchema";
import * as TypeScriptGenerator from "../src/TypeScriptGenerator";
function generate(
text: string,
options: TypeGeneratorOptions,
context?: CompilerContext
) {
const schema = TestSchema.extend([
...IRTransforms.schemaExtensions,
/* GraphQL */ `
scalar Color
extend type User {
color: Color
}
type ExtraUser implements Actor {
address: StreetAddress
allPhones: [Phone]
birthdate: Date
emailAddresses: [String]
firstName(if: Boolean, unless: Boolean): String
friends(
after: ID
before: ID
first: Int
last: Int
orderby: [String]
find: String
isViewerFriend: Boolean
if: Boolean
unless: Boolean
traits: [PersonalityTraits]
): FriendsConnection
hometown: Page
id: ID!
lastName: String
name: String
nameRenderer(supported: [String!]): UserNameRenderer
nameRenderable(supported: [String!]): UserNameRenderable
profilePicture(size: [Int], preset: PhotoSize): Image
screennames: [Screenname]
subscribeStatus: String
subscribers(first: Int): SubscribersConnection
url(relative: Boolean, site: String): String
websites: [String]
username: String
}
`,
]);
const { definitions, schema: extendedSchema } = parseGraphQLText(
schema,
text
);
return new CompilerContext(extendedSchema)
.addAll(definitions)
.applyTransforms(TypeScriptGenerator.transforms)
.documents()
.map(
(doc) =>
`// ${doc.name}.graphql\n${TypeScriptGenerator.generate(
extendedSchema,
doc as any,
{
...options,
normalizationIR: context
? (context.get(doc.name) as Root)
: undefined,
}
)}`
)
.join("\n\n");
}
describe("Snapshot tests", () => {
function generateContext(text: string) {
const relaySchema = TestSchema.extend(IRTransforms.schemaExtensions);
const { definitions, schema: extendedSchema } = parseGraphQLText(
relaySchema,
text
);
return new CompilerContext(extendedSchema)
.addAll(definitions)
.applyTransforms([
...IRTransforms.commonTransforms,
...IRTransforms.queryTransforms,
...IRTransforms.codegenTransforms,
]);
}
describe("TypeScriptGenerator with a single artifact directory", () => {
generateTestsFromFixtures(
`${__dirname}/fixtures/type-generator`,
(text: string) => {
const context = generateContext(text);
return generate(
text,
{
customScalars: {},
// enumsHasteModule: null,
existingFragmentNames: new Set(["PhotoFragment"]),
optionalInputFields: [],
useHaste: false,
useSingleArtifactDirectory: true,
noFutureProofEnums: false,
},
context
);
}
);
});
describe("TypeScriptGenerator without a single artifact directory", () => {
generateTestsFromFixtures(
`${__dirname}/fixtures/type-generator`,
(text: string) => {
const context = generateContext(text);
return generate(
text,
{
customScalars: {},
// enumsHasteModule: null,
existingFragmentNames: new Set(["PhotoFragment"]),
optionalInputFields: [],
useHaste: false,
useSingleArtifactDirectory: false,
noFutureProofEnums: false,
},
context
);
}
);
});
});
describe("Does not add `%future added values` when the noFutureProofEnums option is set", () => {
const text = `
fragment ScalarField on User {
traits
}
`;
const types = generate(text, {
customScalars: {},
// enumsHasteModule: null,
existingFragmentNames: new Set(["PhotoFragment"]),
optionalInputFields: [],
useHaste: true,
useSingleArtifactDirectory: false,
// This is what's different from the tests above.
noFutureProofEnums: true,
});
// Without the option, PersonalityTraits would be `"CHEERFUL" | ... | "%future added value");`
expect(types).toContain(
'export type PersonalityTraits = "CHEERFUL" | "DERISIVE" | "HELPFUL" | "SNARKY";'
);
});
describe.each`
mapping | type
${"String"} | ${"string"}
${"Url"} | ${"string"}
${"ID"} | ${"string"}
${"Int"} | ${"number"}
${"Color"} | ${"Color"}
${"{}"} | ${"{}"}
${"[]"} | ${"[]"}
`("Custom scalar mapping $mapping to $type", ({ mapping, type }) => {
const text = `
fragment Test on User {
color
}
`;
const types = generate(text, {
customScalars: {
Color: mapping,
},
// enumsHasteModule: null,
existingFragmentNames: new Set(["PhotoFragment"]),
optionalInputFields: [],
useHaste: false,
useSingleArtifactDirectory: true,
noFutureProofEnums: false,
});
expect(types).toContain(`color: ${type} | null`);
});