"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const ChannelModel_1 = require("./ChannelModel");
const ChannelModelRepo_1 = require("./ChannelModelRepo");
function makeTestRig() {
    const appObjectRepo = (0, core_1.makeAppObjectRepo)();
    const repoAppObj = appObjectRepo.getOrCreate("repo");
    const repo = (0, ChannelModelRepo_1.makeChannelModelRepo)(repoAppObj);
    const observer = jest.fn();
    repo.addChangeObserver(observer);
    const appObj = appObjectRepo.getOrCreate("model1");
    const model = (0, ChannelModel_1.makeChannelModelEntity)(appObj);
    repo.add(model);
    observer.mockClear();
    return { observer, repo, model, appObjectRepo, repoAppObj };
}
describe("Channel Model Repo", () => {
    it("Removes itself from the App Object when disposed", () => {
        const { repo, repoAppObj } = makeTestRig();
        repo.dispose();
        expect(repoAppObj.hasComponent(ChannelModelRepo_1.ChannelModelRepo.type)).toEqual(false);
    });
    it("Notifies when the active model changes", () => {
        const { repo, observer } = makeTestRig();
        expect(repo.activeModel).toEqual("");
        repo.activeModel = "model1";
        expect(observer).toHaveBeenCalledTimes(1);
        expect(repo.activeModel).toEqual("model1");
    });
    it("Does not notify if there is no change in the active model", () => {
        const { repo, observer } = makeTestRig();
        repo.activeModel = "model1";
        repo.activeModel = "model1";
        repo.activeModel = "model1";
        repo.activeModel = "model1";
        expect(observer).toHaveBeenCalledTimes(1);
    });
    it("Gets the Repo", () => {
        const { repo, appObjectRepo } = makeTestRig();
        expect(ChannelModelRepo_1.ChannelModelRepo.get(appObjectRepo)).toEqual(repo);
    });
    it("Warns and returns undefined if there is no repo", () => {
        const { repo, appObjectRepo } = makeTestRig();
        repo.appObject.dispose();
        console.warn = jest.fn();
        expect(ChannelModelRepo_1.ChannelModelRepo.get(appObjectRepo)).toBeUndefined();
        expect(console.warn).toHaveBeenCalled();
    });
    it("Gets a channel model", () => {
        const { repo, model } = makeTestRig();
        expect(repo.getChannelModel("model1", false)).toEqual(model);
    });
    it("Returns undefined for an unknown model", () => {
        const { repo, model } = makeTestRig();
        expect(repo.getChannelModel("unknown", false)).toEqual(undefined);
    });
    it("Adds a new new model if the flag is set", () => {
        const { repo } = makeTestRig();
        const newModel = repo.getChannelModel("newModel", true);
        expect(newModel?.appObject.id).toEqual("newModel");
        expect(repo.hasForAppObject("newModel")).toEqual(true);
    });
    it("Creates a channel model with an ID", () => {
        const { repo } = makeTestRig();
        expect(repo.hasForAppObject("newMode")).toEqual(false);
        const model = repo.createChannelModel("newMode");
        expect(repo.hasForAppObject("newMode")).toEqual(true);
        expect(model.id).toEqual("newMode");
    });
    it("Creates a channel model without an ID", () => {
        const { repo } = makeTestRig();
        const model = repo.createChannelModel();
        expect(repo.hasForAppObject(model.id)).toEqual(true);
        expect(model.id).not.toBeUndefined();
    });
});
//# sourceMappingURL=ChannelModelRepo.test.js.map