mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add helper for testing against a mock WebSocket and server
This commit is contained in:
parent
fb0f0c7f6e
commit
410a32ffbd
5 changed files with 48 additions and 56 deletions
|
@ -1,3 +1,3 @@
|
|||
#= require action_cable
|
||||
#= require_tree ./test_helpers
|
||||
#= require ./test_helpers
|
||||
#= require_tree ./unit
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
#= require mock-socket
|
||||
|
||||
{TestHelpers} = ActionCable
|
||||
|
||||
TestHelpers.consumerTest = (name, options = {}, callback) ->
|
||||
unless callback?
|
||||
callback = options
|
||||
options = {}
|
||||
|
||||
options.url ?= TestHelpers.testURL
|
||||
|
||||
QUnit.test name, (assert) ->
|
||||
doneAsync = assert.async()
|
||||
|
||||
ActionCable.WebSocket = MockWebSocket
|
||||
server = new MockServer options.url
|
||||
consumer = ActionCable.createConsumer(options.url)
|
||||
|
||||
server.on "connection", ->
|
||||
clients = server.clients()
|
||||
assert.equal clients.length, 1
|
||||
assert.equal clients[0].readyState, WebSocket.OPEN
|
||||
|
||||
done = ->
|
||||
consumer.disconnect()
|
||||
server.close()
|
||||
doneAsync()
|
||||
|
||||
testData = {assert, consumer, server, done}
|
||||
|
||||
if options.connect is false
|
||||
callback(testData)
|
||||
else
|
||||
server.on "connection", ->
|
||||
testData.client = server.clients()[0]
|
||||
callback(testData)
|
||||
consumer.connect()
|
|
@ -3,3 +3,6 @@
|
|||
|
||||
ActionCable.TestHelpers =
|
||||
testURL: "ws://cable.example.com/"
|
||||
|
||||
originalWebSocket = ActionCable.WebSocket
|
||||
QUnit.testDone -> ActionCable.WebSocket = originalWebSocket
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
#= require mock-socket
|
||||
|
||||
ActionCable.TestHelpers.MockWebSocket = MockWebSocket
|
||||
|
||||
server = null
|
||||
consumer = null
|
||||
|
||||
ActionCable.TestHelpers.createConsumer = (url, callback) ->
|
||||
server = new MockServer url
|
||||
consumer = ActionCable.createConsumer(url)
|
||||
callback(consumer, server)
|
||||
|
||||
QUnit.testDone ->
|
||||
if consumer?
|
||||
consumer.disconnect()
|
||||
|
||||
if server?
|
||||
server.clients().forEach (client) -> client.close()
|
||||
server.close()
|
|
@ -1,40 +1,11 @@
|
|||
{module, test} = QUnit
|
||||
{testURL, MockWebSocket, createConsumer} = ActionCable.TestHelpers
|
||||
{consumerTest} = ActionCable.TestHelpers
|
||||
|
||||
module "ActionCable.Consumer", ->
|
||||
module 'with MockWebSocket', (hooks) ->
|
||||
originalWebSocket = ActionCable.WebSocket
|
||||
|
||||
hooks.beforeEach ->
|
||||
ActionCable.WebSocket = MockWebSocket
|
||||
|
||||
hooks.afterEach ->
|
||||
ActionCable.WebSocket = originalWebSocket
|
||||
|
||||
test "#connect", (assert) ->
|
||||
done = assert.async()
|
||||
|
||||
createConsumer testURL, (consumer, server) ->
|
||||
server.on "connection", ->
|
||||
clients = server.clients()
|
||||
assert.equal clients.length, 1
|
||||
assert.equal clients[0].readyState, WebSocket.OPEN
|
||||
done()
|
||||
|
||||
consumerTest "#connect", connect: false, ({consumer, server, done}) ->
|
||||
server.on("connection", done)
|
||||
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()
|
||||
|
||||
consumerTest "#disconnect", ({consumer, client, done}) ->
|
||||
client.addEventListener("close", done)
|
||||
consumer.disconnect()
|
||||
|
||||
consumer.connect()
|
||||
|
|
Loading…
Reference in a new issue