"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const AppHandler_1 = require("./AppHandler");
class MockHandler {
    constructor() {
        this.requestType = "A_REQUEST";
        this.payloadVersion = 1;
        this.handleRequest = jest.fn();
    }
}
function makeTestRig() {
    const appObjectRepo = (0, core_1.makeAppObjectRepo)();
    const handlerAO = appObjectRepo.getOrCreate("Handler");
    const appHandler = (0, AppHandler_1.makeAppHandler)(handlerAO);
    const mockRequestHandler = new MockHandler();
    appHandler.registerRequestHandler(mockRequestHandler);
    return { appHandler, mockRequestHandler };
}
describe("App Handler", () => {
    it("Warns if multiple request handlers are added for the same type", () => {
        const { appHandler } = makeTestRig();
        console.warn = jest.fn();
        const anotherHandler = new MockHandler();
        appHandler.registerRequestHandler(anotherHandler);
        expect(console.warn).toHaveBeenCalled();
    });
    it("Triggers the handler when a request received", () => {
        const { appHandler, mockRequestHandler } = makeTestRig();
        const request = {
            type: "A_REQUEST",
            version: 2,
            payload: { foo: "bar" },
        };
        appHandler.handler(request);
        expect(mockRequestHandler.handleRequest).toHaveBeenCalledWith(2, {
            foo: "bar",
        });
    });
    it("Warns if a request type has not been registered", () => {
        const { appHandler } = makeTestRig();
        console.warn = jest.fn();
        const request = {
            type: "A_DIFFERENT_REQUEST",
            version: 2,
            payload: { foo: "bar" },
        };
        appHandler.handler(request);
        expect(console.warn).toHaveBeenCalled();
    });
    it("Consoles a warn if the the handler throw an error", () => {
        const { appHandler, mockRequestHandler } = makeTestRig();
        console.warn = jest.fn();
        const request = {
            type: "A_REQUEST",
            version: 2,
            payload: { foo: "bar" },
        };
        mockRequestHandler.handleRequest.mockImplementation(() => {
            throw new AppHandler_1.UnsupportedRequestVersion("A_REQUEST", 2);
        });
        appHandler.handler(request);
        expect(console.warn).toHaveBeenCalled();
    });
    it("Handles the GET_APP_PAYLOAD_VERSION request", (done) => {
        const { appHandler } = makeTestRig();
        const expectedResponse = new Map([["A_REQUEST", 1]]);
        function callback(payloadVersions) {
            try {
                expect(payloadVersions).toEqual(expectedResponse);
                done();
            }
            catch (e) {
                done(e);
            }
        }
        const request = {
            type: "GET_APP_PAYLOAD_VERSION",
            version: 2,
            payload: { callback },
        };
        appHandler.handler(request);
    });
    it("Throws for an unsupported version of the get payload version request", () => {
        const { appHandler } = makeTestRig();
        const request = {
            type: "GET_APP_PAYLOAD_VERSION",
            version: 1,
            payload: { callback: () => { } },
        };
        expect(() => appHandler.handler(request)).toThrowError();
    });
    it("Throws for a bungled payload of the get payload version request", () => {
        const { appHandler } = makeTestRig();
        const request = {
            type: "GET_APP_PAYLOAD_VERSION",
            version: 2,
            payload: { foo: "bar" },
        };
        expect(() => appHandler.handler(request)).toThrowError();
    });
    it("Handles the GET_APP_HANDLER_VERSION request", (done) => {
        const { appHandler } = makeTestRig();
        function callback(appHandlerVersion) {
            try {
                expect(appHandlerVersion).toEqual(1);
                done();
            }
            catch (e) {
                done(e);
            }
        }
        const request = {
            type: "GET_APP_HANDLER_VERSION",
            version: 1,
            payload: { callback },
        };
        appHandler.handler(request);
    });
    it("Throws for an unsupported version of the get payload version request", () => {
        const { appHandler } = makeTestRig();
        const request = {
            type: "GET_APP_HANDLER_VERSION",
            version: 0,
            payload: { callback: () => { } },
        };
        expect(() => appHandler.handler(request)).toThrowError();
    });
    it("Throws for a bungled payload of the get payload version request", () => {
        const { appHandler } = makeTestRig();
        const request = {
            type: "GET_APP_HANDLER_VERSION",
            version: 1,
            payload: { foo: "bar" },
        };
        expect(() => appHandler.handler(request)).toThrowError();
    });
});
//# sourceMappingURL=AppHandler.test.js.map