import { makeAppObjectRepo } from "@vived/core";
import { makeTouchInput, TouchInputEntity, } from "../Entities/TouchInput";
class MockController {
    constructor() {
        this.onTouchStart = jest.fn();
        this.onTouchMove = jest.fn();
        this.onTouchEnd = jest.fn();
        this.onTouchCancel = jest.fn();
        this.onTap = jest.fn();
    }
}
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const ao = appObjects.getOrCreate("App");
    const touchInput = makeTouchInput(ao);
    const mockController = new MockController();
    touchInput.registerController(mockController);
    const element = document.createElement("div");
    touchInput.setEventElement(element);
    return { touchInput, mockController, element, appObjects };
}
describe("Touch input", () => {
    it("Sets the element", () => {
        const { touchInput, element } = makeTestRig();
        expect(touchInput.eventElement).toEqual(element);
    });
    it("Clears the element", () => {
        const { touchInput, element } = makeTestRig();
        expect(touchInput.eventElement).toEqual(element);
        touchInput.clearInputElement();
        expect(touchInput.eventElement).toBeUndefined();
    });
    it("Allows a controller to be removed", () => {
        const { element, mockController, touchInput } = makeTestRig();
        touchInput.removeController(mockController);
        const e = new TouchEvent("touchstart");
        element.dispatchEvent(e);
        expect(mockController.onTouchStart).not.toHaveBeenCalled();
    });
    it("Gets the entity", () => {
        const { touchInput, appObjects } = makeTestRig();
        expect(TouchInputEntity.get(appObjects)).toEqual(touchInput);
    });
    it("Warns if it cannot find the entity", () => {
        const { touchInput, appObjects } = makeTestRig();
        console.warn = jest.fn();
        touchInput.dispose();
        expect(TouchInputEntity.get(appObjects)).toEqual(undefined);
        expect(console.warn).toHaveBeenCalled();
    });
    it("Warns if it finds multiple entity", () => {
        const { touchInput, appObjects } = makeTestRig();
        const anotherAO = appObjects.getOrCreate("anotherAppObject");
        makeTouchInput(anotherAO);
        console.warn = jest.fn();
        expect(TouchInputEntity.get(appObjects)).toEqual(touchInput);
        expect(console.warn).toHaveBeenCalled();
    });
});
describe("Touch input - On touch start", () => {
    it("Notifies controller with the event", () => {
        const { element, mockController } = makeTestRig();
        const e = new TouchEvent("touchstart");
        element.dispatchEvent(e);
        const touchInputEvent = mockController.onTouchStart.mock
            .calls[0][0];
        expect(touchInputEvent.event).toEqual(e);
    });
    it("Removes event listener when the element is cleared", () => {
        const { element, mockController, touchInput } = makeTestRig();
        touchInput.clearInputElement();
        const e = new TouchEvent("touchstart");
        element.dispatchEvent(e);
        expect(mockController.onTouchStart).not.toHaveBeenCalled();
    });
    it("Removes event listener when the element is changed", () => {
        const { element, mockController, touchInput } = makeTestRig();
        const newElement = document.createElement("div");
        touchInput.setEventElement(newElement);
        const e = new TouchEvent("touchstart");
        element.dispatchEvent(e);
        expect(mockController.onTouchStart).not.toHaveBeenCalled();
    });
});
describe("Touch input - On touch move", () => {
    it("Notifies controller with the event", () => {
        const { element, mockController } = makeTestRig();
        const e = new TouchEvent("touchmove");
        element.dispatchEvent(e);
        const touchInputEvent = mockController.onTouchMove.mock
            .calls[0][0];
        expect(touchInputEvent.event).toEqual(e);
    });
    it("Removes event listener when the element is cleared", () => {
        const { element, mockController, touchInput } = makeTestRig();
        touchInput.clearInputElement();
        const e = new TouchEvent("touchmove");
        element.dispatchEvent(e);
        expect(mockController.onTouchMove).not.toHaveBeenCalled();
    });
    it("Removes event listener when the element is changed", () => {
        const { element, mockController, touchInput } = makeTestRig();
        const newElement = document.createElement("div");
        touchInput.setEventElement(newElement);
        const e = new TouchEvent("touchmove");
        element.dispatchEvent(e);
        expect(mockController.onTouchMove).not.toHaveBeenCalled();
    });
});
describe("Touch input - On touch end", () => {
    it("Notifies controller with the event", () => {
        const { element, mockController } = makeTestRig();
        const e = new TouchEvent("touchend");
        element.dispatchEvent(e);
        const touchInputEvent = mockController.onTouchEnd.mock
            .calls[0][0];
        expect(touchInputEvent.event).toEqual(e);
    });
    it("Removes event listener when the element is cleared", () => {
        const { element, mockController, touchInput } = makeTestRig();
        touchInput.clearInputElement();
        const e = new TouchEvent("touchend");
        element.dispatchEvent(e);
        expect(mockController.onTouchEnd).not.toHaveBeenCalled();
    });
    it("Removes event listener when the element is changed", () => {
        const { element, mockController, touchInput } = makeTestRig();
        const newElement = document.createElement("div");
        touchInput.setEventElement(newElement);
        const e = new TouchEvent("touchend");
        element.dispatchEvent(e);
        expect(mockController.onTouchEnd).not.toHaveBeenCalled();
    });
});
describe("Touch input - On touch cancel", () => {
    it("Notifies controller with the event", () => {
        const { element, mockController } = makeTestRig();
        const e = new TouchEvent("touchcancel");
        element.dispatchEvent(e);
        const touchInputEvent = mockController.onTouchCancel.mock
            .calls[0][0];
        expect(touchInputEvent.event).toEqual(e);
    });
    it("Removes event listener when the element is cleared", () => {
        const { element, mockController, touchInput } = makeTestRig();
        touchInput.clearInputElement();
        const e = new TouchEvent("touchcancel");
        element.dispatchEvent(e);
        expect(mockController.onTouchCancel).not.toHaveBeenCalled();
    });
    it("Removes event listener when the element is changed", () => {
        const { element, mockController, touchInput } = makeTestRig();
        const newElement = document.createElement("div");
        touchInput.setEventElement(newElement);
        const e = new TouchEvent("touchcancel");
        element.dispatchEvent(e);
        expect(mockController.onTouchCancel).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=TouchInput.test.js.map