forked from apollographql/graphql-tag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
453 lines (411 loc) · 14.1 KB
/
test.js
File metadata and controls
453 lines (411 loc) · 14.1 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
const gqlRequire = require('./src');
const gqlDefault = require('./src').default;
const loader = require('./loader');
const assert = require('chai').assert;
[gqlRequire, gqlDefault].forEach((gql, i) => {
describe(`gql ${i}`, () => {
it('parses queries', () => {
assert.equal(gql`{ testQuery }`.kind, 'Document');
});
it('parses queries when called as a function', () => {
assert.equal(gql('{ testQuery }').kind, 'Document');
});
it('parses queries with weird substitutions', () => {
const obj = {};
assert.equal(gql`{ field(input: "${obj.missing}") }`.kind, 'Document');
assert.equal(gql`{ field(input: "${null}") }`.kind, 'Document');
assert.equal(gql`{ field(input: "${0}") }`.kind, 'Document');
});
it('allows interpolation of documents generated by the webpack loader', () => {
const sameFragment = "fragment SomeFragmentName on SomeType { someField }";
const jsSource = loader.call(
{ cacheable() {} },
"fragment SomeFragmentName on SomeType { someField }"
);
const module = { exports: undefined };
eval(jsSource);
const document = gql`query { ...SomeFragmentName } ${module.exports}`;
assert.equal(document.kind, 'Document');
assert.equal(document.definitions.length, 2);
assert.equal(document.definitions[0].kind, 'OperationDefinition');
assert.equal(document.definitions[1].kind, 'FragmentDefinition');
});
it('parses queries through webpack loader', () => {
const jsSource = loader.call({ cacheable() {} }, '{ testQuery }');
const module = { exports: undefined };
eval(jsSource);
assert.equal(module.exports.kind, 'Document');
});
it('correctly imports other files through the webpack loader', () => {
const query = `#import "./fragment_definition.graphql"
query {
author {
...authorDetails
}
}`;
const jsSource = loader.call({ cacheable() {} }, query);
const oldRequire = require;
const module = { exports: undefined };
const require = (path) => {
assert.equal(path, './fragment_definition.graphql');
return gql`
fragment authorDetails on Author {
firstName
lastName
}`;
};
eval(jsSource);
assert.equal(module.exports.kind, 'Document');
const definitions = module.exports.definitions;
assert.equal(definitions.length, 2);
assert.equal(definitions[0].kind, 'OperationDefinition');
assert.equal(definitions[1].kind, 'FragmentDefinition');
});
it('does not complain when presented with normal comments', (done) => {
assert.doesNotThrow(() => {
const query = `#normal comment
query {
author {
...authorDetails
}
}`;
const jsSource = loader.call({ cacheable() {} }, query);
const module = { exports: undefined };
eval(jsSource);
assert.equal(module.exports.kind, 'Document');
done();
});
});
it('returns the same object for the same query', () => {
assert.isTrue(gql`{ sameQuery }` === gql`{ sameQuery }`);
});
it('returns the same object for the same query, even with whitespace differences', () => {
assert.isTrue(gql`{ sameQuery }` === gql` { sameQuery, }`);
});
it('is correct for a simple query', () => {
const ast = gql`
{
user(id: 5) {
firstName
lastName
}
}
`;
assert.equal(ast.kind, "Document");
assert.deepEqual(ast.definitions, [
{
"kind": "OperationDefinition",
"operation": "query",
"name": null,
"variableDefinitions": null,
"directives": [],
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"alias": null,
"name": {
"kind": "Name",
"value": "user"
},
"arguments": [
{
"kind": "Argument",
"name": {
"kind": "Name",
"value": "id"
},
"value": {
"kind": "IntValue",
"value": "5"
}
}
],
"directives": [],
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"alias": null,
"name": {
"kind": "Name",
"value": "firstName"
},
"arguments": [],
"directives": [],
"selectionSet": null
},
{
"kind": "Field",
"alias": null,
"name": {
"kind": "Name",
"value": "lastName"
},
"arguments": [],
"directives": [],
"selectionSet": null
}
]
}
}
]
}
}
]);
});
it('returns the same object for the same fragment', () => {
assert.isTrue(gql`fragment same on Same { sameQuery }` ===
gql`fragment same on Same { sameQuery }`);
});
it('is correct for a fragment', () => {
const ast = gql`
fragment UserFragment on User {
firstName
lastName
}
`;
assert.equal(ast.kind, "Document");
assert.deepEqual(ast.definitions, [
{
"kind": "FragmentDefinition",
"name": {
"kind": "Name",
"value": "UserFragment"
},
"typeCondition": {
kind: "NamedType",
"name": {
"kind": "Name",
"value": "User"
}
},
"directives": [],
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"alias": null,
"name": {
"kind": "Name",
"value": "firstName"
},
"arguments": [],
"directives": [],
"selectionSet": null
},
{
"kind": "Field",
"alias": null,
"name": {
"kind": "Name",
"value": "lastName"
},
"arguments": [],
"directives": [],
"selectionSet": null
}
]
}
}
]);
});
const fragmentAst = gql`
fragment UserFragment on User {
firstName
lastName
}
`;
it('can reference a fragment passed as a document', () => {
const ast = gql`
{
user(id: 5) {
...UserFragment
}
}
${fragmentAst}
`;
assert.deepEqual(ast, gql`
{
user(id: 5) {
...UserFragment
}
}
fragment UserFragment on User {
firstName
lastName
}
`);
});
it('returns the same object for the same document with substitution', () => {
// We know that calling `gql` on a fragment string will always return
// the same document, so we can reuse `fragmentAst`
assert.isTrue(gql`{ ...UserFragment } ${fragmentAst}` ===
gql`{ ...UserFragment } ${fragmentAst}`);
});
it('can reference a fragment that references as fragment', () => {
const secondFragmentAst = gql`
fragment SecondUserFragment on User {
...UserFragment
}
${fragmentAst}
`;
const ast = gql`
{
user(id: 5) {
...SecondUserFragment
}
}
${secondFragmentAst}
`;
assert.deepEqual(ast, gql`
{
user(id: 5) {
...SecondUserFragment
}
}
fragment SecondUserFragment on User {
...UserFragment
}
fragment UserFragment on User {
firstName
lastName
}
`);
});
describe('fragment warnings', () => {
let warnings = [];
const oldConsoleWarn = console.warn;
beforeEach(() => {
gqlRequire.resetCaches();
warnings = [];
console.warn = (w) => warnings.push(w);
});
afterEach(() => {
console.warn = oldConsoleWarn;
});
it('warns if you use the same fragment name for different fragments', () => {
const frag1 = gql`fragment TestSame on Bar { fieldOne }`;
const frag2 = gql`fragment TestSame on Bar { fieldTwo }`;
assert.isFalse(frag1 === frag2);
assert.equal(warnings.length, 1);
});
it('does not warn if you use the same fragment name for the same fragment', () => {
const frag1 = gql`fragment TestDifferent on Bar { fieldOne }`;
const frag2 = gql`fragment TestDifferent on Bar { fieldOne }`;
assert.isTrue(frag1 === frag2);
assert.equal(warnings.length, 0);
});
it('does not warn if you use the same embedded fragment in two different queries', () => {
const frag1 = gql`fragment TestEmbedded on Bar { field }`;
const query1 = gql`{ bar { fieldOne ...TestEmbedded } } ${frag1}`;
const query2 = gql`{ bar { fieldTwo ...TestEmbedded } } ${frag1}`;
assert.isFalse(query1 === query2);
assert.equal(warnings.length, 0);
});
it('does not warn if you use the same fragment name for embedded and non-embedded fragments', () => {
const frag1 = gql`fragment TestEmbeddedTwo on Bar { field }`;
const query1 = gql`{ bar { ...TestEmbedded } } ${frag1}`;
const query2 = gql`{ bar { ...TestEmbedded } } fragment TestEmbeddedTwo on Bar { field }`;
assert.equal(warnings.length, 0);
});
});
describe('unique fragments', () => {
beforeEach(() => {
gqlRequire.resetCaches();
});
it('strips duplicate fragments from the document', () => {
const frag1 = gql`fragment TestDuplicate on Bar { field }`;
const query1 = gql`{ bar { fieldOne ...TestDuplicate } } ${frag1} ${frag1}`;
const query2 = gql`{ bar { fieldOne ...TestDuplicate } } ${frag1}`;
assert.equal(query1.definitions.length, 2);
assert.equal(query1.definitions[1].kind, 'FragmentDefinition');
// We don't test strict equality between the two queries because the source.body parsed from the
// document is not the same, but the set of definitions should be.
assert.deepEqual(query1.definitions, query2.definitions);
});
it('ignores duplicate fragments from second-level imports when using the webpack loader', () => {
// take a require function and a query string, use the webpack loader to process it
const load = (require, query) => {
const jsSource = loader.call({ cacheable() {} }, query);
const module = { exports: undefined };
eval(jsSource);
return module.exports;
}
const test_require = (path) => {
switch (path) {
case './friends.graphql':
return load(test_require, [
'#import "./person.graphql"',
'fragment friends on Hero { friends { ...person } }',
].join('\n'));
case './enemies.graphql':
return load(test_require, [
'#import "./person.graphql"',
'fragment enemies on Hero { enemies { ...person } }',
].join('\n'));
case './person.graphql':
return load(test_require, 'fragment person on Person { name }\n');
default:
return null;
};
};
const result = load(test_require, [
'#import "./friends.graphql"',
'#import "./enemies.graphql"',
'query { hero { ...friends ...enemies } }',
].join('\n'));
assert.equal(result.kind, 'Document');
assert.equal(result.definitions.length, 4, 'after deduplication, only 4 fragments should remain');
assert.equal(result.definitions[0].kind, 'OperationDefinition');
// the rest of the definitions should be fragments and contain one of
// each: "friends", "enemies", "person". Order does not matter
const fragments = result.definitions.slice(1)
assert(fragments.every(fragment => fragment.kind === 'FragmentDefinition'))
assert(fragments.some(fragment => fragment.name.value === 'friends'))
assert(fragments.some(fragment => fragment.name.value === 'enemies'))
assert(fragments.some(fragment => fragment.name.value === 'person'))
});
});
// How to make this work?
// it.only('can reference a fragment passed as a document via shorthand', () => {
// const ast = gql`
// {
// user(id: 5) {
// ...${userFragmentDocument}
// }
// }
// `;
//
// assert.deepEqual(ast, gql`
// {
// user(id: 5) {
// ...UserFragment
// }
// }
// fragment UserFragment on User {
// firstName
// lastName
// }
// `);
// });
describe('descriptors', function() {
it('adds comments as descriptors', function() {
const ast = gql`
# This is a type descriptor
type Foo {
# This is a field descriptor
bar: String
baz: Int
}
`;
assert.equal(ast.definitions[0].description, 'This is a type descriptor');
assert.equal(ast.definitions[0].fields[0].description, 'This is a field descriptor');
assert.equal(ast.definitions[0].fields[1].description, undefined);
})
})
});
});