index.ios.spec.js 8.66 KB
Newer Older
yogevbd's avatar
yogevbd committed
1
describe('NotificationsIOS', () => {
2
  let deviceEvents = [
yogevbd's avatar
yogevbd committed
3 4 5 6 7
    'pushKitRegistered',
    'remoteNotificationsRegistered',
    'remoteNotificationsRegistrationFailed',
    'notificationReceivedForeground',
    'notificationOpened'
8 9
  ];

10
  let NotificationsIOS, NotificationAction, NotificationCategory;
yogevbd's avatar
yogevbd committed
11 12
  let constantGuid = 'some-random-uuid';
  let identifiers = ['some-random-uuid', 'other-random-uuid'];
yogevbd's avatar
yogevbd committed
13 14
  let someHandler = () => {};
  let nativeModule;
yogevbd's avatar
yogevbd committed
15
  let DeviceEventEmitter;
yogevbd's avatar
yogevbd committed
16 17 18

  beforeEach(() => {
    jest.mock('react-native', () => {
yogevbd's avatar
yogevbd committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
      const RNBridgeModule = {
        requestPermissionsWithCategories: jest.fn(),
        abandonPermissions: jest.fn(),
        registerPushKit: jest.fn(),
        backgroundTimeRemaining: jest.fn(),
        consumeBackgroundQueue: jest.fn(),
        localNotification: jest.fn(),
        cancelLocalNotification: jest.fn(),
        cancelAllLocalNotifications: jest.fn(),
        getBadgesCount: jest.fn(),
        setBadgesCount: jest.fn(),
        isRegisteredForRemoteNotifications: jest.fn(),
        checkPermissions: jest.fn(),
        removeAllDeliveredNotifications: jest.fn(),
        removeDeliveredNotifications: jest.fn(),
        getDeliveredNotifications: jest.fn()
      };
yogevbd's avatar
yogevbd committed
36
      return {
37
        NativeModules: {
yogevbd's avatar
yogevbd committed
38
          RNBridgeModule
39
        },
40
        DeviceEventEmitter: {
yogevbd's avatar
yogevbd committed
41 42 43 44 45
          addListener: jest.fn(() => {
            return {
              remove: jest.fn()
            };
          })
yogevbd's avatar
yogevbd committed
46 47 48
        }
      };
    });
49

yogevbd's avatar
yogevbd committed
50 51
    nativeModule = require('react-native').NativeModules.RNBridgeModule;
    DeviceEventEmitter = require('react-native').DeviceEventEmitter;
yogevbd's avatar
yogevbd committed
52 53
    jest.mock('uuid', () => {
      return {
yogevbd's avatar
yogevbd committed
54
        v4: () => 'some-random-uuid'
yogevbd's avatar
yogevbd committed
55
      };
56 57
    });

yogevbd's avatar
yogevbd committed
58
    let libUnderTest = require('../lib/src/index.ios');
59
    NotificationsIOS = libUnderTest.default;
60 61
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
62 63
  });

yogevbd's avatar
yogevbd committed
64
  describe('Add Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
65 66
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
67
        NotificationsIOS.addEventListener(event, someHandler);
yogevbd's avatar
yogevbd committed
68
        expect(DeviceEventEmitter.addListener).toHaveBeenCalledWith(event, expect.any(Function));
69 70 71
      });
    });

yogevbd's avatar
yogevbd committed
72 73
    it('should not subscribe to unknown device events', () => {
      NotificationsIOS.addEventListener('someUnsupportedEvent', someHandler);
74

yogevbd's avatar
yogevbd committed
75
      expect(DeviceEventEmitter.addListener).toHaveBeenCalledTimes(0);
76 77 78
    });
  });

yogevbd's avatar
yogevbd committed
79
  describe('Remove Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
80 81
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
yogevbd's avatar
yogevbd committed
82 83 84 85
        const removeCallback = jest.fn();
        DeviceEventEmitter.addListener.mockReturnValueOnce({
          remove: removeCallback
        });
86 87
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
88

yogevbd's avatar
yogevbd committed
89
        expect(removeCallback).toHaveBeenCalledTimes(1);
90 91 92
      });
    });

