import { makeAppObjectRepo } from "@vived/core";
import { makeUndoRedoEntity, submitUndoRedoAction, } from "./UndoRedoEntity";
class MockAction {
    constructor() {
        this.undoAction = jest.fn();
        this.redoAction = jest.fn();
    }
}
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const ao = appObjects.getOrCreate("App");
    const undoRedo = makeUndoRedoEntity(ao);
    const observer = jest.fn();
    undoRedo.addChangeObserver(observer);
    return { undoRedo, observer, appObjects };
}
test("Stack starts out empty", () => {
    const { undoRedo } = makeTestRig();
    expect(undoRedo.hasRedo).toEqual(false);
    expect(undoRedo.hasUndo).toEqual(false);
});
test("Submitting an action", () => {
    const { undoRedo, observer } = makeTestRig();
    observer.mockClear();
    undoRedo.submitAction(new MockAction());
    expect(observer).toHaveBeenCalled();
    expect(undoRedo.hasRedo).toEqual(false);
    expect(undoRedo.hasUndo).toEqual(true);
});
test("Doing an undo", () => {
    const { undoRedo, observer } = makeTestRig();
    const mockAction = new MockAction();
    undoRedo.submitAction(mockAction);
    observer.mockClear();
    undoRedo.doUndo();
    expect(observer).toHaveBeenCalled();
    expect(undoRedo.hasRedo).toEqual(true);
    expect(undoRedo.hasUndo).toEqual(false);
    expect(mockAction.undoAction).toHaveBeenCalled();
});
test("Doing a redo", () => {
    const { undoRedo, observer } = makeTestRig();
    const mockAction = new MockAction();
    undoRedo.submitAction(mockAction);
    undoRedo.doUndo();
    observer.mockClear();
    undoRedo.doRedo();
    expect(observer).toHaveBeenCalled();
    expect(undoRedo.hasRedo).toEqual(false);
    expect(undoRedo.hasUndo).toEqual(true);
    expect(mockAction.redoAction).toHaveBeenCalled();
});
test("Submitting a new action clears the redo stack", () => {
    const { undoRedo } = makeTestRig();
    undoRedo.submitAction(new MockAction());
    undoRedo.submitAction(new MockAction());
    undoRedo.doUndo();
    expect(undoRedo.hasRedo).toEqual(true);
    undoRedo.submitAction(new MockAction());
    expect(undoRedo.hasRedo).toEqual(false);
});
test("Clearing the stack", () => {
    const { undoRedo, observer } = makeTestRig();
    undoRedo.submitAction(new MockAction());
    undoRedo.submitAction(new MockAction());
    undoRedo.doUndo();
    expect(undoRedo.hasRedo).toEqual(true);
    expect(undoRedo.hasUndo).toEqual(true);
    observer.mockClear();
    undoRedo.clearStack();
    expect(undoRedo.hasRedo).toEqual(false);
    expect(undoRedo.hasUndo).toEqual(false);
    expect(observer).toHaveBeenCalled();
});
test("Removing an observer", () => {
    const { undoRedo, observer } = makeTestRig();
    observer.mockClear();
    undoRedo.removeChangeObserver(observer);
    undoRedo.submitAction(new MockAction());
    undoRedo.submitAction(new MockAction());
    undoRedo.doUndo();
    undoRedo.clearStack();
    expect(observer).not.toHaveBeenCalled();
});
test("Calling undo when there is nothing to undo should warn", () => {
    const { undoRedo } = makeTestRig();
    undoRedo.warn = jest.fn();
    undoRedo.doUndo();
    expect(undoRedo.warn).toHaveBeenCalled();
});
test("Calling redo when there is nothing to undo should warn", () => {
    const { undoRedo } = makeTestRig();
    undoRedo.warn = jest.fn();
    undoRedo.doRedo();
    expect(undoRedo.warn).toHaveBeenCalled();
});
test("Finding and submitting to the UC", () => {
    const { appObjects, undoRedo } = makeTestRig();
    const mockAction = new MockAction();
    undoRedo.submitAction = jest.fn();
    submitUndoRedoAction(mockAction, appObjects);
    expect(undoRedo.submitAction).toHaveBeenCalledWith(mockAction);
});
//# sourceMappingURL=UndoRedoEntity.test.js.map