import { makeAppObjectRepo } from "@vived/core";
import { AppDispatcher } from "../Entities/AppDispatcher";
import { ShowSelectModelUC, makeShowSelectModelUC } from "./ShowSelectModelUC";
class MockDispatcher extends AppDispatcher {
    constructor() {
        super(...arguments);
        this.dispatch = jest.fn();
        this.formRequestAndDispatch = (type, version, payload) => {
            const cb = payload.callback;
            cb("modelID", "dataID");
        };
    }
}
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const dispatcher = appObjects.getOrCreate("Dispatcher");
    const mockDispatcher = new MockDispatcher(dispatcher, AppDispatcher.type);
    const uc = makeShowSelectModelUC(dispatcher);
    return { uc, dispatcher, appObjects, mockDispatcher };
}
describe("Dispatch a request", () => {
    it("Gets the UC", () => {
        const { uc, appObjects } = makeTestRig();
        expect(ShowSelectModelUC.get(appObjects)).toEqual(uc);
    });
    it("Warns and returns undefined if there is no repo", () => {
        const { uc, appObjects } = makeTestRig();
        uc.appObject.dispose();
        console.warn = jest.fn();
        expect(ShowSelectModelUC.get(appObjects)).toBeUndefined();
        expect(console.warn).toHaveBeenCalled();
    });
    it("Warns if there are more than two repos found", () => {
        const { appObjects } = makeTestRig();
        const newAO = appObjects.getOrCreate("newAO");
        new MockDispatcher(newAO, AppDispatcher.type);
        makeShowSelectModelUC(newAO);
        console.warn = jest.fn();
        expect(ShowSelectModelUC.get(appObjects)).not.toBeUndefined();
        expect(console.warn).toHaveBeenCalled();
    });
    it("Dispatches the correct type", () => {
        const { uc, mockDispatcher } = makeTestRig();
        const spy = jest.spyOn(mockDispatcher, "formRequestAndDispatch");
        uc.doDispatch();
        expect(spy).toHaveBeenCalledTimes(1);
        expect(spy.mock.calls[0][0]).toEqual(uc.requestType);
    });
    it("Dispatches the correct version", () => {
        const { uc, mockDispatcher } = makeTestRig();
        const spy = jest.spyOn(mockDispatcher, "formRequestAndDispatch");
        uc.doDispatch();
        expect(spy).toHaveBeenCalledTimes(1);
        expect(spy.mock.calls[0][1]).toEqual(1);
    });
    it("Resolves with the model and data id", async () => {
        const { uc } = makeTestRig();
        const rVal = await uc.doDispatch();
        expect(rVal.dataId).toEqual("dataID");
        expect(rVal.modelId).toEqual("modelID");
    });
});
//# sourceMappingURL=ShowSelectModelUC.test.js.map