spec/javascripts/generated/specs/websocket_connection_spec.js in websocket-rails-0.6.2 vs spec/javascripts/generated/specs/websocket_connection_spec.js in websocket-rails-0.7.0

- old
+ new

@@ -1,33 +1,57 @@ (function() { describe('WebsocketRails.WebSocketConnection:', function() { + var SAMPLE_EVENT, SAMPLE_EVENT_DATA; + SAMPLE_EVENT_DATA = ['event', 'message']; + SAMPLE_EVENT = { + data: JSON.stringify(SAMPLE_EVENT_DATA) + }; beforeEach(function() { - var dispatcher; - dispatcher = { + var WebSocketStub; + this.dispatcher = { new_message: function() { return true; }, dispatch: function() { return true; }, state: 'connected' }; - window.WebSocket = function(url) { - this.url = url; - return this.send = function() { + window.WebSocket = WebSocketStub = (function() { + function WebSocketStub(url, dispatcher) { + this.url = url; + this.dispatcher = dispatcher; + } + + WebSocketStub.prototype.send = function() { return true; }; - }; - this.dispatcher = dispatcher; - return this.connection = new WebSocketRails.WebSocketConnection('localhost:3000/websocket', dispatcher); + + WebSocketStub.prototype.close = function() { + return this.onclose(null); + }; + + return WebSocketStub; + + })(); + this.connection = new WebSocketRails.WebSocketConnection('localhost:3000/websocket', this.dispatcher); + return this.dispatcher._conn = this.connection; }); describe('constructor', function() { - it('should set the onmessage event on the WebSocket object to this.on_message', function() { - return expect(this.connection._conn.onmessage).toEqual(this.connection.on_message); + it('should redirect onmessage events\' data from the WebSocket object to this.on_message', function() { + var mock_connection; + mock_connection = sinon.mock(this.connection); + mock_connection.expects('on_message').once().withArgs(SAMPLE_EVENT_DATA); + this.connection._conn.onmessage(SAMPLE_EVENT); + return mock_connection.verify(); }); - it('should set the onclose event on the WebSocket object to this.on_close', function() { - return expect(this.connection._conn.onclose).toEqual(this.connection.on_close); + it('should redirect onclose events from the WebSocket object to this.on_close', function() { + var mock_connection; + mock_connection = sinon.mock(this.connection); + mock_connection.expects('on_close').once().withArgs(SAMPLE_EVENT); + this.connection._conn.onclose(SAMPLE_EVENT); + return mock_connection.verify(); }); describe('with ssl', function() { return it('should not add the ws:// prefix to the URL', function() { var connection; connection = new WebSocketRails.WebSocketConnection('wss://localhost.com'); @@ -38,10 +62,16 @@ return it('should add the ws:// prefix to the URL', function() { return expect(this.connection.url).toEqual('ws://localhost:3000/websocket'); }); }); }); + describe('.close', function() { + return it('should close the connection', function() { + this.connection.close(); + return expect(this.dispatcher.state).toEqual('disconnected'); + }); + }); describe('.trigger', function() { describe('before the connection has been fully established', function() { return it('should queue up the events', function() { var event, mock_queue; this.connection.dispatcher.state = 'connecting'; @@ -67,18 +97,14 @@ }); }); }); describe('.on_message', function() { return it('should decode the message and pass it to the dispatcher', function() { - var encoded_data, event, mock_dispatcher; - encoded_data = JSON.stringify(['event', 'message']); - event = { - data: encoded_data - }; + var mock_dispatcher; mock_dispatcher = sinon.mock(this.connection.dispatcher); - mock_dispatcher.expects('new_message').once().withArgs(JSON.parse(encoded_data)); - this.connection.on_message(event); + mock_dispatcher.expects('new_message').once().withArgs(SAMPLE_EVENT_DATA); + this.connection.on_message(SAMPLE_EVENT_DATA); return mock_dispatcher.verify(); }); }); describe('.on_close', function() { it('should dispatch the connection_closed event and pass the original event', function() { @@ -116,9 +142,36 @@ return it('sets the connection state on the dispatcher to disconnected', function() { var close_event; close_event = new WebSocketRails.Event(['connection_closed', {}]); this.connection.on_error(close_event); return expect(this.dispatcher.state).toEqual('disconnected'); + }); + }); + describe("it's no longer active connection", function() { + beforeEach(function() { + this.new_connection = new WebSocketRails.WebSocketConnection('localhost:3000/websocket', this.dispatcher); + return this.dispatcher._conn = this.new_connection; + }); + it(".on_error should not react to the event response", function() { + var mock_dispatcher; + mock_dispatcher = sinon.mock(this.connection.dispatcher); + mock_dispatcher.expects('dispatch').never(); + this.connection.on_error(SAMPLE_EVENT_DATA); + return mock_dispatcher.verify(); + }); + it(".on_close should not react to the event response", function() { + var mock_dispatcher; + mock_dispatcher = sinon.mock(this.connection.dispatcher); + mock_dispatcher.expects('dispatch').never(); + this.connection.on_close(SAMPLE_EVENT_DATA); + return mock_dispatcher.verify(); + }); + return it(".on_message should not react to the event response", function() { + var mock_dispatcher; + mock_dispatcher = sinon.mock(this.connection.dispatcher); + mock_dispatcher.expects('new_message').never(); + this.connection.on_message(SAMPLE_EVENT_DATA); + return mock_dispatcher.verify(); }); }); return describe('.flush_queue', function() { beforeEach(function() { this.event = new WebSocketRails.Event(['event', 'message']);