aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test/channel/base_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actioncable/test/channel/base_test.rb')
-rw-r--r--actioncable/test/channel/base_test.rb81
1 files changed, 47 insertions, 34 deletions
diff --git a/actioncable/test/channel/base_test.rb b/actioncable/test/channel/base_test.rb
index daa782eeb3..866bd7c21b 100644
--- a/actioncable/test/channel/base_test.rb
+++ b/actioncable/test/channel/base_test.rb
@@ -1,6 +1,8 @@
-require 'test_helper'
-require 'stubs/test_connection'
-require 'stubs/room'
+# frozen_string_literal: true
+
+require "test_helper"
+require "stubs/test_connection"
+require "stubs/room"
class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
class ActionCable::Channel::Base
@@ -58,7 +60,7 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
end
def get_latest
- transmit data: 'latest'
+ transmit data: "latest"
end
def receive
@@ -74,14 +76,16 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
setup do
@user = User.new "lifo"
@connection = TestConnection.new(@user)
- @channel = ChatChannel.new @connection, "{id: 1}", { id: 1 }
+ @channel = ChatChannel.new @connection, "{id: 1}", id: 1
end
- test "should subscribe to a channel on initialize" do
+ test "should subscribe to a channel" do
+ @channel.subscribe_to_channel
assert_equal 1, @channel.room.id
end
test "on subscribe callbacks" do
+ @channel.subscribe_to_channel
assert @channel.subscribed
end
@@ -90,6 +94,8 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
end
test "unsubscribing from a channel" do
+ @channel.subscribe_to_channel
+
assert @channel.room
assert @channel.subscribed?
@@ -104,54 +110,59 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
end
test "callable action without any argument" do
- @channel.perform_action 'action' => :leave
+ @channel.perform_action "action" => :leave
assert_equal [ :leave ], @channel.last_action
end
test "callable action with arguments" do
- data = { 'action' => :speak, 'content' => "Hello World" }
+ data = { "action" => :speak, "content" => "Hello World" }
@channel.perform_action data
assert_equal [ :speak, data ], @channel.last_action
end
test "should not dispatch a private method" do
- @channel.perform_action 'action' => :rm_rf
+ @channel.perform_action "action" => :rm_rf
assert_nil @channel.last_action
end
test "should not dispatch a public method defined on Base" do
- @channel.perform_action 'action' => :kick
+ @channel.perform_action "action" => :kick
assert_nil @channel.last_action
end
test "should dispatch a public method defined on Base and redefined on channel" do
- data = { 'action' => :topic, 'content' => "This is Sparta!" }
+ data = { "action" => :topic, "content" => "This is Sparta!" }
@channel.perform_action data
assert_equal [ :topic, data ], @channel.last_action
end
test "should dispatch calling a public method defined in an ancestor" do
- @channel.perform_action 'action' => :chatters
+ @channel.perform_action "action" => :chatters
assert_equal [ :chatters ], @channel.last_action
end
test "should dispatch receive action when perform_action is called with empty action" do
- data = { 'content' => 'hello' }
+ data = { "content" => "hello" }
@channel.perform_action data
assert_equal [ :receive ], @channel.last_action
end
test "transmitting data" do
- @channel.perform_action 'action' => :get_latest
+ @channel.perform_action "action" => :get_latest
- expected = { "identifier" => "{id: 1}", "message" => { "data" => "latest" }}
+ expected = { "identifier" => "{id: 1}", "message" => { "data" => "latest" } }
assert_equal expected, @connection.last_transmission
end
- test "subscription confirmation" do
+ test "do not send subscription confirmation on initialize" do
+ assert_nil @connection.last_transmission
+ end
+
+ test "subscription confirmation on subscribe_to_channel" do
expected = { "identifier" => "{id: 1}", "type" => "confirm_subscription" }
+ @channel.subscribe_to_channel
assert_equal expected, @connection.last_transmission
end
@@ -162,7 +173,7 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
test "invalid action on Channel" do
assert_logged("Unable to process ActionCable::Channel::BaseTest::ChatChannel#invalid_action") do
- @channel.perform_action 'action' => :invalid_action
+ @channel.perform_action "action" => :invalid_action
end
end
@@ -173,12 +184,12 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
events << ActiveSupport::Notifications::Event.new(*args)
end
- data = {'action' => :speak, 'content' => 'hello'}
+ data = { "action" => :speak, "content" => "hello" }
@channel.perform_action data
assert_equal 1, events.length
- assert_equal 'perform_action.action_cable', events[0].name
- assert_equal 'ActionCable::Channel::BaseTest::ChatChannel', events[0].payload[:channel_class]
+ assert_equal "perform_action.action_cable", events[0].name
+ assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
assert_equal :speak, events[0].payload[:action]
assert_equal data, events[0].payload[:data]
ensure
@@ -189,27 +200,29 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
test "notification for transmit" do
begin
events = []
- ActiveSupport::Notifications.subscribe 'transmit.action_cable' do |*args|
+ ActiveSupport::Notifications.subscribe "transmit.action_cable" do |*args|
events << ActiveSupport::Notifications::Event.new(*args)
end
- @channel.perform_action 'action' => :get_latest
- expected_data = {data: 'latest'}
+ @channel.perform_action "action" => :get_latest
+ expected_data = { data: "latest" }
assert_equal 1, events.length
- assert_equal 'transmit.action_cable', events[0].name
- assert_equal 'ActionCable::Channel::BaseTest::ChatChannel', events[0].payload[:channel_class]
+ assert_equal "transmit.action_cable", events[0].name
+ assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
assert_equal expected_data, events[0].payload[:data]
assert_nil events[0].payload[:via]
ensure
- ActiveSupport::Notifications.unsubscribe 'transmit.action_cable'
+ ActiveSupport::Notifications.unsubscribe "transmit.action_cable"
end
end
test "notification for transmit_subscription_confirmation" do
begin
+ @channel.subscribe_to_channel
+
events = []
- ActiveSupport::Notifications.subscribe 'transmit_subscription_confirmation.action_cable' do |*args|
+ ActiveSupport::Notifications.subscribe "transmit_subscription_confirmation.action_cable" do |*args|
events << ActiveSupport::Notifications::Event.new(*args)
end
@@ -217,27 +230,27 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
@channel.send(:transmit_subscription_confirmation)
assert_equal 1, events.length
- assert_equal 'transmit_subscription_confirmation.action_cable', events[0].name
- assert_equal 'ActionCable::Channel::BaseTest::ChatChannel', events[0].payload[:channel_class]
+ assert_equal "transmit_subscription_confirmation.action_cable", events[0].name
+ assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
ensure
- ActiveSupport::Notifications.unsubscribe 'transmit_subscription_confirmation.action_cable'
+ ActiveSupport::Notifications.unsubscribe "transmit_subscription_confirmation.action_cable"
end
end
test "notification for transmit_subscription_rejection" do
begin
events = []
- ActiveSupport::Notifications.subscribe 'transmit_subscription_rejection.action_cable' do |*args|
+ ActiveSupport::Notifications.subscribe "transmit_subscription_rejection.action_cable" do |*args|
events << ActiveSupport::Notifications::Event.new(*args)
end
@channel.send(:transmit_subscription_rejection)
assert_equal 1, events.length
- assert_equal 'transmit_subscription_rejection.action_cable', events[0].name
- assert_equal 'ActionCable::Channel::BaseTest::ChatChannel', events[0].payload[:channel_class]
+ assert_equal "transmit_subscription_rejection.action_cable", events[0].name
+ assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
ensure
- ActiveSupport::Notifications.unsubscribe 'transmit_subscription_rejection.action_cable'
+ ActiveSupport::Notifications.unsubscribe "transmit_subscription_rejection.action_cable"
end
end