index.ios.spec.js 10.3 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 32
    deviceRemoveEventListener,
    nativeAppAddEventListener,
    nativeAppRemoveEventListener,
    nativeRequestPermissionsWithCategories,
    nativeAbandonPermissions,
    nativeRegisterPushKit,
    nativeBackgroundTimeRemaining,
    nativeConsumeBackgroundQueue,
    nativeLocalNotification,
    nativeCancelLocalNotification,
    nativeCancelAllLocalNotifications,
    nativeSetBadgesCount,
    nativeIsRegisteredForRemoteNotifications;
33

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

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

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

            return { remove: nativeAppRemoveEventListener };
          }
        },
81 82
        DeviceEventEmitter: {
          addListener: (...args) => {
83
            deviceAddEventListener(...args);
84

85
            return { remove: deviceRemoveEventListener };
86 87 88 89
          }
        },
        "@noCallThru": true
      }
90 91
    });

92
    NotificationsIOS = libUnderTest.default;
93 94
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
95 96 97
  });

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

  after(() => {
114 115 116 117
    deviceAddEventListener = null;
    deviceRemoveEventListener = null;
    nativeAppAddEventListener = null;
    nativeAppRemoveEventListener = null;
118 119 120 121
    nativeRequestPermissionsWithCategories = null;
    nativeAbandonPermissions = null;
    nativeRegisterPushKit = null;
    nativeBackgroundTimeRemaining = null;
122
    nativeConsumeBackgroundQueue = null;
123
    nativeLocalNotification = null;
124 125
    nativeCancelLocalNotification = null;
    nativeCancelAllLocalNotifications = null;
Ran Greenberg's avatar
Ran Greenberg committed
126
    nativeIsRegisteredForRemoteNotifications = null;
127 128

    NotificationsIOS = null;
129 130
    NotificationAction = null;
    NotificationCategory = null;
131 132
  });

Lidan Hifi's avatar
Lidan Hifi committed
133 134 135
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
136
        NotificationsIOS.addEventListener(event, someHandler);
137

138
        expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func);
139 140 141
      });
    });

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

145
      expect(deviceAddEventListener).to.not.have.been.called;
146 147 148
    });
  });

Lidan Hifi's avatar
Lidan Hifi committed
149 150 151
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
152 153
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
154

155
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
156 157 158
      });
    });

Lidan Hifi's avatar
Lidan Hifi committed
159
    it("should not unsubscribe to unknown device events", () => {
160
      let someUnsupportedEvent = "someUnsupportedEvent";
161 162
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
163

164
      expect(deviceRemoveEventListener).to.not.have.been.called;
165
    });
Lidan Hifi's avatar
Lidan Hifi committed
166
  });
167

168
  describe("Notification actions handling", () => {
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    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"
      });
    });

188 189 190
    describe("register push notifications", () => {
      it("should call native request permissions with array of categories", () => {
        NotificationsIOS.requestPermissions([someCategory]);
191

192
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([{
193 194 195 196 197
          identifier: "SOME_CATEGORY",
          actions: [actionOpts],
          context: "default"
        }]);
      });
198

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

202
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([]);
203
      });
204

205
      it("should subscribe to 'notificationActionReceived' event once, with a single event handler", () => {
206
        NotificationsIOS.requestPermissions([someCategory]);
207 208

        expect(nativeAppAddEventListener).to.have.been.calledOnce;
209
        expect(nativeAppAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
210
      });
211 212
    });

213
    describe("reset categories", () => {
214
      it("should remove 'notificationActionReceived' event handler", () => {
215
        NotificationsIOS.resetCategories();
216

217 218
        expect(nativeAppRemoveEventListener).to.have.been.calledOnce;
      });
219
    });
220 221 222 223 224 225 226 227 228

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

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

229
  });
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

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

251 252 253
      NotificationsIOS.backgroundTimeRemaining(someCallback);

      expect(nativeBackgroundTimeRemaining).to.have.been.calledWith(someCallback);
254 255
    });
  });
256

257
  describe("Consume background queue which holds background notificiations and actions until js thread is ready", () => {
258 259 260 261 262 263
    it("should call native consume background queue method", () => {
      NotificationsIOS.consumeBackgroundQueue();

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

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

      NotificationsIOS.localNotification(someLocalNotification);

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


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

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