|
30 | 30 |
|
31 | 31 | before(:context) do |
32 | 32 | class CallBack |
33 | | - def call; end |
| 33 | + attr_reader :args |
| 34 | + |
| 35 | + def call(*args) |
| 36 | + @args = args |
| 37 | + end |
34 | 38 | end |
35 | 39 |
|
36 | 40 | @callback = CallBack.new |
@@ -73,6 +77,15 @@ def call; end |
73 | 77 | expect(spy_logger).to have_received(:log).once |
74 | 78 | .with(Logger::ERROR, 'Invalid notification callback given.') |
75 | 79 | end |
| 80 | + |
| 81 | + it 'should log and return nil if given both callback and block' do |
| 82 | + result = notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE], |
| 83 | + @callback_reference) {} |
| 84 | + |
| 85 | + expect(result).to be_nil |
| 86 | + expect(spy_logger).to have_received(:log).once |
| 87 | + .with(Logger::ERROR, 'Callback and block are mutually exclusive.') |
| 88 | + end |
76 | 89 | end |
77 | 90 |
|
78 | 91 | describe 'test add notification with valid type and callback' do |
@@ -484,6 +497,39 @@ def deliver_three; end |
484 | 497 | expect(spy_logger).to_not have_received(:log) |
485 | 498 | .with(Logger::INFO, 'delivered three.') |
486 | 499 | end |
| 500 | + |
| 501 | + it 'should send notifications to blocks' do |
| 502 | + actual_args = [] |
| 503 | + notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK]) do |*args| |
| 504 | + actual_args = args |
| 505 | + end |
| 506 | + |
| 507 | + notification_center.send_notifications(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK], |
| 508 | + :arg1, 'arg2', arg3: 4) |
| 509 | + |
| 510 | + expect(actual_args).to eq([:arg1, 'arg2', arg3: 4]) |
| 511 | + end |
| 512 | + |
| 513 | + it 'should send notifications to lambdas' do |
| 514 | + actual_args = [] |
| 515 | + notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK], |
| 516 | + ->(*args) { actual_args = args }) |
| 517 | + |
| 518 | + notification_center.send_notifications(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK], |
| 519 | + :arg1, 'arg2', arg3: 4) |
| 520 | + |
| 521 | + expect(actual_args).to eq([:arg1, 'arg2', arg3: 4]) |
| 522 | + end |
| 523 | + |
| 524 | + it 'should send notifications to callables' do |
| 525 | + callback = CallBack.new |
| 526 | + notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK], callback) |
| 527 | + |
| 528 | + notification_center.send_notifications(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK], |
| 529 | + :arg1, 'arg2', arg3: 4) |
| 530 | + |
| 531 | + expect(callback.args).to eq([:arg1, 'arg2', arg3: 4]) |
| 532 | + end |
487 | 533 | end |
488 | 534 |
|
489 | 535 | describe '.notification_count' do |
|
0 commit comments