aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test/javascript/src/unit/subscription_test.js
blob: bf32e5f27d979f3049d5289d50ab1846b5826291 (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
import consumerTest from "../test_helpers/consumer_test_helper"

const {module} = QUnit

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}) => {
    const subscription = consumer.subscriptions.create("chat", {
      connected() {
        assert.ok(true)
        done()
      }
    })

    server.broadcastTo(subscription, {message_type: "confirmation"})
  })

  consumerTest("#disconnected callback", ({server, consumer, assert, done}) => {
    const subscription = consumer.subscriptions.create("chat", {
      disconnected() {
        assert.ok(true)
        done()
      }
    })

    server.broadcastTo(subscription, {message_type: "confirmation"}, () => server.close())
  })

  consumerTest("#perform", ({consumer, server, assert, done}) => {
    const subscription = consumer.subscriptions.create("chat", {
      connected() {
        this.perform({publish: "hi"})
      }
    })

    server.on("message", (message) => {
      const 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"})
  })
})