"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("../../../src/events/index.js");
const RoomstayEvent_1 = require("../../../src/events/RoomstayEvent");
const RoomstayEventManager_1 = __importDefault(require("../../../src/events/RoomstayEventManager"));
const BasketRow_1 = __importDefault(require("../../../src/models/BasketRow"));
describe('Generic event manager tests', () => {
    describe('Adding new events', () => {
        it('Successfully adds a new event listener', () => {
            const addResponse = RoomstayEventManager_1.default.addEvent(RoomstayEvent_1.RoomstayEvent, () => __awaiter(void 0, void 0, void 0, function* () { return true; }));
            expect(addResponse).toBe(true);
        });
        it('Fails to add a new event listener without a callback', () => {
            //@ts-ignore
            const addResponse = RoomstayEventManager_1.default.addEvent('');
            expect(addResponse).toBe(false);
        });
        it('Fails to add a new event listener that is not callable', () => {
            //@ts-ignore
            const addResponse = RoomstayEventManager_1.default.addEvent('', 'things');
            console.log(events_1.StepCheckoutViewEvent.getLabel());
            expect(addResponse).toBe(false);
        });
        it('Is possible to add an event listener by name, or class', () => __awaiter(void 0, void 0, void 0, function* () {
            const callbackForClass = jest.fn((event) => __awaiter(void 0, void 0, void 0, function* () {
                return true;
            }));
            const callbackForString = jest.fn((event) => __awaiter(void 0, void 0, void 0, function* () {
                return true;
            }));
            RoomstayEventManager_1.default.addEvent(events_1.AddToCartEvent, callbackForClass);
            RoomstayEventManager_1.default.addEvent('Basket/Add', callbackForString);
            const basketAddEvent = new events_1.AddToCartEvent(new BasketRow_1.default(), []);
            yield RoomstayEventManager_1.default.raise(basketAddEvent);
            expect(callbackForClass).toHaveBeenCalled();
            expect(callbackForString).toHaveBeenCalled();
        }));
        it('Does not crash the rest of the app if one of the event listeners dies', () => __awaiter(void 0, void 0, void 0, function* () {
            const callback = () => __awaiter(void 0, void 0, void 0, function* () {
                throw new Error('Deliberate failure!');
            });
            RoomstayEventManager_1.default.addEvent(RoomstayEvent_1.RoomstayEvent, callback);
            const event = new RoomstayEvent_1.RoomstayEvent();
            expect((() => __awaiter(void 0, void 0, void 0, function* () {
                yield RoomstayEventManager_1.default.raise(event);
            }))()).resolves.not.toThrowError();
        }));
    });
    describe('Raising events', () => {
        it('Successfully calls a raised event', () => __awaiter(void 0, void 0, void 0, function* () {
            const callback = jest.fn((event) => __awaiter(void 0, void 0, void 0, function* () {
                return true;
            }));
            RoomstayEventManager_1.default.addEvent(RoomstayEvent_1.RoomstayEvent, callback);
            const newEvent = new RoomstayEvent_1.RoomstayEvent();
            yield RoomstayEventManager_1.default.raise(newEvent);
            expect(callback).toHaveBeenCalled();
        }));
        it('Successfully calls all raised events, not just the first', () => __awaiter(void 0, void 0, void 0, function* () {
            const callback = jest.fn((event) => __awaiter(void 0, void 0, void 0, function* () {
                return true;
            }));
            const callback2 = jest.fn((event) => __awaiter(void 0, void 0, void 0, function* () {
                return true;
            }));
            RoomstayEventManager_1.default.addEvent(RoomstayEvent_1.RoomstayEvent, callback);
            RoomstayEventManager_1.default.addEvent(RoomstayEvent_1.RoomstayEvent, callback2);
            const newEvent = new RoomstayEvent_1.RoomstayEvent();
            yield RoomstayEventManager_1.default.raise(newEvent);
            expect(callback).toHaveBeenCalled();
            expect(callback2).toHaveBeenCalled();
        }));
        it('Throws an exception if an improper object is passed to raise event function', () => __awaiter(void 0, void 0, void 0, function* () {
            const raiseResponseNoPrototype = RoomstayEventManager_1.default.raise('');
            expect(raiseResponseNoPrototype).rejects.toThrowError();
            const raiseResponseNoGetLabel = RoomstayEventManager_1.default.raise({});
            expect(raiseResponseNoGetLabel).rejects.toThrowError();
        }));
        it("Does not add event listener, if it's not possible to get a label", () => __awaiter(void 0, void 0, void 0, function* () {
            //@ts-ignore
            const addResponse = RoomstayEventManager_1.default.addEvent({}, () => { });
            expect(addResponse).toBeFalsy();
            //@ts-ignore
            const undefinedAddResponse = RoomstayEventManager_1.default.addEvent(undefined, () => { });
            expect(undefinedAddResponse).toBeFalsy();
        }));
    });
});
//# sourceMappingURL=EventManager.test.js.map