index.ios.spec.js 9.38 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 13 14 15 16
    "notificationReceivedForeground",
    "notificationReceivedBackground",
    "notificationOpened"
  ];

17 18 19 20 21 22 23 24
  /*eslint-disable indent*/
  let deviceAddEventListener,
      deviceRemoveEventListener,
      nativeAppAddEventListener,
      nativeAppRemoveEventListener,
      nativeRequestPermissionsWithCategories,
      nativeAbandonPermissions,
      nativeRegisterPushKit,
25
      nativeBackgroundTimeRemaining,
26
      nativeConsumeBackgroundQueue,
27 28 29
      nativeLocalNotification,
      nativeCancelLocalNotification,
      nativeCancelAllLocalNotifications;
30
  let NotificationsIOS, NotificationAction, NotificationCategory;
31
  let someHandler = () => {};
32
  let constantGuid = "some-random-uuid";
33
  /*eslint-enable indent*/
34 35

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

232 233 234
      NotificationsIOS.backgroundTimeRemaining(someCallback);

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

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

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

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

      NotificationsIOS.localNotification(someLocalNotification);

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