Repository: graphql-markdown/graphql-markdown
Pull request #1248 (2024-01-29 13:48:50)

:test_tube:  fix printer-legacy relations tests
# Description

Fix tests for module `printer-legacy/relation` based on Stryker report.


# Checklist

- [x] My changes follow the [contributing guidelines](https://github.com/graphql-markdown/graphql-markdown/blob/main/CONTRIBUTING.md) of this project.
- [x] I have performed a self-review of my code.
- [ ] I have made corresponding changes to the documentation.
- [x] I have added tests that prove my fix is effective or that my changes work.
- [x] New and existing unit tests pass locally with my changes.


sonarcloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_graphql-markdown&pullRequest=1248) **Quality Gate passed**  
Kudos, no new issues were introduced!

[0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_graphql-markdown&pullRequest=1248&resolved=false&inNewCodePeriod=true)  
[0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_graphql-markdown&pullRequest=1248&resolved=false&inNewCodePeriod=true)  
No data about Coverage  
[0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_graphql-markdown&pullRequest=1248&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=graphql-markdown_graphql-markdown&pullRequest=1248)


codesee-maps[bot]: <details open>
<summary><b>👇 <a href="https://app.codesee.io/r/reviews?pr=1248&src=https%3A%2F%2Fgithub.com%2Fgraphql-markdown%2Fgraphql-markdown">Click on the image</a> for a new way to code review</b>
</summary>

####

