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

Add more Action Cable JavaScript tests

This commit is contained in:
Javan Makhmali 2016-11-20 17:08:54 -05:00
parent 6f30cb3559
commit 7083fa27a4
4 changed files with 78 additions and 0 deletions

View file

@ -21,6 +21,16 @@ TestHelpers.consumerTest = (name, options = {}, callback) ->
assert.equal clients.length, 1
assert.equal clients[0].readyState, WebSocket.OPEN
server.broadcastTo = (subscription, data = {}, callback) ->
data.identifier = subscription.identifier
if data.message_type
data.type = ActionCable.INTERNAL.message_types[data.message_type]
delete data.message_type
server.send(JSON.stringify(data))
TestHelpers.defer(callback)
done = ->
consumer.disconnect()
server.close()

View file

@ -4,5 +4,8 @@
ActionCable.TestHelpers =
testURL: "ws://cable.example.com/"
defer: (callback) ->
setTimeout(callback, 1)
originalWebSocket = ActionCable.WebSocket
QUnit.testDone -> ActionCable.WebSocket = originalWebSocket

View file

@ -0,0 +1,40 @@
{module, test} = QUnit
{consumerTest} = ActionCable.TestHelpers
module "ActionCable.Subscription", ->
consumerTest "#initialized callback", ({server, consumer, assert, done}) ->
consumer.subscriptions.create "chat",
initialized: ->
assert.ok true
done()
consumerTest "#connected callback", ({server, consumer, assert, done}) ->
subscription = consumer.subscriptions.create "chat",
connected: ->
assert.ok true
done()
server.broadcastTo(subscription, message_type: "confirmation")
consumerTest "#disconnected callback", ({server, consumer, assert, done}) ->
subscription = consumer.subscriptions.create "chat",
disconnected: ->
assert.ok true
done()
server.broadcastTo subscription, message_type: "confirmation", ->
server.close()
consumerTest "#perform", ({consumer, server, assert, done}) ->
subscription = consumer.subscriptions.create "chat",
connected: ->
@perform(publish: "hi")
server.on "message", (message) ->
data = JSON.parse(message)
assert.equal data.identifier, subscription.identifier
assert.equal data.command, "message"
assert.deepEqual data.data, JSON.stringify(action: { publish: "hi" })
done()
server.broadcastTo(subscription, message_type: "confirmation")

View file

@ -0,0 +1,25 @@
{module, test} = QUnit
{consumerTest} = ActionCable.TestHelpers
module "ActionCable.Subscriptions", ->
consumerTest "create subscription with channel string", ({consumer, server, assert, done}) ->
channel = "chat"
server.on "message", (message) ->
data = JSON.parse(message)
assert.equal data.command, "subscribe"
assert.equal data.identifier, JSON.stringify({channel})
done()
consumer.subscriptions.create(channel)
consumerTest "create subscription with channel object", ({consumer, server, assert, done}) ->
channel = channel: "chat", room: "action"
server.on "message", (message) ->
data = JSON.parse(message)
assert.equal data.command, "subscribe"
assert.equal data.identifier, JSON.stringify(channel)
done()
consumer.subscriptions.create(channel)