1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reorganize MockWebSocket

This commit is contained in:
Jon Moss 2016-05-27 14:37:26 -04:00
parent 66901c1849
commit fb0f0c7f6e
2 changed files with 32 additions and 22 deletions

View file

@ -1,10 +1,11 @@
#= require mock-socket
ActionCable.TestHelpers.MockWebSocket = MockWebSocket
server = null
consumer = null
ActionCable.TestHelpers.createConsumer = (url, callback) ->
ActionCable.WebSocket = MockWebSocket
server = new MockServer url
consumer = ActionCable.createConsumer(url)
callback(consumer, server)

View file

@ -1,31 +1,40 @@
{module, test} = QUnit
{testURL, createConsumer} = ActionCable.TestHelpers
{testURL, MockWebSocket, createConsumer} = ActionCable.TestHelpers
module "ActionCable.Consumer", ->
test "#connect", (assert) ->
done = assert.async()
module 'with MockWebSocket', (hooks) ->
originalWebSocket = ActionCable.WebSocket
createConsumer testURL, (consumer, server) ->
server.on "connection", ->
clients = server.clients()
assert.equal clients.length, 1
assert.equal clients[0].readyState, WebSocket.OPEN
done()
hooks.beforeEach ->
ActionCable.WebSocket = MockWebSocket
consumer.connect()
hooks.afterEach ->
ActionCable.WebSocket = originalWebSocket
test "#disconnect", (assert) ->
done = assert.async()
test "#connect", (assert) ->
done = assert.async()
createConsumer testURL, (consumer, server) ->
server.on "connection", ->
clients = server.clients()
assert.equal clients.length, 1
clients[0].addEventListener "close", (event) ->
assert.equal event.type, "close"
createConsumer testURL, (consumer, server) ->
server.on "connection", ->
clients = server.clients()
assert.equal clients.length, 1
assert.equal clients[0].readyState, WebSocket.OPEN
done()
consumer.disconnect()
consumer.connect()
consumer.connect()
test "#disconnect", (assert) ->
done = assert.async()
createConsumer testURL, (consumer, server) ->
server.on "connection", ->
clients = server.clients()
assert.equal clients.length, 1
clients[0].addEventListener "close", (event) ->
assert.equal event.type, "close"
done()
consumer.disconnect()
consumer.connect()