import { makeAppObjectRepo } from "@vived/core";
import { KeyboardInputEntity, makeKeyboardInput, } from "./KeyboardInput";
class MockController {
    constructor() {
        this.onKeyDown = jest.fn();
        this.onKeyUp = jest.fn();
    }
}
function makeTestRig() {
    const appObjects = makeAppObjectRepo();
    const ao = appObjects.getOrCreate("App");
    const input = makeKeyboardInput(ao);
    const element = document.createElement("div");
    input.setEventElement(element);
    const mockController = new MockController();
    input.registerController(mockController);
    return { input, mockController, element, appObjects };
}
test("Submitting a key down should notify", () => {
    const { mockController, element } = makeTestRig();
    const downEvent = new KeyboardEvent("keydown", { code: "key" });
    element.dispatchEvent(downEvent);
    expect(mockController.onKeyDown).toHaveBeenCalledWith(downEvent);
});
test("Submitting a key up should notify", () => {
    const { element, mockController } = makeTestRig();
    const downEvent = new KeyboardEvent("keydown", { code: "key" });
    element.dispatchEvent(downEvent);
    const upEvent = new KeyboardEvent("keyup", { code: "key" });
    element.dispatchEvent(upEvent);
    expect(mockController.onKeyUp).toHaveBeenCalledWith(upEvent);
});
test("Removing a controller", () => {
    const { input, mockController, element } = makeTestRig();
    input.removeController(mockController);
    const downEvent = new KeyboardEvent("keydown", { code: "key" });
    element.dispatchEvent(downEvent);
    const upEvent = new KeyboardEvent("keyup", { code: "key" });
    element.dispatchEvent(upEvent);
    expect(mockController.onKeyDown).not.toHaveBeenCalled();
    expect(mockController.onKeyUp).not.toHaveBeenCalled();
});
test("If an element is cleared its events should be cancelled", () => {
    const { input, mockController, element } = makeTestRig();
    input.clearInputElement();
    const downEvent = new KeyboardEvent("keydown", { code: "key" });
    element.dispatchEvent(downEvent);
    const upEvent = new KeyboardEvent("keyup", { code: "key" });
    element.dispatchEvent(upEvent);
    expect(mockController.onKeyDown).not.toHaveBeenCalled();
    expect(mockController.onKeyUp).not.toHaveBeenCalled();
});
test("If an element is replaced the original events should be cancelled", () => {
    const { input, mockController, element } = makeTestRig();
    const newElement = document.createElement("div");
    input.setEventElement(newElement);
    const downEvent = new KeyboardEvent("keydown", { code: "key" });
    element.dispatchEvent(downEvent);
    const upEvent = new KeyboardEvent("keyup", { code: "key" });
    element.dispatchEvent(upEvent);
    expect(mockController.onKeyDown).not.toHaveBeenCalled();
    expect(mockController.onKeyUp).not.toHaveBeenCalled();
});
test("Clearing the element", () => {
    const { input } = makeTestRig();
    expect(input.eventElement).not.toBeUndefined();
    input.clearInputElement();
    expect(input.eventElement).toBeUndefined();
});
test("Should add the key codes when Submitting a key down", () => {
    const { input, element } = makeTestRig();
    const downEvent1 = new KeyboardEvent("keydown", { code: "key1" });
    element.dispatchEvent(downEvent1);
    const downEvent2 = new KeyboardEvent("keydown", { code: "key2" });
    element.dispatchEvent(downEvent2);
    expect(input.isKeyPressed("key1")).toBeTruthy();
    expect(input.isKeyPressed("key2")).toBeTruthy();
});
test("Should remove the key codes when Submitting a key up", () => {
    const { input, element } = makeTestRig();
    const downEvent1 = new KeyboardEvent("keydown", { code: "key1" });
    element.dispatchEvent(downEvent1);
    const downEvent2 = new KeyboardEvent("keydown", { code: "key2" });
    element.dispatchEvent(downEvent2);
    expect(input.isKeyPressed("key1")).toBeTruthy();
    expect(input.isKeyPressed("key2")).toBeTruthy();
    const upEvent1 = new KeyboardEvent("keyup", { code: "key1" });
    element.dispatchEvent(upEvent1);
    const upEvent2 = new KeyboardEvent("keyup", { code: "key2" });
    element.dispatchEvent(upEvent2);
    expect(input.isKeyPressed("key1")).toBeFalsy();
    expect(input.isKeyPressed("key2")).toBeFalsy();
});
test("Returns the presed key status", () => {
    const { input, element } = makeTestRig();
    const downEvent = new KeyboardEvent("keydown", { code: "key" });
    element.dispatchEvent(downEvent);
    expect(input.isKeyPressed("key")).toBeTruthy();
    const upEvent = new KeyboardEvent("keyup", { code: "key" });
    element.dispatchEvent(upEvent);
    expect(input.isKeyPressed("key")).toBeFalsy();
});
it("Gets the entity", () => {
    const { input, appObjects } = makeTestRig();
    expect(KeyboardInputEntity.get(appObjects)).toEqual(input);
});
it("Warns if it cannot find the entity", () => {
    const { input, appObjects } = makeTestRig();
    console.warn = jest.fn();
    input.dispose();
    expect(KeyboardInputEntity.get(appObjects)).toEqual(undefined);
    expect(console.warn).toHaveBeenCalled();
});
it("Warns if it finds multiple entity", () => {
    const { input, appObjects } = makeTestRig();
    const anotherAO = appObjects.getOrCreate("anotherAppObject");
    makeKeyboardInput(anotherAO);
    console.warn = jest.fn();
    expect(KeyboardInputEntity.get(appObjects)).toEqual(input);
    expect(console.warn).toHaveBeenCalled();
});
//# sourceMappingURL=KeyboardInput.test.js.map