index.ios.spec.js 12.6 KB
Newer Older
yogevbd's avatar
yogevbd committed
1 2 3 4
'use strict';
let expect = require('chai').use(require('sinon-chai')).expect;
import proxyquire from 'proxyquire';
import sinon from 'sinon';
Lidan Hifi's avatar
Lidan Hifi committed
5

6 7
/* eslint-disable no-unused-vars */

yogevbd's avatar
yogevbd committed
8
describe('NotificationsIOS', () => {
9
  let deviceEvents = [
yogevbd's avatar
yogevbd committed
10 11 12 13 14 15
    'pushKitRegistered',
    'remoteNotificationsRegistered',
    'remoteNotificationsRegistrationFailed',
    'notificationReceivedForeground',
    'notificationReceivedBackground',
    'notificationOpened'
16 17
  ];

18 19
  /*eslint-disable indent*/
  let deviceAddEventListener,
Ran Greenberg's avatar
Ran Greenberg committed
20
    deviceRemoveEventListener,
21 22
    nativeAppAddEventListener,
    nativeAppRemoveEventListener,
Ran Greenberg's avatar
Ran Greenberg committed
23 24 25 26 27 28 29 30
    nativeRequestPermissionsWithCategories,
    nativeAbandonPermissions,
    nativeRegisterPushKit,
    nativeBackgroundTimeRemaining,
    nativeConsumeBackgroundQueue,
    nativeLocalNotification,
    nativeCancelLocalNotification,
    nativeCancelAllLocalNotifications,
31
    nativeGetBadgesCount,
Ran Greenberg's avatar
Ran Greenberg committed
32
    nativeSetBadgesCount,
Ryan Eberhardt's avatar
Ryan Eberhardt committed
33
    nativeIsRegisteredForRemoteNotifications,
34 35 36 37
    nativeCheckPermissions,
    nativeRemoveAllDeliveredNotifications,
    nativeRemoveDeliveredNotifications,
    nativeGetDeliveredNotifications;
38

39
  let NotificationsIOS, NotificationAction, NotificationCategory;
40
  let someHandler = () => {};
yogevbd's avatar
yogevbd committed
41 42
  let constantGuid = 'some-random-uuid';
  let identifiers = ['some-random-uuid', 'other-random-uuid'];
43
  /*eslint-enable indent*/
44 45

  before(() => {
46 47
    deviceAddEventListener = sinon.spy();
    deviceRemoveEventListener = sinon.spy();
48 49
    nativeAppAddEventListener = sinon.spy();
    nativeAppRemoveEventListener = sinon.spy();
50 51 52 53
    nativeRequestPermissionsWithCategories = sinon.spy();
    nativeAbandonPermissions = sinon.spy();
    nativeRegisterPushKit = sinon.spy();
    nativeBackgroundTimeRemaining = sinon.spy();
54
    nativeConsumeBackgroundQueue = sinon.spy();
55
    nativeLocalNotification = sinon.spy();
56 57
    nativeCancelLocalNotification = sinon.spy();
    nativeCancelAllLocalNotifications = sinon.spy();
58
    nativeGetBadgesCount = sinon.spy();
59
    nativeSetBadgesCount = sinon.spy();
Ran Greenberg's avatar
Ran Greenberg committed
60
    nativeIsRegisteredForRemoteNotifications = sinon.spy();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
61
    nativeCheckPermissions = sinon.spy();
62 63 64
    nativeRemoveAllDeliveredNotifications = sinon.spy();
    nativeRemoveDeliveredNotifications = sinon.spy();
    nativeGetDeliveredNotifications = sinon.spy();
65

yogevbd's avatar
yogevbd committed
66 67
    let libUnderTest = proxyquire('../index.ios', {
      'uuid': {
68 69
        v4: () => constantGuid
      },
yogevbd's avatar
yogevbd committed
70
      'react-native': {
71
        NativeModules: {
yogevbd's avatar
WIP  
yogevbd committed
72
          RNBridgeModule: {
73 74 75
            requestPermissionsWithCategories: nativeRequestPermissionsWithCategories,
            abandonPermissions: nativeAbandonPermissions,
            registerPushKit: nativeRegisterPushKit,
76
            backgroundTimeRemaining: nativeBackgroundTimeRemaining,
77
            consumeBackgroundQueue: nativeConsumeBackgroundQueue,
78 79
            localNotification: nativeLocalNotification,
            cancelLocalNotification: nativeCancelLocalNotification,
80
            cancelAllLocalNotifications: nativeCancelAllLocalNotifications,
81
            getBadgesCount: nativeGetBadgesCount,
Ran Greenberg's avatar
Ran Greenberg committed
82
            setBadgesCount: nativeSetBadgesCount,
Ryan Eberhardt's avatar
Ryan Eberhardt committed
83
            isRegisteredForRemoteNotifications: nativeIsRegisteredForRemoteNotifications,
84
            checkPermissions: nativeCheckPermissions,
85 86 87
            removeAllDeliveredNotifications: nativeRemoveAllDeliveredNotifications,
            removeDeliveredNotifications: nativeRemoveDeliveredNotifications,
            getDeliveredNotifications: nativeGetDeliveredNotifications
88
          }
89
        },
90 91 92 93 94 95 96 97
        NativeAppEventEmitter: {
          addListener: (...args) => {
            nativeAppAddEventListener(...args);

            return { remove: nativeAppRemoveEventListener };
          }
        },
        DeviceEventEmitter: {
98
          addListener: (...args) => {
99
            deviceAddEventListener(...args);
100 101

            return { remove: deviceRemoveEventListener };
102
          }
103
        },
yogevbd's avatar
yogevbd committed
104
        '@noCallThru': true
105
      }
106 107
    });

108
    NotificationsIOS = libUnderTest.default;
109 110
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
111 112 113
  });

  afterEach(() => {
114 115
    deviceAddEventListener.reset();
    deviceRemoveEventListener.reset();
116 117
    nativeAppAddEventListener.reset();
    nativeAppRemoveEventListener.reset();
118 119 120 121
    nativeRequestPermissionsWithCategories.reset();
    nativeAbandonPermissions.reset();
    nativeRegisterPushKit.reset();
    nativeBackgroundTimeRemaining.reset();
122
    nativeConsumeBackgroundQueue.reset();
123
    nativeLocalNotification.reset();
124 125
    nativeCancelLocalNotification.reset();
    nativeCancelAllLocalNotifications.reset();
Ran Greenberg's avatar
Ran Greenberg committed
126
    nativeIsRegisteredForRemoteNotifications.reset();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
127
    nativeCheckPermissions.reset();
128 129 130
    nativeRemoveAllDeliveredNotifications.reset();
    nativeRemoveDeliveredNotifications.reset();
    nativeGetDeliveredNotifications.reset();
131 132 133
  });

  after(() => {
134 135
    deviceAddEventListener = null;
    deviceRemoveEventListener = null;
136 137
    nativeAppAddEventListener = null;
    nativeAppRemoveEventListener = null;
138 139 140 141
    nativeRequestPermissionsWithCategories = null;
    nativeAbandonPermissions = null;
    nativeRegisterPushKit = null;
    nativeBackgroundTimeRemaining = null;
142
    nativeConsumeBackgroundQueue = null;
143
    nativeLocalNotification = null;
144 145
    nativeCancelLocalNotification = null;
    nativeCancelAllLocalNotifications = null;
Ran Greenberg's avatar
Ran Greenberg committed
146
    nativeIsRegisteredForRemoteNotifications = null;
Ryan Eberhardt's avatar
Ryan Eberhardt committed
147
    nativeCheckPermissions = null;
148 149 150
    nativeRemoveAllDeliveredNotifications = null;
    nativeRemoveDeliveredNotifications = null;
    nativeGetDeliveredNotifications = null;
151 152

    NotificationsIOS = null;
153 154
    NotificationAction = null;
    NotificationCategory = null;
155 156
  });

yogevbd's avatar
yogevbd committed
157
  describe('Add Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
158 159
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
160
        NotificationsIOS.addEventListener(event, someHandler);
161

162
        expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func);
163 164 165
      });
    });

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

