"use strict";
// Originally from @graphql-tools/mock
// https://github.com/ardatan/graphql-tools/blob/4b56b04d69b02919f6c5fa4f97d33da63f36e8c8/packages/mock/tests/addMocksToSchema.spec.ts
Object.defineProperty(exports, "__esModule", { value: true });
const graphql_1 = require("graphql");
const utils_js_1 = require("./utils.cjs");
const mockDate = new Date().toJSON().split("T")[0];
const mocks = {
    Int: () => 6,
    Float: () => 22.1,
    String: () => "string",
    ID: () => "id",
    Date: () => mockDate,
};
const typeDefs = /* GraphQL */ `
  type User {
    id: ID!
    age: Int!
    name: String!
    image: UserImage!
    book: Book!
  }

  type Author {
    _id: ID!
    name: String!
    book: Book!
  }

  union UserImage = UserImageSolidColor | UserImageURL

  type UserImageSolidColor {
    color: String!
  }

  type UserImageURL {
    url: String!
  }

  scalar Date

  interface Book {
    id: ID!
    title: String
    publishedAt: Date
  }

  type TextBook implements Book {
    id: ID!
    title: String
    publishedAt: Date
    text: String
  }

  type ColoringBook implements Book {
    id: ID!
    title: String
    publishedAt: Date
    colors: [String]
  }

  type Query {
    viewer: User!
    userById(id: ID!): User!
    author: Author!
  }

  type Mutation {
    changeViewerName(newName: String!): User!
  }
`;
const schema = (0, graphql_1.buildSchema)(typeDefs);
describe("addMocksToSchema", () => {
    it("basic", async () => {
        const query = /* GraphQL */ `
      query {
        viewer {
          id
          name
          age
        }
      }
    `;
        const mockedSchema = (0, utils_js_1.createMockSchema)(schema, mocks);
        const { data, errors } = await (0, graphql_1.graphql)({
            schema: mockedSchema,
            source: query,
        });
        expect(errors).not.toBeDefined();
        expect(data).toBeDefined();
        const viewerData = data?.["viewer"];
        expect(typeof viewerData["id"]).toBe("string");
        expect(typeof viewerData["name"]).toBe("string");
        expect(typeof viewerData["age"]).toBe("number");
        const { data: data2 } = await (0, graphql_1.graphql)({
            schema: mockedSchema,
            source: query,
        });
        const viewerData2 = data2?.["viewer"];
        expect(viewerData2["id"]).toEqual(viewerData["id"]);
    });
    it("handle _id key field", async () => {
        const query = /* GraphQL */ `
      query {
        author {
          _id
          name
        }
      }
    `;
        const mockedSchema = (0, utils_js_1.createMockSchema)(schema, mocks);
        const { data, errors } = await (0, graphql_1.graphql)({
            schema: mockedSchema,
            source: query,
        });
        expect(errors).not.toBeDefined();
        expect(data).toBeDefined();
        const viewerData = data?.["author"];
        expect(typeof viewerData["_id"]).toBe("string");
        expect(typeof viewerData["name"]).toBe("string");
        const { data: data2 } = await (0, graphql_1.graphql)({
            schema: mockedSchema,
            source: query,
        });
        const viewerData2 = data2?.["author"];
        expect(viewerData2["_id"]).toEqual(viewerData["_id"]);
    });
    it("should handle union type", async () => {
        const query = /* GraphQL */ `
      query {
        viewer {
          image {
            __typename
            ... on UserImageURL {
              url
            }
            ... on UserImageSolidColor {
              color
            }
          }
        }
      }
    `;
        const mockedSchema = (0, utils_js_1.createMockSchema)(schema, mocks);
        const { data, errors } = await (0, graphql_1.graphql)({
            schema: mockedSchema,
            source: query,
        });
        expect(errors).not.toBeDefined();
        expect(data).toBeDefined();
        expect(data["viewer"]["image"]["__typename"]).toBeDefined();
    });
    it("should handle interface type", async () => {
        const query = /* GraphQL */ `
      query {
        viewer {
          book {
            title
            __typename
            ... on TextBook {
              text
            }
            ... on ColoringBook {
              colors
            }
          }
        }
      }
    `;
        const mockedSchema = (0, utils_js_1.createMockSchema)(schema, mocks);
        const { data, errors } = await (0, graphql_1.graphql)({
            schema: mockedSchema,
            source: query,
        });
        expect(errors).not.toBeDefined();
        expect(data).toBeDefined();
        expect(data["viewer"]["book"]["__typename"]).toBeDefined();
    });
    it("should handle custom scalars", async () => {
        const query = /* GraphQL */ `
      query {
        viewer {
          book {
            title
            publishedAt
          }
        }
      }
    `;
        const mockedSchema = (0, utils_js_1.createMockSchema)(schema, mocks);
        const { data, errors } = await (0, graphql_1.graphql)({
            schema: mockedSchema,
            source: query,
        });
        expect(errors).not.toBeDefined();
        expect(data).toBeDefined();
        expect(data["viewer"]["book"]["publishedAt"]).toBe(mockDate);
    });
});
//# sourceMappingURL=utils.test.cjs.map
