index.ios.spec.js 7.25 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 26
      nativeBackgroundTimeRemaining,
      nativeConsumeBackgroundQueue;
27
  let NotificationsIOS, NotificationAction, NotificationCategory;
28
  let someHandler = () => {};
29
  /*eslint-enable indent*/
30 31

  before(() => {
32 33 34 35
    deviceAddEventListener = sinon.spy();
    deviceRemoveEventListener = sinon.spy();
    nativeAppAddEventListener = sinon.spy();
    nativeAppRemoveEventListener = sinon.spy();
36 37 38 39
    nativeRequestPermissionsWithCategories = sinon.spy();
    nativeAbandonPermissions = sinon.spy();
    nativeRegisterPushKit = sinon.spy();
    nativeBackgroundTimeRemaining = sinon.spy();
40
    nativeConsumeBackgroundQueue = sinon.spy();
41 42

    let libUnderTest = proxyquire("../index.ios", {
43 44
      "react-native": {
        NativeModules: {
45
          RNNotifications: {
46 47 48
            requestPermissionsWithCategories: nativeRequestPermissionsWithCategories,
            abandonPermissions: nativeAbandonPermissions,
            registerPushKit: nativeRegisterPushKit,
49 50
            backgroundTimeRemaining: nativeBackgroundTimeRemaining,
            consumeBackgroundQueue: nativeConsumeBackgroundQueue
51
          }
52
        },
53 54 55 56 57 58 59
        NativeAppEventEmitter: {
          addListener: (...args) => {
            nativeAppAddEventListener(...args);

            return { remove: nativeAppRemoveEventListener };
          }
        },
60 61
        DeviceEventEmitter: {
          addListener: (...args) => {
62
            deviceAddEventListener(...args);
63

64
            return { remove: deviceRemoveEventListener };
65 66 67 68
          }
        },
        "@noCallThru": true
      }
69 70
    });

71
    NotificationsIOS = libUnderTest.default;
72 73
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
74 75 76
  });

  afterEach(() => {
77 78 79 80
    deviceAddEventListener.reset();
    deviceRemoveEventListener.reset();
    nativeAppAddEventListener.reset();
    nativeAppRemoveEventListener.reset();
81 82 83 84
    nativeRequestPermissionsWithCategories.reset();
    nativeAbandonPermissions.reset();
    nativeRegisterPushKit.reset();
    nativeBackgroundTimeRemaining.reset();
85
    nativeConsumeBackgroundQueue.reset();
86 87 88
  });

  after(() => {
89 90 91 92
    deviceAddEventListener = null;
    deviceRemoveEventListener = null;
    nativeAppAddEventListener = null;
    nativeAppRemoveEventListener = null;
93 94 95 96
    nativeRequestPermissionsWithCategories = null;
    nativeAbandonPermissions = null;
    nativeRegisterPushKit = null;
    nativeBackgroundTimeRemaining = null;
97
    nativeConsumeBackgroundQueue = null;
98 99

    NotificationsIOS = null;
100 101
    NotificationAction = null;
    NotificationCategory = null;
102 103
  });

Lidan Hifi's avatar
Lidan Hifi committed
104 105 106
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
107
        NotificationsIOS.addEventListener(event, someHandler);
108

109
        expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func);
110 111 112
      });
    });

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

116
      expect(deviceAddEventListener).to.not.have.been.called;
117 118 119
    });
  });

Lidan Hifi's avatar
Lidan Hifi committed
120 121 122
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
123 124
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
125

126
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
127 128 129
      });
    });

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

135
      expect(deviceRemoveEventListener).to.not.have.been.called;
136
    });
Lidan Hifi's avatar
Lidan Hifi committed
137
  });
138

139
  describe("Notification actions handling", () => {
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    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"
      });
    });

159 160 161
    describe("register push notifications", () => {
      it("should call native request permissions with array of categories", () => {
        NotificationsIOS.requestPermissions([someCategory]);
162

163
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([{
164 165 166 167 168
          identifier: "SOME_CATEGORY",
          actions: [actionOpts],
          context: "default"
        }]);
      });
169

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

173
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([]);
174
      });
175

176
      it("should subscribe to 'notificationActionReceived' event once, with a single event handler", () => {
177
        NotificationsIOS.requestPermissions([someCategory]);
178 179

        expect(nativeAppAddEventListener).to.have.been.calledOnce;
180
        expect(nativeAppAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
181
      });
182 183
    });

184
    describe("reset categories", () => {
185
      it("should remove 'notificationActionReceived' event handler", () => {
186
        NotificationsIOS.resetCategories();
187

188 189
        expect(nativeAppRemoveEventListener).to.have.been.calledOnce;
      });
190 191
    });
  });
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

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

213 214 215
      NotificationsIOS.backgroundTimeRemaining(someCallback);

      expect(nativeBackgroundTimeRemaining).to.have.been.calledWith(someCallback);
216 217
    });
  });
218 219 220 221 222 223 224 225

  describe("Get background remaining time", () => {
    it("should call native consume background queue method", () => {
      NotificationsIOS.consumeBackgroundQueue();

      expect(nativeConsumeBackgroundQueue).to.have.been.called;
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
226
});