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

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

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

            return { remove: nativeAppRemoveEventListener };
          }
        },
63 64
        DeviceEventEmitter: {
          addListener: (...args) => {
65
            deviceAddEventListener(...args);
66

67
            return { remove: deviceRemoveEventListener };
68 69 70 71
          }
        },
        "@noCallThru": true
      }
72 73
    });

74
    NotificationsIOS = libUnderTest.default;
75 76
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
77 78 79
  });

  afterEach(() => {
80 81 82 83
    deviceAddEventListener.reset();
    deviceRemoveEventListener.reset();
    nativeAppAddEventListener.reset();
    nativeAppRemoveEventListener.reset();
84 85 86 87
    nativeRequestPermissionsWithCategories.reset();
    nativeAbandonPermissions.reset();
    nativeRegisterPushKit.reset();
    nativeBackgroundTimeRemaining.reset();
88
    nativeConsumeBackgroundQueue.reset();
89
    nativeLocalNotification.reset();
90 91 92
  });

  after(() => {
93 94 95 96
    deviceAddEventListener = null;
    deviceRemoveEventListener = null;
    nativeAppAddEventListener = null;
    nativeAppRemoveEventListener = null;
97 98 99 100
    nativeRequestPermissionsWithCategories = null;
    nativeAbandonPermissions = null;
    nativeRegisterPushKit = null;
    nativeBackgroundTimeRemaining = null;
101
    nativeConsumeBackgroundQueue = null;
102
    nativeLocalNotification = null;
103 104

    NotificationsIOS = null;
105 106
    NotificationAction = null;
    NotificationCategory = null;
107 108
  });

Lidan Hifi's avatar
Lidan Hifi committed
109 110 111
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
112
        NotificationsIOS.addEventListener(event, someHandler);
113

114
        expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func);
115 116 117
      });
    });

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

121
      expect(deviceAddEventListener).to.not.have.been.called;
122 123 124
    });
  });

Lidan Hifi's avatar
Lidan Hifi committed
125 126 127
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
128 129
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
130

131
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
132 133 134
      });
    });

Lidan Hifi's avatar
Lidan Hifi committed
135
    it("should not unsubscribe to unknown device events", () => {
136
      let someUnsupportedEvent = "someUnsupportedEvent";
137 138
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
139

140
      expect(deviceRemoveEventListener).to.not.have.been.called;
141
    });
Lidan Hifi's avatar
Lidan Hifi committed
142
  });
143

144
  describe("Notification actions handling", () => {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    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"
      });
    });

164 165 166
    describe("register push notifications", () => {
      it("should call native request permissions with array of categories", () => {
        NotificationsIOS.requestPermissions([someCategory]);
167

168
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([{
169 170 171 172 173
          identifier: "SOME_CATEGORY",
          actions: [actionOpts],
          context: "default"
        }]);
      });
174

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

178
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([]);
179
      });
180

181
      it("should subscribe to 'notificationActionReceived' event once, with a single event handler", () => {
182
        NotificationsIOS.requestPermissions([someCategory]);
183 184

        expect(nativeAppAddEventListener).to.have.been.calledOnce;
185
        expect(nativeAppAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
186
      });
187 188
    });

189
    describe("reset categories", () => {
190
      it("should remove 'notificationActionReceived' event handler", () => {
191
        NotificationsIOS.resetCategories();
192

193 194
        expect(nativeAppRemoveEventListener).to.have.been.calledOnce;
      });
195 196
    });
  });
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

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

218 219 220
      NotificationsIOS.backgroundTimeRemaining(someCallback);

      expect(nativeBackgroundTimeRemaining).to.have.been.calledWith(someCallback);
221 222
    });
  });
223

224
  describe("Consume background queue which holds background notificiations and actions until js thread is ready", () => {
225 226 227 228 229 230
    it("should call native consume background queue method", () => {
      NotificationsIOS.consumeBackgroundQueue();

      expect(nativeConsumeBackgroundQueue).to.have.been.called;
    });
  });
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

  describe("Get background remaining time", () => {
    it("should call native consume background queue method", () => {
      let someLocalNotification = {
        alertBody: "some body",
        alertTitle: "some title",
        alertAction: "some action",
        soundName: "sound",
        category: "SOME_CATEGORY",
        userInfo: {
          "key": "value"
        }
      };

      NotificationsIOS.localNotification(someLocalNotification);

      expect(nativeLocalNotification).to.have.been.calledWith(someLocalNotification);
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
250
});