diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2015-07-11 20:27:44 -0500 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2015-07-12 11:44:56 -0500 |
commit | 8fde483eb02f6d89eaab8590a4ddcd406d74900f (patch) | |
tree | 2dbdce926945a4b9fb09b3d0bd64d31b62a370b5 /test | |
parent | f207245cc76f24c691daf7d223f33c247d0dc66c (diff) | |
download | rails-8fde483eb02f6d89eaab8590a4ddcd406d74900f.tar.gz rails-8fde483eb02f6d89eaab8590a4ddcd406d74900f.tar.bz2 rails-8fde483eb02f6d89eaab8590a4ddcd406d74900f.zip |
Tests for the Channel API
Diffstat (limited to 'test')
-rw-r--r-- | test/channel_test.rb | 138 |
1 files changed, 120 insertions, 18 deletions
diff --git a/test/channel_test.rb b/test/channel_test.rb index 145308e3fc..3f7847a12d 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -1,20 +1,122 @@ require 'test_helper' -# FIXME: Currently busted. -# -# class ChannelTest < ActionCableTest -# class PingChannel < ActionCable::Channel::Base -# end -# -# class PingServer < ActionCable::Server::Base -# register_channels PingChannel -# end -# -# def app -# PingServer -# end -# -# test "channel callbacks" do -# ws = Faye::WebSocket::Client.new(websocket_url) -# end -# end
\ No newline at end of file +class ChannelTest < ActiveSupport::TestCase + Room = Struct.new(:id) + User = Struct.new(:name) + + class TestConnection + attr_reader :identifiers, :logger, :current_user, :transmissions + + def initialize(user) + @identifiers = [ :current_user ] + + @current_user = user + @logger = Logger.new(StringIO.new) + @transmissions = [] + end + + def transmit(data) + @transmissions << data + end + + def last_transmission + @transmissions.last + end + end + + 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 |