Skip to content

Commit 80cf777

Browse files
committed
added more tests to the buildTypeWeights function to check for more schema cases
1 parent 1bbb5fc commit 80cf777

File tree

1 file changed

+336
-9
lines changed

1 file changed

+336
-9
lines changed

test/analysis/buildTypeWeights.test.ts

Lines changed: 336 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
2828

2929
test('multiple types', () => {
3030
schema = buildSchema(`
31+
type Query {
32+
user: User,
33+
movie: Movie,
34+
}
3135
type User {
3236
name: String
3337
email: String
3438
}
35-
3639
type Movie {
3740
name: String
3841
director: String
3942
}
4043
`);
4144

4245
expect(buildTypeWeightsFromSchema(schema)).toEqual({
46+
Query: {
47+
weight: 1,
48+
fields: {},
49+
},
4350
User: {
4451
weight: 1,
4552
fields: {
@@ -63,12 +70,10 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
6370
user: User
6471
movie: Movie
6572
}
66-
6773
type User {
6874
name: String
69-
email: String
75+
film: Movie
7076
}
71-
7277
type Movie {
7378
name: String
7479
director: User
@@ -121,14 +126,183 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
121126
});
122127
});
123128

129+
test('types with arguments', () => {
130+
schema = buildSchema(`
131+
type Query {
132+
character(id: ID!): Character
133+
}
134+
type Character {
135+
id: ID!
136+
name: String!
137+
}`);
138+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
139+
Query: {
140+
weight: 1,
141+
fields: {},
142+
},
143+
Character: {
144+
weight: 1,
145+
fields: {
146+
id: 0,
147+
name: 0,
148+
},
149+
},
150+
});
151+
});
152+
153+
test('enum types', () => {
154+
schema = buildSchema(`
155+
type Query {
156+
hero(episode: Episode): Character
157+
}
158+
type Character {
159+
id: ID!
160+
name: String!
161+
}
162+
enum Episode {
163+
NEWHOPE
164+
EMPIRE
165+
JEDI
166+
}`);
167+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
168+
Query: {
169+
weight: 1,
170+
fields: {},
171+
},
172+
Character: {
173+
weight: 1,
174+
fields: {
175+
id: 0,
176+
name: 0,
177+
},
178+
},
179+
Episode: {
180+
weight: 0,
181+
fields: {},
182+
},
183+
});
184+
});
185+
186+
// ? varibale weight
187+
test('fields returning lists of objects', () => {
188+
schema = buildSchema(`
189+
type Query {
190+
reviews(episode: Episode!, first: Int): [Review]
191+
}
192+
type Review {
193+
episode: Episode
194+
stars: Int!
195+
commentary: String
196+
}
197+
enum Episode {
198+
NEWHOPE
199+
EMPIRE
200+
JEDI
201+
}`);
202+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
203+
Query: {
204+
weight: 1,
205+
fields: {},
206+
},
207+
Review: {
208+
weight: 1, // ? weight is the argument passed as 'first'. it's variable...
209+
fields: {
210+
id: 0,
211+
name: 0,
212+
},
213+
},
214+
Episode: {
215+
weight: 0,
216+
fields: {},
217+
},
218+
});
219+
});
220+
221+
test('interface types', () => {
222+
schema = buildSchema(`
223+
interface Character {
224+
id: ID!
225+
name: String!
226+
friends: [Character]
227+
}
228+
229+
type Human implements Character {
230+
id: ID!
231+
name: String!
232+
homePlanet: String
233+
friends: [Character]
234+
}
235+
236+
type Droid implements Character {
237+
id: ID!
238+
name: String!
239+
friends: [Character]
240+
primaryFunction: String
241+
}`);
242+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
243+
character: {
244+
weight: 1,
245+
fields: {
246+
id: 0,
247+
name: 0,
248+
},
249+
},
250+
human: {
251+
weight: 1,
252+
fields: {
253+
id: 0,
254+
name: 0,
255+
homePlanet: 0,
256+
},
257+
},
258+
droid: {
259+
weight: 1,
260+
fields: {
261+
id: 0,
262+
name: 0,
263+
primaryFunction: 0,
264+
},
265+
},
266+
Episode: {
267+
weight: 0,
268+
fields: {},
269+
},
270+
});
271+
});
272+
273+
test('union tyes', () => {
274+
schema = buildSchema(`
275+
union SearchResult = Human | Droid
276+
type Human{
277+
homePlanet: String
278+
}
279+
type Droid {
280+
primaryFunction: String
281+
}`);
282+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
283+
SearchResult: {
284+
weight: 1,
285+
fields: {},
286+
},
287+
human: {
288+
weight: 1,
289+
fields: {
290+
homePlanet: 0,
291+
},
292+
},
293+
droid: {
294+
weight: 1,
295+
fields: {
296+
primaryFunction: 0,
297+
},
298+
},
299+
});
300+
});
301+
124302
// TODO: Tests should be written to acount for the additional scenarios possible in a schema
125303
// Mutation type
304+
// Input types (a part of mutations?)
126305
// Subscription type
127-
// List type
128-
// Enem types
129-
// Interface
130-
// Unions
131-
// Input types
132306
});
133307

