aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test/javascript/src/unit/action_cable_test.js
blob: 8847d875459919a5d90d28ae63c9e39ebd16629d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import ActionCable from "../../../../app/javascript/action_cable/index"
import {testURL} from "../test_helpers/index"

const {module, test} = QUnit

module("ActionCable", () => {
  module("Adapters", () => {
    module("WebSocket", () => {
      test("default is window.WebSocket", assert => {
        assert.equal(ActionCable.WebSocket, window.WebSocket)
      })

      test("configurable", assert => {
        ActionCable.WebSocket = ""
        assert.equal(ActionCable.WebSocket, "")
      })
    })

    module("logger", () => {
      test("default is window.console", assert => {
        assert.equal(ActionCable.logger, window.console)
      })

      test("configurable", assert => {
        ActionCable.logger = ""
        assert.equal(ActionCable.logger, "")
      })
    })
  })

  module("#createConsumer", () => {
    test("uses specified URL", assert => {
      const consumer = ActionCable.createConsumer(testURL)
      assert.equal(consumer.url, testURL)
    })

    test("uses default URL", assert => {
      const pattern = new RegExp(`${ActionCable.INTERNAL.default_mount_path}$`)
      const consumer = ActionCable.createConsumer()
      assert.ok(pattern.test(consumer.url), `Expected ${consumer.url} to match ${pattern}`)
    })

    test("uses URL from meta tag", assert => {
      const element = document.createElement("meta")
      element.setAttribute("name", "action-cable-url")
      element.setAttribute("content", testURL)

      document.head.appendChild(element)
      const consumer = ActionCable.createConsumer()
      document.head.removeChild(element)

      assert.equal(consumer.url, testURL)
    })
  })
})