websocket-rails 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +32 -0
- data/Gemfile +2 -1
- data/README.md +29 -34
- data/lib/assets/javascripts/websocket_rails/abstract_connection.js.coffee +45 -0
- data/lib/assets/javascripts/websocket_rails/channel.js.coffee +34 -17
- data/lib/assets/javascripts/websocket_rails/event.js.coffee +13 -11
- data/lib/assets/javascripts/websocket_rails/http_connection.js.coffee +44 -45
- data/lib/assets/javascripts/websocket_rails/main.js +1 -0
- data/lib/assets/javascripts/websocket_rails/websocket_connection.js.coffee +20 -34
- data/lib/assets/javascripts/websocket_rails/websocket_rails.js.coffee +60 -15
- data/lib/generators/websocket_rails/install/templates/websocket_rails.rb +15 -0
- data/lib/rails/config/routes.rb +1 -1
- data/lib/rails/tasks/websocket_rails.tasks +6 -2
- data/lib/websocket_rails/channel.rb +28 -2
- data/lib/websocket_rails/channel_manager.rb +16 -0
- data/lib/websocket_rails/configuration.rb +26 -1
- data/lib/websocket_rails/connection_adapters/http.rb +7 -0
- data/lib/websocket_rails/connection_adapters/web_socket.rb +3 -1
- data/lib/websocket_rails/connection_manager.rb +1 -1
- data/lib/websocket_rails/controller_factory.rb +1 -1
- data/lib/websocket_rails/event.rb +9 -2
- data/lib/websocket_rails/logging.rb +0 -1
- data/lib/websocket_rails/synchronization.rb +11 -7
- data/lib/websocket_rails/version.rb +1 -1
- data/spec/javascripts/generated/assets/abstract_connection.js +71 -0
- data/spec/javascripts/generated/assets/channel.js +58 -34
- data/spec/javascripts/generated/assets/event.js +12 -16
- data/spec/javascripts/generated/assets/http_connection.js +67 -65
- data/spec/javascripts/generated/assets/websocket_connection.js +36 -51
- data/spec/javascripts/generated/assets/websocket_rails.js +68 -21
- data/spec/javascripts/generated/specs/channel_spec.js +102 -19
- data/spec/javascripts/generated/specs/helpers.js +17 -0
- data/spec/javascripts/generated/specs/websocket_connection_spec.js +72 -19
- data/spec/javascripts/generated/specs/websocket_rails_spec.js +146 -47
- data/spec/javascripts/support/jasmine.yml +10 -2
- data/spec/javascripts/support/jasmine_helper.rb +38 -0
- data/spec/javascripts/websocket_rails/channel_spec.coffee +66 -12
- data/spec/javascripts/websocket_rails/event_spec.coffee +7 -7
- data/spec/javascripts/websocket_rails/helpers.coffee +6 -0
- data/spec/javascripts/websocket_rails/websocket_connection_spec.coffee +53 -15
- data/spec/javascripts/websocket_rails/websocket_rails_spec.coffee +108 -25
- data/spec/unit/base_controller_spec.rb +41 -0
- data/spec/unit/channel_manager_spec.rb +21 -0
- data/spec/unit/channel_spec.rb +43 -3
- data/spec/unit/connection_adapters/http_spec.rb +24 -3
- data/spec/unit/connection_adapters_spec.rb +2 -2
- data/spec/unit/connection_manager_spec.rb +1 -1
- data/spec/unit/event_spec.rb +25 -1
- data/spec/unit/logging_spec.rb +1 -1
- metadata +57 -67
- data/spec/javascripts/support/jasmine_config.rb +0 -63
@@ -1,20 +1,64 @@
|
|
1
1
|
describe 'WebSocketRails.Channel:', ->
|
2
2
|
beforeEach ->
|
3
|
-
@dispatcher =
|
3
|
+
@dispatcher = new class WebSocketRailsStub
|
4
4
|
new_message: -> true
|
5
5
|
dispatch: -> true
|
6
6
|
trigger_event: (event) -> true
|
7
7
|
state: 'connected'
|
8
|
-
|
9
|
-
|
8
|
+
_conn:
|
9
|
+
connection_id: 12345
|
10
|
+
@channel = new WebSocketRails.Channel('public', @dispatcher)
|
10
11
|
sinon.spy @dispatcher, 'trigger_event'
|
11
12
|
|
12
13
|
afterEach ->
|
13
14
|
@dispatcher.trigger_event.restore()
|
14
15
|
|
16
|
+
describe '.bind', ->
|
17
|
+
it 'should add a function to the callbacks collection', ->
|
18
|
+
test_func = ->
|
19
|
+
@channel.bind 'event_name', test_func
|
20
|
+
expect(@channel._callbacks['event_name'].length).toBe 1
|
21
|
+
expect(@channel._callbacks['event_name']).toContain test_func
|
22
|
+
|
23
|
+
describe '.trigger', ->
|
24
|
+
describe 'before the channel token is set', ->
|
25
|
+
it 'queues the events', ->
|
26
|
+
@channel.trigger 'someEvent', 'someData'
|
27
|
+
queue = @channel._queue
|
28
|
+
expect(queue[0].name).toEqual 'someEvent'
|
29
|
+
expect(queue[0].data).toEqual 'someData'
|
30
|
+
|
31
|
+
describe 'when channel token is set', ->
|
32
|
+
it 'adds token to event metadata and dispatches event', ->
|
33
|
+
@channel._token = 'valid token'
|
34
|
+
@channel.trigger 'someEvent', 'someData'
|
35
|
+
expect(@dispatcher.trigger_event.calledWith(['someEvent',{token: 'valid token', data: 'someData'}]))
|
36
|
+
|
37
|
+
describe '.destroy', ->
|
38
|
+
it 'should destroy all callbacks', ->
|
39
|
+
event_callback = -> true
|
40
|
+
@channel.bind('new_message', @event_callback)
|
41
|
+
|
42
|
+
@channel.destroy()
|
43
|
+
|
44
|
+
expect(@channel._callbacks).toEqual {}
|
45
|
+
|
46
|
+
describe 'when this channel\'s connection is still active', ->
|
47
|
+
it 'should send unsubscribe event', ->
|
48
|
+
@channel.destroy()
|
49
|
+
expect(@dispatcher.trigger_event.args[0][0].name).toEqual 'websocket_rails.unsubscribe'
|
50
|
+
|
51
|
+
describe 'when this channel\'s connection is no more active', ->
|
52
|
+
beforeEach ->
|
53
|
+
@dispatcher._conn.connection_id++
|
54
|
+
|
55
|
+
it 'should not send unsubscribe event', ->
|
56
|
+
@channel.destroy()
|
57
|
+
expect(@dispatcher.trigger_event.notCalled).toEqual true
|
58
|
+
|
15
59
|
describe 'public channels', ->
|
16
60
|
beforeEach ->
|
17
|
-
@channel = new WebSocketRails.Channel('forchan'
|
61
|
+
@channel = new WebSocketRails.Channel('forchan', @dispatcher, false)
|
18
62
|
@event = @dispatcher.trigger_event.lastCall.args[0]
|
19
63
|
|
20
64
|
it 'should trigger an event containing the channel name', ->
|
@@ -30,22 +74,32 @@ describe 'WebSocketRails.Channel:', ->
|
|
30
74
|
it 'should be public', ->
|
31
75
|
expect(@channel.is_private).toBeFalsy
|
32
76
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
77
|
+
describe 'channel tokens', ->
|
78
|
+
it 'should set token when event_name is websocket_rails.channel_token', ->
|
79
|
+
@channel.dispatch('websocket_rails.channel_token', {token: 'abc123'})
|
80
|
+
expect(@channel._token).toEqual 'abc123'
|
81
|
+
|
82
|
+
it "should refresh channel's connection_id after channel_token has been received", ->
|
83
|
+
# this is needed in case we would init the channel connection
|
84
|
+
# just before the connection has been established
|
85
|
+
@channel.connection_id = null
|
86
|
+
@channel.dispatch('websocket_rails.channel_token', {token: 'abc123'})
|
87
|
+
expect(@channel.connection_id).toEqual @dispatcher._conn.connection_id
|
88
|
+
|
89
|
+
it 'should flush the event queue after setting token', ->
|
90
|
+
@channel.trigger 'someEvent', 'someData'
|
91
|
+
@channel.dispatch('websocket_rails.channel_token', {token: 'abc123'})
|
92
|
+
expect(@channel._queue.length).toEqual(0)
|
39
93
|
|
40
94
|
describe 'private channels', ->
|
41
95
|
beforeEach ->
|
42
|
-
@channel = new WebSocketRails.Channel('forchan'
|
96
|
+
@channel = new WebSocketRails.Channel('forchan', @dispatcher, true)
|
43
97
|
@event = @dispatcher.trigger_event.lastCall.args[0]
|
44
98
|
|
45
99
|
it 'should trigger a subscribe_private event when created', ->
|
46
100
|
expect(@event.name).toEqual 'websocket_rails.subscribe_private'
|
47
101
|
|
48
102
|
it 'should be private', ->
|
49
|
-
expect(@channel.is_private).
|
103
|
+
expect(@channel.is_private).toBeTruthy
|
50
104
|
|
51
105
|
|
@@ -2,7 +2,7 @@ describe 'WebSocketRails.Event', ->
|
|
2
2
|
|
3
3
|
describe 'standard events', ->
|
4
4
|
beforeEach ->
|
5
|
-
@data = ['event',{data: { message: 'test'} },12345]
|
5
|
+
@data = ['event', {data: { message: 'test'} }, 12345]
|
6
6
|
@event = new WebSocketRails.Event(@data)
|
7
7
|
|
8
8
|
it 'should generate an ID', ->
|
@@ -51,19 +51,19 @@ describe 'WebSocketRails.Event', ->
|
|
51
51
|
data
|
52
52
|
failure_func = (data) ->
|
53
53
|
data
|
54
|
-
@data = ['event',{data: { message: 'test'} },12345]
|
55
|
-
@event = new WebSocketRails.Event(@data,success_func,failure_func)
|
54
|
+
@data = ['event', {data: { message: 'test'} }, 12345]
|
55
|
+
@event = new WebSocketRails.Event(@data, success_func, failure_func)
|
56
56
|
|
57
57
|
describe 'when successful', ->
|
58
58
|
it 'should run the success callback when passed true', ->
|
59
|
-
expect(@event.run_callbacks(true,'success')).toEqual 'success'
|
59
|
+
expect(@event.run_callbacks(true, 'success')).toEqual 'success'
|
60
60
|
|
61
61
|
it 'should not run the failure callback', ->
|
62
|
-
expect(@event.run_callbacks(true,'success')).toBeUndefined
|
62
|
+
expect(@event.run_callbacks(true, 'success')).toBeUndefined
|
63
63
|
|
64
64
|
describe 'when failure', ->
|
65
65
|
it 'should run the failure callback when passed true', ->
|
66
|
-
expect(@event.run_callbacks(false,'failure')).toEqual 'failure'
|
66
|
+
expect(@event.run_callbacks(false, 'failure')).toEqual 'failure'
|
67
67
|
|
68
68
|
it 'should not run the failure callback', ->
|
69
|
-
expect(@event.run_callbacks(false,'failure')).toBeUndefined
|
69
|
+
expect(@event.run_callbacks(false, 'failure')).toBeUndefined
|
@@ -1,23 +1,34 @@
|
|
1
1
|
describe 'WebsocketRails.WebSocketConnection:', ->
|
2
|
+
SAMPLE_EVENT_DATA = ['event','message']
|
3
|
+
SAMPLE_EVENT =
|
4
|
+
data: JSON.stringify(SAMPLE_EVENT_DATA)
|
5
|
+
|
2
6
|
beforeEach ->
|
3
|
-
dispatcher =
|
7
|
+
@dispatcher =
|
4
8
|
new_message: -> true
|
5
9
|
dispatch: -> true
|
6
10
|
state: 'connected'
|
7
11
|
# Have to stub the WebSocket object due to Firefox error during jasmine:ci
|
8
|
-
window.WebSocket =
|
9
|
-
@url
|
10
|
-
|
11
|
-
|
12
|
-
@connection = new WebSocketRails.WebSocketConnection('localhost:3000/websocket', dispatcher)
|
12
|
+
window.WebSocket = class WebSocketStub
|
13
|
+
constructor: (@url, @dispatcher) ->
|
14
|
+
send: -> true
|
15
|
+
close: -> @onclose(null)
|
16
|
+
@connection = new WebSocketRails.WebSocketConnection('localhost:3000/websocket', @dispatcher)
|
17
|
+
@dispatcher._conn = @connection
|
13
18
|
|
14
19
|
describe 'constructor', ->
|
15
20
|
|
16
|
-
it 'should
|
17
|
-
|
21
|
+
it 'should redirect onmessage events\' data from the WebSocket object to this.on_message', ->
|
22
|
+
mock_connection = sinon.mock @connection
|
23
|
+
mock_connection.expects('on_message').once().withArgs SAMPLE_EVENT_DATA
|
24
|
+
@connection._conn.onmessage(SAMPLE_EVENT)
|
25
|
+
mock_connection.verify()
|
18
26
|
|
19
|
-
it 'should
|
20
|
-
|
27
|
+
it 'should redirect onclose events from the WebSocket object to this.on_close', ->
|
28
|
+
mock_connection = sinon.mock @connection
|
29
|
+
mock_connection.expects('on_close').once().withArgs SAMPLE_EVENT
|
30
|
+
@connection._conn.onclose(SAMPLE_EVENT)
|
31
|
+
mock_connection.verify()
|
21
32
|
|
22
33
|
describe 'with ssl', ->
|
23
34
|
it 'should not add the ws:// prefix to the URL', ->
|
@@ -28,6 +39,11 @@ describe 'WebsocketRails.WebSocketConnection:', ->
|
|
28
39
|
it 'should add the ws:// prefix to the URL', ->
|
29
40
|
expect(@connection.url).toEqual 'ws://localhost:3000/websocket'
|
30
41
|
|
42
|
+
describe '.close', ->
|
43
|
+
it 'should close the connection', ->
|
44
|
+
@connection.close()
|
45
|
+
expect(@dispatcher.state).toEqual 'disconnected'
|
46
|
+
|
31
47
|
describe '.trigger', ->
|
32
48
|
|
33
49
|
describe 'before the connection has been fully established', ->
|
@@ -51,14 +67,13 @@ describe 'WebsocketRails.WebSocketConnection:', ->
|
|
51
67
|
describe '.on_message', ->
|
52
68
|
|
53
69
|
it 'should decode the message and pass it to the dispatcher', ->
|
54
|
-
encoded_data = JSON.stringify ['event','message']
|
55
|
-
event =
|
56
|
-
data: encoded_data
|
57
70
|
mock_dispatcher = sinon.mock @connection.dispatcher
|
58
|
-
mock_dispatcher.expects('new_message').once().withArgs
|
59
|
-
@connection.on_message
|
71
|
+
mock_dispatcher.expects('new_message').once().withArgs SAMPLE_EVENT_DATA
|
72
|
+
@connection.on_message SAMPLE_EVENT_DATA
|
60
73
|
mock_dispatcher.verify()
|
61
74
|
|
75
|
+
|
76
|
+
|
62
77
|
describe '.on_close', ->
|
63
78
|
it 'should dispatch the connection_closed event and pass the original event', ->
|
64
79
|
event = new WebSocketRails.Event ['event','message']
|
@@ -100,6 +115,29 @@ describe 'WebsocketRails.WebSocketConnection:', ->
|
|
100
115
|
|
101
116
|
expect(@dispatcher.state).toEqual('disconnected')
|
102
117
|
|
118
|
+
describe "it's no longer active connection", ->
|
119
|
+
beforeEach ->
|
120
|
+
@new_connection = new WebSocketRails.WebSocketConnection('localhost:3000/websocket', @dispatcher)
|
121
|
+
@dispatcher._conn = @new_connection
|
122
|
+
|
123
|
+
it ".on_error should not react to the event response", ->
|
124
|
+
mock_dispatcher = sinon.mock @connection.dispatcher
|
125
|
+
mock_dispatcher.expects('dispatch').never()
|
126
|
+
@connection.on_error SAMPLE_EVENT_DATA
|
127
|
+
mock_dispatcher.verify()
|
128
|
+
|
129
|
+
it ".on_close should not react to the event response", ->
|
130
|
+
mock_dispatcher = sinon.mock @connection.dispatcher
|
131
|
+
mock_dispatcher.expects('dispatch').never()
|
132
|
+
@connection.on_close SAMPLE_EVENT_DATA
|
133
|
+
mock_dispatcher.verify()
|
134
|
+
|
135
|
+
it ".on_message should not react to the event response", ->
|
136
|
+
mock_dispatcher = sinon.mock @connection.dispatcher
|
137
|
+
mock_dispatcher.expects('new_message').never()
|
138
|
+
@connection.on_message SAMPLE_EVENT_DATA
|
139
|
+
mock_dispatcher.verify()
|
140
|
+
|
103
141
|
describe '.flush_queue', ->
|
104
142
|
beforeEach ->
|
105
143
|
@event = new WebSocketRails.Event ['event','message']
|
@@ -1,15 +1,17 @@
|
|
1
1
|
describe 'WebSocketRails:', ->
|
2
2
|
beforeEach ->
|
3
3
|
@url = 'localhost:3000/websocket'
|
4
|
-
WebSocketRails.WebSocketConnection =
|
4
|
+
WebSocketRails.WebSocketConnection = class WebSocketConnectionStub extends WebSocketRails.AbstractConnection
|
5
5
|
connection_type: 'websocket'
|
6
|
-
|
7
|
-
WebSocketRails.HttpConnection = ->
|
6
|
+
WebSocketRails.HttpConnection = class HttpConnectionStub extends WebSocketRails.AbstractConnection
|
8
7
|
connection_type: 'http'
|
9
|
-
flush_queue: -> true
|
10
8
|
@dispatcher = new WebSocketRails @url
|
11
9
|
|
12
10
|
describe 'constructor', ->
|
11
|
+
it 'should start connection automatically', ->
|
12
|
+
expect(@dispatcher.state).toEqual 'connecting'
|
13
|
+
|
14
|
+
describe '.connect', ->
|
13
15
|
|
14
16
|
it 'should set the new_message method on connection to this.new_message', ->
|
15
17
|
expect(@dispatcher._conn.new_message).toEqual @dispatcher.new_message
|
@@ -22,7 +24,7 @@ describe 'WebSocketRails:', ->
|
|
22
24
|
dispatcher = new WebSocketRails @url, true
|
23
25
|
expect(dispatcher._conn.connection_type).toEqual 'websocket'
|
24
26
|
|
25
|
-
describe 'when
|
27
|
+
describe 'when use_websockets is false', ->
|
26
28
|
it 'should use the Http Connection', ->
|
27
29
|
dispatcher = new WebSocketRails @url, false
|
28
30
|
expect(dispatcher._conn.connection_type).toEqual 'http'
|
@@ -33,39 +35,110 @@ describe 'WebSocketRails:', ->
|
|
33
35
|
dispatcher = new WebSocketRails @url, true
|
34
36
|
expect(dispatcher._conn.connection_type).toEqual 'http'
|
35
37
|
|
38
|
+
describe '.disconnect', ->
|
39
|
+
beforeEach ->
|
40
|
+
@dispatcher.disconnect()
|
41
|
+
|
42
|
+
it 'should close the connection', ->
|
43
|
+
expect(@dispatcher.state).toEqual 'disconnected'
|
44
|
+
|
45
|
+
it 'existing connection should be destroyed', ->
|
46
|
+
expect(@dispatcher._conn).toBeUndefined()
|
47
|
+
|
48
|
+
describe '.reconnect', ->
|
49
|
+
OLD_CONNECTION_ID = 1
|
50
|
+
NEW_CONNECTION_ID = 2
|
51
|
+
|
52
|
+
it 'should connect, when disconnected', ->
|
53
|
+
mock_dispatcher = sinon.mock @dispatcher
|
54
|
+
mock_dispatcher.expects('connect').once()
|
55
|
+
@dispatcher.disconnect()
|
56
|
+
@dispatcher.reconnect()
|
57
|
+
mock_dispatcher.verify()
|
58
|
+
|
59
|
+
it 'should recreate the connection', ->
|
60
|
+
helpers.startConnection(@dispatcher, OLD_CONNECTION_ID)
|
61
|
+
@dispatcher.reconnect()
|
62
|
+
helpers.startConnection(@dispatcher, NEW_CONNECTION_ID)
|
63
|
+
|
64
|
+
expect(@dispatcher._conn.connection_id).toEqual NEW_CONNECTION_ID
|
65
|
+
|
66
|
+
it 'should resend all uncompleted events', ->
|
67
|
+
event = @dispatcher.trigger('create_post')
|
68
|
+
|
69
|
+
helpers.startConnection(@dispatcher, OLD_CONNECTION_ID)
|
70
|
+
@dispatcher.reconnect()
|
71
|
+
helpers.startConnection(@dispatcher, NEW_CONNECTION_ID)
|
72
|
+
|
73
|
+
expect(@dispatcher.queue[event.id].connection_id).toEqual NEW_CONNECTION_ID
|
74
|
+
|
75
|
+
it 'should not resend completed events', ->
|
76
|
+
event = @dispatcher.trigger('create_post')
|
77
|
+
event.run_callbacks(true, {})
|
78
|
+
|
79
|
+
helpers.startConnection(@dispatcher, OLD_CONNECTION_ID)
|
80
|
+
@dispatcher.reconnect()
|
81
|
+
helpers.startConnection(@dispatcher, NEW_CONNECTION_ID)
|
82
|
+
|
83
|
+
expect(@dispatcher.queue[event.id].connection_id).toEqual OLD_CONNECTION_ID
|
84
|
+
|
85
|
+
it 'should reconnect to all channels', ->
|
86
|
+
mock_dispatcher = sinon.mock @dispatcher
|
87
|
+
mock_dispatcher.expects('reconnect_channels').once()
|
88
|
+
@dispatcher.reconnect()
|
89
|
+
mock_dispatcher.verify()
|
90
|
+
|
91
|
+
describe '.reconnect_channels', ->
|
92
|
+
beforeEach ->
|
93
|
+
@channel_callback = -> true
|
94
|
+
helpers.startConnection(@dispatcher, 1)
|
95
|
+
@dispatcher.subscribe('public 4chan')
|
96
|
+
@dispatcher.subscribe_private('private 4chan')
|
97
|
+
@dispatcher.channels['public 4chan'].bind('new_post', @channel_callback)
|
98
|
+
|
99
|
+
it 'should recreate existing channels, keeping their private/public type', ->
|
100
|
+
@dispatcher.reconnect_channels()
|
101
|
+
expect(@dispatcher.channels['public 4chan'].is_private).toEqual false
|
102
|
+
expect(@dispatcher.channels['private 4chan'].is_private).toEqual true
|
103
|
+
|
104
|
+
it 'should move all existing callbacks from old channel objects to new ones', ->
|
105
|
+
old_public_channel = @dispatcher.channels['public 4chan']
|
106
|
+
|
107
|
+
@dispatcher.reconnect_channels()
|
108
|
+
|
109
|
+
expect(old_public_channel._callbacks).toEqual {}
|
110
|
+
expect(@dispatcher.channels['public 4chan']._callbacks).toEqual {new_post: [@channel_callback]}
|
111
|
+
|
36
112
|
describe '.new_message', ->
|
37
113
|
|
38
114
|
describe 'when this.state is "connecting"', ->
|
39
115
|
beforeEach ->
|
40
|
-
@
|
41
|
-
data:
|
42
|
-
connection_id: 123
|
43
|
-
@data = [['client_connected', @message]]
|
116
|
+
@connection_id = 123
|
44
117
|
|
45
118
|
it 'should call this.connection_established on the "client_connected" event', ->
|
46
119
|
mock_dispatcher = sinon.mock @dispatcher
|
47
|
-
mock_dispatcher.expects('connection_established').once().withArgs @
|
48
|
-
@dispatcher
|
120
|
+
mock_dispatcher.expects('connection_established').once().withArgs(connection_id: @connection_id)
|
121
|
+
helpers.startConnection(@dispatcher, @connection_id)
|
49
122
|
mock_dispatcher.verify()
|
50
123
|
|
51
124
|
it 'should set the state to connected', ->
|
52
|
-
@dispatcher
|
125
|
+
helpers.startConnection(@dispatcher, @connection_id)
|
53
126
|
expect(@dispatcher.state).toEqual 'connected'
|
54
127
|
|
55
128
|
it 'should flush any messages queued before the connection was established', ->
|
56
129
|
mock_con = sinon.mock @dispatcher._conn
|
57
130
|
mock_con.expects('flush_queue').once()
|
58
|
-
@dispatcher
|
131
|
+
helpers.startConnection(@dispatcher, @connection_id)
|
59
132
|
mock_con.verify()
|
60
133
|
|
61
134
|
it 'should set the correct connection_id', ->
|
62
|
-
@dispatcher
|
63
|
-
expect(@dispatcher.connection_id).toEqual 123
|
135
|
+
helpers.startConnection(@dispatcher, @connection_id)
|
136
|
+
expect(@dispatcher._conn.connection_id).toEqual 123
|
64
137
|
|
65
138
|
it 'should call the user defined on_open callback', ->
|
66
139
|
spy = sinon.spy()
|
67
140
|
@dispatcher.on_open = spy
|
68
|
-
@dispatcher
|
141
|
+
helpers.startConnection(@dispatcher, @connection_id)
|
69
142
|
expect(spy.calledOnce).toEqual true
|
70
143
|
|
71
144
|
describe 'after the connection has been established', ->
|
@@ -96,12 +169,17 @@ describe 'WebSocketRails:', ->
|
|
96
169
|
@event = { run_callbacks: (data) -> }
|
97
170
|
@event_mock = sinon.mock @event
|
98
171
|
@dispatcher.queue[1] = @event
|
172
|
+
@event_data = [['event',@attributes]]
|
99
173
|
|
100
174
|
it 'should run callbacks for result events', ->
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
175
|
+
@event_mock.expects('run_callbacks').once()
|
176
|
+
@dispatcher.new_message @event_data
|
177
|
+
@event_mock.verify()
|
178
|
+
|
179
|
+
it 'should remove the event from the queue', ->
|
180
|
+
@dispatcher.new_message @event_data
|
181
|
+
expect(@dispatcher.queue[1]).toBeUndefined()
|
182
|
+
|
105
183
|
|
106
184
|
describe '.bind', ->
|
107
185
|
|
@@ -121,18 +199,23 @@ describe 'WebSocketRails:', ->
|
|
121
199
|
|
122
200
|
describe 'triggering events with', ->
|
123
201
|
beforeEach ->
|
124
|
-
@dispatcher.connection_id = 123
|
125
202
|
@dispatcher._conn =
|
203
|
+
connection_id: 123
|
126
204
|
trigger: ->
|
127
|
-
trigger_channel: ->
|
128
205
|
|
129
206
|
describe '.trigger', ->
|
207
|
+
it 'should add the event to the queue', ->
|
208
|
+
event = @dispatcher.trigger 'event', 'message'
|
209
|
+
expect(@dispatcher.queue[event.id]).toEqual event
|
130
210
|
|
131
211
|
it 'should delegate to the connection object', ->
|
132
|
-
|
212
|
+
conn_trigger = sinon.spy @dispatcher._conn, 'trigger'
|
213
|
+
@dispatcher.trigger 'event', 'message'
|
214
|
+
expect(conn_trigger.called).toEqual true
|
215
|
+
|
216
|
+
it "should not delegate to the connection object, if it's not available", ->
|
217
|
+
@dispatcher._conn = null
|
133
218
|
@dispatcher.trigger 'event', 'message'
|
134
|
-
event = new WebSocketRails.Event ['websocket_rails.subscribe', {channel: 'awesome'}, 123]
|
135
|
-
expect(con_trigger.called).toEqual true
|
136
219
|
|
137
220
|
describe '.connection_stale', ->
|
138
221
|
describe 'when state is connected', ->
|