{"size": 2638, "ext": "js", "max_stars_repo_path": "test/util/parse-typedefs.test.js", "max_stars_repo_name": "danstarns/idio-graphql", "max_stars_repo_stars_event_min_datetime": "2019-12-08T01:27:20.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-12T12:09:37.000Z", "max_issues_repo_path": "test/util/parse-typedefs.test.js", "max_issues_repo_name": "danstarns/idio-graphql", "max_issues_repo_issues_event_min_datetime": "2019-11-25T08:46:33.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-03T00:38:44.000Z", "max_forks_repo_path": "test/util/parse-typedefs.test.js", "max_forks_repo_name": "danstarns/idio-graphql", "max_forks_repo_forks_event_min_datetime": "2019-12-23T21:36:37.000Z", "max_forks_repo_forks_event_max_datetime": "2020-10-29T14:34:09.000Z"}
{"max_stars_count": 12, "max_issues_count": 342, "max_forks_count": 3, "avg_line_length": 28.6739130435, "max_line_length": 92, "alphanum_fraction": 0.4988627748}
const { expect } = require("chai");
const path = require("path");
const { parse } = require("graphql/language");

const parseTypeDefs = require("../../src/util/parse-typedefs.js");

describe("parseTypeDefs", () => {
    it("should return a promise that resolves a file", () => {
        const filePath = path.join(__dirname, "../dummy-data/User.gql");

        const result = parseTypeDefs(filePath);

        expect(result).to.be.a("string");
        expect(result).to.contain("type User");
        expect(result).to.contain("type Query");
    });

    it("should throw error parsing typeDefs when providing invalid SDL from string", () => {
        try {
            parseTypeDefs(
                `terp User {
                    name: String
                    age: Int
                
                terp QUery {
                    getUserById(id: ID!!!); User
                ]    `
            );

            throw new Error();
        } catch (error) {
            expect(error.message).to.contain("cannot resolve typeDefs");
        }
    });

    it("should return a promise that resolves the initial string provided", async () => {
        const result = parseTypeDefs(`
            type User {
                name: String
                age: Int
            }
            
            type Query {
                getUserByID(id: ID!): User
            }
        `);

        expect(result).to.be.a("string");
        expect(result).to.contain("type User");
        expect(result).to.contain("type Query");
    });

    it("should return a promise that resolves<String> from gql-tag/AST", async () => {
        const GQLTagResult = parse(`
            type User {
                name: String
                age: Int
            }
            
            type Query {
                getUserByID(id: ID!): User
            }
        `);

        const result = parseTypeDefs(GQLTagResult);

        expect(result).to.be.a("string");
        expect(result).to.contain("type User");
        expect(result).to.contain("type Query");
    });

    it("should throw cannot resolve typeDefs when invalid AST is provided", () => {
        try {
            parseTypeDefs({ invalid: "AST" });

            throw new Error();
        } catch (error) {
            expect(error.message).to.contain("cannot resolve typeDefs: ");
        }
    });

    it("should throw cannot parse typeDefs when a number is provided", () => {
        try {
            parseTypeDefs(278);

            throw new Error();
        } catch (error) {
            expect(error.message).to.contain("cannot parse typeDefs: 278");
        }
    });
});
