"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const MockShowConfirm_1 = require("../../Dispatcher/Mocks/MockShowConfirm");
const MockDispatchStateChange_1 = require("../../Dispatcher/Mocks/MockDispatchStateChange");
const UndoRedoEntity_1 = require("../../UndoRedo/Entities/UndoRedoEntity");
const LabelDropRepo_1 = require("../Entities/LabelDropRepo");
const EditLabelDropUC_1 = require("./EditLabelDropUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const registerSpy = jest.spyOn(appObjects, "registerSingleton");
    const undo = (0, UndoRedoEntity_1.makeUndoRedoEntity)(appObjects.getOrCreate("Undo"));
    const mockDispatchStateChange = (0, MockDispatchStateChange_1.makeMockDispatchStateChange)(appObjects);
    const doDispatchMock = jest.fn();
    mockDispatchStateChange.doDispatch = doDispatchMock;
    const labelDropAO = appObjects.getOrCreate("repo");
    const repo = (0, LabelDropRepo_1.makeLabelDropRepo)(labelDropAO);
    const mockConfirm = (0, MockShowConfirm_1.makeMockShowConfirmUC)(appObjects);
    mockConfirm.showConfirm = (dto) => {
        dto.confirmCallback();
    };
    const editLabelDropUC = (0, EditLabelDropUC_1.makeEditLabelDropUC)(labelDropAO);
    const dropItem = repo.makeLabelDropItem("labelDropTestID");
    dropItem.labelEntity.custom = "Test Label";
    dropItem.targetIds = ["obj1"];
    return {
        appObjects,
        repo,
        editLabelDropUC,
        dropItem,
        mockConfirm,
        doDispatchMock,
        registerSpy,
        undo,
    };
}
describe("Edit Label Drop UC", () => {
    it("Get it UC", () => {
        const { appObjects, editLabelDropUC } = makeTestRig();
        expect(EditLabelDropUC_1.EditLabelDropUC.get(appObjects)).toEqual(editLabelDropUC);
    });
    it("Registers as the singleton", () => {
        const { editLabelDropUC, registerSpy } = makeTestRig();
        expect(registerSpy).toHaveBeenCalledWith(editLabelDropUC);
    });
    it("Sets the editing id when startEditing is called", () => {
        const { repo, editLabelDropUC, dropItem } = makeTestRig();
        expect(repo.editingID).toBeUndefined();
        editLabelDropUC.start(dropItem.id);
        expect(repo.editingID).toEqual(dropItem.id);
    });
    it("Sets isEditingNewAnswer to true when start is called with no id", () => {
        const { repo, editLabelDropUC } = makeTestRig();
        expect(repo.isEditingNewAnswer).toBe(false);
        editLabelDropUC.start();
        expect(repo.isEditingNewAnswer).toEqual(true);
    });
    it("Clears the editing id when startEditing is called with an ID that's not found", () => {
        const { repo, editLabelDropUC } = makeTestRig();
        repo.editingID = "some id";
        editLabelDropUC.start("bad id");
        expect(repo.editingID).toEqual(undefined);
    });
    it("Sets the isEditingNewAnswer to false when startEditing is called with isNewAnswer param equal to true but the ID isn't found", () => {
        const { repo, editLabelDropUC } = makeTestRig();
        repo.editingID = "some id";
        editLabelDropUC.start("bad id");
        expect(repo.isEditingNewAnswer).toEqual(false);
    });
    it("Discards label and correct id changes when discardEdits is called and confirm is clicked", () => {
        const { repo, editLabelDropUC, dropItem } = makeTestRig();
        editLabelDropUC.start(dropItem.id);
        dropItem.label = "new label";
        dropItem.addTargetID("obj2");
        editLabelDropUC.discard();
        expect(repo.editingID).toEqual(undefined);
        expect(dropItem.label).toEqual("Test Label");
        expect(dropItem.targetIds).toEqual(["obj1"]);
    });
    it("Sets isEditingNewAnswer to false after discardEdits is called and confirm is clicked", () => {
        const { repo, editLabelDropUC } = makeTestRig();
        editLabelDropUC.start();
        const newItem = repo.getForAppObject(repo.editingID);
        newItem.label = "new label";
        newItem.addTargetID("obj2");
        expect(repo.isEditingNewAnswer).toBe(true);
        editLabelDropUC.discard();
        expect(repo.editingID).toEqual(undefined);
        expect(repo.isEditingNewAnswer).toEqual(false);
    });
    it("Does not discard changes when discard is called and cancel is clicked", () => {
        const { repo, editLabelDropUC, dropItem, mockConfirm } = makeTestRig();
        mockConfirm.showConfirm = (dto) => {
            dto.cancelCallback();
        };
        editLabelDropUC.start(dropItem.id);
        dropItem.label = "new label";
        dropItem.addTargetID("obj2");
        editLabelDropUC.discard();
        expect(repo.editingID).toEqual(dropItem.id);
        expect(dropItem.label).toEqual("new label");
        expect(dropItem.targetIds).toEqual(["obj1", "obj2"]);
        expect(repo.isEditingNewAnswer).toEqual(false);
    });
    it("Keeps changes when finishEditing is called", () => {
        const { repo, editLabelDropUC, dropItem } = makeTestRig();
        editLabelDropUC.start(dropItem.id);
        dropItem.label = "new label";
        dropItem.addTargetID("obj2");
        editLabelDropUC.finish();
        expect(repo.editingID).toEqual(undefined);
        expect(dropItem.label).toEqual("new label");
        expect(dropItem.targetIds).toEqual(["obj1", "obj2"]);
        expect(repo.isEditingNewAnswer).toEqual(false);
    });
    it("Deletes a newly created label drop when discardEdits is called", () => {
        const { repo, editLabelDropUC } = makeTestRig();
        editLabelDropUC.start();
        const newItem = repo.getForAppObject(repo.editingID);
        expect(repo.hasForAppObject(newItem.id)).toEqual(true);
        expect(repo.isEditingNewAnswer).toEqual(true);
        editLabelDropUC.discard();
        expect(repo.hasForAppObject(newItem.id)).toEqual(false);
        expect(repo.isEditingNewAnswer).toEqual(false);
    });
    it("Dispatches a state change when finishEditing is called and there is a change", () => {
        const { editLabelDropUC, dropItem, doDispatchMock } = makeTestRig();
        editLabelDropUC.start(dropItem.id);
        dropItem.label = "new label";
        doDispatchMock.mockClear();
        editLabelDropUC.finish();
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Does not dispatch a state change when finishEditing is called and there is no change", () => {
        const { editLabelDropUC, dropItem, doDispatchMock } = makeTestRig();
        editLabelDropUC.start(dropItem.id);
        doDispatchMock.mockClear();
        editLabelDropUC.finish();
        expect(doDispatchMock).not.toHaveBeenCalled();
    });
    it("Dispatches a state change when finis is called on a newly created label drop answer", () => {
        const { editLabelDropUC, doDispatchMock } = makeTestRig();
        editLabelDropUC.start();
        doDispatchMock.mockClear();
        editLabelDropUC.finish();
        expect(doDispatchMock).toHaveBeenCalled();
    });
    it("Should not show confirmation dialog if there is no change", () => {
        const { editLabelDropUC, dropItem, mockConfirm } = makeTestRig();
        mockConfirm.showConfirm = jest.fn();
        editLabelDropUC.start(dropItem.id);
        editLabelDropUC.discard();
        expect(mockConfirm.showConfirm).not.toHaveBeenCalled();
    });
    it("Shows confirmation dialog when discards the new answer", () => {
        const { editLabelDropUC, mockConfirm } = makeTestRig();
        mockConfirm.showConfirm = jest.fn();
        editLabelDropUC.start();
        editLabelDropUC.discard();
        expect(mockConfirm.showConfirm).toHaveBeenCalled();
    });
    it("Shows confirmation dialog if the label was changed", () => {
        const { editLabelDropUC, dropItem, mockConfirm } = makeTestRig();
        mockConfirm.showConfirm = jest.fn();
        editLabelDropUC.start(dropItem.id);
        dropItem.label = "new label";
        editLabelDropUC.discard();
        expect(mockConfirm.showConfirm).toHaveBeenCalled();
    });
    it("Shows confirmation dialog if the correct object ids were changed", () => {
        const { editLabelDropUC, dropItem, mockConfirm } = makeTestRig();
        mockConfirm.showConfirm = jest.fn();
        editLabelDropUC.start(dropItem.id);
        dropItem.addTargetID("target2");
        editLabelDropUC.discard();
        expect(mockConfirm.showConfirm).toHaveBeenCalled();
    });
    it("Can undo and redo changes to existing", () => {
        const { undo, editLabelDropUC, dropItem, doDispatchMock } = makeTestRig();
        editLabelDropUC.start(dropItem.id);
        dropItem.label = "new label";
        dropItem.addTargetID("obj2");
        editLabelDropUC.finish();
        expect(dropItem.label).toEqual("new label");
        expect(dropItem.targetIds).toEqual(["obj1", "obj2"]);
        doDispatchMock.mockClear();
        undo.doUndo();
        expect(dropItem.label).toEqual("Test Label");
        expect(dropItem.targetIds).toEqual(["obj1"]);
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
        undo.doRedo();
        expect(dropItem.label).toEqual("new label");
        expect(dropItem.targetIds).toEqual(["obj1", "obj2"]);
        expect(doDispatchMock).toHaveBeenCalledTimes(2);
    });
    it("Can undo and redo changes to new item", () => {
        const { undo, editLabelDropUC, doDispatchMock, repo } = makeTestRig();
        editLabelDropUC.start();
        const newItem = repo.getForAppObject(repo.editingID);
        const newItemId = newItem.id;
        newItem.label = "A Label";
        newItem.addTargetID("obj1");
        editLabelDropUC.finish();
        expect(newItem.label).toEqual("A Label");
        expect(newItem.targetIds).toEqual(["obj1"]);
        expect(repo.hasForAppObject(newItemId)).toEqual(true);
        doDispatchMock.mockClear();
        undo.doUndo();
        expect(repo.hasForAppObject(newItemId)).toEqual(false);
        expect(doDispatchMock).toHaveBeenCalledTimes(1);
        undo.doRedo();
        expect(repo.hasForAppObject(newItemId)).toEqual(true);
        expect(repo.getForAppObject(newItemId)?.label).toEqual("A Label");
        expect(repo.getForAppObject(newItemId)?.targetIds).toEqual(["obj1"]);
        expect(doDispatchMock).toHaveBeenCalledTimes(2);
    });
});
//# sourceMappingURL=EditLabelDropUC.test.js.map