Skip to content

Renames notification-center methods #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/optimizely/notification_center.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2017, Optimizely and contributors
# Copyright 2017-2018, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,20 +86,31 @@ def remove_notification_listener(notification_id)
false
end

# @deprecated Use {#clear_notification_listeners} instead.
def clear_notifications(notification_type)
# Removes notifications for a certain notification type
#
# Args:
# notification_type: one of the constants in NOTIFICATION_TYPES
@logger.log Logger::WARN, "'clear_notifications' is deprecated. Call 'clear_notification_listeners' instead."
clear_notification_listeners(notification_type)
end

# Removes notifications for a certain notification type
#
# @param notification_type - one of the constants in NOTIFICATION_TYPES

def clear_notification_listeners(notification_type)
return nil unless notification_type_valid?(notification_type)

@notifications[notification_type] = []
@logger.log Logger::INFO, "All callbacks for notification type #{notification_type} have been removed."
end

# @deprecated Use {#clear_all_notification_listeners} instead.
def clean_all_notifications
# Removes all notifications
@logger.log Logger::WARN, "'clean_all_notifications' is deprecated. Call 'clear_all_notification_listeners' instead."
clear_all_notification_listeners
end

# Removes all notifications
def clear_all_notification_listeners
@notifications.each_key { |key| @notifications[key] = [] }
end

Expand Down
33 changes: 25 additions & 8 deletions spec/notification_center_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def call; end

it 'should not remove notifications for an invalid notification type' do
invalid_type = 'Invalid notification'
expect { @inner_notification_center.clear_notifications(invalid_type) }
expect { @inner_notification_center.clear_notification_listeners(invalid_type) }
.to raise_error(Optimizely::InvalidNotificationType)
expect(spy_logger).to have_received(:log).once
.with(Logger::ERROR, 'Invalid notification type.')
Expand All @@ -273,7 +273,7 @@ def call; end

it 'should remove all notifications for a valid notification type' do
notification_type = Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE]
@inner_notification_center.clear_notifications(notification_type)
@inner_notification_center.clear_notification_listeners(notification_type)
expect(spy_logger).to have_received(:log).once
.with(Logger::INFO, "All callbacks for notification type #{notification_type} have been removed.")
expect(
Expand All @@ -288,10 +288,19 @@ def call; end
).to eq(1)
end

it 'should not throw an error when clear_notifications is called again for the same notification type' do
it 'should call clear_notification_listeners and log depreciation message' do
notification_type = Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE]
expect(@inner_notification_center).to receive(:clear_notification_listeners).once.with(notification_type)
@inner_notification_center.clear_notifications(notification_type)
expect { @inner_notification_center.clear_notifications(notification_type) }
expect(spy_logger).to have_received(:log).once.with(
Logger::WARN, "'clear_notifications' is deprecated. Call 'clear_notification_listeners' instead."
)
end

it 'should not throw an error when clear_notification_listeners is called again for the same notification type' do
notification_type = Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE]
@inner_notification_center.clear_notification_listeners(notification_type)
expect { @inner_notification_center.clear_notification_listeners(notification_type) }
.to_not raise_error(Optimizely::InvalidNotificationType)
expect(
@inner_notification_center.notifications[
Expand Down Expand Up @@ -361,17 +370,25 @@ def call; end
end

it 'should remove all notifications for each notification type' do
@inner_notification_center.clean_all_notifications
@inner_notification_center.clear_all_notification_listeners
@inner_notification_center.notifications.each_key do |key|
expect(@inner_notification_center.notifications[key]).to be_empty
end
end

it 'clean_all_notifications does not throw an error when called again' do
@inner_notification_center.clean_all_notifications
expect { @inner_notification_center.clean_all_notifications }
it 'clear_all_notification_listeners does not throw an error when called again' do
@inner_notification_center.clear_all_notification_listeners
expect { @inner_notification_center.clear_all_notification_listeners }
.to_not raise_error
end

it 'should call clear_all_notification_listeners and log depreciation message' do
expect(@inner_notification_center).to receive(:clear_all_notification_listeners).once
@inner_notification_center.clean_all_notifications
expect(spy_logger).to have_received(:log).once.with(
Logger::WARN, "'clean_all_notifications' is deprecated. Call 'clear_all_notification_listeners' instead."
)
end
end

describe '.send_notifications' do
Expand Down