import { ApolloServer } from "apollo-server-lambda";
import { expect } from "chai";
import { GraphQLResponse } from "graphql-extensions";
import { createMockServer } from "../../helpers";

describe("createPlatform", () => {
  let server: ApolloServer;

  beforeEach(() => {
    server = createMockServer(
      {
        Mutation: {
          createPlatform: () => true,
        },
      },
      "private",
    );
  });

  describe("mutation platform", () => {
    let response: GraphQLResponse;

    beforeEach(async () => {
      response = await server.executeOperation({
        query: `
                    mutation createPlatform($input: PlatformMutationInput!) {
                        createPlatform(input: $input)
                    }
                `,
        variables: {
          input: {
            icon: { content: "xxx" },
            os: "linux",
            dist: "Ubuntu Linux",
            codename: "disco",
            dockerImage: "mock:18.04",
          },
        },
      });
    });

    it("should resolve without errors", () => {
      const { errors } = response;

      expect(errors, JSON.stringify(errors)).to.be.undefined;
    });

    it("should respond with correct data", () => {
      const { data } = response;
      expect(data).to.deep.equal({
        createPlatform: true,
      });
    });
  });

  describe("query platform without invalid input", () => {
    let response: GraphQLResponse;

    beforeEach(async () => {
      response = await server.executeOperation({
        query: `
                    mutation createPlatform($input: PlatformMutationInput!) {
                        createPlatform(input: $input)
                    }
                `,
        variables: {
          input: {},
        },
      });
    });

    it("should resolve with error='got invalid value'", async () => {
      const { errors } = response;
      expect(errors).to.lengthOf(5);

      expect(errors[0].message).to.contain("Field value.icon of required");
      expect(errors[1].message).to.contain("Field value.os of required");
      expect(errors[2].message).to.contain("Field value.dist of required");
      expect(errors[3].message).to.contain("Field value.codename of required");
      expect(errors[4].message).to.contain(
        "Field value.dockerImage of required",
      );
    });
  });
});