yogevbd's avatar
yogevbd committed
93 94
    it('should not unsubscribe to unknown device events', () => {
      let someUnsupportedEvent = 'someUnsupportedEvent';
yogevbd's avatar
yogevbd committed
95 96 97 98 99
      const removeCallback = jest.fn();
      DeviceEventEmitter.addListener.mockReturnValueOnce({
        remove: removeCallback
      });

100 101
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
102

yogevbd's avatar
yogevbd committed
103
      expect(removeCallback).toHaveBeenCalledTimes(0);
104
    });
Lidan Hifi's avatar
Lidan Hifi committed
105
  });
106

yogevbd's avatar
yogevbd committed
107
  describe('Notification actions handling', () => {
108 109 110
    let someAction, someCategory;

    let actionOpts = {
yogevbd's avatar
yogevbd committed
111 112 113 114
      activationMode: 'foreground',
      title: 'someAction',
      behavior: 'default',
      identifier: 'SOME_ACTION'
115 116 117
    };

    beforeEach(() => {
118
      someAction = new NotificationAction(actionOpts, () => {});
119 120

      someCategory = new NotificationCategory({
yogevbd's avatar
yogevbd committed
121
        identifier: 'SOME_CATEGORY',
122
        actions: [someAction],
yogevbd's avatar
yogevbd committed
123
        context: 'default'
124 125 126
      });
    });

yogevbd's avatar
yogevbd committed
127 128
    describe('register push notifications', () => {
      it('should call native request permissions with array of categories', () => {
129
        NotificationsIOS.requestPermissions([someCategory]);
130

yogevbd's avatar
yogevbd committed
131
        expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([{
yogevbd's avatar
yogevbd committed
132
          identifier: 'SOME_CATEGORY',
133
          actions: [actionOpts],
yogevbd's avatar
yogevbd committed
134
          context: 'default'
135 136
        }]);
      });
137

yogevbd's avatar
yogevbd committed
138
      it('should call native request permissions with empty array if no categories specified', () => {
139
        NotificationsIOS.requestPermissions();
140

yogevbd's avatar
yogevbd committed
141
        expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([]);
142
      });
143
    });
144

yogevbd's avatar
yogevbd committed
145 146
    describe('get badges count', () => {
      it('should call native getBadgesCount', () => {
147 148 149
        const callback = (count) => console.log(count);
        NotificationsIOS.getBadgesCount(callback);

yogevbd's avatar
yogevbd committed
150
        expect(nativeModule.getBadgesCount).toHaveBeenCalledWith(callback);
151 152 153
      });
    });

yogevbd's avatar
yogevbd committed
154 155
    describe('set badges count', () => {
      it('should call native setBadgesCount', () => {
156 157
        NotificationsIOS.setBadgesCount(44);

yogevbd's avatar
yogevbd committed
158
        expect(nativeModule.setBadgesCount).toHaveBeenCalledWith(44);
159 160 161
      });
    });

162
  });
163

yogevbd's avatar
yogevbd committed
164 165
  describe('register push kit for background notifications', function () {
    it('should call native register push kit method', function () {
166 167
      NotificationsIOS.registerPushKit();

yogevbd's avatar
yogevbd committed
168
      expect(nativeModule.registerPushKit).toHaveBeenCalledTimes(1);
169 170 171
    });
  });

yogevbd's avatar
yogevbd committed
172 173
  describe('Abandon push notifications permissions', () => {
    it('should call native abandon permissions method', () => {
174 175
      NotificationsIOS.abandonPermissions();

yogevbd's avatar
yogevbd committed
176
      expect(nativeModule.abandonPermissions).toHaveBeenCalledTimes(1);
177 178 179
    });
  });

yogevbd's avatar
yogevbd committed
180 181
  describe('Get background remaining time', () => {
    it('should call native background remaining time method', () => {
yogevbd's avatar
yogevbd committed
182
      let someCallback = () => {};
183

184 185
      NotificationsIOS.backgroundTimeRemaining(someCallback);

yogevbd's avatar
yogevbd committed
186
      expect(nativeModule.backgroundTimeRemaining).toHaveBeenCalledWith(someCallback);
187 188
    });
  });
189

