Skip to content

Commit 02e221b

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 02e221b

File tree

1 file changed

+186
-4
lines changed

1 file changed

+186
-4
lines changed

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 186 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,65 @@ describe('Test getQueryTypeComplexity function', () => {
407407

408408
xdescribe('with inline fragments', () => {
409409
describe('on union types', () => {
410+
beforeAll(() => {
411+
// type Query {
412+
// hero(episode: Episode): Character
413+
// }
414+
// type Character = Human | Droid
415+
//
416+
// type Human {
417+
// name: String!
418+
// homePlanet: String
419+
// friends(first: Int): [Character]
420+
// humanFriends(first: Int): [Human]
421+
// }
422+
//
423+
// type Droid implements Character {
424+
// name: String!
425+
// primaryFunction: String
426+
// friends(first: Int): [Character]
427+
// }
428+
typeWeights = {
429+
query: {
430+
weight: 1,
431+
fields: {
432+
hero: {
433+
resolveTo: 'character',
434+
},
435+
},
436+
},
437+
human: {
438+
weight: 1,
439+
fields: {
440+
name: { weight: 0 },
441+
homePlanet: { weight: 0 },
442+
friends: {
443+
resolveTo: 'character',
444+
weight: mockCharacterFriendsFunction,
445+
},
446+
humanFriends: {
447+
resolveTo: 'human',
448+
weight: mockHumanFriendsFunction,
449+
},
450+
},
451+
},
452+
droid: {
453+
weight: 1,
454+
fields: {
455+
name: { weight: 0 },
456+
primaryFunction: { weight: 0 },
457+
friends: {
458+
resolveTo: 'character',
459+
weight: mockDroidFriendsFunction,
460+
},
461+
},
462+
},
463+
};
464+
});
410465
test('that have a complexity of zero', () => {
411466
query = `
412467
query {
413-
heroUnion(episode: EMPIRE) {
468+
hero(episode: EMPIRE) {
414469
name
415470
... on Droid {
416471
primaryFunction
@@ -427,7 +482,7 @@ describe('Test getQueryTypeComplexity function', () => {
427482
test('that have differing complexities', () => {
428483
query = `
429484
query {
430-
heroUnion(episode: EMPIRE) {
485+
hero(episode: EMPIRE) {
431486
name
432487
... on Droid {
433488
primaryFunction
@@ -448,7 +503,7 @@ describe('Test getQueryTypeComplexity function', () => {
448503
test('that contain an object and a non-zero complexity', () => {
449504
query = `
450505
query {
451-
heroUnion(episode: EMPIRE) {
506+
hero(episode: EMPIRE) {
452507
name
453508
friends(first: 3) {
454509
name
@@ -470,7 +525,7 @@ describe('Test getQueryTypeComplexity function', () => {
470525
test('that use a variable', () => {
471526
query = `
472527
query {
473-
heroUnion(episode: EMPIRE) {
528+
hero(episode: EMPIRE) {
474529
name
475530
... on Droid {
476531
primaryFunction
@@ -488,6 +543,69 @@ describe('Test getQueryTypeComplexity function', () => {
488543
// Query 1 + 1 hero + max(Droid 3, Human 0) = 5
489544
expect(getQueryTypeComplexity(parse(query), variables, typeWeights)).toBe(5);
490545
});
546+
547+
test('that do not have a TypeCondition', () => {
548+
query = `
549+
query {
550+
hero(episode: EMPIRE) {
551+
... {
552+
name
553+
friends(first: 3) {
554+
name
555+
}
556+
}
557+
... on Human {
558+
homePlanet
559+
}
560+
}
561+
}`;
562+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
563+
// Query 1 + 1 hero + max(Character 3, Human 0) = 5
564+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(5);
565+
});
566+
567+
xtest('that include a directive', () => {
568+
query = `
569+
query {
570+
hero(episode: EMPIRE) {
571+
...@include(if: true) {
572+
name
573+
friends(first: 3) {
574+
name
575+
}
576+
}
577+
... on Human {
578+
homePlanet
579+
}
580+
}
581+
}`;
582+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
583+
// Query 1 + 1 hero + max(...Character 3, ...Human 0) = 5
584+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(5);
585+
});
586+
587+
test('and multiple fragments apply to the selection set', () => {
588+
query = `
589+
query {
590+
hero(episode: EMPIRE) {
591+
...@include(if: true) {
592+
name
593+
friends(first: 3) {
594+
name
595+
}
596+
}
597+
... on Human {
598+
humanFriends(first: 2) {
599+
name
600+
}
601+
}
602+
}
603+
}`;
604+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
605+
mockHumanFriendsFunction.mockReturnValueOnce(2);
606+
// Query 1 + 1 hero + ...Character 3 + ...Human 2 = 7
607+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(7);
608+
});
491609
});
492610

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

0 commit comments

Comments
 (0)