import { makeAppObjectRepo } from "@vived/core";
import { makeMockDispatchStateChange } from "../../Dispatcher";
import { makeQuestionEntity } from "../Entities/QuestionEntity";
import { addQuestionAnswer, AddQuestionAnswerUC, makeAddQuestionAnswerUC, } from "./AddQuestionAnswerUC";
import { AddAnswerUndoRedo } from "./AddAnswerUndoRedo";
import { mocked } from "jest-mock";
import { makeQuestionAnswerEntity } from "../Entities";
jest.mock("./AddAnswerUndoRedo", () => {
    return {
        AddAnswerUndoRedo: jest.fn().mockImplementation(() => {
            return;
        }),
    };
});
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const questionAO = appObjects.getOrCreate("Question");
    const questionEntity = makeQuestionEntity(questionAO);
    questionEntity.answerFactory = (id) => {
        const ao = appObjects.getOrCreate(id ?? "answerID");
        return makeQuestionAnswerEntity(ao);
    };
    const appAO = appObjects.getOrCreate("App");
    const mockDispatch = makeMockDispatchStateChange(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    const MockedUndoRedoAction = mocked(AddAnswerUndoRedo);
    const uc = makeAddQuestionAnswerUC(questionAO);
    return {
        questionEntity,
        uc,
        appObjects,
        mockDispatch,
        doDispatchMock,
        MockedUndoRedoAction,
    };
}
describe("Add Answer use case", () => {
    it("Adds an answer", () => {
        const { questionEntity, uc } = makeTestRig();
        expect(questionEntity.getAllAnswers()).toHaveLength(0);
        uc.addAnswer();
        expect(questionEntity.getAllAnswers()).toHaveLength(1);
    });
    it("Warns if it is added to an App Object that does not have an Question Entity", () => {
        const { appObjects } = makeTestRig();
        appObjects.submitWarning = jest.fn();
        makeAddQuestionAnswerUC(appObjects.getOrCreate("AnotherAO"));
        expect(appObjects.submitWarning).toHaveBeenCalled();
    });
    it("Dispatches a change", () => {
        const { uc, doDispatchMock } = makeTestRig();
        uc.addAnswer();
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Gets the UC", () => {
        const { uc, appObjects } = makeTestRig();
        expect(AddQuestionAnswerUC.get(appObjects)).toEqual(uc);
    });
    it("Finds and calls the UC", () => {
        const { appObjects, uc } = makeTestRig();
        uc.addAnswer = jest.fn();
        addQuestionAnswer(appObjects);
        expect(uc.addAnswer).toHaveBeenCalled();
    });
    it("Creates an Undo Redo action", () => {
        const { uc, MockedUndoRedoAction, appObjects } = makeTestRig();
        MockedUndoRedoAction.mockClear();
        uc.addAnswer();
        expect(MockedUndoRedoAction).toHaveBeenCalledTimes(1);
        expect(MockedUndoRedoAction).toHaveBeenCalledWith("answerID", appObjects);
    });
});
//# sourceMappingURL=AddQuestionAnswerUC.test.js.map