1- import { ArgumentNode } from 'graphql/language' ;
21import { parse } from 'graphql' ;
32import getQueryTypeComplexity from '../../src/analysis/typeComplexityAnalysis' ;
4- import { TypeWeightObject } from '../../src/@types/buildTypeWeights' ;
3+ import { TypeWeightObject , Variables } from '../../src/@types/buildTypeWeights' ;
54
65/**
76 * Here is the schema that creates the followning 'typeWeightsObject' used for the tests
@@ -97,15 +96,23 @@ import { TypeWeightObject } from '../../src/@types/buildTypeWeights';
9796 to character, human and droid
9897*/
9998
99+ const mockWeightFunction = jest . fn ( ) ;
100+ const mockHumanFriendsFunction = jest . fn ( ) ;
101+ const mockDroidFriendsFunction = jest . fn ( ) ;
102+
100103// this object is created by the schema above for use in all the tests below
101104const typeWeights : TypeWeightObject = {
102105 query : {
103106 // object type
104107 weight : 1 ,
105108 fields : {
106- // FIXME: update the function def that is supposed te be here to match implementation
107- // FIXME: add the function definition for the 'search' field which returns a list
108- reviews : ( args : ArgumentNode [ ] ) : number => 10 ,
109+ reviews : mockWeightFunction ,
110+ hero : 1 ,
111+ search : jest . fn ( ) , // FIXME: Unbounded list result
112+ character : 1 ,
113+ droid : 1 ,
114+ human : 1 ,
115+ scalars : 1 ,
109116 } ,
110117 } ,
111118 episode : {
@@ -129,6 +136,8 @@ const typeWeights: TypeWeightObject = {
129136 id : 0 ,
130137 name : 0 ,
131138 homePlanet : 0 ,
139+ appearsIn : jest . fn ( ) , // FIXME: resolves to an unbounded list of enums. We aren't handling this yet
140+ friends : mockHumanFriendsFunction ,
132141 } ,
133142 } ,
134143 droid : {
@@ -137,6 +146,8 @@ const typeWeights: TypeWeightObject = {
137146 fields : {
138147 id : 0 ,
139148 name : 0 ,
149+ appearsIn : jest . fn ( ) , // FIXME: resolves to an unbounded list of enums. We aren't handling this yet
150+ friends : mockDroidFriendsFunction ,
140151 } ,
141152 } ,
142153 review : {
@@ -170,8 +181,13 @@ const typeWeights: TypeWeightObject = {
170181} ;
171182
172183describe ( 'Test getQueryTypeComplexity function' , ( ) => {
184+ afterEach ( ( ) => {
185+ jest . clearAllMocks ( ) ;
186+ } ) ;
187+
173188 let query = '' ;
174- let variables : any | undefined ;
189+ let variables : Variables = { } ;
190+
175191 describe ( 'Calculates the correct type complexity for queries' , ( ) => {
176192 test ( 'with one feild' , ( ) => {
177193 query = `query { scalars { num } }` ;
@@ -203,14 +219,14 @@ describe('Test getQueryTypeComplexity function', () => {
203219 expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + scalar 1
204220 } ) ;
205221
206- xtest ( 'with arguments and variables' , ( ) => {
222+ test ( 'with arguments and variables' , ( ) => {
207223 query = `query { hero(episode: EMPIRE) { id, name } }` ;
208224 expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + hero/character 1
209- query = `query { human(id: 1) { id, name, appearsIn } }` ;
210- expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 3 ) ; // Query 1 + human/character 1 + appearsIn/episode
225+ query = `query { human(id: 1) { id, name, homePlanet } }` ;
226+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + human/character 1
211227 // argument passed in as a variable
212228 variables = { ep : 'EMPIRE' } ;
213- query = `query varibaleQuery ($ep: Episode){ hero(episode: $ep) { id, name } }` ;
229+ query = `query variableQuery ($ep: Episode){ hero(episode: $ep) { id, name } }` ;
214230 expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 2 ) ; // Query 1 + hero/character 1
215231 } ) ;
216232
@@ -263,15 +279,22 @@ describe('Test getQueryTypeComplexity function', () => {
263279 expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( false ) ; // ?
264280 } ) ;
265281
266- xtest ( 'with lists detrmined by arguments and variables' , ( ) => {
267- query = `Query {reviews(episode: EMPIRE, first: 3) { stars, commentary } }` ;
268- expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 4 ) ; // 1 Query + 3 reviews
269- variables = { first : 3 } ;
270- query = `Query queryVaribales($first: Int) {reviews(episode: EMPIRE, first: $first) { stars, commentary } }` ;
271- expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 4 ) ; // 1 Query + 3 reviews
282+ test ( 'with lists determined by arguments and variables' , ( ) => {
283+ query = `query {reviews(episode: EMPIRE, first: 3) { stars, commentary } }` ;
284+ mockWeightFunction . mockReturnValueOnce ( 3 ) ;
285+ expect ( getQueryTypeComplexity ( parse ( query ) , { } , typeWeights ) ) . toBe ( 4 ) ; // 1 Query + 3 reviews
286+ expect ( mockWeightFunction . mock . calls . length ) . toBe ( 1 ) ;
287+ expect ( mockWeightFunction . mock . calls [ 0 ] . length ) . toBe ( 1 ) ;
288+
289+ variables = { first : 4 } ;
290+ mockWeightFunction . mockReturnValueOnce ( 4 ) ;
291+ query = `query queryVariables($first: Int) {reviews(episode: EMPIRE, first: $first) { stars, commentary } }` ;
292+ expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 5 ) ; // 1 Query + 3 reviews
293+ expect ( mockWeightFunction . mock . calls . length ) . toBe ( 2 ) ;
294+ expect ( mockWeightFunction . mock . calls [ 1 ] . length ) . toBe ( 1 ) ;
272295 } ) ;
273296
274- xtest ( 'with nested lists' , ( ) => {
297+ test ( 'with nested lists' , ( ) => {
275298 query = `
276299 query {
277300 human(id: 1) {
@@ -284,7 +307,9 @@ describe('Test getQueryTypeComplexity function', () => {
284307 }
285308 }
286309 }` ;
310+ mockHumanFriendsFunction . mockReturnValueOnce ( 5 ) . mockReturnValueOnce ( 3 ) ;
287311 expect ( getQueryTypeComplexity ( parse ( query ) , variables , typeWeights ) ) . toBe ( 17 ) ; // 1 Query + 1 human/character + (5 friends/character X 3 friends/characters)
312+ expect ( mockHumanFriendsFunction . mock . calls . length ) . toBe ( 2 ) ;
288313 } ) ;
289314
290315 xtest ( 'accounting for __typename feild' , ( ) => {
0 commit comments