"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const Dispatcher_1 = require("../../Dispatcher");
const MockUndoRedo_1 = require("../../UndoRedo/Mocks/MockUndoRedo");
const Entities_1 = require("../Entities");
const ToggleIsCorrectUndoRedo_1 = require("./ToggleIsCorrectUndoRedo");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const ao = appObjects.getOrCreate("App");
    const mockUndo = new MockUndoRedo_1.MockUndoRedo(ao);
    // Override the submitAction with a jest.fn()
    mockUndo.submitAction = jest.fn();
    const mockDispatcher = (0, Dispatcher_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatcher.doDispatch = doDispatchMock;
    const questionEntity = (0, Entities_1.makeQuestionEntity)(ao);
    questionEntity.answerFactory = (id) => {
        const answerAO = appObjects.getOrCreate(id ?? "answer");
        return (0, Entities_1.makeQuestionAnswerEntity)(answerAO);
    };
    const answer = questionEntity.createNewAnswer();
    return { mockUndo, mockDispatcher, doDispatchMock, appObjects, answer };
}
describe("Add Answer Undo Redo action", () => {
    it("Submits itself to the entity", () => {
        const { mockUndo, appObjects } = makeTestRig();
        const action = new ToggleIsCorrectUndoRedo_1.ToggleIsCorrectUndoRedo("questionID", appObjects);
        expect(mockUndo.submitAction).toHaveBeenCalledWith(action);
    });
    it("Undo adds the deleted answer", () => {
        const { appObjects, answer } = makeTestRig();
        expect(answer.isCorrect).toEqual(false);
        const action = new ToggleIsCorrectUndoRedo_1.ToggleIsCorrectUndoRedo(answer.id, appObjects);
        action.undoAction();
        expect(answer.isCorrect).toEqual(true);
    });
    it("Redo removes the answer", () => {
        const { appObjects, answer } = makeTestRig();
        const action = new ToggleIsCorrectUndoRedo_1.ToggleIsCorrectUndoRedo(answer.id, appObjects);
        expect(answer.isCorrect).toEqual(false);
        action.undoAction();
        action.redoAction();
        expect(answer.isCorrect).toEqual(false);
    });
    it("Dispatches state changes for both the undo and redo action", () => {
        const { appObjects, doDispatchMock, answer } = makeTestRig();
        const action = new ToggleIsCorrectUndoRedo_1.ToggleIsCorrectUndoRedo(answer.id, appObjects);
        action.undoAction();
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
        action.redoAction();
        expect(doDispatchMock).toHaveBeenCalledTimes(2);
    });
});
//# sourceMappingURL=ToggleIsCorrectUndoRedo.test.js.map