import { makeAppObjectRepo } from "@vived/core";
import { makeMockDispatchStateChange } from "../../Dispatcher";
import { makeUndoRedoEntity } from "../../UndoRedo";
import { Environments, makeEnvironmentEntity } from "../Entities/Environment";
import { makeSetTexturalEnvironmentUC, SetTexturalEnvironmentUC, } from "./SetTexturalEnvironmentUC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const registerSpy = jest.spyOn(appObjects, "registerSingleton");
    const mockDispatchStateChange = makeMockDispatchStateChange(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatchStateChange.doDispatch = doDispatchMock;
    const environment = makeEnvironmentEntity(appObjects.getOrCreate("Env"));
    environment.currentEnvironment = Environments.FOREST;
    const undo = makeUndoRedoEntity(appObjects.getOrCreate("Undo"));
    const uc = makeSetTexturalEnvironmentUC(appObjects.getOrCreate("UC"));
    return {
        uc,
        environment,
        doDispatchMock,
        undo,
        appObjects,
        registerSpy,
    };
}
describe("Test Set Textural Environment", () => {
    it("Get it UC", () => {
        const { appObjects, uc } = makeTestRig();
        expect(SetTexturalEnvironmentUC.get(appObjects)).toEqual(uc);
    });
    it("Registers as the singleton", () => {
        const { uc, registerSpy } = makeTestRig();
        expect(registerSpy).toHaveBeenCalledWith(uc);
    });
    it("Sets textural environment for existing environment", () => {
        const { uc, environment } = makeTestRig();
        uc.setEnvironment(Environments.NIGHT);
        expect(environment.currentEnvironment).toEqual(Environments.NIGHT);
    });
    it("Dispatches a state change if there is a change", () => {
        const { uc, doDispatchMock } = makeTestRig();
        uc.setEnvironment(Environments.NIGHT);
        uc.setEnvironment(Environments.NIGHT);
        uc.setEnvironment(Environments.NIGHT);
        uc.setEnvironment(Environments.NIGHT);
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
    });
    it("Registers an undo if there is a change", () => {
        const { uc, undo } = makeTestRig();
        undo.submitAction = jest.fn();
        uc.setEnvironment(Environments.NIGHT);
        uc.setEnvironment(Environments.NIGHT);
        uc.setEnvironment(Environments.NIGHT);
        uc.setEnvironment(Environments.NIGHT);
        expect(undo.submitAction).toHaveBeenCalledTimes(1);
    });
    it("Can undo and redo the change", () => {
        const { uc, undo, doDispatchMock, environment } = makeTestRig();
        expect(environment.currentEnvironment).toEqual(Environments.FOREST);
        uc.setEnvironment(Environments.NIGHT);
        expect(environment.currentEnvironment).toEqual(Environments.NIGHT);
        doDispatchMock.mockClear();
        undo.doUndo();
        expect(environment.currentEnvironment).toEqual(Environments.FOREST);
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
        undo.doRedo();
        expect(environment.currentEnvironment).toEqual(Environments.NIGHT);
        expect(doDispatchMock).toHaveBeenCalledTimes(2);
    });
});
//# sourceMappingURL=SetTexturalEnvironmentUC.test.js.map