index.ios.spec.js 9.42 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 20 21 22 23 24 25
  /*eslint-disable indent*/
  let deviceAddEventListener,
      deviceRemoveEventListener,
      nativeAppAddEventListener,
      nativeAppRemoveEventListener,
      nativeRequestPermissionsWithCategories,
      nativeAbandonPermissions,
      nativeRegisterPushKit,
26
      nativeBackgroundTimeRemaining,
27
      nativeConsumeBackgroundQueue,
28 29 30
      nativeLocalNotification,
      nativeCancelLocalNotification,
      nativeCancelAllLocalNotifications;
31
  let NotificationsIOS, NotificationAction, NotificationCategory;
32
  let someHandler = () => {};
33
  let constantGuid = "some-random-uuid";
34
  /*eslint-enable indent*/
35 36

  before(() => {
37 38 39 40
    deviceAddEventListener = sinon.spy();
    deviceRemoveEventListener = sinon.spy();
    nativeAppAddEventListener = sinon.spy();
    nativeAppRemoveEventListener = sinon.spy();
41 42 43 44
    nativeRequestPermissionsWithCategories = sinon.spy();
    nativeAbandonPermissions = sinon.spy();
    nativeRegisterPushKit = sinon.spy();
    nativeBackgroundTimeRemaining = sinon.spy();
45
    nativeConsumeBackgroundQueue = sinon.spy();
46
    nativeLocalNotification = sinon.spy();
47 48
    nativeCancelLocalNotification = sinon.spy();
    nativeCancelAllLocalNotifications = sinon.spy();
49 50

    let libUnderTest = proxyquire("../index.ios", {
51 52 53
      "uuid": {
        v4: () => constantGuid
      },
54 55
      "react-native": {
        NativeModules: {
56
          RNNotifications: {
57 58 59
            requestPermissionsWithCategories: nativeRequestPermissionsWithCategories,
            abandonPermissions: nativeAbandonPermissions,
            registerPushKit: nativeRegisterPushKit,
60
            backgroundTimeRemaining: nativeBackgroundTimeRemaining,
61
            consumeBackgroundQueue: nativeConsumeBackgroundQueue,
62 63 64
            localNotification: nativeLocalNotification,
            cancelLocalNotification: nativeCancelLocalNotification,
            cancelAllLocalNotifications: nativeCancelAllLocalNotifications
65
          }
66
        },
67 68 69 70 71 72 73
        NativeAppEventEmitter: {
          addListener: (...args) => {
            nativeAppAddEventListener(...args);

            return { remove: nativeAppRemoveEventListener };
          }
        },
74 75
        DeviceEventEmitter: {
          addListener: (...args) => {
76
            deviceAddEventListener(...args);
77

78
            return { remove: deviceRemoveEventListener };
79 80 81 82
          }
        },
        "@noCallThru": true
      }
83 84
    });

85
    NotificationsIOS = libUnderTest.default;
86 87
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
88 89 90
  });

  afterEach(() => {
91 92 93 94
    deviceAddEventListener.reset();
    deviceRemoveEventListener.reset();
    nativeAppAddEventListener.reset();
    nativeAppRemoveEventListener.reset();
95 96 97 98
    nativeRequestPermissionsWithCategories.reset();
    nativeAbandonPermissions.reset();
    nativeRegisterPushKit.reset();
    nativeBackgroundTimeRemaining.reset();
99
    nativeConsumeBackgroundQueue.reset();
100
    nativeLocalNotification.reset();
101 102
    nativeCancelLocalNotification.reset();
    nativeCancelAllLocalNotifications.reset();
103 104 105
  });

  after(() => {
106 107 108 109
    deviceAddEventListener = null;
    deviceRemoveEventListener = null;
    nativeAppAddEventListener = null;
    nativeAppRemoveEventListener = null;
110 111 112 113
    nativeRequestPermissionsWithCategories = null;
    nativeAbandonPermissions = null;
    nativeRegisterPushKit = null;
    nativeBackgroundTimeRemaining = null;
114
    nativeConsumeBackgroundQueue = null;
115
    nativeLocalNotification = null;
116 117
    nativeCancelLocalNotification = null;
    nativeCancelAllLocalNotifications = null;
118 119

    NotificationsIOS = null;
120 121
    NotificationAction = null;
    NotificationCategory = null;
122 123
  });

Lidan Hifi's avatar
Lidan Hifi committed
124 125 126
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
127
        NotificationsIOS.addEventListener(event, someHandler);
128

129
        expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func);
130 131 132
      });
    });

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

136
      expect(deviceAddEventListener).to.not.have.been.called;
137 138 139
    });
  });

Lidan Hifi's avatar
Lidan Hifi committed
140 141 142
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
143 144
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
145

146
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
147 148 149
      });
    });

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

155
      expect(deviceRemoveEventListener).to.not.have.been.called;
156
    });
Lidan Hifi's avatar
Lidan Hifi committed
157
  });
158

159
  describe("Notification actions handling", () => {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    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"
      });
    });

179 180 181
    describe("register push notifications", () => {
      it("should call native request permissions with array of categories", () => {
        NotificationsIOS.requestPermissions([someCategory]);
182

183
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([{
184 185 186 187 188
          identifier: "SOME_CATEGORY",
          actions: [actionOpts],
          context: "default"
        }]);
      });
189

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

193
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([]);
194
      });
195

196
      it("should subscribe to 'notificationActionReceived' event once, with a single event handler", () => {
197
        NotificationsIOS.requestPermissions([someCategory]);
198 199

        expect(nativeAppAddEventListener).to.have.been.calledOnce;
200
        expect(nativeAppAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
201
      });
202 203
    });

204
    describe("reset categories", () => {
205
      it("should remove 'notificationActionReceived' event handler", () => {
206
        NotificationsIOS.resetCategories();
207

208 209
        expect(nativeAppRemoveEventListener).to.have.been.calledOnce;
      });
210 211
    });
  });
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

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

233 234 235
      NotificationsIOS.backgroundTimeRemaining(someCallback);

      expect(nativeBackgroundTimeRemaining).to.have.been.calledWith(someCallback);
236 237
    });
  });
238

239
  describe("Consume background queue which holds background notificiations and actions until js thread is ready", () => {
240 241 242 243 244 245
    it("should call native consume background queue method", () => {
      NotificationsIOS.consumeBackgroundQueue();

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

247 248 249 250 251 252
  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", () => {
253 254 255 256 257 258 259 260 261 262 263 264 265
      let someLocalNotification = {
        alertBody: "some body",
        alertTitle: "some title",
        alertAction: "some action",
        soundName: "sound",
        category: "SOME_CATEGORY",
        userInfo: {
          "key": "value"
        }
      };

      NotificationsIOS.localNotification(someLocalNotification);

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
      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();
283 284
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
285
});