import { makeAppObjectRepo } from "@vived/core";
import { makeTransform3D } from "../../Transform3D";
import { CalloutRepo, makeCalloutRepoEntity } from "./CalloutRepo";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const repoAO = appObjects.getOrCreate("repo");
    const repo = makeCalloutRepoEntity(repoAO);
    const observer = jest.fn();
    repo.addChangeObserver(observer);
    return { repo, appObjects, repoAO, observer };
}
describe("Callout Repo", () => {
    it("Gets the repo for the App Object", () => {
        const { repoAO, repo } = makeTestRig();
        expect(CalloutRepo.get(repoAO)).toEqual(repo);
    });
    it("Gets the repo by ID", () => {
        const { appObjects, repo } = makeTestRig();
        expect(CalloutRepo.getById("repo", appObjects)).toEqual(repo);
    });
    it("Makes a new callout", () => {
        const { repo } = makeTestRig();
        expect(repo.getAll()).toHaveLength(0);
        repo.makeCallout();
        expect(repo.getAll()).toHaveLength(1);
    });
    it("Uses the factory to create the new callout", () => {
        const { repo } = makeTestRig();
        const spy = jest.spyOn(repo, "calloutFactory");
        repo.makeCallout();
        expect(spy).toHaveBeenCalled();
    });
    it("Makes a callout with a given ID", () => {
        const { repo } = makeTestRig();
        const callout = repo.makeCallout("calloutID");
        expect(callout.appObject.id).toEqual("calloutID");
        expect(repo.hasForAppObject("calloutID")).toEqual(true);
    });
    it("Stores itself as the repo in the callout", () => {
        const { repo } = makeTestRig();
        const callout = repo.makeCallout("calloutID");
        expect(callout.inRepo).toEqual(repo);
    });
    it("Sets itself at the parent of the callout transforms", () => {
        const { repo } = makeTestRig();
        const callout = repo.makeCallout("calloutID");
        expect(callout.labelTransform.parent).toEqual(repo.transform);
        expect(callout.rootTransform.parent).toEqual(repo.transform);
    });
    it("Changes in the Callout are forwarded", () => {
        const { repo, observer } = makeTestRig();
        const callout = repo.makeCallout("calloutID");
        observer.mockClear();
        callout.notifyOnChange();
        expect(observer).toHaveBeenCalledTimes(1);
    });
    it("Delete a callout", () => {
        const { repo } = makeTestRig();
        repo.makeCallout("calloutID");
        repo.deleteCallout("calloutID");
        expect(repo.hasForAppObject("calloutID")).toEqual(false);
    });
    it("Delete a callout disposes the callout app object", () => {
        const { repo } = makeTestRig();
        const callout = repo.makeCallout("calloutID");
        const disposeSpy = jest.spyOn(callout.appObject, "dispose");
        repo.deleteCallout("calloutID");
        expect(disposeSpy).toHaveBeenCalled();
    });
    it("Stops forwarding changes", () => {
        const { repo, observer } = makeTestRig();
        const callout = repo.makeCallout("calloutID");
        repo.deleteCallout("calloutID");
        observer.mockClear();
        callout.notifyOnChange();
        expect(observer).not.toHaveBeenCalled();
    });
    it("Replace the Transform with a surrogate", () => {
        const { repo, appObjects } = makeTestRig();
        const surrogateT = makeTransform3D(appObjects.getOrCreate("surrogate"));
        repo.applySurrogateTransform(surrogateT);
        expect(repo.transform).toEqual(surrogateT);
    });
});
//# sourceMappingURL=CalloutRepo.test.js.map