aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test/channel/log_subscriber_test.rb
blob: 18acdc9fbdb0c0634f076a77219387248ccafb70 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'test_helper'
require 'stubs/test_connection'
require 'active_support/log_subscriber/test_helper'
require 'action_cable/channel/log_subscriber'

class ActionCable::Channel::LogSubscriberTest < ActiveSupport::TestCase
  include ActiveSupport::LogSubscriber::TestHelper

  class ChatChannel < ActionCable::Channel::Base
    attr_reader :last_action

    def speak(data)
      @last_action = [ :speak, data ]
    end

    def get_latest
      transmit data: 'latest'
    end
  end

  def setup
    super
    @connection = TestConnection.new
    @channel = ChatChannel.new @connection, "{id: 1}", { id: 1 }
    ActionCable::Channel::LogSubscriber.attach_to :action_cable
  end

  def test_perform_action
    data = {'action' => :speak, 'content' => 'hello'}
    @channel.perform_action(data)
    wait

    assert_equal(1, logs.size)
    assert_match(/Completed #{channel_class}#speak in \d+ms/, logs.first)
  end

  def test_transmit
    @channel.perform_action('action' => :get_latest)
    wait

    assert_equal(2, logs.size)
    assert_match(/^#{channel_class} transmitting/, logs.first)
  end

  def test_transmit_subscription_confirmation
    @channel.stubs(:subscription_confirmation_sent?).returns(false)
    @channel.send(:transmit_subscription_confirmation)
    wait

    assert_equal(1, logs.size)
    assert_equal("#{channel_class} is transmitting the subscription confirmation", logs.first)
  end

  def test_transmit_subscription_rejection
    @channel.send(:transmit_subscription_rejection)
    wait

    assert_equal(1, logs.size)
    assert_equal("#{channel_class} is transmitting the subscription rejection", logs.first)
  end

  def channel_class
    "ActionCable::Channel::LogSubscriberTest::ChatChannel"
  end

  def logs
    @logs ||= @logger.logged(:info)
  end
end