import ListDetailModel from "../ListDetailModel";

import data from "./caselist-34.data.json";
import contributions from "./caselist-34.contributions.json";

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

describe("listDetailModel", () => {
  it("should be able to create an instance of ListDetailModel", () => {
    const listDetail = new ListDetailModel();

    expect(listDetail).toBeInstanceOf(ListDetailModel);
  });

  it("should be able to create a ListDetail", () => {
    const book = new ListDetailModel({
      request: {
        href: new Href("/Book/34"),
      },
      key: "Book",
      data: data.Book,
      contributions: contributions.Book,
    });

    expect(book.attributeCollection).toHaveLength(7);
    expect(book.titleAttribute.readonlyvalue).toBe(
      "Highly Inappropriate Tales for Young People",
    );
  });

  it("should be able to handle different kind of introtext", () => {
    const listdetailOldStructure = new ListDetailModel({
      key: "Book",
      data: data.Book,
      contributions: contributions.Book,
    });

    expect(listdetailOldStructure.introtext).toBe("<p>This is introtext</p>");

    const listdetailFromDataPlain = new ListDetailModel({
      key: "Book",
      data: {
        _content: { text: { message: "<p>This is introtext</p>" } },
        ...data.Book,
      },
      contributions: contributions.Book,
    });

    expect(listdetailFromDataPlain.introtext).toBe("<p>This is introtext</p>");

    const listdetailFromDataApplicationMessage = new ListDetailModel({
      key: "Book",
      data: {
        ...data.Book,
        _content: {
          text: {
            message: "<p>This is introtext</p>",
          },
        },
      },
      contributions: contributions.Book,
    });

    expect(listdetailFromDataApplicationMessage.introtext).toBe(
      "<p>This is introtext</p>",
    );

    const listdetailFromNewContributionsRawTextMessage = new ListDetailModel({
      key: "Book",
      data: data.Book,
      contributions: {
        ...contributions.Book,
        text: {
          message: "<p>This is introtext</p>",
        },
      },
    });

    expect(listdetailFromNewContributionsRawTextMessage.introtext).toBe(
      "<p>This is introtext</p>",
    );

    const listdetailFromNewContributionsSimpleMessage = new ListDetailModel({
      key: "Book",
      data: data.Book,
      contributions: {
        ...contributions.Book,
        text: "<p>This is introtext</p>",
      },
    });

    expect(listdetailFromNewContributionsSimpleMessage.introtext).toBe(
      "<p>This is introtext</p>",
    );
  });
});
