"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const LabelEntity_1 = require("./LabelEntity");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const appObj = appObjects.getOrCreate("appObj");
    const label = (0, LabelEntity_1.makeLabelEntity)(appObj);
    const observer = jest.fn();
    label.addChangeObserver(observer);
    return { label, observer, appObj, appObjects };
}
describe("Label Entity", () => {
    it("Sets the id", () => {
        const { label, appObj } = makeTestRig();
        expect(label.appObject).toEqual(appObj);
    });
    it("Adds itself to the App Object", () => {
        const { label, appObj } = makeTestRig();
        expect(appObj.hasComponent(LabelEntity_1.LabelEntity.type)).toEqual(true);
        expect(appObj.getComponent(LabelEntity_1.LabelEntity.type)).toEqual(label);
    });
    it("Removes itself from the App Object when disposed", () => {
        const { label, appObj } = makeTestRig();
        label.dispose();
        expect(appObj.hasComponent(LabelEntity_1.LabelEntity.type)).toEqual(false);
    });
    it("Notifies when the default label changes", () => {
        const { label, observer } = makeTestRig();
        label.default = "default label";
        label.default = "default label";
        label.default = "default label";
        expect(observer).toHaveBeenCalledTimes(1);
        expect(label.default).toEqual("default label");
    });
    it("Notifies when the custom label changes", () => {
        const { label, observer } = makeTestRig();
        label.custom = "custom label";
        label.custom = "custom label";
        label.custom = "custom label";
        expect(observer).toHaveBeenCalledTimes(1);
        expect(label.custom).toEqual("custom label");
    });
    it("Returns the custom label as the display if it is not empty", () => {
        const { label } = makeTestRig();
        label.default = "default label";
        label.custom = "custom label";
        expect(label.display).toEqual("custom label");
    });
    it("Returns the default label and the distplay if the custom is empty", () => {
        const { label } = makeTestRig();
        label.default = "default label";
        label.custom = "";
        expect(label.display).toEqual("default label");
    });
    it("Returns has custom label correctly", () => {
        const { label } = makeTestRig();
        label.default = "default label";
        label.custom = "custom label";
        expect(label.hasCustomLabel).toEqual(true);
        label.default = "default label";
        label.custom = "";
        expect(label.hasCustomLabel).toEqual(false);
    });
    it("Validator can be injected", () => {
        const { label } = makeTestRig();
        label.customLabelValidator = (val) => {
            if (val.length > 1) {
                return "Too Long!";
            }
            else
                return null;
        };
        label.custom = "X";
        expect(label.customLabelValidationMessage).toEqual(null);
        label.custom = "XX";
        expect(label.customLabelValidationMessage).toEqual("Too Long!");
    });
    it("Gets the entity from an AO", () => {
        const { label, appObj } = makeTestRig();
        expect(LabelEntity_1.LabelEntity.get(appObj)).toEqual(label);
    });
    it("Returns the existing when calling add if missing ", () => {
        const { label, appObj } = makeTestRig();
        expect(LabelEntity_1.LabelEntity.addIfMissing(appObj)).toEqual(label);
    });
    it("Creates a new one if addIfMissing is called", () => {
        const { appObjects } = makeTestRig();
        const newAO = appObjects.getOrCreate("anotherAO");
        expect(newAO.hasComponent(LabelEntity_1.LabelEntity.type)).toEqual(false);
        LabelEntity_1.LabelEntity.addIfMissing(newAO);
        expect(newAO.hasComponent(LabelEntity_1.LabelEntity.type)).toEqual(true);
    });
});
//# sourceMappingURL=LabelEntity.test.js.map