import { makeAppObjectRepo } from "@vived/core";
import { makeMockDispatchStateChange } from "../../Dispatcher";
import { makeQuestionEntity } from "../Entities/QuestionEntity";
import { deleteQuestionAnswer, DeleteQuestionAnswerUC, makeDeleteQuestionAnswerUC, } from "./DeleteQuestionAnswerUC";
import { MockQuestionAnswerEntity } from "../Mocks/MockQuestionAnswerEntity";
import { DeleteAnswerUndoRedo } from "./DeleteAnswerUndoRedo";
import { mocked } from "jest-mock";
jest.mock("./DeleteAnswerUndoRedo", () => {
    return {
        DeleteAnswerUndoRedo: jest.fn().mockImplementation(() => {
            return;
        }),
    };
});
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const questionAO = appObjects.getOrCreate("Question");
    const questionEntity = makeQuestionEntity(questionAO);
    const ansAO = appObjects.getOrCreate("Ans");
    const mockAns = new MockQuestionAnswerEntity(ansAO);
    questionEntity.addAnswer(mockAns);
    const appAO = appObjects.getOrCreate("App");
    const mockDispatch = makeMockDispatchStateChange(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    const MockedUndoRedoAction = mocked(DeleteAnswerUndoRedo);
    const uc = 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();
        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.get(appObjects)).toEqual(uc);
    });
    it("Finds and calls the UC", () => {
        const { appObjects, uc } = makeTestRig();
        uc.deleteAnswer = jest.fn();
        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