index.ios.spec.js 10.7 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 29 30 31
    deviceRemoveEventListener,
    nativeAppAddEventListener,
    nativeAppRemoveEventListener,
    nativeRequestPermissionsWithCategories,
    nativeAbandonPermissions,
    nativeRegisterPushKit,
    nativeBackgroundTimeRemaining,
    nativeConsumeBackgroundQueue,
    nativeLocalNotification,
    nativeCancelLocalNotification,
    nativeCancelAllLocalNotifications,
    nativeSetBadgesCount,
Ryan Eberhardt's avatar
Ryan Eberhardt committed
32 33
    nativeIsRegisteredForRemoteNotifications,
    nativeCheckPermissions;
34

35
  let NotificationsIOS, NotificationAction, NotificationCategory;
36
  let someHandler = () => {};
37
  let constantGuid = "some-random-uuid";
38
  /*eslint-enable indent*/
39 40

  before(() => {
41 42 43 44
    deviceAddEventListener = sinon.spy();
    deviceRemoveEventListener = sinon.spy();
    nativeAppAddEventListener = sinon.spy();
    nativeAppRemoveEventListener = sinon.spy();
45 46 47 48
    nativeRequestPermissionsWithCategories = sinon.spy();
    nativeAbandonPermissions = sinon.spy();
    nativeRegisterPushKit = sinon.spy();
    nativeBackgroundTimeRemaining = sinon.spy();
49
    nativeConsumeBackgroundQueue = sinon.spy();
50
    nativeLocalNotification = sinon.spy();
51 52
    nativeCancelLocalNotification = sinon.spy();
    nativeCancelAllLocalNotifications = sinon.spy();
53
    nativeSetBadgesCount = sinon.spy();
Ran Greenberg's avatar
Ran Greenberg committed
54
    nativeIsRegisteredForRemoteNotifications = sinon.spy();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
55
    nativeCheckPermissions = sinon.spy();
56 57

    let libUnderTest = proxyquire("../index.ios", {
58 59 60
      "uuid": {
        v4: () => constantGuid
      },
61 62
      "react-native": {
        NativeModules: {
63
          RNNotifications: {
64 65 66
            requestPermissionsWithCategories: nativeRequestPermissionsWithCategories,
            abandonPermissions: nativeAbandonPermissions,
            registerPushKit: nativeRegisterPushKit,
67
            backgroundTimeRemaining: nativeBackgroundTimeRemaining,
68
            consumeBackgroundQueue: nativeConsumeBackgroundQueue,
69 70
            localNotification: nativeLocalNotification,
            cancelLocalNotification: nativeCancelLocalNotification,
71
            cancelAllLocalNotifications: nativeCancelAllLocalNotifications,
Ran Greenberg's avatar
Ran Greenberg committed
72
            setBadgesCount: nativeSetBadgesCount,
Ryan Eberhardt's avatar
Ryan Eberhardt committed
73
            isRegisteredForRemoteNotifications: nativeIsRegisteredForRemoteNotifications,
Ryan Eberhardt's avatar
Ryan Eberhardt committed
74
            checkPermissions: nativeCheckPermissions
75
          }
76
        },
77 78 79 80 81 82 83
        NativeAppEventEmitter: {
          addListener: (...args) => {
            nativeAppAddEventListener(...args);

            return { remove: nativeAppRemoveEventListener };
          }
        },
84 85
        DeviceEventEmitter: {
          addListener: (...args) => {
86
            deviceAddEventListener(...args);
87

88
            return { remove: deviceRemoveEventListener };
89 90 91 92
          }
        },
        "@noCallThru": true
      }
93 94
    });

95
    NotificationsIOS = libUnderTest.default;
96 97
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
98 99 100
  });

  afterEach(() => {
101 102 103 104
    deviceAddEventListener.reset();
    deviceRemoveEventListener.reset();
    nativeAppAddEventListener.reset();
    nativeAppRemoveEventListener.reset();
105 106 107 108
    nativeRequestPermissionsWithCategories.reset();
    nativeAbandonPermissions.reset();
    nativeRegisterPushKit.reset();
    nativeBackgroundTimeRemaining.reset();
109
    nativeConsumeBackgroundQueue.reset();
110
    nativeLocalNotification.reset();
111 112
    nativeCancelLocalNotification.reset();
    nativeCancelAllLocalNotifications.reset();
Ran Greenberg's avatar
Ran Greenberg committed
113
    nativeIsRegisteredForRemoteNotifications.reset();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
114
    nativeCheckPermissions.reset();
115 116 117
  });

  after(() => {
118 119 120 121
    deviceAddEventListener = null;
    deviceRemoveEventListener = null;
    nativeAppAddEventListener = null;
    nativeAppRemoveEventListener = null;
122 123 124 125
    nativeRequestPermissionsWithCategories = null;
    nativeAbandonPermissions = null;
    nativeRegisterPushKit = null;
    nativeBackgroundTimeRemaining = null;
126
    nativeConsumeBackgroundQueue = null;
127
    nativeLocalNotification = null;
128 129
    nativeCancelLocalNotification = null;
    nativeCancelAllLocalNotifications = null;
Ran Greenberg's avatar
Ran Greenberg committed
130
    nativeIsRegisteredForRemoteNotifications = null;
Ryan Eberhardt's avatar
Ryan Eberhardt committed
131
    nativeCheckPermissions = null;
132 133

    NotificationsIOS = null;
134 135
    NotificationAction = null;
    NotificationCategory = null;
136 137
  });

