Skip to content

Commit ca6a114

Browse files
committed
expanded fragment tests to include cases for fragments without a type condition, directives and multiple applicable fragments.
created separate typeWeight object for union tests on fragments for clarity
1 parent 5169bb2 commit ca6a114

File tree

1 file changed

+191
-4
lines changed

1 file changed

+191
-4
lines changed

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 191 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { TypeWeightObject, Variables } from '../../src/@types/buildTypeWeights';
3636
name: String!
3737
homePlanet: String
3838
friends(first: Int): [Character]
39+
humanFriends(first: Int): [Human]
3940
appearsIn: [Episode]!
4041
}
4142
@@ -188,6 +189,10 @@ describe('Test getQueryTypeComplexity function', () => {
188189
resolveTo: 'character',
189190
weight: mockHumanFriendsFunction,
190191
},
192+
humanFriends: {
193+
resolveTo: 'human',
194+
weight: mockHumanFriendsFunction,
195+
},
191196
},
192197
},
193198
droid: {
@@ -407,10 +412,65 @@ describe('Test getQueryTypeComplexity function', () => {
407412

408413
xdescribe('with inline fragments', () => {
409414
describe('on union types', () => {
415+
beforeAll(() => {
416+
// type Query {
417+
// hero(episode: Episode): Character
418+
// }
419+
// type Character = Human | Droid
420+
//
421+
// type Human {
422+
// name: String!
423+
// homePlanet: String
424+
// friends(first: Int): [Character]
425+
// humanFriends(first: Int): [Human]
426+
// }
427+
//
428+
// type Droid implements Character {
429+
// name: String!
430+
// primaryFunction: String
431+
// friends(first: Int): [Character]
432+
// }
433+
typeWeights = {
434+
query: {
435+
weight: 1,
436+
fields: {
437+
hero: {
438+
resolveTo: 'character',
439+
},
440+
},
441+
},
442+
human: {
443+
weight: 1,
444+
fields: {
445+
name: { weight: 0 },
446+
homePlanet: { weight: 0 },
447+
friends: {
448+
resolveTo: 'character',
449+
weight: mockCharacterFriendsFunction,
450+
},
451+
humanFriends: {
452+
resolveTo: 'human',
453+
weight: mockHumanFriendsFunction,
454+
},
455+
},
456+
},
457+
droid: {
458+
weight: 1,
459+
fields: {
460+
name: { weight: 0 },
461+
primaryFunction: { weight: 0 },
462+
friends: {
463+
resolveTo: 'character',
464+
weight: mockDroidFriendsFunction,
465+
},
466+
},
467+
},
468+
};
469+
});
410470
test('that have a complexity of zero', () => {
411471
query = `
412472
query {
413-
heroUnion(episode: EMPIRE) {
473+
hero(episode: EMPIRE) {
414474
name
415475
... on Droid {
416476
primaryFunction
@@ -427,7 +487,7 @@ describe('Test getQueryTypeComplexity function', () => {
427487
test('that have differing complexities', () => {
428488
query = `
429489
query {
430-
heroUnion(episode: EMPIRE) {
490+
hero(episode: EMPIRE) {
431491
name
432492
... on Droid {
433493
primaryFunction
@@ -448,7 +508,7 @@ describe('Test getQueryTypeComplexity function', () => {
448508
test('that contain an object and a non-zero complexity', () => {
449509
query = `
450510
query {
451-
heroUnion(episode: EMPIRE) {
511+
hero(episode: EMPIRE) {
452512
name
453513
friends(first: 3) {
454514
name
@@ -470,7 +530,7 @@ describe('Test getQueryTypeComplexity function', () => {
470530
test('that use a variable', () => {
471531
query = `
472532
query {
473-
heroUnion(episode: EMPIRE) {
533+
hero(episode: EMPIRE) {
474534
name
475535
... on Droid {
476536
primaryFunction
@@ -488,6 +548,69 @@ describe('Test getQueryTypeComplexity function', () => {
488548
// Query 1 + 1 hero + max(Droid 3, Human 0) = 5
489549
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(5);
490550
});
551+
552+
test('that do not have a TypeCondition', () => {
553+
query = `
554+
query {
555+
hero(episode: EMPIRE) {
556+
... {
557+
name
558+
friends(first: 3) {
559+
name
560+
}
561+
}
562+
... on Human {
563+
homePlanet
564+
}
565+
}
566+
}`;
567+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
568+
// Query 1 + 1 hero + max(Character 3, Human 0) = 5
569+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(5);
570+
});
571+
572+
xtest('that include a directive', () => {
573+
query = `
574+
query {
575+
hero(episode: EMPIRE) {
576+
...@include(if: true) {
577+
name
578+
friends(first: 3) {
579+
name
580+
}
581+
}
582+
... on Human {
583+
homePlanet
584+
}
585+
}
586+
}`;
587+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
588+
// Query 1 + 1 hero + max(...Character 3, ...Human 0) = 5
589+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(5);
590+
});
591+
592+
test('and multiple fragments apply to the selection set', () => {
593+
query = `
594+
query {
595+
hero(episode: EMPIRE) {
596+
...@include(if: true) {
597+
name
598+
friends(first: 3) {
599+
name
600+
}
601+
}
602+
... on Human {
603+
humanFriends(first: 2) {
604+
name
605+
}
606+
}
607+
}
608+
}`;
609+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
610+
mockHumanFriendsFunction.mockReturnValueOnce(2);
611+
// Query 1 + 1 hero + ...Character 3 + ...Human 2 = 7
612+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(7);
613+
});
491614
});
492615

493616
describe('on interface types', () => {
@@ -572,6 +695,70 @@ describe('Test getQueryTypeComplexity function', () => {
572695
// Query 1 + 1 hero + max(Droid 3, Human 0) = 5
573696
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(5);
574697
});
698+
699+
test('that do not have a TypeCondition', () => {
700+
query = `
701+
query {
702+
hero(episode: EMPIRE) {
703+
... {
704+
name
705+
ScalarList(first: 1)
706+
friends(first: 3) {
707+
name
708+
}
709+
}
710+
... on Human {
711+
homePlanet
712+
}
713+
}
714+
}`;
715+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
716+
// Query 1 + 1 hero + max(Character 3, Human 0) = 5
717+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(5);
718+
});
719+
720+
xtest('that include a directive', () => {
721+
query = `
722+
query {
723+
hero(episode: EMPIRE) {
724+
...@include(if: true) {
725+
name
726+
friends(first: 3) {
727+
name
728+
}
729+
}
730+
... on Human {
731+
homePlanet
732+
}
733+
}
734+
}`;
735+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
736+
// Query 1 + 1 hero + max(...Character 3, ...Human 0) = 5
737+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(5);
738+
});
739+
740+
test('and multiple fragments apply to the selection set', () => {
741+
query = `
742+
query {
743+
hero(episode: EMPIRE) {
744+
...@include(if: true) {
745+
name
746+
friends(first: 3) {
747+
name
748+
}
749+
}
750+
... on Human {
751+
humanFriends(first: 2) {
752+
name
753+
}
754+
}
755+
}
756+
}`;
757+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
758+
mockHumanFriendsFunction.mockReturnValueOnce(2);
759+
// Query 1 + 1 hero + ...Character 3 + ...Human 2 = 7
760+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(5);
761+
});
575762
});
576763
});
577764

0 commit comments

Comments
 (0)