Skip to content

Commit 5f0b037

Browse files
committed
added a test for fragments with multiple levels of nesting and unions with non-null types
1 parent 5fe7133 commit 5f0b037

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

test/analysis/buildTypeWeights.test.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,6 @@ describe('Test buildTypeWeightsFromSchema function', () => {
509509
},
510510
});
511511
});
512-
513-
xtest('additional test cases for ...', () => {
514-
// TODO: unions with non-null types
515-
// unions with lists of non-null types
516-
// lists with > 2 levels of nesting (may need to add these for lists on other types as well)
517-
});
518512
});
519513

520514
xdescribe('Not null operator (!) is used', () => {
@@ -612,6 +606,55 @@ describe('Test buildTypeWeightsFromSchema function', () => {
612606
},
613607
});
614608
});
609+
610+
test('on union types', () => {
611+
schema = buildSchema(`
612+
union SearchResult = Human | Droid
613+
type Human{
614+
name: String
615+
homePlanet: String
616+
search(first: Int!): [SearchResult!]!
617+
}
618+
type Droid {
619+
name: String
620+
primaryFunction: String!
621+
search(first: Int!): [SearchResult!]!
622+
}`);
623+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
624+
searchresult: {
625+
weight: 1,
626+
fields: {
627+
name: { weight: 0 },
628+
search: {
629+
resolveTo: 'searchresult',
630+
weight: expect.any(Function),
631+
},
632+
},
633+
},
634+
human: {
635+
weight: 1,
636+
fields: {
637+
name: { weight: 0 },
638+
homePlanet: { weight: 0 },
639+
search: {
640+
resolveTo: 'searchresult',
641+
weight: expect.any(Function),
642+
},
643+
},
644+
},
645+
droid: {
646+
weight: 1,
647+
fields: {
648+
name: { weight: 0 },
649+
primaryFunction: { weight: 0 },
650+
search: {
651+
resolveTo: 'searchresult',
652+
weight: expect.any(Function),
653+
},
654+
},
655+
},
656+
});
657+
});
615658
});
616659

617660
// TODO: Tests should be written to account for the additional scenarios possible in a schema

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,40 @@ describe('Test getQueryTypeComplexity function', () => {
600600
expect(getQueryTypeComplexity(parse(query), {}, unionTypeWeights)).toBe(5);
601601
});
602602

603+
test('that have greater than 2 levels of nesting', () => {
604+
query = `
605+
query {
606+
hero(episode: EMPIRE) {
607+
name
608+
... on Droid {
609+
primaryFunction
610+
friends(first: 5) {
611+
name
612+
friends(first: 3) {
613+
name
614+
}
615+
}
616+
}
617+
... on Human {
618+
homePlanet
619+
friends(first: 5) {
620+
name
621+
friends(first: 3) {
622+
name
623+
}
624+
}
625+
}
626+
}
627+
}`;
628+
mockCharacterFriendsFunction.mockReturnValue(3);
629+
mockDroidFriendsFunction.mockReturnValueOnce(20);
630+
mockHumanFriendsFunction.mockReturnValueOnce(20);
631+
// Query 1 + 1 hero + 3 friends/character
632+
expect(getQueryTypeComplexity(parse(query), variables, unionTypeWeights)).toBe(
633+
22
634+
);
635+
});
636+
603637
xtest('that include a directive', () => {
604638
query = `
605639
query {

0 commit comments

Comments
 (0)