import { makeAppObjectRepo } from "@vived/core";
import { MockDispatcher } from "../Mocks/MockDispatcher";
import { NO_ASSET_ID_ERROR, SubmitActivityAssetUC, makeSubmitActivityAssetUC, submitActivityAsset, } from "./SubmitActivityAssetUC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const appObject = appObjects.getOrCreate("Dispatcher");
    const mockDispatcher = new MockDispatcher(appObject);
    const uc = makeSubmitActivityAssetUC(appObject);
    const fileBits = ["test", "file"];
    const testFile = new File(fileBits, "filename.jpg");
    return { uc, dispatcher: appObject, appObjects, mockDispatcher, testFile };
}
describe("Submit Activity Asset UC", () => {
    it("Gets the UC", () => {
        const { uc, appObjects } = makeTestRig();
        expect(SubmitActivityAssetUC.get(appObjects)).toEqual(uc);
    });
    it("Finds and dispatches to the UC", async () => {
        const { appObjects, uc, testFile } = makeTestRig();
        uc.doDispatch = jest.fn();
        await submitActivityAsset(testFile, appObjects);
        expect(uc.doDispatch).toHaveBeenCalledWith(testFile);
    });
    it("Dispatches the correct type", () => {
        const { uc, mockDispatcher, testFile } = makeTestRig();
        uc.doDispatch(testFile);
        expect(mockDispatcher.formRequestAndDispatch).toHaveBeenCalledTimes(1);
        expect(mockDispatcher.formRequestAndDispatch.mock.calls[0][0]).toEqual("SUBMIT_ACTIVITY_ASSET");
    });
    it("Dispatches the correct version", () => {
        const { uc, mockDispatcher, testFile } = makeTestRig();
        uc.doDispatch(testFile);
        expect(mockDispatcher.formRequestAndDispatch).toHaveBeenCalledTimes(1);
        expect(mockDispatcher.formRequestAndDispatch.mock.calls[0][1]).toEqual(1);
    });
    it("Dispatches a payload", () => {
        const { uc, mockDispatcher, testFile } = makeTestRig();
        uc.doDispatch(testFile);
        expect(mockDispatcher.formRequestAndDispatch).toHaveBeenCalledTimes(1);
        const payload = mockDispatcher.formRequestAndDispatch.mock.calls[0][2];
        expect(payload).not.toBeUndefined();
    });
    it("Dispatches resolves with an id", async () => {
        const { uc, mockDispatcher, testFile } = makeTestRig();
        const dispatcher = mockDispatcher;
        const mockID = "anID";
        dispatcher.formRequestAndDispatch = (type, version, payload) => {
            if (!payload)
                return;
            const cb = payload.callback;
            if (!cb)
                return;
            cb(mockID);
        };
        const id = await uc.doDispatch(testFile);
        expect(id).toEqual(mockID);
    });
    it("Rejects if the id is undefined", async () => {
        const { uc, mockDispatcher, testFile } = makeTestRig();
        const dispatcher = mockDispatcher;
        dispatcher.formRequestAndDispatch = (type, version, payload) => {
            if (!payload)
                return;
            const cb = payload.callback;
            if (!cb)
                return;
            cb(undefined);
        };
        return expect(uc.doDispatch(testFile)).rejects.toEqual(NO_ASSET_ID_ERROR);
    });
});
//# sourceMappingURL=SubmitActivityAssetUC.test.js.map