import { makeAppObjectRepo } from "@vived/core";
import { makeMockDispatchStateChange } from "../../Dispatcher";
import { MockUndoRedo } from "../../UndoRedo/Mocks/MockUndoRedo";
import { makeQuestionAnswerEntity, makeQuestionEntity, } from "../Entities";
import { AddAnswerUndoRedo } from "./AddAnswerUndoRedo";
function setupQuestionAnswer(appObjects, id) {
    const ao = appObjects.getOrCreate(id ?? "answer");
    return makeQuestionAnswerEntity(ao);
}
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const ao = appObjects.getOrCreate("App");
    const mockUndo = new MockUndoRedo(ao);
    // Override the submitAction with a jest.fn()
    mockUndo.submitAction = jest.fn();
    const mockDispatcher = makeMockDispatchStateChange(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatcher.doDispatch = doDispatchMock;
    const questionEntity = makeQuestionEntity(ao);
    questionEntity.answerFactory = (id) => setupQuestionAnswer(appObjects, id);
    return {
        mockUndo,
        mockDispatcher,
        doDispatchMock,
        questionEntity,
        appObjects,
    };
}
describe("Add Answer Undo Redo action", () => {
    it("Submits itself to the entity", () => {
        const { mockUndo, appObjects } = makeTestRig();
        const action = new AddAnswerUndoRedo("questionID", appObjects);
        expect(mockUndo.submitAction).toHaveBeenCalledWith(action);
    });
    it("Undo removes the added answer", () => {
        const { appObjects, questionEntity } = makeTestRig();
        const ans = questionEntity.createNewAnswer();
        const action = new AddAnswerUndoRedo(ans.id, appObjects);
        action.undoAction();
        expect(questionEntity.hasAnswer(ans.id)).toEqual(false);
    });
    it("Redo adds the answer back in", () => {
        const { appObjects, questionEntity } = makeTestRig();
        const ans = questionEntity.createNewAnswer();
        const action = new AddAnswerUndoRedo(ans.id, appObjects);
        action.undoAction();
        action.redoAction();
        expect(questionEntity.hasAnswer(ans.id)).toEqual(true);
    });
    it("Dispatches state changes for both the undo and redo action", () => {
        const { appObjects, questionEntity, doDispatchMock } = makeTestRig();
        const ans = questionEntity.createNewAnswer();
        const action = new AddAnswerUndoRedo(ans.id, appObjects);
        action.undoAction();
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
        action.redoAction();
        expect(doDispatchMock).toHaveBeenCalledTimes(2);
    });
});
//# sourceMappingURL=AddAnswerUndoRedo.test.js.map