"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const BaseAppState_1 = require("../../BaseApp/Entity/BaseAppState");
const StateMachine_1 = require("../../StateMachine");
const MockDispatcher_1 = require("../Mocks/MockDispatcher");
const DispatchStateChangeUC_1 = require("./DispatchStateChangeUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const dispatcherAO = appObjects.getOrCreate("dispatcher");
    const mockDispatcher = new MockDispatcher_1.MockDispatcher(dispatcherAO);
    const app = appObjects.getOrCreate("App");
    const baseApp = (0, BaseAppState_1.makeBaseAppState)(app);
    baseApp.isAuthoring = true;
    const mockStateMachine = (0, StateMachine_1.makeMockStateMachine)(appObjects);
    mockStateMachine.getSerializedStates.mockReturnValue({ mock: "state" });
    const uc = (0, DispatchStateChangeUC_1.makeDispatchStateChangeUC)(dispatcherAO);
    return {
        uc,
        mockDispatcher,
        appObjects,
        baseApp,
        mockStateMachine,
    };
}
describe("Dispatch State Change", () => {
    it("Gets the UC", () => {
        const { uc, appObjects } = makeTestRig();
        expect(DispatchStateChangeUC_1.DispatchStateChangeUC.get(appObjects)).toEqual(uc);
    });
    it("Finds and dispatches to the UC", () => {
        const { appObjects, uc } = makeTestRig();
        uc.doDispatch = jest.fn();
        (0, DispatchStateChangeUC_1.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