import { makeAppObjectRepo } from "@vived/core";
import { makeBaseAppState } from "../../BaseApp/Entity/BaseAppState";
import { makeMockStateMachine } from "../../StateMachine";
import { MockDispatcher } from "../Mocks/MockDispatcher";
import { dispatchStateChange, DispatchStateChangeUC, makeDispatchStateChangeUC, } from "./DispatchStateChangeUC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const dispatcherAO = appObjects.getOrCreate("dispatcher");
    const mockDispatcher = new MockDispatcher(dispatcherAO);
    const app = appObjects.getOrCreate("App");
    const baseApp = makeBaseAppState(app);
    baseApp.isAuthoring = true;
    const mockStateMachine = makeMockStateMachine(appObjects);
    mockStateMachine.getSerializedStates.mockReturnValue({ mock: "state" });
    const uc = makeDispatchStateChangeUC(dispatcherAO);
    return {
        uc,
        mockDispatcher,
        appObjects,
        baseApp,
        mockStateMachine,
    };
}
describe("Dispatch State Change", () => {
    it("Gets the UC", () => {
        const { uc, appObjects } = makeTestRig();
        expect(DispatchStateChangeUC.get(appObjects)).toEqual(uc);
    });
    it("Finds and dispatches to the UC", () => {
        const { appObjects, uc } = makeTestRig();
        uc.doDispatch = jest.fn();
        dispatchStateChange(appObjects);
        expect(uc.doDispatch).toHaveBeenCalled();
    });
    it("Does not dispatch if base app is not authoring", () => {
        const { baseApp, uc, mockDispatcher } = makeTestRig();
        baseApp.isAuthoring = false;
        uc.doDispatch();
        expect(mockDispatcher.formRequestAndDispatch).not.toHaveBeenCalled();
    });
    it("Dispatches the correct type", () => {
        const { uc, mockDispatcher } = makeTestRig();
        uc.doDispatch();
        expect(mockDispatcher.formRequestAndDispatch).toHaveBeenCalledTimes(1);
        expect(mockDispatcher.formRequestAndDispatch.mock.calls[0][0]).toEqual(uc.requestType);
    });
    it("Dispatches the correct version", () => {
        const { uc, mockDispatcher } = makeTestRig();
        uc.doDispatch();
        expect(mockDispatcher.formRequestAndDispatch).toHaveBeenCalledTimes(1);
        expect(mockDispatcher.formRequestAndDispatch.mock.calls[0][1]).toEqual(3);
    });
    it("Dispatches the payload validation message as undefined", () => {
        const { uc, mockDispatcher } = makeTestRig();
        uc.doDispatch();
        expect(mockDispatcher.formRequestAndDispatch).toHaveBeenCalledTimes(1);
        const payload = mockDispatcher.formRequestAndDispatch.mock
            .calls[0][2];
        expect(payload.validationErrorMessage).toBeUndefined();
    });
    it("Dispatches the payload validation message", () => {
        const { uc, mockDispatcher } = makeTestRig();
        uc.validateState = jest.fn().mockReturnValue("Some validation message");
        uc.doDispatch();
        expect(mockDispatcher.formRequestAndDispatch).toHaveBeenCalledTimes(1);
        const payload = mockDispatcher.formRequestAndDispatch.mock
            .calls[0][2];
        expect(payload.validationErrorMessage).toEqual("Some validation message");
    });
    it("Dispatches the payload assets", () => {
        const { uc, mockDispatcher } = makeTestRig();
        uc.getStateAssets = jest.fn().mockReturnValue(["model1"]);
        uc.doDispatch();
        expect(mockDispatcher.formRequestAndDispatch).toHaveBeenCalledTimes(1);
        const payload = mockDispatcher.formRequestAndDispatch.mock
            .calls[0][2];
        expect(payload.assets).toEqual(["model1"]);
    });
    it("Dispatches the payload state", () => {
        const { uc, mockDispatcher } = makeTestRig();
        uc.doDispatch();
        expect(mockDispatcher.formRequestAndDispatch).toHaveBeenCalledTimes(1);
        const payload = mockDispatcher.formRequestAndDispatch.mock
            .calls[0][2];
        expect(payload.stateObject).toEqual({ mock: "state" });
    });
});
//# sourceMappingURL=DispatchStateChangeUC.test.js.map