From 1af531dcf7b8d9ee4237ea5fd392b43875309954 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 12 Jul 2015 10:07:31 -0500 Subject: Add some more tests --- test/channel/base_test.rb | 102 +++++++++++++++++++++++++++++++++++ test/channel/periodic_timers_test.rb | 41 ++++++++++++++ test/channel/stream_test.rb | 25 +++++++++ 3 files changed, 168 insertions(+) create mode 100644 test/channel/base_test.rb create mode 100644 test/channel/periodic_timers_test.rb create mode 100644 test/channel/stream_test.rb (limited to 'test/channel') 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 -- cgit v1.2.3