spec/flirt/flirt_spec.rb in flirt-0.0.2 vs spec/flirt/flirt_spec.rb in flirt-0.0.3
- old
+ new
@@ -4,96 +4,103 @@
describe Flirt do
describe "with a single :pancake_fried subscriber" do
let(:response) { { topping: "cream" } }
+
let(:event) { :pancake_fried }
+
let(:wrong_event) { :spud_baked }
+
let(:callback) { :respond }
+
let!(:listener) { Object.new }
- before(:each) do
- Flirt.listen listener, event, with: callback
- end
- it "listens to the correct broadcast event" do
- expect(listener).to receive(callback).with(response)
- Flirt.broadcast event, response
- end
+ [:subscribe, :listen].each do |method|
- it "listens to the correct publish event" do
- expect(listener).to receive(callback).with(response)
- Flirt.publish event, response
- end
+ describe "and a listener added with ##{method}" do
- it "doesn't listen to the wrong broadcast event" do
- expect(listener).not_to receive(callback)
- Flirt.broadcast wrong_event, response
- end
+ before(:each) do
+ Flirt.send method, listener, event, with: callback
+ end
- it "doesn't listen to the wrong publish event" do
- expect(listener).not_to receive(callback)
- Flirt.broadcast wrong_event, response
- end
- describe "when disabled" do
- before(:each) { Flirt.disable }
- after(:each) { Flirt.enable }
+ [:publish, :broadcast].each do |method|
- it "doesn't broadcast" do
- expect(listener).not_to receive(callback)
- Flirt.broadcast event, response
- end
+ it "listens to the correct event published with ##{method}" do
+ expect(listener).to receive(callback).with(response)
+ Flirt.send method, event, response
+ end
- it "doesn't publish" do
- expect(listener).not_to receive(callback)
- Flirt.publish event, response
- end
- end
- describe "when cleared" do
+ it "doesn't listen to the wrong event published with ##{method}" do
+ expect(listener).not_to receive(callback)
+ Flirt.send method, wrong_event, response
+ end
- let(:callback2) { :respond2 }
- before(:each) do
- Flirt.listen listener, event, with: callback2
- Flirt.clear
- end
+ describe "when disabled" do
- it "forgets listeners" do
- expect(listener).not_to receive(callback)
- expect(listener).not_to receive(callback2)
- Flirt.publish event, response
- end
+ before(:each) { Flirt.disable }
- end
+ after(:each) { Flirt.enable }
- describe "when unlistened" do
- let(:callback2) { :respond2 }
+ it "doesn't broadcast an event published with ##{method}" do
+ expect(listener).not_to receive(callback)
+ Flirt.send method, event, response
+ end
- before(:each) do
- Flirt.listen listener, event, with: callback2
- Flirt.unlisten listener, event, with: callback
- end
+ end
- it "forgets the listener" do
- expect(listener).not_to receive(callback)
- expect(listener).to receive(callback2).with(response)
- Flirt.broadcast event, response
- end
+ end
- end
+ describe "when cleared" do
- describe "when unsubscribed" do
- before(:each) do
- Flirt.unsubscribe listener, event, with: callback
- end
+ let(:callback2) { :respond2 }
- it "forgets the listener" do
- expect(listener).not_to receive(callback).with(response)
- Flirt.publish event, response
+
+ before(:each) do
+ Flirt.listen listener, event, with: callback2
+ Flirt.clear
+ end
+
+
+ it "forgets listeners" do
+ expect(listener).not_to receive(callback)
+ expect(listener).not_to receive(callback2)
+ Flirt.publish event, response
+ end
+
+ end
+
+ [:unsubscribe, :unlisten].each do |method|
+
+ describe "when another listener is added and the original unsubscribed with ##{method}" do
+
+ let(:callback2) { :respond2 }
+
+
+ before(:each) do
+ Flirt.listen listener, event, with: callback2
+ Flirt.send method, listener, event, with: callback
+ end
+
+
+ it "it forgets the original listener but remembers the new one" do
+ expect(listener).not_to receive(callback)
+ expect(listener).to receive(callback2).with(response)
+ Flirt.broadcast event, response
+ end
+
+ end
+
+ end
+
end
end
+
end
+
end
\ No newline at end of file