169
      expect(deviceAddEventListener).to.not.have.been.called;
170 171 172
    });
  });

yogevbd's avatar
yogevbd committed
173
  describe('Remove Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
174 175
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
176 177
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
178

179
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
180 181 182
      });
    });

yogevbd's avatar
yogevbd committed
183 184
    it('should not unsubscribe to unknown device events', () => {
      let someUnsupportedEvent = 'someUnsupportedEvent';
185 186
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
187

188
      expect(deviceRemoveEventListener).to.not.have.been.called;
189
    });
Lidan Hifi's avatar
Lidan Hifi committed
190
  });
191

yogevbd's avatar
yogevbd committed
192
  describe('Notification actions handling', () => {
193 194 195
    let someAction, someCategory;

    let actionOpts = {
yogevbd's avatar
yogevbd committed
196 197 198 199
      activationMode: 'foreground',
      title: 'someAction',
      behavior: 'default',
      identifier: 'SOME_ACTION'
200 201 202
    };

    beforeEach(() => {
203
      someAction = new NotificationAction(actionOpts, () => {});
204 205

      someCategory = new NotificationCategory({
yogevbd's avatar
yogevbd committed
206
        identifier: 'SOME_CATEGORY',
207
        actions: [someAction],
yogevbd's avatar
yogevbd committed
208
        context: 'default'
209 210 211
      });
    });

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

216
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([{
yogevbd's avatar
yogevbd committed
217
          identifier: 'SOME_CATEGORY',
218
          actions: [actionOpts],
yogevbd's avatar
yogevbd committed
219
          context: 'default'
220 221
        }]);
      });
