|
| 1 | +import 'ts-jest'; |
| 2 | +import { buildSchema, DocumentNode, parse } from 'graphql'; |
| 3 | +import { TypeWeightObject } from '../../src/@types/buildTypeWeights'; |
| 4 | +import buildTypeWeightsFromSchema from '../../src/analysis/buildTypeWeights'; |
| 5 | +import getQueryTypeComplexity from '../../src/analysis/typeComplexityAnalysis'; |
| 6 | +// Test the weight function generated by the typeweights object when a limiting keyword is provided |
| 7 | + |
| 8 | +// Test cases: |
| 9 | +// Default value provided to schema |
| 10 | +// Arg passed in as variable |
| 11 | +// Arg passed in as scalar |
| 12 | +// Invalid arg type provided |
| 13 | + |
| 14 | +// Default value passed with query |
| 15 | + |
| 16 | +describe('Weight Function correctly parses Argument Nodes if', () => { |
| 17 | + const schema = buildSchema(` |
| 18 | + type Query { |
| 19 | + reviews(episode: Episode!, first: Int = 5): [Review] |
| 20 | + heroes(episode: Episode!, first: Int): [Review] |
| 21 | + villains(episode: Episode!, limit: Int! = 3): [Review] |
| 22 | + characters(episode: Episode!, limit: Int!): [Review] |
| 23 | + } |
| 24 | + type Review { |
| 25 | + episode: Episode |
| 26 | + stars: Int! |
| 27 | + commentary: String |
| 28 | + } |
| 29 | + enum Episode { |
| 30 | + NEWHOPE |
| 31 | + EMPIRE |
| 32 | + JEDI |
| 33 | + }`); |
| 34 | + |
| 35 | + const typeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema); |
| 36 | + |
| 37 | + describe('a default value is provided in the schema', () => { |
| 38 | + test('and a value is not provided with the query', () => { |
| 39 | + const query = `query { reviews(episode: NEWHOPE) { stars, episode } }`; |
| 40 | + const queryAST: DocumentNode = parse(query); |
| 41 | + expect(getQueryTypeComplexity(queryAST, {}, typeWeights)).toBe(5); |
| 42 | + }); |
| 43 | + |
| 44 | + test('and a scalar value is provided with the query', () => { |
| 45 | + const query = `query { reviews(episode: NEWHOPE, first: 3) { stars, episode } }`; |
| 46 | + const queryAST: DocumentNode = parse(query); |
| 47 | + expect(getQueryTypeComplexity(queryAST, {}, typeWeights)).toBe(4); |
| 48 | + }); |
| 49 | + |
| 50 | + test('and the argument is passed in as a variable', () => { |
| 51 | + const query = `query variableQuery ($items: Int){ reviews(episode: NEWHOPE, first: $items) { stars, episode } }`; |
| 52 | + const queryAST: DocumentNode = parse(query); |
| 53 | + expect(getQueryTypeComplexity(queryAST, { items: 7, first: 4 }, typeWeights)).toBe(8); |
| 54 | + expect(getQueryTypeComplexity(queryAST, { first: 4, items: 7 }, typeWeights)).toBe(8); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('a default value is not provided in the schema', () => { |
| 59 | + xtest('and a value is not provied with the query', () => { |
| 60 | + const query = `query { heroes(episode: NEWHOPE) { stars, episode } }`; |
| 61 | + const queryAST: DocumentNode = parse(query); |
| 62 | + // FIXME: Update expected result if unbounded lists are suppored |
| 63 | + expect(getQueryTypeComplexity(queryAST, {}, typeWeights)).toBe(5); |
| 64 | + }); |
| 65 | + |
| 66 | + test('and a scalar value is provided with the query', () => { |
| 67 | + const query = `query { heroes(episode: NEWHOPE, first: 3) { stars, episode } }`; |
| 68 | + const queryAST: DocumentNode = parse(query); |
| 69 | + expect(getQueryTypeComplexity(queryAST, {}, typeWeights)).toBe(4); |
| 70 | + }); |
| 71 | + |
| 72 | + test('and the argument is passed in as a variable', () => { |
| 73 | + const query = `query variableQuery ($items: Int){ heroes(episode: NEWHOPE, first: $items) { stars, episode } }`; |
| 74 | + const queryAST: DocumentNode = parse(query); |
| 75 | + expect(getQueryTypeComplexity(queryAST, { items: 7 }, typeWeights)).toBe(8); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + xtest('an invalid arg type is provided', () => { |
| 80 | + const query = `query { heroes(episode: NEWHOPE, first = 3) { stars, episode } }`; |
| 81 | + const queryAST: DocumentNode = parse(query); |
| 82 | + // FIXME: What is the expected behavior? Treat as unbounded? |
| 83 | + fail('test not implemented'); |
| 84 | + }); |
| 85 | +}); |
0 commit comments