Skip to content

Commit

Permalink
Add target and notifier module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simukappu committed Aug 9, 2016
1 parent c224aec commit 3bf0d1c
Show file tree
Hide file tree
Showing 12 changed files with 643 additions and 29 deletions.
4 changes: 3 additions & 1 deletion lib/activity_notification/apis/notification_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def open_all_of(target, opened_at = nil)
#TODO description
# Call from controllers or views to avoid N+1
def group_member_exists?(notifications)
where(group_owner_id: notifications.pluck(:id)).exists?
notifications.present? ?
where(group_owner_id: notifications.pluck(:id)).exists? :
false
end

def available_options
Expand Down
2 changes: 1 addition & 1 deletion lib/activity_notification/models/concerns/notifiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def notifiable_path(target_type, key = nil)
# TODO docs
# overriding_notification_email_key(target, key)

# Wrapper methods of SimpleNotify class methods
# Wrapper methods of Notification class methods

def notify(target_type, options = {})
Notification.notify(target_type, self, options)
Expand Down
23 changes: 16 additions & 7 deletions lib/activity_notification/models/concerns/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def opened_notification_index(limit = ActivityNotification.config.opened_limit)
end


# Wrapper methods of SimpleNotify class methods
# Wrapper methods of Notification class methods

def notify_to(notifiable, options = {})
Notification.notify_to(self, notifiable, options)
Expand All @@ -70,6 +70,7 @@ def open_all_notifications(opened_at = nil)
Notification.open_all_of(self, opened_at)
end


# Methods to be overriden

# Typical method to get notifications index
Expand All @@ -85,15 +86,23 @@ def notification_index_with_attributes(limit = nil)
end

def unopened_notification_index_with_attributes(limit = nil)
Notification.group_member_exists?(unopened_notification_index(limit)) ?
unopened_notification_index(limit).with_target.with_notifiable.with_group.with_notifier :
unopened_notification_index(limit).with_target.with_notifiable.with_notifier
if unopened_notification_index(limit).present?
Notification.group_member_exists?(unopened_notification_index(limit)) ?
unopened_notification_index(limit).with_target.with_notifiable.with_group.with_notifier :
unopened_notification_index(limit).with_target.with_notifiable.with_notifier
else
notifications.none
end
end

def opened_notification_index_with_attributes(limit = ActivityNotification.config.opened_limit)
Notification.group_member_exists?(opened_notification_index(limit)) ?
opened_notification_index(limit).with_target.with_notifiable.with_group.with_notifier :
opened_notification_index(limit).with_target.with_notifiable.with_notifier
if opened_notification_index(limit).present?
Notification.group_member_exists?(opened_notification_index(limit)) ?
opened_notification_index(limit).with_target.with_notifiable.with_group.with_notifier :
opened_notification_index(limit).with_target.with_notifiable.with_notifier
else
notifications.none
end
end

def authenticate_with_devise?(current_resource)
Expand Down
41 changes: 21 additions & 20 deletions spec/concerns/models/notifiable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
end

describe "notification_targets" do
describe "without any configuration" do
context "without any configuration" do
it "raise NotImplementedError" do
expect { test_instance.notification_targets(User, 'dummy_key') }
.to raise_error(NotImplementedError, /You have to implement .+ or set :targets in acts_as_notifiable/)
end
end

describe "configured with overriden method" do
context "configured with overriden method" do
it "returns specified value" do
module AdditionalMethods
def notification_users(key)
Expand All @@ -55,7 +55,7 @@ def notification_users(key)
end
end

describe "configured with a field" do
context "configured with a field" do
it "returns specified value" do
described_class._notification_targets[:users] = User.all
expect(test_instance.notification_targets(User, 'dummy_key')).to eq(User.all)
Expand Down Expand Up @@ -96,13 +96,13 @@ def custom_notification_users(key)
end

describe "notification_group" do
describe "without any configuration" do
context "without any configuration" do
it "returns nil" do
expect(test_instance.notification_group(User, 'dummy_key')).to be_nil
end
end

describe "configured with overriden method" do
context "configured with overriden method" do
it "returns specified value" do
module AdditionalMethods
def notification_group_for_users(key)
Expand All @@ -114,7 +114,7 @@ def notification_group_for_users(key)
end
end

describe "configured with a field" do
context "configured with a field" do
it "returns specified value" do
described_class._notification_group[:users] = User.all.first
expect(test_instance.notification_group(User, 'dummy_key')).to eq(User.all.first)
Expand Down Expand Up @@ -155,13 +155,13 @@ def custom_notification_group(key)
end

