import ListDetailModel from "../ListDetailModel";

import Href from "../../href/Href";

import mockDetail from "./hierarchy.json";
import mockDetailContributions from "./hierarchyContributions.json";

describe("listDetailModel", () => {
  describe("Event details", () => {
    it("should be able to create a detail from a hierarchical modular UI json structure", () => {
      const detail = new ListDetailModel({
        request: {
          href: new Href(
            "/service-operation/service-operation/44/history/127",
            "EventListPanelDetail",
          ),
        },
        key: "event",
        data: mockDetail.event,
        contributions: mockDetailContributions.event,
      });

      expect(detail).toBeInstanceOf(ListDetailModel);

      expect(detail.key).toBe("event");

      expect(detail.id).toBe(127);

      const selfLink = new Href(
        "/service-operation/service-operation/44/history/127",
        "EventListPanelDetail",
      );

      expect(detail.selfhref).toStrictEqual(selfLink);

      expect(detail.attributeCollection.size).toBe(3);

      expect(detail.hasEventData).toBe(true);

      expect(detail.eventdata).toHaveLength(6);

      expect(detail.eventdata[0].label).toBe("Company");
      expect(detail.eventdata[0].getChildAttributeSets()).toHaveLength(3);

      expect(detail.eventdata[0].getChildAttributeSets()[0].label).toBe(
        "Company contact details",
      );
      expect(detail.eventdata[0].getChildAttributeSets()[1].label).toBe(
        "Company contact details",
      );
      expect(detail.eventdata[0].getChildAttributeSets()[2].label).toBe("CEO");

      expect(
        detail.eventdata[0].getChildAttributeSets()[1].getChildAttributeSets(),
      ).toHaveLength(0);
      expect(
        detail.eventdata[0].getChildAttributeSets()[2].getChildAttributeSets(),
      ).toHaveLength(1);

      expect(
        detail.eventdata[0]
          .getChildAttributeSets()[2]
          .getChildAttributeSets()[0].label,
      ).toBe("CEO Business location");
      expect(
        detail.eventdata[0]
          .getChildAttributeSets()[2]
          .getChildAttributeSets()[0]
          .getChildAttributeSets(),
      ).toHaveLength(2);

      expect(
        detail.eventdata[0]
          .getChildAttributeSets()[2]
          .getChildAttributeSets()[0]
          .getChildAttributeSets()[1]
          .attributeCollection.getAttributeByKey("CEOBLC_Type").label,
      ).toBe("Type");
      expect(
        detail.eventdata[0]
          .getChildAttributeSets()[2]
          .getChildAttributeSets()[0]
          .getChildAttributeSets()[1]
          .attributeCollection.getAttributeByKey("CEOBLC_Type").value,
      ).toBe("Email");
    });
  });
});
