import DatetimeAttributeModel from "../DatetimeAttributeModel";

describe("dateAttributeModel", () => {
  describe("with date related settings set", () => {
    it("should be able to create an empty Attribute object", () => {
      const attribute = new DatetimeAttributeModel();

      expect(attribute).toBeInstanceOf(DatetimeAttributeModel);
      expect(attribute.key).toBeUndefined();
      expect(attribute.label).toBe("");
      expect(attribute.value).toBeNull();
      expect(attribute.inputvalue).toBe("");
      expect(attribute.type).toBe("date");
      expect(attribute.layouthint.all).toStrictEqual([]);
    });

    it("should create date Attribute from typical modular UI response", () => {
      const stringAttribute = {
        key: "date",
        value: "2010-10-23",
      };

      const dateAttrContribution = {
        label: "Date of birth",
        type: "date",
        mandatory: false,
        format: "dd-MM-yyyy",
        formatlabel: "dd-mm-yyyy",
      };

      const attribute = new DatetimeAttributeModel(
        stringAttribute,
        dateAttrContribution,
      );

      expect(attribute).toBeInstanceOf(DatetimeAttributeModel);
      expect(attribute.key).toBe("date");
      expect(attribute.label).toBe("Date of birth");
      expect(attribute.value).toBe("2010-10-23");
      expect(attribute.format).toBe("dd-MM-yyyy");
      expect(attribute.formatLabel).toBe("dd-mm-yyyy");
      expect(attribute.inputvalue).toBe("23-10-2010");
      expect(attribute.type).toBe("date");
      expect(attribute.layouthint.all).toStrictEqual([]);
    });

    it("can set input", () => {
      const attribute = new DatetimeAttributeModel({});
      attribute.inputvalue = "2010-12-10";

      expect(attribute.readonlyvalue).toBe("2010-12-10");

      attribute.inputvalue = "";

      expect(attribute.readonlyvalue).toBe("");
      expect(attribute.value).toBeNull();

      attribute.update("aaaa");
      expect(attribute.value).toBe("Invalid Date");
    });
  });

  describe("without date input and date readonly settings defined", () => {
    it("should be able to return contributions format as date input format", () => {
      const attribute = new DatetimeAttributeModel(
        {},
        {
          type: "date",
          format: "yyyy-MM-dd",
          formatlabel: "yyyy-mm-dd",
          placeholder: "enter the date here",
        },
      );

      attribute.update("2016-08-18");

      expect(attribute.getInputValue()).toBe("2016-08-18");
      expect(attribute.getInitialInputValue("2016-08-18")).toBe("2016-08-18");
      expect(attribute.readonlyvalue).toBe("2016-08-18");

      // date input format should match the format because no setting is used
      expect(attribute.format).toBe("yyyy-MM-dd");

      // date input and readonly format are based on the format contributions property if no setting is defined
      expect(attribute.dateInputFormat).toBe(attribute.format);
      expect(attribute.dateReadonlyFormat).toBe(attribute.format);

      // placeholder value in contributions should be returned - formatlabel is ignored as placeholder
      expect(attribute.placeholder).toBe("enter the date here");
    });

    it("should return formatlabel as placeholder value when no placeholder is configured on the attribute", () => {
      const attribute = new DatetimeAttributeModel(
        {},
        {
          type: "date",
          format: "yyyy-MM-dd",
          formatlabel: "yyyy-mm-dd",
        },
      );

      expect(attribute.placeholder).toBe("yyyy-mm-dd");
    });

    it("should not return a placeholder value when no placeholder and no format label is configured on the attribute", () => {
      const attribute = new DatetimeAttributeModel(
        {},
        {
          type: "date",
          format: "yyyy-MM-dd",
        },
      );

      expect(attribute.placeholder).toBe("");
    });

    it("can update typical date attribute", () => {
      const attribute = new DatetimeAttributeModel(
        {},
        {
          type: "date",
          label: "Date of birth",
          mandatory: false,
          formatlabel: "dd-mm-jjjj",
          format: "dd-MM-yyyy",
        },
      );

      attribute.update("13-12-1819");

      expect(attribute.isValid).toBe(true);
    });
  });
});
