Skip to content

Commit 72ef8cd

Browse files
committed
enhanced tests for bounded lists when building type weights.
1 parent f8efb71 commit 72ef8cd

File tree

1 file changed

+150
-36
lines changed

1 file changed

+150
-36
lines changed

test/analysis/buildTypeWeights.test.ts

Lines changed: 150 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'ts-jest';
12
import { buildSchema } from 'graphql';
23
import { GraphQLSchema } from 'graphql/type/schema';
34
import buildTypeWeightsFromSchema from '../../src/analysis/buildTypeWeights';
@@ -196,45 +197,159 @@ describe('Test buildTypeWeightsFromSchema function', () => {
196197
});
197198
});
198199

199-
test('fields returning lists of objects of determinate size', () => {
200-
schema = buildSchema(`
201-
type Query {
202-
reviews(episode: Episode!, first: Int): [Review]
203-
}
204-
type Review {
205-
episode: Episode
206-
stars: Int!
207-
commentary: String
208-
}
209-
enum Episode {
210-
NEWHOPE
211-
EMPIRE
212-
JEDI
213-
}`);
200+
describe('fields returning lists of objects of determinate size and...', () => {
201+
test('args include limiting keywords: "first", "last", "limit"', () => {
202+
schema = buildSchema(`
203+
type Query {
204+
reviews(episode: Episode!, first: Int): [Review]
205+
heroes(episode: Episode!, last: Int): [Review]
206+
villains(episode: Episode!, limit: Int): [Review]
207+
}
208+
type Review {
209+
episode: Episode
210+
stars: Int!
211+
commentary: String
212+
}
213+
enum Episode {
214+
NEWHOPE
215+
EMPIRE
216+
JEDI
217+
}`);
218+
219+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
220+
query: {
221+
weight: 1,
222+
fields: {
223+
reviews: expect.any(Function),
224+
heroes: expect.any(Function),
225+
villains: expect.any(Function),
226+
},
227+
},
228+
review: {
229+
weight: 1,
230+
fields: {
231+
stars: 0,
232+
commentary: 0,
233+
},
234+
},
235+
episode: {
236+
weight: 0,
237+
fields: {},
238+
},
239+
});
240+
});
214241

215-
expect(buildTypeWeightsFromSchema(schema)).toEqual({
216-
query: {
217-
weight: 1,
218-
fields: {
219-
reviews: expect.any(Function), // TODO: Test this function separately
242+
test('are not on the Query type', () => {
243+
schema = buildSchema(`
244+
type Query {
245+
reviews(episode: Episode!, first: Int): [Movie]
246+
}
247+
type Movie {
248+
episode: Episode
249+
stars: Int!
250+
commentary: String
251+
heroes(episode: Episode!, last: Int): [Character]
252+
villains(episode: Episode!, limit: Int): [Character]
253+
}
254+
type Character {
255+
name: String!
256+
}
257+
enum Episode {
258+
NEWHOPE
259+
EMPIRE
260+
JEDI
261+
}`);
262+
263+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
264+
query: {
265+
weight: 1,
266+
fields: {
267+
reviews: expect.any(Function),
268+
},
220269
},
221-
},
222-
review: {
223-
weight: 1,
224-
fields: {
225-
stars: 0,
226-
commentary: 0,
270+
movie: {
271+
weight: 1,
272+
fields: {
273+
stars: 0,
274+
commentary: 0,
275+
heroes: expect.any(Function),
276+
villains: expect.any(Function),
277+
},
227278
},
228-
},
229-
episode: {
230-
weight: 0,
231-
fields: {},
232-
},
279+
character: {
280+
weight: 1,
281+
fields: {
282+
name: 0,
283+
},
284+
},
285+
episode: {
286+
weight: 0,
287+
fields: {},
288+
},
289+
});
290+
});
291+
292+
test('the list resolves to an enum or scalar', () => {
293+
schema = buildSchema(`
294+
type Query {
295+
episodes(first: Int): [Episode]
296+
heroes(episode: Episode!, first: Int = 3): [Int]
297+
villains(episode: Episode!, limit: Int! = 1): [String]
298+
}
299+
enum Episode {
300+
NEWHOPE
301+
EMPIRE
302+
JEDI
303+
}`);
304+
305+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
306+
query: {
307+
weight: 1,
308+
fields: {
309+
episodes: 0,
310+
heroes: 0,
311+
villains: 0,
312+
},
313+
},
314+
episode: {
315+
weight: 0,
316+
fields: {},
317+
},
318+
});
319+
});
320+
321+
test('the list resolves to an enum or scalar and a custom scalar weight was configured', () => {
322+
schema = buildSchema(`
323+
type Query {
324+
episodes(first: Int): [Episode]
325+
heroes(episode: Episode!, first: Int = 3): [Int]
326+
villains(episode: Episode!, limit: Int! = 1): [String]
327+
}
328+
enum Episode {
329+
NEWHOPE
330+
EMPIRE
331+
JEDI
332+
}`);
333+
334+
expect(buildTypeWeightsFromSchema(schema, { scalar: 11 })).toEqual({
335+
query: {
336+
weight: 1,
337+
fields: {
338+
episodes: 11,
339+
heroes: 11,
340+
villains: 11,
341+
},
342+
},
343+
episode: {
344+
weight: 0,
345+
fields: {},
346+
},
347+
});
233348
});
234349
});
235350

236-
// TODO: need to figure out how to handle this situation. Skip for now.
237-
// The field friends returns a list of an unknown number of objects.
351+
// FIXME: need to figure out how to handle this situation. Skip for now.
352+
// The field 'friends' returns a list of an unknown number of objects.
238353
xtest('fields returning lists of objects of indeterminate size', () => {
239354
schema = buildSchema(`
240355
type Human {
@@ -248,7 +363,6 @@ describe('Test buildTypeWeightsFromSchema function', () => {
248363
human: {
249364
weight: 1,
250365
fields: {
251-
// TODO: Test this function separately.
252366
friends: expect.any(Function),
253367
},
254368
},
@@ -329,7 +443,7 @@ describe('Test buildTypeWeightsFromSchema function', () => {
329443
});
330444
});
331445

332-
// TODO: Tests should be written to acount for the additional scenarios possible in a schema
446+
// TODO: Tests should be written to account for the additional scenarios possible in a schema
333447
// Mutation type
334448
// Input types (a part of mutations?)
335449
// Subscription type
@@ -453,6 +567,6 @@ describe('Test buildTypeWeightsFromSchema function', () => {
453567
});
454568

455569
// TODO: throw validation error if schema is invalid
456-
test('schema is invalid', () => {});
570+
xtest('schema is invalid', () => {});
457571
});
458572
});

0 commit comments

Comments
 (0)