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

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

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

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 = () => {};
41
  let constantGuid = "some-random-uuid";
42
  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 66

    let libUnderTest = proxyquire("../index.ios", {
67 68 69
      "uuid": {
        v4: () => constantGuid
      },
70 71
      "react-native": {
        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
        },
104 105
        "@noCallThru": true
      }
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
  });

Lidan Hifi's avatar
Lidan Hifi committed
157 158 159
  describe("Add Event Listener", () => {
    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
      });
    });

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

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

Lidan Hifi's avatar
Lidan Hifi committed
173 174 175
  describe("Remove Event Listener", () => {
    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
      });
    });

Lidan Hifi's avatar
Lidan Hifi committed
183
    it("should not unsubscribe to unknown device events", () => {
184
      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

192
  describe("Notification actions handling", () => {
193 194 195 196 197 198 199 200 201 202
    let someAction, someCategory;

    let actionOpts = {
      activationMode: "foreground",
      title: "someAction",
      behavior: "default",
      identifier: "SOME_ACTION"
    };

    beforeEach(() => {
203
      someAction = new NotificationAction(actionOpts, () => {});
204 205 206 207 208 209 210 211

      someCategory = new NotificationCategory({
        identifier: "SOME_CATEGORY",
        actions: [someAction],
        context: "default"
      });
    });

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

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

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

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

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

232 233
        expect(nativeAppAddEventListener).to.have.been.calledOnce;
        expect(nativeAppAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
234
      });
235 236
    });

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

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

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

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

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

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

262
  });
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

  describe("register push kit for background notifications", function () {
    it("should call native register push kit method", function () {
      NotificationsIOS.registerPushKit();

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

  describe("Abandon push notifications permissions", () => {
    it("should call native abandon permissions method", () => {
      NotificationsIOS.abandonPermissions();

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

  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

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

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

      NotificationsIOS.localNotification(someLocalNotification);

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
      expect(nativeLocalNotification).to.have.been.calledWith(someLocalNotification, constantGuid);
    });
  });

  describe("Cancel local notification", () => {
    it("should call native cancel local notification method", () => {
      NotificationsIOS.cancelLocalNotification(constantGuid);

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

  describe("Cancel all local notifications", () => {
    it("should call native cancel all local notifications method", () => {
      NotificationsIOS.cancelAllLocalNotifications();

      expect(nativeCancelAllLocalNotifications).to.have.been.calledWith();
326 327
    });
  });
Ran Greenberg's avatar
Ran Greenberg committed
328 329 330 331 332 333 334 335

  describe("Is registered for remote notifications ", () => {
    it("should call native is registered for remote notifications", () => {
      NotificationsIOS.isRegisteredForRemoteNotifications();
      expect(nativeIsRegisteredForRemoteNotifications).to.have.been.calledWith();

    });
  });
Ryan Eberhardt's avatar
Ryan Eberhardt committed
336 337 338 339 340 341 342 343

  describe("Check permissions ", () => {
    it("should call native check permissions", () => {
      NotificationsIOS.checkPermissions();
      expect(nativeCheckPermissions).to.have.been.calledWith();

    });
  });
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  describe("Remove all delivered notifications", () => {
    it("should call native remove all delivered notifications method", () => {
      NotificationsIOS.removeAllDeliveredNotifications();

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

  describe("Remove delivered notifications", () => {
    it("should call native remove delivered notifications method", () => {
      NotificationsIOS.removeDeliveredNotifications(identifiers);

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

  describe("Get delivered notifications", () => {
    it("should call native get delivered notifications method", () => {
      const callback = (notifications) => console.log(notifications);
      NotificationsIOS.getDeliveredNotifications(callback);

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