"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 DeleteQuestionAnswerUC_1 = require("./DeleteQuestionAnswerUC");
const MockQuestionAnswerEntity_1 = require("../Mocks/MockQuestionAnswerEntity");
const DeleteAnswerUndoRedo_1 = require("./DeleteAnswerUndoRedo");
const jest_mock_1 = require("jest-mock");
jest.mock("./DeleteAnswerUndoRedo", () => {
    return {
        DeleteAnswerUndoRedo: jest.fn().mockImplementation(() => {
            return;
        }),
    };
});
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const questionAO = appObjects.getOrCreate("Question");
    const questionEntity = (0, QuestionEntity_1.makeQuestionEntity)(questionAO);
    const ansAO = appObjects.getOrCreate("Ans");
    const mockAns = new MockQuestionAnswerEntity_1.MockQuestionAnswerEntity(ansAO);
    questionEntity.addAnswer(mockAns);
    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)(DeleteAnswerUndoRedo_1.DeleteAnswerUndoRedo);
    const uc = (0, DeleteQuestionAnswerUC_1.makeDeleteQuestionAnswerUC)(questionAO);
    return {
        questionEntity,
        uc,
        appObjects,
        mockDispatch,
        doDispatchMock,
        mockAns,
        MockedUndoRedoAction,
    };
}
describe("Delete Answer use case", () => {
    it("Deletes the answer", () => {
        const { questionEntity, uc, mockAns } = makeTestRig();
        expect(questionEntity.hasAnswer(mockAns.id)).toEqual(true);
        uc.deleteAnswer(mockAns.id);
        expect(questionEntity.hasAnswer(mockAns.id)).toEqual(false);
    });
    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, DeleteQuestionAnswerUC_1.makeDeleteQuestionAnswerUC)(appObjects.getOrCreate("AnotherAO"));
        expect(appObjects.submitWarning).toHaveBeenCalled();
    });
    it("Dispatches a change if a question is deleted", () => {
        const { uc, doDispatchMock, mockAns } = makeTestRig();
        uc.deleteAnswer(mockAns.id);
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Does not Dispatches a change if a question is not deleted", () => {
        const { uc, doDispatchMock } = makeTestRig();
        uc.deleteAnswer("something else");
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Gets the UC", () => {
        const { uc, appObjects } = makeTestRig();
        expect(DeleteQuestionAnswerUC_1.DeleteQuestionAnswerUC.get(appObjects)).toEqual(uc);
    });
    it("Finds and calls the UC", () => {
        const { appObjects, uc } = makeTestRig();
        uc.deleteAnswer = jest.fn();
        (0, DeleteQuestionAnswerUC_1.deleteQuestionAnswer)("answerID", appObjects);
        expect(uc.deleteAnswer).toHaveBeenCalledWith("answerID");
    });
    it("Creates an Undo Redo action", () => {
        const { uc, MockedUndoRedoAction, appObjects, mockAns } = makeTestRig();
        MockedUndoRedoAction.mockClear();
        uc.deleteAnswer("answerID");
        expect(MockedUndoRedoAction).toHaveBeenCalledTimes(1);
        expect(MockedUndoRedoAction).toHaveBeenCalledWith(mockAns, appObjects);
    });
});
//# sourceMappingURL=DeleteQuestionAnswerUC.test.js.map