Lidan Hifi's avatar
Lidan Hifi committed
138 139 140
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
141
        NotificationsIOS.addEventListener(event, someHandler);
142

143
        expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func);
144 145 146
      });
    });

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

150
      expect(deviceAddEventListener).to.not.have.been.called;
151 152 153
    });
  });

Lidan Hifi's avatar
Lidan Hifi committed
154 155 156
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
157 158
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
159

160
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
161 162 163
      });
    });

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

169
      expect(deviceRemoveEventListener).to.not.have.been.called;
170
    });
Lidan Hifi's avatar
Lidan Hifi committed
171
  });
172

173
  describe("Notification actions handling", () => {
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    let someAction, someCategory;

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

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

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

193 194 195
    describe("register push notifications", () => {
      it("should call native request permissions with array of categories", () => {
        NotificationsIOS.requestPermissions([someCategory]);
196

197
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([{
198 199 200 201 202
          identifier: "SOME_CATEGORY",
          actions: [actionOpts],
          context: "default"
        }]);
      });
203

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

207
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([]);
208
      });
209

210
      it("should subscribe to 'notificationActionReceived' event once, with a single event handler", () => {
211
        NotificationsIOS.requestPermissions([someCategory]);
212 213

        expect(nativeAppAddEventListener).to.have.been.calledOnce;
214
        expect(nativeAppAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
215
      });
216 217
    });

218
    describe("reset categories", () => {
219
      it("should remove 'notificationActionReceived' event handler", () => {
220
        NotificationsIOS.resetCategories();
221

222 223
        expect(nativeAppRemoveEventListener).to.have.been.calledOnce;
      });
224
    });
225 226 227 228 229 230 231 232 233

    describe("set badges count", () => {
      it("should call native setBadgesCount", () => {
        NotificationsIOS.setBadgesCount(44);

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

234
  });
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

  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", () => {
254
      let someCallback = (time) => { };
255

256 257 258
      NotificationsIOS.backgroundTimeRemaining(someCallback);

      expect(nativeBackgroundTimeRemaining).to.have.been.calledWith(someCallback);
259 260
    });
  });
261

262
  describe("Consume background queue which holds background notificiations and actions until js thread is ready", () => {
263 264 265 266 267 268
    it("should call native consume background queue method", () => {
      NotificationsIOS.consumeBackgroundQueue();

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

270 271 272 273 274 275
  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", () => {
276 277 278 279 280 281 282 283 284 285 286 287 288
      let someLocalNotification = {
        alertBody: "some body",
        alertTitle: "some title",
        alertAction: "some action",
        soundName: "sound",
        category: "SOME_CATEGORY",
        userInfo: {
          "key": "value"
        }
      };

      NotificationsIOS.localNotification(someLocalNotification);

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
      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();
306 307
    });
  });
Ran Greenberg's avatar
Ran Greenberg committed
308 309 310 311 312 313 314 315 316


  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
317 318 319 320 321 322 323 324

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

    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
325
});