aboutsummaryrefslogblamecommitdiffstats
path: root/actioncable/test/javascript/src/test_helpers/consumer_test_helper.coffee
blob: 6b145dede8d781239a142d95c55aa4597fc11e54 (plain) (tree)




































                                                            
#= 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()