spec/ventable/ventable_spec.rb in ventable-1.2.0 vs spec/ventable/ventable_spec.rb in ventable-1.3.1
- old
+ new
@@ -1,70 +1,73 @@
+# frozen_string_literal: true
+
require 'spec_helper'
describe Ventable do
before do
class TestEvent
include Ventable::Event
end
end
describe '::enabled?' do
- after { Ventable.enable }
+ after { described_class.enable }
it 'is true by default' do
- expect(Ventable.enabled?).to be true
+ expect(described_class.enabled?).to be true
end
it 'is false after Ventable is disabled' do
- Ventable.disable
- expect(Ventable.enabled?).to be false
+ described_class.disable
+ expect(described_class.enabled?).to be false
end
it 'is true after Ventable is re-enabled' do
- Ventable.disable
- Ventable.enable
- expect(Ventable.enabled?).to be true
+ described_class.disable
+ described_class.enable
+ expect(described_class.enabled?).to be true
end
end
describe 'including Ventable::Event' do
- it 'should create a class instance variable to keep observers' do
+ it 'creates a class instance variable to keep observers' do
expect(TestEvent.observers).not_to be_nil
expect(TestEvent.observers.class.name).to eq('Set')
end
- it 'should see observers variable from instance methods' do
+ it 'sees observers variable from instance methods' do
observers = nil
TestEvent.new.instance_eval do
observers = self.class.observers
end
- expect(observers).to_not be_nil
+ expect(observers).not_to be_nil
end
- it 'should maintain separate sets of observers for each event' do
+ it 'maintains separate sets of observers for each event' do
class AnotherEvent
include Ventable::Event
end
- expect(AnotherEvent.observers.object_id).to_not eq(TestEvent.observers.object_id)
+
+ expect(AnotherEvent.observers.object_id).not_to eq(TestEvent.observers.object_id)
end
end
describe '#fire' do
before do
class TestEvent
include Ventable::Event
end
end
- it 'should properly call a Proc observer' do
+ it 'properly call a Proc observer' do
run_block = false
- event = nil
+ event = nil
TestEvent.notifies do |e|
run_block = true
- event = e
+ event = e
end
- expect(run_block).to eq(false)
+ expect(run_block).to be(false)
expect(event).to be_nil
# fire the event
TestEvent.new.fire!
@@ -99,44 +102,43 @@
end
end
end
TestEvent.notifies TestEventObserver, GlobalObserver
- end
-
- let(:event) { TestEvent.new }
-
- before do
expect(TestEvent.flag).to eq('unset')
expect(GlobalObserver).to receive(:handle_event).with(event)
end
- it 'should set the flag and call observers' do
+ let(:event) { TestEvent.new }
+
+ it 'sets the flag and call observers' do
event.fire!
expect(TestEvent.flag).to eq('boo')
end
- it 'should also fire via the publish alias' do
+ it 'also fire via the publish alias' do
event.publish
end
context 'observer without a handler' do
before {
class ObserverWithoutAHandler
end
+
TestEvent.notifies ObserverWithoutAHandler
}
- it 'should raise an exception' do
+
+ it 'raises an exception' do
expect { event.publish }.to raise_error(Ventable::Error)
end
end
end
- it 'should properly call a group of observers' do
- transaction_called = false
+ it 'properly call a group of observers' do
+ transaction_called = false
transaction_completed = false
- transaction = ->(observer_block) {
+ transaction = ->(observer_block) {
transaction_called = true
observer_block.call
transaction_completed = true
}
@@ -144,15 +146,15 @@
observer_block_called = false
# this flag ensures that this block really runs inside
# the transaction group block
transaction_already_completed = false
- event_inside = nil
+ event_inside = nil
TestEvent.notifies inside: :transaction do |event|
- observer_block_called = true
+ observer_block_called = true
transaction_already_completed = transaction_completed
- event_inside = event
+ event_inside = event
end
expect(transaction_called).to be false
expect(transaction_already_completed).to be false
expect(observer_block_called).to be false
@@ -161,22 +163,22 @@
expect(transaction_called).to be true
expect(observer_block_called).to be true
expect(transaction_called).to be true
expect(transaction_already_completed).to be false
- expect(event_inside).to_not be_nil
+ expect(event_inside).not_to be_nil
expect(event_inside).to be_a(TestEvent)
end
context 'when globally disabled' do
- before { Ventable.disable }
- after { Ventable.enable }
+ before { described_class.disable }
+ after { described_class.enable }
it 'does not notify observers' do
observers_notified = false
- TestEvent.notifies do |event|
+ TestEvent.notifies do |_event|
observers_notified = true
end
TestEvent.new.fire!
expect(observers_notified).to be false
@@ -194,29 +196,42 @@
class AnotherSweetEvent
include Ventable::Event
end
end
- class SomeOtherStuffHappened
- include Ventable::Event
- end
-
class ClassWithCustomCallbackMethodEvent
include Ventable::Event
def self.ventable_callback_method_name
:handle_my_special_event
end
end
+
+ module Blah
+ module Foo
+ class BarClosedEvent
+ include Ventable::Event
+ end
+ end
+ end
end
- it 'should properly set the callback method name' do
+ it 'properly sets the callback method on a Module-less class' do
expect(SomeAwesomeEvent.send(:default_callback_method)).to eq(:handle_some_awesome)
+ end
+
+ it 'properly sets the callback method on a Class within a Module' do
expect(Blah::AnotherSweetEvent.send(:default_callback_method)).to eq(:handle_blah__another_sweet)
- expect(SomeOtherStuffHappened.send(:default_callback_method)).to eq(:handle_some_other_stuff_happened)
+ end
+
+ it 'properly sets the callback method on a class with a custom default method handler name' do
expect(ClassWithCustomCallbackMethodEvent.send(:default_callback_method)).to eq(:handle_my_special_event)
end
+
+ it 'properly sets the callback method on a class within a nested module' do
+ expect(Blah::Foo::BarClosedEvent.send(:default_callback_method)).to eq(:handle_blah__foo__bar_closed)
+ end
end
describe '#configure' do
it 'properly configures the event with observers' do
notified_observer = false
@@ -228,11 +243,11 @@
TestEvent.new.fire!
expect(notified_observer).to be true
end
it 'configures observers with groups' do
- notified_observer = false
+ notified_observer = false
called_transaction = false
TestEvent.configure do
group :transaction, &->(b) {
b.call
called_transaction = true
@@ -245,28 +260,25 @@
expect(notified_observer).to be true
expect(called_transaction).to be true
end
it 'throws exception if :inside references unknown group' do
- begin
- TestEvent.configure do
- notifies inside: :transaction do
- # some stuff
- end
+ TestEvent.configure do
+ notifies inside: :transaction do
+ # some stuff
end
- fail 'Shouldn\'t reach here, must throw a valid exception'
- rescue Exception => e
- expect(e.class).to eq(Ventable::Error)
end
+ fail 'Shouldn\'t reach here, must throw a valid exception'
+ rescue Exception => e
+ expect(e.class).to eq(Ventable::Error)
end
+
it 'throws exception if nil observer added to the list' do
- begin
- TestEvent.configure do
- notifies nil
- end
- fail 'Shouldn\'t reach here, must throw a valid exception'
- rescue Exception => e
- expect(e.class).to eq(Ventable::Error)
+ TestEvent.configure do
+ notifies nil
end
+ fail 'Shouldn\'t reach here, must throw a valid exception'
+ rescue Exception => e
+ expect(e.class).to eq(Ventable::Error)
end
end
end