cirandas.net

ref: master

plugins/push_notification/test/unit/push_notification_plugin_test.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require_relative '../../lib/push_notification_helper.rb'
require 'test_helper'

class PushNotificationPluginTest < ActiveSupport::TestCase
  include  PushNotificationHelper

  def setup
    environment = Environment.default
    environment.enable_plugin(PushNotificationPlugin)
  end

  should 'subscribe and unsubscribe to notification' do
    class AnyClass
      def self.push_notification_new_comment_additional_users
        ['YO']
      end
    end

    assert PushNotificationPlugin::subscribe(Environment.default, "new_comment", AnyClass)
    assert_equivalent PushNotificationPlugin::subscribers(Environment.default, "new_comment"), [AnyClass.name.constantize]
    assert PushNotificationPlugin::unsubscribe(Environment.default, "new_comment", AnyClass)
    assert_empty PushNotificationPlugin::subscribers(Environment.default, "new_comment")
  end

  should 'get additional users from subscribers' do
    class AnyClass
      def self.push_notification_new_comment_additional_users
        ['YO']
      end
    end

    PushNotificationPlugin::subscribe(Environment.default, "new_comment", AnyClass)
    AnyClass.expects(:push_notification_new_comment_additional_users).returns(['YO'])
    subscribers_additional_users("new_comment", Environment.default)
  end

  should 'return nill for unknown notification subscription methods' do
    class AnyEventCallbackClass
      def self.push_notification_any_event_additional_users
        ['YO']
      end
    end

    assert_nil PushNotificationPlugin::subscribe(Environment.default, "any_event", AnyEventCallbackClass)
    assert_nil PushNotificationPlugin::unsubscribe(Environment.default, "any_event", AnyEventCallbackClass)
    assert_nil PushNotificationPlugin::subscribers(Environment.default, "any_event")
  end

  should 'return empty list for known notification without subscribers' do
    class CommentCallbackClass
      def self.push_notification_new_comment_additional_users
        ['YO']
      end
    end

    refute PushNotificationPlugin::unsubscribe(Environment.default, "new_comment", CommentCallbackClass)
    assert_empty PushNotificationPlugin::subscribers(Environment.default, "new_comment")
  end

  should 'not subscribe to notification if correspondent method callback is not implemented' do
    class NoCallbackClass
    end

    assert_nil PushNotificationPlugin::subscribe(Environment.default, "new_comment", NoCallbackClass)
    assert_empty PushNotificationPlugin::subscribers(Environment.default, "new_comment")
  end

end