spec/unit/realtime/channel_spec.rb in ably-0.6.2 vs spec/unit/realtime/channel_spec.rb in ably-0.7.0

- old
+ new

@@ -1,16 +1,121 @@ +# encoding: utf-8 require 'spec_helper' -require 'support/protocol_msgbus_helper' +require 'shared/protocol_msgbus_behaviour' describe Ably::Realtime::Channel do let(:client) { double('client').as_null_object } let(:channel_name) { 'test' } subject do Ably::Realtime::Channel.new(client, channel_name) end + describe '#initializer' do + let(:channel_name) { random_str.encode(encoding) } + + context 'as UTF_8 string' do + let(:encoding) { Encoding::UTF_8 } + + it 'is permitted' do + expect(subject.name).to eql(channel_name) + end + + it 'remains as UTF-8' do + expect(subject.name.encoding).to eql(encoding) + end + end + + context 'as SHIFT_JIS string' do + let(:encoding) { Encoding::SHIFT_JIS } + + it 'gets converted to UTF-8' do + expect(subject.name.encoding).to eql(Encoding::UTF_8) + end + + it 'is compatible with original encoding' do + expect(subject.name.encode(encoding)).to eql(channel_name) + end + end + + context 'as ASCII_8BIT string' do + let(:encoding) { Encoding::ASCII_8BIT } + + it 'gets converted to UTF-8' do + expect(subject.name.encoding).to eql(Encoding::UTF_8) + end + + it 'is compatible with original encoding' do + expect(subject.name.encode(encoding)).to eql(channel_name) + end + end + + context 'as Integer' do + let(:channel_name) { 1 } + + it 'raises an argument error' do + expect { subject }.to raise_error ArgumentError, /must be a String/ + end + end + + context 'as Nil' do + let(:channel_name) { nil } + + it 'raises an argument error' do + expect { subject }.to raise_error ArgumentError, /must be a String/ + end + end + end + + describe '#publish name argument' do + let(:encoded_value) { random_str.encode(encoding) } + + before do + allow(subject).to receive(:create_message).and_return('message_stubbed') + end + + context 'as UTF_8 string' do + let(:encoding) { Encoding::UTF_8 } + + it 'is permitted' do + expect(subject.publish(encoded_value, 'data')).to eql('message_stubbed') + end + end + + context 'as SHIFT_JIS string' do + let(:encoding) { Encoding::SHIFT_JIS } + + it 'is permitted' do + expect(subject.publish(encoded_value, 'data')).to eql('message_stubbed') + end + end + + context 'as ASCII_8BIT string' do + let(:encoding) { Encoding::ASCII_8BIT } + + it 'is permitted' do + expect(subject.publish(encoded_value, 'data')).to eql('message_stubbed') + end + end + + context 'as Integer' do + let(:encoded_value) { 1 } + + it 'raises an argument error' do + expect { subject.publish(encoded_value, 'data') }.to raise_error ArgumentError, /must be a String/ + end + end + + context 'as Nil' do + let(:encoded_value) { nil } + + it 'raises an argument error' do + expect { subject.publish(encoded_value, 'data') }.to raise_error ArgumentError, /must be a String/ + end + end + end + describe 'callbacks' do specify 'are supported for valid STATE events' do state = nil subject.on(:initialized) { state = :ready } expect { subject.trigger(:initialized) }.to change { state }.to(:ready) @@ -21,10 +126,10 @@ expect { subject.trigger(:invalid) }.to raise_error KeyError expect { subject.off(:invalid) }.to raise_error KeyError end end - context 'msgbus' do + context 'msgbus', :api_private do let(:message) do Ably::Models::Message.new({ 'name' => 'test', 'data' => 'payload' }, instance_double('Ably::Models::ProtocolMessage'))