describe "notification_parameters" do
describe "without any configuration" do
context "without any configuration" do
it "returns blank hash" do
expect(test_instance.notification_parameters(User, 'dummy_key')).to eq({})
end
end

describe "configured with overriden method" do
context "configured with overriden method" do
it "returns specified value" do
module AdditionalMethods
def notification_parameters_for_users(key)
Expand All @@ -173,7 +173,7 @@ def notification_parameters_for_users(key)
end
end

describe "configured with a field" do
context "configured with a field" do
it "returns specified value" do
described_class._notification_parameters[:users] = { hoge: 'fuga', foo: 'bar' }
expect(test_instance.notification_parameters(User, 'dummy_key')).to eq({ hoge: 'fuga', foo: 'bar' })
Expand Down Expand Up @@ -214,13 +214,13 @@ def custom_notification_parameters(key)
end

describe "notifier" do
describe "without any configuration" do
context "without any configuration" do
it "returns nil" do
expect(test_instance.notifier(User, 'dummy_key')).to be_nil
end
end

describe "configured with overriden method" do
context "configured with overriden method" do
it "returns specified value" do
module AdditionalMethods
def notifier_for_users(key)
Expand All @@ -232,7 +232,7 @@ def notifier_for_users(key)
end
end

describe "configured with a field" do
context "configured with a field" do
it "returns specified value" do
described_class._notifier[:users] = User.all.first
expect(test_instance.notifier(User, 'dummy_key')).to eq(User.all.first)
Expand Down Expand Up @@ -273,17 +273,18 @@ def custom_notifier(key)
end

describe "notification_email_allowed?" do
describe "without any configuration" do
context "without any configuration" do
it "returns ActivityNotification.config.email_enabled" do
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to eq(ActivityNotification.config.email_enabled)
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key'))
.to eq(ActivityNotification.config.email_enabled)
end

it "returns false as default" do
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to be_falsey
end
end

describe "configured with overriden method" do
context "configured with overriden method" do
it "returns specified value" do
module AdditionalMethods
def notification_email_allowed_for_users?(target, key)
Expand All @@ -295,7 +296,7 @@ def notification_email_allowed_for_users?(target, key)
end
end

describe "configured with a field" do
context "configured with a field" do
it "returns specified value" do
described_class._notification_email_allowed[:users] = true
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to eq(true)
Expand Down Expand Up @@ -336,21 +337,21 @@ def custom_notification_email_allowed?(target, key)
end

describe "notifiable_path" do
describe "without any configuration" do
context "without any configuration" do
it "raise NotImplementedError" do
expect { test_instance.notifiable_path(User, 'dummy_key') }
.to raise_error(NotImplementedError, /You have to implement .+, set :notifiable_path in acts_as_notifiable or set polymorphic_path routing for/)
end
end

describe "configured with polymorphic_path" do
context "configured with polymorphic_path" do
it "returns polymorphic_path" do
article = create(:article)
expect(article.notifiable_path(User, 'dummy_key')).to eq(article_path(article))
end
end

describe "configured with overriden method" do
context "configured with overriden method" do
it "returns specified value" do
module AdditionalMethods
def notifiable_path_for_users(key)
Expand All @@ -362,7 +363,7 @@ def notifiable_path_for_users(key)
end
end

describe "configured with a field" do
context "configured with a field" do
it "returns specified value" do
described_class._notifiable_path[:users] = article_path(1)
expect(test_instance.notifiable_path(User, 'dummy_key')).to eq(article_path(1))
Expand Down
23 changes: 23 additions & 0 deletions spec/concerns/models/notifier_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
shared_examples_for :notifier do
let(:test_class_name) { described_class.to_s.underscore.split('/').last.to_sym }
let(:test_instance) { create(test_class_name) }

describe "with association" do
it "has many sent_notifications" do
notification_1 = create(:notification, notifier: test_instance)
notification_2 = create(:notification, notifier: test_instance)
expect(test_instance.sent_notifications.count).to eq(2)
expect(test_instance.sent_notifications.earliest).to eq(notification_1)
expect(test_instance.sent_notifications.latest).to eq(notification_2)
end
end

describe "as public class methods" do
describe "available_as_notifier?" do
it "returns true" do
expect(described_class.available_as_notifier?).to be_truthy
end
end
end

end
Loading

0 comments on commit 3bf0d1c

Please sign in to comment.