Skip to content

Commit 1bf84eb

Browse files
committed
expanded the tests to include edgecases for custom object and scalar lists as well as nesting
1 parent 83f4cee commit 1bf84eb

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/analysis/weightFunction.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
2525
episode: Episode
2626
stars: Int!
2727
commentary: String
28+
scalarList(last: Int): [Int]
29+
objectList(first: Int): [Object]
30+
}
31+
type Object {
32+
hi: String
2833
}
2934
enum Episode {
3035
NEWHOPE
@@ -86,12 +91,47 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
8691
expect(getQueryTypeComplexity(queryAST, {}, customTypeWeights)).toBe(10);
8792
});
8893

94+
test('a custom object weight was set to 0', () => {
95+
const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, {
96+
object: 0,
97+
});
98+
const query = `query { heroes(episode: NEWHOPE, first: 3) { stars, episode } }`;
99+
const queryAST: DocumentNode = parse(query);
100+
expect(getQueryTypeComplexity(queryAST, {}, customTypeWeights)).toBe(4); // 1 query and 4 enums
101+
});
102+
test('a custom scalar weight was set to greater than 0', () => {
103+
const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, {
104+
scalar: 2,
105+
});
106+
const query = `query { heroes(episode: NEWHOPE, first: 3) { stars, episode } }`;
107+
const queryAST: DocumentNode = parse(query);
108+
expect(getQueryTypeComplexity(queryAST, {}, customTypeWeights)).toBe(16);
109+
});
110+
89111
test('variable names matching limiting keywords do not interfere with scalar argument values', () => {
90112
const query = `query variableQuery ($items: Int){ heroes(episode: NEWHOPE, first: 3) { stars, episode } }`;
91113
const queryAST: DocumentNode = parse(query);
92114
expect(getQueryTypeComplexity(queryAST, { first: 7 }, typeWeights)).toBe(4);
93115
});
94116

117+
test('nested queries with lists', () => {
118+
const query = `query { reviews(episode: NEWHOPE, first: 2) {stars, objectList(first: 3) {hi}}} `;
119+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(9); // 1 Query + 2 review + (2 * 3 objects)
120+
});
121+
122+
test('queries with inner scalar lists', () => {
123+
const query = `query { reviews(episode: NEWHOPE, first: 2) {stars, scalarList(last: 3) }}`;
124+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(3); // 1 Query + 2 reviews
125+
});
126+
127+
test('queries with inner scalar lists and custom scalar weight greater than 0', () => {
128+
const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, {
129+
scalar: 2,
130+
});
131+
const query = `query { reviews(episode: NEWHOPE, first: 2) {stars, scalarList(last: 3) }}`;
132+
expect(getQueryTypeComplexity(parse(query), {}, customTypeWeights)).toBe(19); // 1 Query + 2 reviews + 2 * (2 stars + (3 * 2 scalarList)
133+
});
134+
95135
xtest('an invalid arg type is provided', () => {
96136
const query = `query { heroes(episode: NEWHOPE, first = 3) { stars, episode } }`;
97137
const queryAST: DocumentNode = parse(query);

0 commit comments

Comments
 (0)