import { describe, expect, inject, it } from "vitest";
import { createGraphQLClient, graphql } from "@/tests/utils/graphql-client";

const listAttributes = graphql(`
  query ViewerListProductAttributes {
    productAttributes {
      edges {
        node {
          id
          code
          name
        }
      }
    }
  }
`);

const getAttribute = graphql(`
  query ViewerGetProductAttribute($id: ID!) {
    productAttribute(id: $id) {
      id
      code
      name
    }
  }
`);

describe("viewer--browse-attribute-list", () => {
  const gql = createGraphQLClient(inject("url"), inject("token"));

  it("Given attributes exist, when the attribute list is opened, then each attribute is shown with its code, name, and value count.", async () => {
    const res = await gql.query(listAttributes, {}).toPromise();

    expect(res.data?.productAttributes.edges.length).toBeGreaterThan(0);
    const node = res.data?.productAttributes.edges[0].node;
    expect(node?.id).toBeDefined();
    expect(node?.code).toBeDefined();
    expect(node?.name).toBeDefined();
  });

  it("Given the attribute list is displayed, when an attribute is selected, then the attribute detail screen is shown in read-only mode.", async () => {
    const listRes = await gql.query(listAttributes, {}).toPromise();
    const firstAttribute = listRes.data!.productAttributes.edges[0].node;

    const res = await gql.query(getAttribute, { id: firstAttribute.id }).toPromise();

    const viewerAttr = res.data!.productAttribute!;
    expect(viewerAttr.id).toBe(firstAttribute.id);
    expect(viewerAttr.code).toBe(firstAttribute.code);
    expect(viewerAttr.name).toBe(firstAttribute.name);
  });
});
