spec/lib/wisper_spec.rb in wisper-0.0.2 vs spec/lib/wisper_spec.rb in wisper-1.0.0
- old
+ new
@@ -3,64 +3,138 @@
describe Wisper do
let(:listener) { double('listener') }
let(:publisher) { Object.new.extend(Wisper) }
describe '.add_listener' do
- it 'subscribes listener to all published events' do
+ it 'subscribes given listener to all published events' do
listener.should_receive(:this_happened)
listener.should_receive(:so_did_this)
publisher.add_listener(listener)
publisher.send(:broadcast, 'this_happened')
publisher.send(:broadcast, 'so_did_this')
end
- it 'subscribes listener to selected events' do
- listener.should_receive(:this_happened)
- listener.stub(:so_did_this)
- listener.should_not_receive(:so_did_this)
+ describe ':on argument' do
+ it 'subscribes given listener to a single event' do
+ listener.should_receive(:this_happened)
+ listener.stub(:so_did_this)
+ listener.should_not_receive(:so_did_this)
- listener.respond_to?(:so_did_this).should be_true
+ listener.respond_to?(:so_did_this).should be_true
- publisher.add_listener(listener, :on => 'this_happened')
+ publisher.add_listener(listener, :on => 'this_happened')
- publisher.send(:broadcast, 'this_happened')
- publisher.send(:broadcast, 'so_did_this')
+ publisher.send(:broadcast, 'this_happened')
+ publisher.send(:broadcast, 'so_did_this')
+ end
+
+ it 'subscribes given listener to many events' do
+ listener.should_receive(:this_happened)
+ listener.should_receive(:and_this)
+ listener.stub(:so_did_this)
+ listener.should_not_receive(:so_did_this)
+
+ listener.respond_to?(:so_did_this).should be_true
+
+ publisher.add_listener(listener, :on => ['this_happened', 'and_this'])
+
+ publisher.send(:broadcast, 'this_happened')
+ publisher.send(:broadcast, 'so_did_this')
+ publisher.send(:broadcast, 'and_this')
+ end
end
- it 'does not receive events it can not respond to' do
- listener.should_not_receive(:so_did_this)
- listener.stub(:respond_to?, false)
+ describe ':with argument' do
+ it 'sets method to call listener with on event' do
+ listener.should_receive(:different_method).twice
- listener.respond_to?(:so_did_this).should be_false
+ publisher.add_listener(listener, :with => :different_method)
- publisher.add_listener(listener, :on => 'so_did_this')
+ publisher.send(:broadcast, 'this_happened')
+ publisher.send(:broadcast, 'so_did_this')
+ end
+ end
- publisher.send(:broadcast, 'so_did_this')
+ it 'returns publisher so methods can be chained' do
+ publisher.add_listener(listener, :on => 'so_did_this').should == publisher
end
end
- describe 'Block listeners' do
- it 'subscribes block to all events' do
+ describe '.add_block_listener' do
+ it 'subscribes given block to all events' do
insider = double('insider')
- insider.should_receive(:it_happened)
+ insider.should_receive(:it_happened).twice
publisher.add_block_listener do
insider.it_happened
end
publisher.send(:broadcast, 'something_happened')
+ publisher.send(:broadcast, 'and_so_did_this')
end
- it 'subscribes block to selected events' do
- insider = double('insider')
- insider.should_not_receive(:it_happened)
+ describe ':on argument' do
+ it '.add_block_listener subscribes block to an event' do
+ insider = double('insider')
+ insider.should_not_receive(:it_happened).once
+ publisher.add_block_listener(:on => 'something_happened') do
+ insider.it_happened
+ end
+
+ publisher.send(:broadcast, 'something_happened')
+ publisher.send(:broadcast, 'and_so_did_this')
+ end
+ end
+
+ it 'returns publisher so methods can be chained' do
publisher.add_block_listener(:on => 'this_thing_happened') do
+ end.should == publisher
+ end
+ end
+
+ describe '.on (alternative block syntax)' do
+ it 'subscribes given block to an event' do
+ insider = double('insider')
+ insider.should_receive(:it_happened)
+
+ publisher.on(:something_happened) do
insider.it_happened
end
publisher.send(:broadcast, 'something_happened')
+ end
+ end
+
+ describe '.broadcast' do
+ it 'does not publish events which cannot be responded to' do
+ listener.should_not_receive(:so_did_this)
+ listener.stub(:respond_to?, false)
+
+ publisher.add_listener(listener, :on => 'so_did_this')
+
+ publisher.send(:broadcast, 'so_did_this')
+ end
+
+ describe ':event argument' do
+ it 'is indifferent to string and symbol' do
+ listener.should_receive(:this_happened).twice
+
+ publisher.add_listener(listener)
+
+ publisher.send(:broadcast, 'this_happened')
+ publisher.send(:broadcast, :this_happened)
+ end
+
+ it 'is indifferent to dasherized and underscored strings' do
+ listener.should_receive(:this_happened).twice
+
+ publisher.add_listener(listener)
+
+ publisher.send(:broadcast, 'this_happened')
+ publisher.send(:broadcast, 'this-happened')
+ end
end
end
end