222

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

226
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([]);
227
      });
228

yogevbd's avatar
yogevbd committed
229
      it('should subscribe to notificationActionReceived event once, with a single event handler', () => {
230
        NotificationsIOS.requestPermissions([someCategory]);
231

232
        expect(nativeAppAddEventListener).to.have.been.calledOnce;
yogevbd's avatar
yogevbd committed
233
        expect(nativeAppAddEventListener).to.have.been.calledWith('notificationActionReceived', sinon.match.func);
234
      });
235 236
    });

yogevbd's avatar
yogevbd committed
237 238
    describe('reset categories', () => {
      it('should remove notificationActionReceived event handler', () => {
239
        NotificationsIOS.resetCategories();
240

241
        expect(nativeAppRemoveEventListener).to.have.been.calledOnce;
242
      });
243
    });
244

yogevbd's avatar
yogevbd committed
245 246
    describe('get badges count', () => {
      it('should call native getBadgesCount', () => {
247 248 249 250 251 252 253
        const callback = (count) => console.log(count);
        NotificationsIOS.getBadgesCount(callback);

        expect(nativeGetBadgesCount).to.have.been.calledWith(callback);
      });
    });

yogevbd's avatar
yogevbd committed
254 255
    describe('set badges count', () => {
      it('should call native setBadgesCount', () => {
256 257 258 259 260 261
        NotificationsIOS.setBadgesCount(44);

        expect(nativeSetBadgesCount).to.have.been.calledWith(44);
      });
    });

262
  });
263

yogevbd's avatar
yogevbd committed
264 265
  describe('register push kit for background notifications', function () {
    it('should call native register push kit method', function () {
266 267 268 269 270 271
      NotificationsIOS.registerPushKit();

      expect(nativeRegisterPushKit).to.have.been.called;
    });
  });

yogevbd's avatar
yogevbd committed
272 273
  describe('Abandon push notifications permissions', () => {
    it('should call native abandon permissions method', () => {
274 275 276 277 278 279
      NotificationsIOS.abandonPermissions();

      expect(nativeAbandonPermissions).to.have.been.called;
    });
  });

