"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const PiElementReferenceM_1 = require("./PiElementReferenceM");
const TestScoper_1 = require("./TestScoper");
const MobxModel_1 = require("./MobxModel");
const mobx_1 = require("mobx");
describe("Mobx Model", () => {
    describe("container settings", () => {
        const ctx = new MobxModel_1.ModelContext();
        let root;
        let element;
        let part1;
        let part2;
        let part3;
        let reaktion = 0;
        let observedLeft = 0;
        beforeEach(done => {
            root = new MobxModel_1.MobxTestRoot("root");
            element = new MobxModel_1.MobxTestParts("partsContainer");
            part1 = new MobxModel_1.MobxTestElement("part1");
            part2 = new MobxModel_1.MobxTestElement("part2");
            part3 = new MobxModel_1.MobxTestElement("part3");
            root.element = element;
            element.manyPart.push(part1);
            element.manyPart.push(part2);
            element.singlePart = part3;
            element.singleReference = PiElementReferenceM_1.PiElementReferenceM.create(part2, "tt");
            ctx.root = root;
            TestScoper_1.TestScoper.getInstance().root = root;
            observedLeft = 0;
            mobx_1.observe(ctx, "root", () => observedLeft++);
            mobx_1.reaction(() => {
                return [root.element];
            }, element => {
                reaktion++;
            });
            reaktion = 0;
            done();
        });
        it("references 1", () => {
            expect(element.singleReference.name).toBe("part2");
            expect(element.singleReference.referred).toBe(part2);
            checkUnchanged();
            element.singleReference.name = "part1";
            expect(element.manyPart.length).toBe(2);
            expect(element.singleReference.referred).toBe(part1);
            expect(element.singleReference.name).toBe("part1");
            checkUnchanged();
            element.singleReference.name = "part44";
            expect(element.singleReference.name).toBe("part44");
            expect(element.singleReference.referred).toBe(undefined);
            element.singleReference.name = "part1";
            expect(element.singleReference.name).toBe("part1");
            expect(element.singleReference.referred).toBe(part1);
            checkUnchanged();
            part1.name = "part1-newname";
            expect(element.singleReference.name).toBe("part1-newname");
            expect(element.singleReference.referred).toBe(part1);
            checkUnchanged();
            element.singleReference.name = "part1";
            expect(element.singleReference.name).toBe("part1");
            expect(element.singleReference.referred).toBe(undefined);
            checkUnchanged();
            part1.name = "part1";
            expect(element.singleReference.name).toBe("part1");
            expect(element.singleReference.referred).toBe(part1);
            checkUnchanged();
            element.singleReference.referred = part2;
            expect(element.singleReference.name).toBe("part2");
            expect(element.singleReference.referred).toBe(part2);
            checkUnchanged();
            const singleRef = element.singleReference;
            element.singleReference = PiElementReferenceM_1.PiElementReferenceM.create(part1, "tt");
            expect(element.singleReference.name).toBe("part1");
            expect(element.singleReference.referred).toBe(part1);
            expect(singleRef.piContainer() === null);
        });
        it("of children should be set at start", () => {
            expect(element.container).toBe(root);
            expect(element.propertyName).toBe("element");
            expect(root.element.propertyIndex).toBe(undefined);
            expect(part1.propertyIndex).toBe(0);
            expect(part1.container).toBe(element);
            expect(part1.propertyName).toBe("manyPart");
            expect(part2.propertyIndex).toBe(1);
            expect(part2.container).toBe(element);
            expect(part2.propertyName).toBe("manyPart");
            expect(element.singlePart).toBe(part3);
        });
        it("should be unset when assigned to null", () => {
            root.element = null;
            expect(root.element).toBe(null);
            expect(element.container).toBe(null);
            expect(element.propertyName).toBe("");
            expect(element.propertyIndex).toBe(undefined);
            expect(reaktion).toBe(1);
            root.element = element;
            expect(element.container).toBe(root);
            expect(element.propertyName).toBe("element");
            expect(root.element.propertyIndex).toBe(undefined);
        });
        it("should be changed when moved", () => {
            element.singlePart = part1;
            expect(part1.container).toBe(element);
            expect(part1.propertyName).toBe("singlePart");
            expect(part1.propertyIndex).toBe(undefined);
            expect(part2.container).toBe(element);
            expect(part2.propertyName).toBe("manyPart");
            expect(part2.propertyIndex).toBe(0);
            expect(part3.container).toBe(null);
            expect(part3.propertyName).toBe("");
            expect(part3.propertyIndex).toBe(undefined);
        });
        it("should be changed when element is assigned to array", () => {
            element.manyPart.splice(0, 0, part3);
            expect(element.singlePart).toBe(null);
            expect(part1.container).toBe(element);
            expect(part1.propertyName).toBe("manyPart");
            expect(part1.propertyIndex).toBe(1);
            expect(part2.container).toBe(element);
            expect(part2.propertyName).toBe("manyPart");
            expect(part2.propertyIndex).toBe(2);
            expect(part3.container).toBe(element);
            expect(part3.propertyName).toBe("manyPart");
            expect(part3.propertyIndex).toBe(0);
        });
        it("should be changed when array is cleared", () => {
            element.manyPart.splice(0, 2);
            expect(part1.container).toBe(null);
            expect(part1.propertyName).toBe("");
            expect(part1.propertyIndex).toBe(undefined);
            expect(part2.container).toBe(null);
            expect(part2.propertyName).toBe("");
            expect(part2.propertyIndex).toBe(undefined);
            expect(element.manyPart.length).toBe(0);
        });
        it("should be changed when array element assigned null", () => {
            element.manyPart[0] = null;
            expect(part1.container).toBe(null);
            expect(part1.propertyName).toBe("");
            expect(part1.propertyIndex).toBe(undefined);
            expect(element.manyPart[0]).toBe(null);
            expect(part2.container).toBe(element);
            expect(part2.propertyName).toBe("manyPart");
            expect(part2.propertyIndex).toBe(1);
            expect(element.manyPart[1]).toBe(part2);
            expect(element.manyPart.length).toBe(2);
        });
        it("push null", () => {
            element.manyPart.push(null);
            expect(element.manyPart.length).toBe(3);
            expect(element.manyPart[2]).toBe(null);
        });
        it("splice null", () => {
            element.manyPart.splice(0, 0, null);
            expect(element.manyPart.length).toBe(3);
            expect(element.manyPart[0]).toBe(null);
            expect(part1.container).toBe(element);
            expect(part1.propertyName).toBe("manyPart");
            expect(part1.propertyIndex).toBe(1);
            expect(part2.container).toBe(element);
            expect(part2.propertyName).toBe("manyPart");
            expect(part2.propertyIndex).toBe(2);
        });
        it("should be changed when array element is removed", () => {
            element.manyPart.splice(0, 1);
            expect(part1.container).toBe(null);
            expect(part1.propertyName).toBe("");
            expect(part1.propertyIndex).toBe(undefined);
            expect(part2.container).toBe(element);
            expect(part2.propertyName).toBe("manyPart");
            expect(part2.propertyIndex).toBe(0);
            expect(element.manyPart.length).toBe(1);
        });
        function checkUnchanged() {
            expect(part1.propertyIndex).toBe(0);
            expect(part1.container).toBe(element);
            expect(part1.propertyName).toBe("manyPart");
            expect(part2.propertyIndex).toBe(1);
            expect(part2.container).toBe(element);
            expect(part2.propertyName).toBe("manyPart");
        }
    });
});
//# sourceMappingURL=MobxTest.spec.js.map