import { makeAppObjectRepo } from "@vived/core";
import { makeMockDispatchStateChange } from "../../Dispatcher";
import { MockQuestionAnswerEntity } from "../Mocks/MockQuestionAnswerEntity";
import { makeSetAnswerTextUC } from "./SetAnswerTextUC";
import { mocked } from "jest-mock";
import { SetAnswerTextUndoRedo } from "./SetAnswerTextUndoRedo";
jest.mock("./SetAnswerTextUndoRedo", () => {
    return {
        SetAnswerTextUndoRedo: jest.fn().mockImplementation(() => {
            return;
        }),
    };
});
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const ansAO = appObjects.getOrCreate("Ans");
    const mockAns = new MockQuestionAnswerEntity(ansAO);
    const appAO = appObjects.getOrCreate("App");
    const mockDispatch = makeMockDispatchStateChange(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatch.doDispatch = doDispatchMock;
    const MockedUndoRedoAction = mocked(SetAnswerTextUndoRedo);
    const setAnswerText = makeSetAnswerTextUC(ansAO);
    return {
        mockAns,
        setAnswerText,
        appObjects,
        mockDispatch,
        doDispatchMock,
        MockedUndoRedoAction,
    };
}
describe("Set Answer Text use case", () => {
    it("Sets the text", () => {
        const { mockAns, setAnswerText } = makeTestRig();
        mockAns.text = "";
        setAnswerText.setText("Hello World");
        expect(mockAns.text).toEqual("Hello World");
    });
    it("Warns if it is added to an App Object that does not have an Answer Entity", () => {
        const { appObjects } = makeTestRig();
        appObjects.submitWarning = jest.fn();
        makeSetAnswerTextUC(appObjects.getOrCreate("AnotherAO"));
        expect(appObjects.submitWarning).toHaveBeenCalled();
    });
    it("Dispatches if there is a change", () => {
        const { mockAns, setAnswerText, doDispatchMock } = makeTestRig();
        mockAns.text = "";
        setAnswerText.setText("Hello World");
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Does not dispatch if there is no change", () => {
        const { mockAns, setAnswerText, doDispatchMock } = makeTestRig();
        mockAns.text = "Hello World";
        setAnswerText.setText("Hello World");
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Creates an Undo Redo action", () => {
        const { setAnswerText, MockedUndoRedoAction, appObjects, mockAns } = makeTestRig();
        MockedUndoRedoAction.mockClear();
        mockAns.text = "Initial text";
        setAnswerText.setText("Hello World");
        expect(MockedUndoRedoAction).toHaveBeenCalledTimes(1);
        expect(MockedUndoRedoAction).toHaveBeenCalledWith("Initial text", mockAns.id, appObjects);
    });
});
//# sourceMappingURL=SetAnswerTextUC.test.js.map