"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const MockDispatchStateChange_1 = require("../../Dispatcher/Mocks/MockDispatchStateChange");
const MockUndoRedo_1 = require("../../UndoRedo/Mocks/MockUndoRedo");
const Entities_1 = require("../Entities");
const DeleteAnswerUndoRedo_1 = require("./DeleteAnswerUndoRedo");
function setupQuestionAnswer(appObjects, id) {
    const ao = appObjects.getOrCreate(id ?? "answer");
    return (0, Entities_1.makeQuestionAnswerEntity)(ao);
}
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const ao = appObjects.getOrCreate("App");
    const mockUndo = new MockUndoRedo_1.MockUndoRedo(ao);
    const mockDispatcher = new MockDispatchStateChange_1.MockDispatchStateChange(ao);
    const questionEntity = (0, Entities_1.makeQuestionEntity)(ao);
    questionEntity.answerFactory = (id) => setupQuestionAnswer(appObjects, id);
    const answer = questionEntity.createNewAnswer();
    questionEntity.removeAnswer(answer.id);
    return { mockUndo, mockDispatcher, questionEntity, appObjects, answer };
}
describe("Add Answer Undo Redo action", () => {
    it("Submits itself to the entity", () => {
        const { mockUndo, appObjects, answer } = makeTestRig();
        const action = new DeleteAnswerUndoRedo_1.DeleteAnswerUndoRedo(answer, appObjects);
        expect(mockUndo.submitAction).toHaveBeenCalledWith(action);
    });
    it("Undo adds the deleted answer and restores the data", () => {
        const { appObjects, questionEntity, answer } = makeTestRig();
        answer.text = "Some answer text";
        answer.feedbackText = "Some answer feedback";
        answer.isCorrect = true;
        expect(questionEntity.hasAnswer(answer.id)).toEqual(false);
        const action = new DeleteAnswerUndoRedo_1.DeleteAnswerUndoRedo(answer, appObjects);
        action.undoAction();
        expect(questionEntity.hasAnswer(answer.id)).toEqual(true);
        expect(questionEntity.getAnswer(answer.id)?.text).toEqual(answer.text);
        expect(questionEntity.getAnswer(answer.id)?.feedbackText).toEqual(answer.feedbackText);
        expect(questionEntity.getAnswer(answer.id)?.isCorrect).toEqual(answer.isCorrect);
    });
    it("Redo removes the answer", () => {
        const { appObjects, questionEntity, answer } = makeTestRig();
        const action = new DeleteAnswerUndoRedo_1.DeleteAnswerUndoRedo(answer, appObjects);
        action.undoAction();
        action.redoAction();
        expect(questionEntity.hasAnswer(answer.id)).toEqual(false);
    });
    it("Dispatches state changes for both the undo and redo action", () => {
        const { appObjects, mockDispatcher, answer } = makeTestRig();
        const action = new DeleteAnswerUndoRedo_1.DeleteAnswerUndoRedo(answer, appObjects);
        action.undoAction();
        expect(mockDispatcher.doDispatch).toHaveBeenCalledTimes(1);
        action.redoAction();
        expect(mockDispatcher.doDispatch).toHaveBeenCalledTimes(2);
    });
});
//# sourceMappingURL=DeleteAnswerUndoRedo.test.js.map