spec/unit/connection_adapters_spec.rb in websocket-rails-0.5.0 vs spec/unit/connection_adapters_spec.rb in websocket-rails-0.6.0

- old
+ new

@@ -16,47 +16,61 @@ ConnectionAdapters.adapters.include?( ConnectionAdapters::Test ).should be_true end end context ".establish_connection" do + before do + connection_manager = double(ConnectionManager) + connection_manager.stub(:connections).and_return({}) + @dispatcher = double(Dispatcher).as_null_object + @dispatcher.stub(:connection_manager).and_return(connection_manager) + end + it "should return the correct connection adapter instance" do - adapter = ConnectionAdapters.establish_connection( mock_request, double('Dispatcher').as_null_object ) + adapter = ConnectionAdapters.establish_connection(mock_request, @dispatcher) adapter.class.should == ConnectionAdapters::Test end end end module ConnectionAdapters describe Base do - let(:dispatcher) { double('Dispatcher').as_null_object } - let(:channel_manager) { double('ChannelManager').as_null_object } - let(:event) { double('Event').as_null_object } - before { Event.stub(:new_from_json).and_return(event) } - subject { Base.new( mock_request, dispatcher ) } + let(:connection_manager) { double(ConnectionManager).as_null_object } + let(:dispatcher) { double(Dispatcher).as_null_object } + let(:channel_manager) { double(ChannelManager).as_null_object } + let(:event) { double(Event).as_null_object } + before do + connection_manager.stub(:connections).and_return({}) + dispatcher.stub(:connection_manager).and_return(connection_manager) + Event.stub(:new_from_json).and_return(event) + end + + subject { Base.new(mock_request, dispatcher) } + context "new adapter" do it "should register itself in the adapters array when inherited" do adapter = Class.new( ConnectionAdapters::Base ) ConnectionAdapters.adapters.include?( adapter ).should be_true end it "should create a new DataStore::Connection instance" do subject.data_store.should be_a DataStore::Connection end - before do - WebsocketRails.config.stub(:user_identifier).and_return(:name) - WebsocketRails::DelegationController.any_instance - .stub_chain(:current_user, :name) - .and_return('Frank') - subject - end + #before do + # WebsocketRails.config.stub(:user_identifier).and_return(:name) + # WebsocketRails::DelegationController.any_instance + # .stub_chain(:current_user, :name) + # .and_return('Frank') + # subject + #end - it "adds itself to the UserManager Hash" do - WebsocketRails.users['Frank'].should == subject - end + #it "adds itself to the UserManager Hash" do + # WebsocketRails.users['Frank'].should == subject + #end end describe "#on_open" do it "should dispatch an on_open event" do on_open_event = double('event').as_null_object @@ -79,15 +93,10 @@ on_close_event = double('event') Event.should_receive(:new_on_close).and_return(on_close_event) dispatcher.should_receive(:dispatch).with(on_close_event) subject.on_close("data") end - - it "removes itself from the global UserMnaager" do - subject.on_close - WebsocketRails.users[subject.id].nil?.should == true - end end describe "#on_error" do it "should dispatch an on_error event" do subject.stub(:on_close) @@ -135,48 +144,75 @@ it "should raise a NotImplementedError exception" do expect { subject.send :message }.to raise_exception(NotImplementedError) end end - describe "#enqueue" do - it "should add the event to the queue" do - subject.enqueue 'event' - subject.queue.queue.should == ['event'] + describe "#user_connection?" do + context "when a user is signed in" do + before do + subject.stub(:user_identifier).and_return("Jimbo Jones") + end + + it "returns true" do + subject.user_connection?.should == true + end end + + context "when a user is signed out" do + before do + subject.stub(:user_identifier).and_return(nil) + end + + it "returns true" do + subject.user_connection?.should == false + end + end end - describe "#trigger" do -=begin - it "should add the event to the queue" do - pending - subject.stub(:flush) - subject.should_receive(:enqueue).with('event') - subject.trigger 'event' + describe "#user" do + it "provides access to the current_user object" do + user = double('User') + subject.stub(:user_identifier).and_return true + subject.stub_chain(:controller_delegate, :current_user).and_return user + subject.user.should == user end + end - it "should flush the queue" do - subject.should_receive(:flush) - subject.trigger 'event' + describe "#enqueue" do + it "should add the event to the queue" do + subject.enqueue 'event' + subject.queue.queue.should == ['event'] end -=end end describe "#flush" do before do - event = double('event') - event.stub!(:serialize).and_return("['event']") - 3.times { subject.enqueue event } + event = Event.new(:queued_event, data: 'test') + 2.times { subject.enqueue event } end it "should serialize all events into one array" do - serialized_array = "[['event'],['event'],['event']]" - subject.should_receive(:send).with(serialized_array) + serialized_array = <<-EOF.strip_heredoc + [["queued_event",{"id":null,"channel":null,"user_id":null,"data":"test","success":null,"result":null,"server_token":null}], + ["queued_event",{"id":null,"channel":null,"user_id":null,"data":"test","success":null,"result":null,"server_token":null}]] + EOF + + subject.should_receive(:send).with(serialized_array.gsub(/\n/,'').strip) subject.flush end end + describe "#trigger" do + it "passes a serialized event to the connections #send method" do + event.stub(:serialize).and_return('test') + subject.should_receive(:send).with "[test]" + subject.trigger event + end + end + describe "#close_connection" do before do + subject.stub(:user_identifier).and_return(1) @connection_manager = double('connection_manager').as_null_object subject.stub_chain(:dispatcher, :connection_manager).and_return(@connection_manager) end it "calls delegates to the conection manager" do