"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const Entities_1 = require("../Entities");
const ChannelModelPM_1 = require("./ChannelModelPM");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const ao = appObjects.getOrCreate("AO");
    const channelModel = (0, Entities_1.makeChannelModelEntity)(ao);
    const addObserverSpy = jest.spyOn(channelModel, "addChangeObserver");
    const pm = (0, ChannelModelPM_1.makeChannelModelPM)(ao);
    return { appObjects, ao, channelModel, pm, addObserverSpy };
}
describe("Channel Model PM", () => {
    it("Registers as an observer", () => {
        const { addObserverSpy } = makeTestRig();
        expect(addObserverSpy).toHaveBeenCalled();
    });
    it("Checks for equal VMs", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            childHasValidationMessage: true,
            modelObjectIds: ["obj1", "obj2"],
        };
        const vm2 = { ...vm1 };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(true);
    });
    it("Checks for change in the childHasValidationMessage", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            childHasValidationMessage: true,
            modelObjectIds: ["obj1", "obj2"],
        };
        const vm2 = { ...vm1, childHasValidationMessage: false };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
    });
    it("Checks for an object  to be added", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            childHasValidationMessage: true,
            modelObjectIds: ["obj1", "obj2"],
        };
        const vm2 = {
            ...vm1,
            modelObjectIds: ["obj1", "obj1", "obj3"],
        };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
    });
    it("Checks for an object to be removed", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            childHasValidationMessage: true,
            modelObjectIds: ["obj1", "obj2"],
        };
        const vm2 = { ...vm1, modelObjectIds: ["obj2"] };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
    });
    it("Checks for an object to be changed", () => {
        const { pm } = makeTestRig();
        const vm1 = {
            childHasValidationMessage: true,
            modelObjectIds: ["obj1", "obj2"],
        };
        const vm2 = { ...vm1, modelObjectIds: ["obj1", "CHANGED"] };
        expect(pm.vmsAreEqual(vm1, vm2)).toEqual(false);
    });
    it("Sets the object list in the VM", () => {
        const { pm, channelModel } = makeTestRig();
        channelModel.addModelObject("obj1");
        expect(pm.lastVM?.modelObjectIds).toEqual(["obj1"]);
    });
    it("Updates when there is a validation error", () => {
        const { pm, channelModel } = makeTestRig();
        const obj = channelModel.addModelObject("obj1");
        obj.label.customLabelValidator = () => {
            return "Some Error";
        };
        obj.label.custom = "lbl";
        expect(pm.lastVM?.childHasValidationMessage).toEqual(true);
        obj.label.customLabelValidator = () => {
            return null;
        };
        obj.label.custom = "new label";
        expect(pm.lastVM?.childHasValidationMessage).toEqual(false);
    });
    it("Initializes the VM", () => {
        const { pm } = makeTestRig();
        expect(pm.lastVM).not.toBeUndefined();
    });
    it("Gets the PM for an ID", () => {
        const { pm, ao, appObjects } = makeTestRig();
        expect(ChannelModelPM_1.ChannelModelPM.get(ao.id, appObjects)).toEqual(pm);
    });
});
//# sourceMappingURL=ChannelModelPM.test.js.map