134308
describe('changes "type weight object" type weights with user configuration of...', () => {
@@ -251,3 +425,156 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
251425
});
252426
});
253427
});
428+
429+
/**
430+
* Here is the schema that creates the followning typeWeightsObject used for the tests
431+
*
432+
type Query {
433+
hero(episode: Episode): Character
434+
reviews(episode: Episode!, first: Int): [Review]
435+
search(text: String): [SearchResult]
436+
character(id: ID!): Character
437+
droid(id: ID!): Droid
438+
human(id: ID!): Human
439+
}
440+
441+
enum Episode {
442+
NEWHOPE
443+
EMPIRE
444+
JEDI
445+
}
446+
447+
interface Character {
448+
id: ID!
449+
name: String!
450+
friends: [Character]
451+
appearsIn: [Episode]!
452+
}
453+
454+
type Human implements Character {
455+
id: ID!
456+
name: String!
457+
homePlanet: String
458+
friends: [Character]
459+
appearsIn: [Episode]!
460+
}
461+
462+
type Droid implements Character {
463+
id: ID!
464+
name: String!
465+
friends: [Character]
466+
primaryFunction: String
467+
appearsIn: [Episode]!
468+
}
469+
470+
type Review {
471+
episode: Episode
472+
stars: Int!
473+
commentary: String
474+
}
475+
476+
union SearchResult = Human | Droid
477+
478+
type Scalars {
479+
num: Int,
480+
id: ID,
481+
float: Float,
482+
bool: Boolean,
483+
string: String
484+
test: Test,
485+
}
486+
487+
type Test {
488+
name: String,
489+
variable: Scalars
490+
}
491+
type Topic {
492+
relatedTopics(first: Int): [Topic]
493+
name: String
494+
}
495+
*
496+
* TODO: extend this schema to include mutations, subscriptions and pagination
497+
*
498+
type Mutation {
499+
createReview(episode: Episode, review: ReviewInput!): Review
500+
}
501+
type Subscription {
502+
reviewAdded(episode: Episode): Review
503+
}
504+
type FriendsConnection {
505+
totalCount: Int
506+
edges: [FriendsEdge]
507+
friends: [Character]
508+
pageInfo: PageInfo!
509+
}
510+
type FriendsEdge {
511+
cursor: ID!
512+
node: Character
513+
}
514+
type PageInfo {
515+
startCursor: ID
516+
endCursor: ID
517+
hasNextPage: Boolean!
518+
}
519+
520+
add
521+
friendsConnection(first: Int, after: ID): FriendsConnection!
522+
to character, human and droid
523+
*/
524+
const typeWeights: TypeWeightObject = {
525+
query: {
526+
// object type
527+
weight: 1,
528+
fields: {},
529+
},
530+
episode: {
531+
// enum
532+
weight: 0,
533+
fields: {},
534+
},
535+
human: {
536+
// implements an interface
537+
weight: 1,
538+
fields: {
539+
id: 0,
540+
name: 0,
541+
homePlanet: 0,
542+
},
543+
},
544+
droid: {
545+
// implements an interface
546+
weight: 1,
547+
fields: {
548+
id: 0,
549+
name: 0,
550+
},
551+
},
552+
review: {
553+
weight: 1,
554+
fields: {
555+
stars: 0,
556+
commentary: 0,
557+
},
558+
},
559+
searchResult: {
560+
// union type
561+
weight: 1,
562+
fields: {},
563+
},
564+
scalars: {
565+
weight: 1,
566+
fields: {
567+
num: 0,
568+
id: 0,
569+
float: 0,
570+
bool: 0,
571+
string: 0,
572+
},
573+
},
574+
test: {
575+
weight: 1,
576+
fields: {
577+
name: 0,
578+
},
579+
},
580+
};

0 commit comments

Comments
 (0)