import { ModularUIResponse } from "../../../modularui";

import initialData from "./FormValidationDataInitial.json";
import contributions from "./FormValidationContributions.json";
import updateData from "./FormValidationDataUpdate1.json";
import updateData2 from "./FormValidationDataUpdate2.json";

import FormModel from "../FormModel";

describe("Form validation update", () => {
  it("should be able to update the validations of a form", () => {
    const response = ModularUIResponse.create({
      key: "Form",
      data: initialData,
      contributions: contributions,
    });

    const form = new FormModel(response);
    const formModel = form.currentFormObject;

    const validation = formModel.getAttributeByKey("Validation");
    const compareWith = formModel.getAttributeByKey("CompareWith");
    const optional = formModel.getAttributeByKey("ThisOneIsOptional");

    expect(validation.mandatory).toBe(true);
    expect(compareWith.mandatory).toBe(true);
    expect(optional.mandatory).toBe(false);

    const newForm = form.clone();
    newForm.currentFormObject.updateAttribute(validation, "test");

    expect(validation.mandatory).toBe(true);
    expect(compareWith.mandatory).toBe(true);
    expect(optional.mandatory).toBe(false);

    const updateResponse = ModularUIResponse.create({
      key: "Form",
      data: updateData,
      contributions: contributions,
    });
    const updateForm = new FormModel(updateResponse);

    newForm.updateValidations(updateForm.data);

    expect(
      newForm.currentFormObject.getAttributeByKey("Validation").mandatory,
    ).toBe(true);
    expect(
      newForm.currentFormObject.getAttributeByKey("CompareWith").mandatory,
    ).toBe(true);
    expect(
      newForm.currentFormObject.getAttributeByKey("ThisOneIsOptional")
        .mandatory,
    ).toBe(false);

    const newForm2 = form.clone();
    newForm2.currentFormObject.updateAttribute(validation, "");

    const update2Response = ModularUIResponse.create({
      key: "Form",
      data: updateData2,
      contributions: contributions,
    });
    const update2Form = new FormModel(update2Response);

    newForm.updateValidations(update2Form.data);

    expect(
      newForm.currentFormObject.getAttributeByKey("Validation").mandatory,
    ).toBe(true);
    expect(
      newForm.currentFormObject.getAttributeByKey("CompareWith").mandatory,
    ).toBe(true);
    expect(
      newForm.currentFormObject.getAttributeByKey("ThisOneIsOptional")
        .mandatory,
    ).toBe(false);
  });
});