yogevbd's avatar
yogevbd committed
280 281
  describe('Get background remaining time', () => {
    it('should call native background remaining time method', () => {
282
      let someCallback = (time) => { };
283

284 285 286
      NotificationsIOS.backgroundTimeRemaining(someCallback);

      expect(nativeBackgroundTimeRemaining).to.have.been.calledWith(someCallback);
287 288
    });
  });
289

yogevbd's avatar
yogevbd committed
290 291
  describe('Dispatch local notification', () => {
    it('should return generated notification guid', () => {
292 293 294
      expect(NotificationsIOS.localNotification({})).to.equal(constantGuid);
    });

yogevbd's avatar
yogevbd committed
295
    it('should call native local notification method with generated notification guid and notification object', () => {
296
      let someLocalNotification = {
yogevbd's avatar
yogevbd committed
297 298 299 300 301
        alertBody: 'some body',
        alertTitle: 'some title',
        alertAction: 'some action',
        soundName: 'sound',
        category: 'SOME_CATEGORY',
302
        userInfo: {
yogevbd's avatar
yogevbd committed
303
          'key': 'value'
304 305 306 307 308
        }
      };

      NotificationsIOS.localNotification(someLocalNotification);

309 310 311 312
      expect(nativeLocalNotification).to.have.been.calledWith(someLocalNotification, constantGuid);
    });
  });

yogevbd's avatar
yogevbd committed
313 314
  describe('Cancel local notification', () => {
    it('should call native cancel local notification method', () => {
315 316 317 318 319 320
      NotificationsIOS.cancelLocalNotification(constantGuid);

      expect(nativeCancelLocalNotification).to.have.been.calledWith(constantGuid);
    });
  });

yogevbd's avatar
yogevbd committed
321 322
  describe('Cancel all local notifications', () => {
    it('should call native cancel all local notifications method', () => {
323 324 325
      NotificationsIOS.cancelAllLocalNotifications();

      expect(nativeCancelAllLocalNotifications).to.have.been.calledWith();
326 327
    });
  });
Ran Greenberg's avatar
Ran Greenberg committed
328

yogevbd's avatar
yogevbd committed
329 330
  describe('Is registered for remote notifications ', () => {
    it('should call native is registered for remote notifications', () => {
Ran Greenberg's avatar
Ran Greenberg committed
331 332 333 334 335
      NotificationsIOS.isRegisteredForRemoteNotifications();
      expect(nativeIsRegisteredForRemoteNotifications).to.have.been.calledWith();

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

yogevbd's avatar
yogevbd committed
337 338
  describe('Check permissions ', () => {
    it('should call native check permissions', () => {
Ryan Eberhardt's avatar
Ryan Eberhardt committed
339 340 341 342 343
      NotificationsIOS.checkPermissions();
      expect(nativeCheckPermissions).to.have.been.calledWith();

    });
  });
344

yogevbd's avatar
yogevbd committed
345 346
  describe('Remove all delivered notifications', () => {
    it('should call native remove all delivered notifications method', () => {
347 348 349 350 351 352
      NotificationsIOS.removeAllDeliveredNotifications();

      expect(nativeRemoveAllDeliveredNotifications).to.have.been.calledWith();
    });
  });

yogevbd's avatar
yogevbd committed
353 354
  describe('Remove delivered notifications', () => {
    it('should call native remove delivered notifications method', () => {
355 356 357 358 359 360
      NotificationsIOS.removeDeliveredNotifications(identifiers);

      expect(nativeRemoveDeliveredNotifications).to.have.been.calledWith(identifiers);
    });
  });

yogevbd's avatar
yogevbd committed
361 362
  describe('Get delivered notifications', () => {
    it('should call native get delivered notifications method', () => {
363 364 365 366 367 368
      const callback = (notifications) => console.log(notifications);
      NotificationsIOS.getDeliveredNotifications(callback);

      expect(nativeGetDeliveredNotifications).to.have.been.calledWith(callback);
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
369
});