index.ios.spec.js 12.4 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 21 22 23 24 25 26 27 28
    deviceRemoveEventListener,
    nativeRequestPermissionsWithCategories,
    nativeAbandonPermissions,
    nativeRegisterPushKit,
    nativeBackgroundTimeRemaining,
    nativeConsumeBackgroundQueue,
    nativeLocalNotification,
    nativeCancelLocalNotification,
    nativeCancelAllLocalNotifications,
29
    nativeGetBadgesCount,
Ran Greenberg's avatar
Ran Greenberg committed
30
    nativeSetBadgesCount,
Ryan Eberhardt's avatar
Ryan Eberhardt committed
31
    nativeIsRegisteredForRemoteNotifications,
32 33 34 35
    nativeCheckPermissions,
    nativeRemoveAllDeliveredNotifications,
    nativeRemoveDeliveredNotifications,
    nativeGetDeliveredNotifications;
36

37
  let NotificationsIOS, NotificationAction, NotificationCategory;
38 39
  let someHandler = () => {
  };
40
  let constantGuid = "some-random-uuid";
41
  let identifiers = ["some-random-uuid", "other-random-uuid"];
42
  /*eslint-enable indent*/
43 44

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

63

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

98
    NotificationsIOS = libUnderTest.default;
99 100
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
101 102 103
  });

  afterEach(() => {
104 105
    deviceAddEventListener.reset();
    deviceRemoveEventListener.reset();
106 107 108 109
    nativeRequestPermissionsWithCategories.reset();
    nativeAbandonPermissions.reset();
    nativeRegisterPushKit.reset();
    nativeBackgroundTimeRemaining.reset();
110
    nativeConsumeBackgroundQueue.reset();
111
    nativeLocalNotification.reset();
112 113
    nativeCancelLocalNotification.reset();
    nativeCancelAllLocalNotifications.reset();
Ran Greenberg's avatar
Ran Greenberg committed
114
    nativeIsRegisteredForRemoteNotifications.reset();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
115
    nativeCheckPermissions.reset();
116 117 118
    nativeRemoveAllDeliveredNotifications.reset();
    nativeRemoveDeliveredNotifications.reset();
    nativeGetDeliveredNotifications.reset();
119 120 121
  });

  after(() => {
122 123
    deviceAddEventListener = null;
    deviceRemoveEventListener = null;
124 125 126 127
    nativeRequestPermissionsWithCategories = null;
    nativeAbandonPermissions = null;
    nativeRegisterPushKit = null;
    nativeBackgroundTimeRemaining = null;
128
    nativeConsumeBackgroundQueue = null;
129
    nativeLocalNotification = null;
130 131
    nativeCancelLocalNotification = null;
    nativeCancelAllLocalNotifications = null;
Ran Greenberg's avatar
Ran Greenberg committed
132
    nativeIsRegisteredForRemoteNotifications = null;
Ryan Eberhardt's avatar
Ryan Eberhardt committed
133
    nativeCheckPermissions = null;
134 135 136
    nativeRemoveAllDeliveredNotifications = null;
    nativeRemoveDeliveredNotifications = null;
    nativeGetDeliveredNotifications = null;
137 138

    NotificationsIOS = null;
139 140
    NotificationAction = null;
    NotificationCategory = null;
141 142
  });

Lidan Hifi's avatar
Lidan Hifi committed
143 144 145
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
146
        NotificationsIOS.addEventListener(event, someHandler);
147

148
        expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func);
149 150 151
      });
    });

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

155
      expect(deviceAddEventListener).to.not.have.been.called;
156 157 158
    });
  });

Lidan Hifi's avatar
Lidan Hifi committed
159 160 161
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
162 163
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
164

165
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
166 167 168
      });
    });

Lidan Hifi's avatar
Lidan Hifi committed
169
    it("should not unsubscribe to unknown device events", () => {
170
      let someUnsupportedEvent = "someUnsupportedEvent";
171 172
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
173

174
      expect(deviceRemoveEventListener).to.not.have.been.called;
175
    });
Lidan Hifi's avatar
Lidan Hifi committed
176
  });
177

178
  describe("Notification actions handling", () => {
179 180 181 182 183 184 185 186 187 188
    let someAction, someCategory;

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

    beforeEach(() => {
189 190
      someAction = new NotificationAction(actionOpts, () => {
      });
191 192 193 194 195 196 197 198

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

199 200 201
    describe("register push notifications", () => {
      it("should call native request permissions with array of categories", () => {
        NotificationsIOS.requestPermissions([someCategory]);
202

203
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([{
204 205 206 207 208
          identifier: "SOME_CATEGORY",
          actions: [actionOpts],
          context: "default"
        }]);
      });
209

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

213
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([]);
214
      });
215

216
      ("should subscribe to 'notificationActionReceived' event once, with a single event handler", () => {
217
        NotificationsIOS.requestPermissions([someCategory]);
218

219 220
        expect(deviceAddEventListener).to.have.been.calledOnce;
        // expect(nativeAppAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
221
      });
222 223
    });

224
    describe("reset categories", () => {
225
      it("should remove 'notificationActionReceived' event handler", () => {
226
        NotificationsIOS.resetCategories();
227

228
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
229
      });
230
    });
231

232 233 234 235 236 237 238 239 240
    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);
      });
    });

241 242 243 244 245 246 247 248
    describe("set badges count", () => {
      it("should call native setBadgesCount", () => {
        NotificationsIOS.setBadgesCount(44);

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

249
  });
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

  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", () => {
269 270
      let someCallback = (time) => {
      };
271

272 273 274
      NotificationsIOS.backgroundTimeRemaining(someCallback);

      expect(nativeBackgroundTimeRemaining).to.have.been.calledWith(someCallback);
275 276
    });
  });
277

278
  describe("Consume background queue which holds background notificiations and actions until js thread is ready", () => {
279 280 281 282 283 284
    it("should call native consume background queue method", () => {
      NotificationsIOS.consumeBackgroundQueue();

      expect(nativeConsumeBackgroundQueue).to.have.been.called;
    });
  });
285

286 287 288 289 290 291
  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", () => {
292 293 294 295 296 297 298 299 300 301 302 303 304
      let someLocalNotification = {
        alertBody: "some body",
        alertTitle: "some title",
        alertAction: "some action",
        soundName: "sound",
        category: "SOME_CATEGORY",
        userInfo: {
          "key": "value"
        }
      };

      NotificationsIOS.localNotification(someLocalNotification);

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
      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();
322 323
    });
  });
Ran Greenberg's avatar
Ran Greenberg committed
324 325 326 327 328 329 330 331

  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
332 333 334 335 336 337 338 339

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

    });
  });
340

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  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
365
});