"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const MockDispatchStateChange_1 = require("../../Dispatcher/Mocks/MockDispatchStateChange");
const UndoRedo_1 = require("../../UndoRedo");
const ChallengeEntity_1 = require("../Entities/ChallengeEntity");
const ChallengeMock_1 = require("../Mocks/ChallengeMock");
const SetChallengeUC_1 = require("./SetChallengeUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const registerSingletonSpy = jest.spyOn(appObjects, "registerSingleton");
    const ao = appObjects.getOrCreate("AO");
    const entity = (0, ChallengeEntity_1.makeChallengeEntity)(ao);
    const undo = (0, UndoRedo_1.makeUndoRedoEntity)(appObjects.getOrCreate("Undo"));
    const mockChallenge = new ChallengeMock_1.ChallengeMock("MOCK_CHALLENGE");
    entity.registerChallenge(mockChallenge);
    const uc = (0, SetChallengeUC_1.makeSetChallengeUC)(ao);
    const mockDispatchStateChange = (0, MockDispatchStateChange_1.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_1.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