aboutsummaryrefslogtreecommitdiffstats
path: root/test/channel
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2015-07-12 10:07:31 -0500
committerPratik Naik <pratiknaik@gmail.com>2015-07-12 11:44:56 -0500
commit1af531dcf7b8d9ee4237ea5fd392b43875309954 (patch)
tree4eba7ddbaad86b7453cec0bab3117e9bcf0231d2 /test/channel
parent53c82f6dc036fab537b67b011ae604af646a58a3 (diff)
downloadrails-1af531dcf7b8d9ee4237ea5fd392b43875309954.tar.gz
rails-1af531dcf7b8d9ee4237ea5fd392b43875309954.tar.bz2
rails-1af531dcf7b8d9ee4237ea5fd392b43875309954.zip
Add some more tests
Diffstat (limited to 'test/channel')
-rw-r--r--test/channel/base_test.rb102
-rw-r--r--test/channel/periodic_timers_test.rb41
-rw-r--r--test/channel/stream_test.rb25
3 files changed, 168 insertions, 0 deletions
diff --git a/test/channel/base_test.rb b/test/channel/base_test.rb
new file mode 100644
index 0000000000..e525631642
--- /dev/null
+++ b/test/channel/base_test.rb
@@ -0,0 +1,102 @@
+require 'test_helper'
+require 'stubs/test_connection'
+
+class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
+ Room = Struct.new(:id)
+
+ class ChatChannel < ActionCable::Channel::Base
+ attr_reader :room, :last_action
+ on_subscribe :toggle_subscribed
+ on_unsubscribe :toggle_subscribed
+
+ def subscribed
+ @room = Room.new params[:id]
+ @actions = []
+ end
+
+ def unsubscribed
+ @room = nil
+ end
+
+ def toggle_subscribed
+ @subscribed = !@subscribed
+ end
+
+ def leave
+ @last_action = [ :leave ]
+ end
+
+ def speak(data)
+ @last_action = [ :speak, data ]
+ end
+
+ def subscribed?
+ @subscribed
+ end
+
+ def get_latest
+ transmit data: 'latest'
+ end
+
+ private
+ def rm_rf
+ @last_action = [ :rm_rf ]
+ end
+ end
+
+ setup do
+ @user = User.new "lifo"
+ @connection = TestConnection.new(@user)
+ @channel = ChatChannel.new @connection, "{id: 1}", { id: 1 }
+ end
+
+ test "should subscribe to a channel on initialize" do
+ assert_equal 1, @channel.room.id
+ end
+
+ test "on subscribe callbacks" do
+ assert @channel.subscribed
+ end
+
+ test "channel params" do
+ assert_equal({ id: 1 }, @channel.params)
+ end
+
+ test "unsubscribing from a channel" do
+ assert @channel.room
+ assert @channel.subscribed?
+
+ @channel.unsubscribe_from_channel
+
+ assert ! @channel.room
+ assert ! @channel.subscribed?
+ end
+
+ test "connection identifiers" do
+ assert_equal @user.name, @channel.current_user.name
+ end
+
+ test "callable action without any argument" do
+ @channel.perform_action 'action' => :leave
+ assert_equal [ :leave ], @channel.last_action
+ end
+
+ test "callable action with arguments" do
+ data = { 'action' => :speak, 'content' => "Hello World" }
+
+ @channel.perform_action data
+ assert_equal [ :speak, data ], @channel.last_action
+ end
+
+ test "try calling a private method" do
+ @channel.perform_action 'action' => :rm_rf
+ assert_nil @channel.last_action
+ end
+
+ test "transmitting data" do
+ @channel.perform_action 'action' => :get_latest
+
+ expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "message" => { "data" => "latest" }
+ assert_equal expected, @connection.last_transmission
+ end
+end
diff --git a/test/channel/periodic_timers_test.rb b/test/channel/periodic_timers_test.rb
new file mode 100644
index 0000000000..96d3e01783
--- /dev/null
+++ b/test/channel/periodic_timers_test.rb
@@ -0,0 +1,41 @@
+require 'test_helper'
+require 'stubs/test_connection'
+
+class ActionCable::Channel::PeriodicTimersTest < ActiveSupport::TestCase
+ Room = Struct.new(:id)
+
+ class ChatChannel < ActionCable::Channel::Base
+ periodically -> { ping }, every: 5
+ periodically :send_updates, every: 1
+
+ private
+ def ping
+ end
+ end
+
+ setup do
+ @connection = TestConnection.new
+ end
+
+ test "periodic timers definition" do
+ timers = ChatChannel.periodic_timers
+
+ assert_equal 2, timers.size
+
+ first_timer = timers[0]
+ assert_kind_of Proc, first_timer[0]
+ assert_equal 5, first_timer[1][:every]
+
+ second_timer = timers[1]
+ assert_equal :send_updates, second_timer[0]
+ assert_equal 1, second_timer[1][:every]
+ end
+
+ test "timer start and stop" do
+ EventMachine::PeriodicTimer.expects(:new).times(2).returns(true)
+ channel = ChatChannel.new @connection, "{id: 1}", { id: 1 }
+
+ channel.expects(:stop_periodic_timers).once
+ channel.unsubscribe_from_channel
+ end
+end
diff --git a/test/channel/stream_test.rb b/test/channel/stream_test.rb
new file mode 100644
index 0000000000..2f4c988adf
--- /dev/null
+++ b/test/channel/stream_test.rb
@@ -0,0 +1,25 @@
+require 'test_helper'
+require 'stubs/test_connection'
+
+class ActionCable::Channel::StreamTest < ActiveSupport::TestCase
+ Room = Struct.new(:id)
+
+ class ChatChannel < ActionCable::Channel::Base
+ def subscribed
+ @room = Room.new params[:id]
+ stream_from "test_room_#{@room.id}"
+ end
+ end
+
+ setup do
+ @connection = TestConnection.new
+ end
+
+ test "streaming start and stop" do
+ @connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe) }
+ channel = ChatChannel.new @connection, "{id: 1}", { id: 1 }
+
+ @connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe_proc) }
+ channel.unsubscribe_from_channel
+ end
+end