"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const Dispatcher_1 = require("../../Dispatcher");
const QuestionEntity_1 = require("../Entities/QuestionEntity");
const AddQuestionAnswerUC_1 = require("./AddQuestionAnswerUC");
const AddAnswerUndoRedo_1 = require("./AddAnswerUndoRedo");
const jest_mock_1 = require("jest-mock");
const Entities_1 = require("../Entities");
jest.mock("./AddAnswerUndoRedo", () => {
    return {
        AddAnswerUndoRedo: jest.fn().mockImplementation(() => {
            return;
        }),
    };
});
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const questionAO = appObjects.getOrCreate("Question");
    const questionEntity = (0, QuestionEntity_1.makeQuestionEntity)(questionAO);
    questionEntity.answerFactory = (id) => {
        const ao = appObjects.getOrCreate(id ?? "answerID");
        return (0, Entities_1.makeQuestionAnswerEntity)(ao);
    };
    const appAO = appObjects.getOrCreate("App");
    const mockDispatch = (0, Dispatcher_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    const MockedUndoRedoAction = (0, jest_mock_1.mocked)(AddAnswerUndoRedo_1.AddAnswerUndoRedo);
    const uc = (0, AddQuestionAnswerUC_1.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();
        (0, AddQuestionAnswerUC_1.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_1.AddQuestionAnswerUC.get(appObjects)).toEqual(uc);
    });
    it("Finds and calls the UC", () => {
        const { appObjects, uc } = makeTestRig();
        uc.addAnswer = jest.fn();
        (0, AddQuestionAnswerUC_1.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