(function(context, $) {

    describe("JamServer", function() {
        var jamserver;

        beforeEach(function() {
            var opts = {
                layoutOpts: {
                    allowBodyOverflow: true
                }
            };
            jamserver = context.JK.JamServer;
        });

        describe("Event Subscription", function() {

            it("Subscribing to ping should call function", function() {
                var called = false;
                jamserver.registerMessageCallback(context.JK.MessageType.PING_ACK, function() {
                    called = true;
                });
                var msg = {type: context.JK.MessageType.PING_ACK};
                msg[context.JK.MessageType.PING_ACK] = {};
                var e = {data:JSON.stringify(msg)};
                jamserver.onMessage(e);
                expect(called).toBeTruthy();
            });

            it("All registerMessageCallbackrs should be called", function() {
                var callCount = 0;
                jamserver.registerMessageCallback(context.JK.MessageType.PING_ACK, function() {callCount += 1;});
                jamserver.registerMessageCallback(context.JK.MessageType.PING_ACK, function() {callCount += 2;});
                var msg = {type: context.JK.MessageType.PING_ACK};
                msg[context.JK.MessageType.PING_ACK] = {};
                var e = {data:JSON.stringify(msg)};
                jamserver.onMessage(e);
                expect(callCount).toEqual(3);
            });

            it("An error in a callback should be caught", function() {
                var callCount = 0;
                jamserver.registerMessageCallback(context.JK.MessageType.PING_ACK, function() {callCount += 1;});
                jamserver.registerMessageCallback(context.JK.MessageType.PING_ACK, function() {throw "Intentional Error";});
                jamserver.registerMessageCallback(context.JK.MessageType.PING_ACK, function() {callCount += 1;});
                var msg = {type: context.JK.MessageType.PING_ACK};
                msg[context.JK.MessageType.PING_ACK] = {};
                var e = {data:JSON.stringify(msg)};
                jamserver.onMessage(e);
                expect(callCount).toEqual(2);
            });

        });

    });


}(window, jQuery));