[![Review these changes using an interactive CodeSee Map](https://s3.us-east-2.amazonaws.com/maps.codesee.io/images/github/graphql-markdown/graphql-markdown/1248/4ee50e86/27f99c20bae47a74678500cdf9ca5595733e6e6a.svg)](https://app.codesee.io/r/reviews?pr=1248&src=https%3A%2F%2Fgithub.com%2Fgraphql-markdown%2Fgraphql-markdown)

#### Legend
<img src="https://app.codesee.io/Legend_v3.png" alt="CodeSee Map legend" width="220" height="156">
</details>
coveralls: 
[![Coverage Status](https://coveralls.io/builds/65354630/badge)](https://coveralls.io/builds/65354630)

coverage: 94.208% (+0.2%) from 94.01%
when pulling **27f99c20bae47a74678500cdf9ca5595733e6e6a on chore/fix-relation-tests**
into **a6eb871b64a967997348c1d0e640085ed8207552 on main**.

sonarcloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_graphql-markdown&pullRequest=1248) **Quality Gate passed**  
Kudos, no new issues were introduced!

[0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_graphql-markdown&pullRequest=1248&resolved=false&inNewCodePeriod=true)  
[0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_graphql-markdown&pullRequest=1248&resolved=false&inNewCodePeriod=true)  
No data about Coverage  
[0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_graphql-markdown&pullRequest=1248&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=graphql-markdown_graphql-markdown&pullRequest=1248)



Patch:
diff --git a/packages/printer-legacy/src/relation.ts b/packages/printer-legacy/src/relation.ts
index 2c85d596..92ff796b 100644
--- a/packages/printer-legacy/src/relation.ts
+++ b/packages/printer-legacy/src/relation.ts
@@ -93,7 +93,7 @@ export const printRelations = (
   options: PrintTypeOptions,
 ): MDXString | string => {
   const relations: Record<string, RelationOf> = {
-    returnedBy: { section: "Returned by", getRelation: getRelationOfReturn },
+    returnedBy: { section: "Returned By", getRelation: getRelationOfReturn },
     memberOf: { section: "Member Of", getRelation: getRelationOfField },
     implementedBy: {
       section: "Implemented By",


Test patch:
diff --git a/packages/printer-legacy/tests/unit/relation.test.ts b/packages/printer-legacy/tests/unit/relation.test.ts
index 45130542..035262fa 100644
--- a/packages/printer-legacy/tests/unit/relation.test.ts
+++ b/packages/printer-legacy/tests/unit/relation.test.ts
@@ -23,6 +23,8 @@ jest.mock("@graphql-markdown/graphql", () => {
       return t;
     }),
     getRelationOfReturn: jest.fn(),
+    getRelationOfField: jest.fn(),
+    getRelationOfImplementation: jest.fn(),
     getSchemaMap: jest.fn(),
     hasDirective: jest.fn(),
     isDirectiveType: jest.fn(),
@@ -42,12 +44,12 @@ jest.mock("@graphql-markdown/graphql", () => {
 });
 import * as GraphQL from "@graphql-markdown/graphql";
 
-import {
-  printRelationOf,
-  getRootTypeLocaleFromString,
-} from "../../src/relation";
+import * as Relation from "../../src/relation";
 import { DEFAULT_OPTIONS } from "../../src/const/options";
 
+const { getRootTypeLocaleFromString, printRelationOf, printRelations } =
+  Relation;
+
 describe("relation", () => {
   describe("printRelationOf()", () => {
     afterAll(() => {
@@ -55,40 +57,52 @@ describe("relation", () => {
       jest.resetAllMocks();
     });
 
-    test("returns empty string if type is undefined", () => {
-      expect.hasAssertions();
-
-      jest.spyOn(GraphQL, "isNamedType").mockReturnValueOnce(false);
-
-      const relation = printRelationOf(
-        undefined,
-        "RelationOf",
-        GraphQL.getRelationOfReturn,
-        DEFAULT_OPTIONS,
-      );
-
-      expect(relation).toBe("");
-    });
-
-    test("returns empty string if type is operation", () => {
-      expect.hasAssertions();
-
-      const type = new GraphQLScalarType({
-        name: "String",
-        description: "Lorem Ipsum",
-      });
-
-      jest.spyOn(GraphQL, "isOperation").mockReturnValueOnce(true);
-
-      const relation = printRelationOf(
-        type,
-        "RelationOf",
-        GraphQL.getRelationOfReturn,
-        DEFAULT_OPTIONS,
-      );
-
-      expect(relation).toBe("");
-    });
+    test.concurrent.each([[true], [false]])(
+      "returns empty string if type is undefined and isOperation is %p",
+      (isOperationMockedValue: boolean) => {
+        expect.hasAssertions();
+
+        jest.spyOn(GraphQL, "isNamedType").mockReturnValueOnce(false);
+        jest
+          .spyOn(GraphQL, "isOperation")
+          .mockReturnValueOnce(isOperationMockedValue);
+
+        const relation = printRelationOf(
+          undefined,
+          "RelationOf",
+          GraphQL.getRelationOfReturn,
+          { ...DEFAULT_OPTIONS, schema: {} as GraphQLSchema },
+        );
+
+        expect(relation).toBe("");
+      },
+    );
+
+    test.concurrent.each([[true], [false]])(
+      "returns empty string if type is operation and isNamedType is %p",
+      (isNamedTypeMockedValue: boolean) => {
+        expect.hasAssertions();
+
+        const type = new GraphQLScalarType({
+          name: "String",
+          description: "Lorem Ipsum",
+        });
+
+        jest
+          .spyOn(GraphQL, "isNamedType")
+          .mockReturnValueOnce(isNamedTypeMockedValue);
+        jest.spyOn(GraphQL, "isOperation").mockReturnValueOnce(true);
+
+        const relation = printRelationOf(
+          type,
+          "RelationOf",
+          GraphQL.getRelationOfReturn,
+          { ...DEFAULT_OPTIONS, schema: {} as GraphQLSchema },
+        );
+
+        expect(relation).toBe("");
+      },
+    );
 
     test("returns empty string if getRelation is not a function", () => {
       expect.hasAssertions();
@@ -98,12 +112,10 @@ describe("relation", () => {
         description: "Lorem Ipsum",
       });
 
-      const relation = printRelationOf(
-        type,
-        "RelationOf",
-        undefined,
-        DEFAULT_OPTIONS,
-      );
+      const relation = printRelationOf(type, "RelationOf", undefined, {
+        ...DEFAULT_OPTIONS,
+        schema: {} as GraphQLSchema,
+      });
 
       expect(relation).toBe("");
     });
@@ -133,6 +145,7 @@ describe("relation", () => {
       });
 
       jest.spyOn(GraphQL, "isNamedType").mockReturnValueOnce(true);
+      jest.spyOn(GraphQL, "isOperation").mockReturnValueOnce(false);
 
       const relation = printRelationOf(
         type,
@@ -140,7 +153,7 @@ describe("relation", () => {
         (): ReturnType<IGetRelation<unknown>> => {
           return undefined as unknown as ReturnType<IGetRelation<unknown>>;
         },
-        DEFAULT_OPTIONS,
+        { ...DEFAULT_OPTIONS, schema: {} as GraphQLSchema },
       );
 
       expect(relation).toBe("");
@@ -160,7 +173,7 @@ describe("relation", () => {
         () => {
           return { objects: [] };
         },
-        DEFAULT_OPTIONS,
+        { ...DEFAULT_OPTIONS, schema: {} as GraphQLSchema },
       );
 
       expect(relation).toBe("");
@@ -215,4 +228,45 @@ describe("relation", () => {
           `);
     });
   });
+
+  describe("printRelations()", () => {
+    test("calls printRelationOf() for each type of relation", () => {
+      expect.hasAssertions();
+
+      const spy = jest.spyOn(Relation, "printRelationOf");
+
+      const type = new GraphQLScalarType({
+        name: "String",
+        description: "Lorem Ipsum",
+      });
+      const options = { ...DEFAULT_OPTIONS, schema: {} as GraphQLSchema };
+
+      jest.spyOn(GraphQL, "isNamedType").mockReturnValue(true);
+
+      printRelations(type, options);
+
+      expect(spy).toHaveBeenCalledTimes(3);
+      expect(spy).toHaveBeenNthCalledWith(
+        1,
+        type,
+        "Returned By",
+        GraphQL.getRelationOfReturn,
+        options,
+      );
+      expect(spy).toHaveBeenNthCalledWith(
+        2,
+        type,
+        "Member Of",
+        GraphQL.getRelationOfField,
+        options,
+      );
+      expect(spy).toHaveBeenNthCalledWith(
+        3,
+        type,
+        "Implemented By",
+        GraphQL.getRelationOfImplementation,
+        options,
+      );
+    });
+  });
 });