spec/unit/modules/event_emitter_spec.rb in ably-0.8.15 vs spec/unit/modules/event_emitter_spec.rb in ably-1.0.0

- old
+ new

@@ -81,17 +81,17 @@ end end allow(obj).to receive(:received_message) end - it 'is unaffected and processes the prior event callbacks once' do + it 'is unaffected and processes the prior event callbacks once (#RTE6b)' do expect(obj).to receive(:received_message).with(msg).twice expect(obj).to_not receive(:received_message_from_new_callbacks).with(msg) subject.emit :message, msg end - it 'adds them for the next emitted event' do + it 'adds them for the next emitted event (#RTE6b)' do expect(obj).to receive(:received_message_from_new_callbacks).with(msg).twice # New callbacks are added in this emit subject.emit :message, msg @@ -108,16 +108,16 @@ subject.off end end end - it 'is unaffected and processes the prior event callbacks once' do + it 'is unaffected and processes the prior event callbacks once (#RTE6b)' do expect(obj).to receive(:received_message).with(msg).twice subject.emit :message, msg end - it 'removes them for the next emitted event' do + it 'removes them for the next emitted event (#RTE6b)' do expect(obj).to receive(:received_message).with(msg).twice # Callbacks are removed in this emit subject.emit :message, msg # No callbacks should exist now @@ -125,21 +125,23 @@ end end end end - context '#on' do + context '#on (#RTE3)' do context 'with event specified' do it 'calls the block every time an event is emitted only' do block_called = 0 subject.on('event') { block_called += 1 } 3.times { subject.emit 'event', 'data' } expect(block_called).to eql(3) end it 'catches exceptions in the provided block, logs the error and continues' do - expect(subject.logger).to receive(:error).with(/Intentional exception/) + expect(subject.logger).to receive(:error) do |*args, &block| + expect(args.concat([block ? block.call : nil]).join(',')).to match(/Intentional exception/) + end subject.on(:event) { raise 'Intentional exception' } subject.emit :event end end @@ -150,11 +152,13 @@ 3.times { subject.emit 'event', 'data' } expect(block_called).to eql(3) end it 'catches exceptions in the provided block, logs the error and continues' do - expect(subject.logger).to receive(:error).with(/Intentional exception/) + expect(subject.logger).to receive(:error) do |*args, &block| + expect(args.concat([block ? block.call : nil]).join(',')).to match(/Intentional exception/) + end subject.on { raise 'Intentional exception' } subject.emit :event end end end @@ -171,11 +175,11 @@ subject.unsafe_on(:event) { raise 'Intentional exception' } expect { subject.emit :event }.to raise_error(/Intentional exception/) end end - context '#once' do + context '#once (#RTE4)' do context 'with event specified' do it 'calls the block the first time an event is emitted only' do block_called = 0 subject.once('event') { block_called += 1 } 3.times { subject.emit 'event', 'data' } @@ -189,11 +193,13 @@ 3.times { subject.emit 'event', 'data' } expect(block_called).to eql(4) end it 'catches exceptions in the provided block, logs the error and continues' do - expect(subject.logger).to receive(:error).with(/Intentional exception/) + expect(subject.logger).to receive(:error) do |*args, &block| + expect(args.concat([block ? block.call : nil]).join(',')).to match(/Intentional exception/) + end subject.once(:event) { raise 'Intentional exception' } subject.emit :event end end @@ -212,11 +218,13 @@ 3.times { subject.emit 'event', 'data' } expect(block_called).to eql(4) end it 'catches exceptions in the provided block, logs the error and continues' do - expect(subject.logger).to receive(:error).with(/Intentional exception/) + expect(subject.logger).to receive(:error) do |*args, &block| + expect(args.concat([block ? block.call : nil]).join(',')).to match(/Intentional exception/) + end subject.once { raise 'Intentional exception' } subject.emit :event end end end @@ -246,16 +254,16 @@ after do subject.emit :message, msg end context 'with event names as arguments' do - it 'deletes matching callbacks' do + it 'deletes matching callbacks when a block is provided' do expect(obj).to_not receive(:received_message).with(msg) subject.off(:message, &callback) end - it 'deletes all callbacks if not block given' do + it 'deletes all matching callbacks when a block is not provided' do expect(obj).to_not receive(:received_message).with(msg) subject.off(:message) end it 'continues if the block does not exist' do @@ -275,20 +283,139 @@ subject.off end end end - it 'removes handler added with no event specified' do - subject.on(&callback) - expect(obj).to_not receive(:received_message).with(msg) - subject.off(&callback) - subject.emit :message, msg + context 'when on callback is configured for all events' do + before do + subject.on(&callback) + end + + after do + subject.emit :message, msg + end + + context 'with event names as arguments' do + it 'does not remove the all events callback when a block is provided' do + expect(obj).to receive(:received_message).with(msg) + subject.off(:message, &callback) + end + + it 'does not remove the all events callback when a block is not provided' do + expect(obj).to receive(:received_message).with(msg) + subject.off(:message) + end + + it 'does not remove the all events callback when the block does not match' do + expect(obj).to receive(:received_message).with(msg) + subject.off(:message) { true } + end + end + + context 'without any event names' do + it 'deletes all matching callbacks' do + expect(obj).to_not receive(:received_message).with(msg) + subject.off(&callback) + end + + it 'deletes all callbacks if not block given' do + expect(obj).to_not receive(:received_message).with(msg) + subject.off + end + end end - it 'leaves handler when event specified' do - subject.on(&callback) - expect(obj).to receive(:received_message).with(msg) - subject.off(:foo, &callback) - subject.emit :message, msg + context 'with unsafe_on subscribers' do + before do + subject.unsafe_on(&callback) + end + + after do + subject.emit :message, msg + end + + it 'does not deregister them' do + expect(obj).to receive(:received_message).with(msg) + subject.off + end + end + + context 'with unsafe_once subscribers' do + before do + subject.unsafe_once(&callback) + end + + after do + subject.emit :message, msg + end + + it 'does not deregister them' do + expect(obj).to receive(:received_message).with(msg) + subject.off + end + end + end + + context '#unsafe_off' do + let(:callback) { Proc.new { |msg| obj.received_message msg } } + + context 'with unsafe_on subscribers' do + before do + subject.unsafe_on(&callback) + end + + after do + subject.emit :message, msg + end + + it 'deregisters them' do + expect(obj).to_not receive(:received_message).with(msg) + subject.unsafe_off + end + end + + context 'with unsafe_once subscribers' do + before do + subject.unsafe_once(&callback) + end + + after do + subject.emit :message, msg + end + + it 'deregister them' do + expect(obj).to_not receive(:received_message).with(msg) + subject.unsafe_off + end + end + + context 'with on subscribers' do + before do + subject.on(&callback) + end + + after do + subject.emit :message, msg + end + + it 'does not deregister them' do + expect(obj).to receive(:received_message).with(msg) + subject.unsafe_off + end + end + + context 'with once subscribers' do + before do + subject.once(&callback) + end + + after do + subject.emit :message, msg + end + + it 'does not deregister them' do + expect(obj).to receive(:received_message).with(msg) + subject.unsafe_off + end end end end