yogevbd's avatar
yogevbd committed
190 191
  describe('Dispatch local notification', () => {
    it('should return generated notification guid', () => {
yogevbd's avatar
yogevbd committed
192
      expect(NotificationsIOS.localNotification({})).toEqual(constantGuid);
193 194
    });

yogevbd's avatar
yogevbd committed
195
    it('should call native local notification method with generated notification guid and notification object', () => {
196
      let someLocalNotification = {
yogevbd's avatar
WIP  
yogevbd committed
197 198
        body: 'some body',
        title: 'some title',
yogevbd's avatar
yogevbd committed
199
        alertAction: 'some action',
yogevbd's avatar
WIP  
yogevbd committed
200
        sound: 'sound',
yogevbd's avatar
yogevbd committed
201
        category: 'SOME_CATEGORY',
202
        userInfo: {
yogevbd's avatar
yogevbd committed
203
          'key': 'value'
204 205 206 207 208
        }
      };

      NotificationsIOS.localNotification(someLocalNotification);

yogevbd's avatar
yogevbd committed
209
      expect(nativeModule.localNotification).toHaveBeenCalledWith(someLocalNotification, constantGuid);
210 211 212
    });
  });

yogevbd's avatar
yogevbd committed
213 214
  describe('Cancel local notification', () => {
    it('should call native cancel local notification method', () => {
215 216
      NotificationsIOS.cancelLocalNotification(constantGuid);

yogevbd's avatar
yogevbd committed
217
      expect(nativeModule.cancelLocalNotification).toHaveBeenCalledWith(constantGuid);
218 219 220
    });
  });

yogevbd's avatar
yogevbd committed
221 222
  describe('Cancel all local notifications', () => {
    it('should call native cancel all local notifications method', () => {
223 224
      NotificationsIOS.cancelAllLocalNotifications();

yogevbd's avatar
yogevbd committed
225
      expect(nativeModule.cancelAllLocalNotifications).toHaveBeenCalledWith();
226 227
    });
  });
Ran Greenberg's avatar
Ran Greenberg committed
228

yogevbd's avatar
yogevbd committed
229 230
  describe('Is registered for remote notifications ', () => {
    it('should call native is registered for remote notifications', () => {
Ran Greenberg's avatar
Ran Greenberg committed
231
      NotificationsIOS.isRegisteredForRemoteNotifications();
yogevbd's avatar
yogevbd committed
232
      expect(nativeModule.isRegisteredForRemoteNotifications).toHaveBeenCalledWith();
Ran Greenberg's avatar
Ran Greenberg committed
233 234 235

    });
  });
Ryan Eberhardt's avatar
Ryan Eberhardt committed
236

yogevbd's avatar
yogevbd committed
237 238
  describe('Check permissions ', () => {
    it('should call native check permissions', () => {
Ryan Eberhardt's avatar
Ryan Eberhardt committed
239
      NotificationsIOS.checkPermissions();
yogevbd's avatar
yogevbd committed
240
      expect(nativeModule.checkPermissions).toHaveBeenCalledWith();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
241 242 243

    });
  });
244

yogevbd's avatar
yogevbd committed
245 246
  describe('Remove all delivered notifications', () => {
    it('should call native remove all delivered notifications method', () => {
247 248
      NotificationsIOS.removeAllDeliveredNotifications();

yogevbd's avatar
yogevbd committed
249
      expect(nativeModule.removeAllDeliveredNotifications).toHaveBeenCalledWith();
250 251 252
    });
  });

yogevbd's avatar
yogevbd committed
253 254
  describe('Remove delivered notifications', () => {
    it('should call native remove delivered notifications method', () => {
255 256
      NotificationsIOS.removeDeliveredNotifications(identifiers);

yogevbd's avatar
yogevbd committed
257
      expect(nativeModule.removeDeliveredNotifications).toHaveBeenCalledWith(identifiers);
258 259 260
    });
  });

yogevbd's avatar
yogevbd committed
261 262
  describe('Get delivered notifications', () => {
    it('should call native get delivered notifications method', () => {
263 264 265
      const callback = (notifications) => console.log(notifications);
      NotificationsIOS.getDeliveredNotifications(callback);

yogevbd's avatar
yogevbd committed
266
      expect(nativeModule.getDeliveredNotifications).toHaveBeenCalledWith(callback);
267 268
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
269
});