import { makeAppObjectRepo } from "@vived/core";
import { makeMockDispatchStateChange } from "../../Dispatcher/Mocks/MockDispatchStateChange";
import { makeUndoRedoEntity } from "../../UndoRedo";
import { makeChallengeEntity } from "../Entities/ChallengeEntity";
import { ChallengeMock } from "../Mocks/ChallengeMock";
import { makeSetChallengeUC, SetChallengeUC } from "./SetChallengeUC";
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const registerSingletonSpy = jest.spyOn(appObjects, "registerSingleton");
    const ao = appObjects.getOrCreate("AO");
    const entity = makeChallengeEntity(ao);
    const undo = makeUndoRedoEntity(appObjects.getOrCreate("Undo"));
    const mockChallenge = new ChallengeMock("MOCK_CHALLENGE");
    entity.registerChallenge(mockChallenge);
    const uc = makeSetChallengeUC(ao);
    const mockDispatchStateChange = makeMockDispatchStateChange(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatchStateChange.doDispatch = doDispatchMock;
    return {
        ao,
        appObjects,
        uc,
        registerSingletonSpy,
        entity,
        doDispatchMock,
        undo,
    };
}
describe("Set Challenge UC", () => {
    it("Gets the singleton", () => {
        const { appObjects, uc } = makeTestRig();
        expect(SetChallengeUC.get(appObjects)).toEqual(uc);
    });
    it("Register the singleton", () => {
        const { registerSingletonSpy, uc } = makeTestRig();
        expect(registerSingletonSpy).toHaveBeenCalledWith(uc);
    });
    it("Sets the challenge on the entity", () => {
        const { entity, uc } = makeTestRig();
        expect(entity.selectedChallenge).toEqual("NONE");
        uc.setChallenge("MOCK_CHALLENGE");
        expect(entity.selectedChallenge).toEqual("MOCK_CHALLENGE");
    });
    it("Clears the challenge on the entity", () => {
        const { entity, uc } = makeTestRig();
        entity.selectedChallenge = "MOCK_CHALLENGE";
        expect(entity.selectedChallenge).toEqual("MOCK_CHALLENGE");
        uc.setChallenge("NONE");
        expect(entity.selectedChallenge).toEqual("NONE");
    });
    it("Dispatches a state change if changed", () => {
        const { entity, uc, doDispatchMock } = makeTestRig();
        expect(entity.selectedChallenge).toEqual("NONE");
        uc.setChallenge("MOCK_CHALLENGE");
        uc.setChallenge("MOCK_CHALLENGE");
        uc.setChallenge("MOCK_CHALLENGE");
        uc.setChallenge("MOCK_CHALLENGE");
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
    });
    it("Can be undone and redone", () => {
        const { entity, uc, undo } = makeTestRig();
        entity.selectedChallenge = "MOCK_CHALLENGE";
        expect(entity.selectedChallenge).toEqual("MOCK_CHALLENGE");
        uc.setChallenge("NONE");
        undo.doUndo();
        expect(entity.selectedChallenge).toEqual("MOCK_CHALLENGE");
        undo.doRedo();
        uc.setChallenge("NONE");
    });
    it("Undo and redo dispatch a state change", () => {
        const { uc, undo, doDispatchMock, entity } = makeTestRig();
        entity.selectedChallenge = "MOCK_CHALLENGE";
        uc.setChallenge("NONE");
        doDispatchMock.mockClear();
        undo.doUndo();
        expect(doDispatchMock).toHaveBeenCalled();
        doDispatchMock.mockClear();
        undo.doRedo();
        expect(doDispatchMock).toHaveBeenCalled();
    });
});
//# sourceMappingURL=SetChallengeUC.test.js.map