index.ios.spec.js 6.71 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 25
  /*eslint-disable indent*/
  let deviceAddEventListener,
      deviceRemoveEventListener,
      nativeAppAddEventListener,
      nativeAppRemoveEventListener,
      nativeRequestPermissionsWithCategories,
      nativeAbandonPermissions,
      nativeRegisterPushKit,
      nativeBackgroundTimeRemaining;
26
  let NotificationsIOS, NotificationAction, NotificationCategory;
27
  let someHandler = () => {};
28
  /*eslint-enable indent*/
29 30

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

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

            return { remove: nativeAppRemoveEventListener };
          }
        },
57 58
        DeviceEventEmitter: {
          addListener: (...args) => {
59
            deviceAddEventListener(...args);
60

61
            return { remove: deviceRemoveEventListener };
62 63 64 65
          }
        },
        "@noCallThru": true
      }
66 67
    });

68
    NotificationsIOS = libUnderTest.default;
69 70
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
71 72 73
  });

  afterEach(() => {
74 75 76 77
    deviceAddEventListener.reset();
    deviceRemoveEventListener.reset();
    nativeAppAddEventListener.reset();
    nativeAppRemoveEventListener.reset();
78 79 80 81
    nativeRequestPermissionsWithCategories.reset();
    nativeAbandonPermissions.reset();
    nativeRegisterPushKit.reset();
    nativeBackgroundTimeRemaining.reset();
82 83 84
  });

  after(() => {
85 86 87 88
    deviceAddEventListener = null;
    deviceRemoveEventListener = null;
    nativeAppAddEventListener = null;
    nativeAppRemoveEventListener = null;
89 90 91 92
    nativeRequestPermissionsWithCategories = null;
    nativeAbandonPermissions = null;
    nativeRegisterPushKit = null;
    nativeBackgroundTimeRemaining = null;
93 94

    NotificationsIOS = null;
95 96
    NotificationAction = null;
    NotificationCategory = null;
97 98
  });

Lidan Hifi's avatar
Lidan Hifi committed
99 100 101
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
102
        NotificationsIOS.addEventListener(event, someHandler);
103

104
        expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func);
105 106 107
      });
    });

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

111
      expect(deviceAddEventListener).to.not.have.been.called;
112 113 114
    });
  });

Lidan Hifi's avatar
Lidan Hifi committed
115 116 117
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
118 119
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
120

121
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
122 123 124
      });
    });

Lidan Hifi's avatar
Lidan Hifi committed
125
    it("should not unsubscribe to unknown device events", () => {
126
      let someUnsupportedEvent = "someUnsupportedEvent";
127 128
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
129

130
      expect(deviceRemoveEventListener).to.not.have.been.called;
131
    });
Lidan Hifi's avatar
Lidan Hifi committed
132
  });
133

134
  describe("Notification actions handling", () => {
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    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"
      });
    });

154 155 156
    describe("register push notifications", () => {
      it("should call native request permissions with array of categories", () => {
        NotificationsIOS.requestPermissions([someCategory]);
157

158
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([{
159 160 161 162 163
          identifier: "SOME_CATEGORY",
          actions: [actionOpts],
          context: "default"
        }]);
      });
164

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

168
        expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([]);
169
      });
170

171
      it("should subscribe to 'notificationActionReceived' event once, with a single event handler", () => {
172
        NotificationsIOS.requestPermissions([someCategory]);
173 174

        expect(nativeAppAddEventListener).to.have.been.calledOnce;
175
        expect(nativeAppAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
176
      });
177 178
    });

179
    describe("reset categories", () => {
180
      it("should remove 'notificationActionReceived' event handler", () => {
181
        NotificationsIOS.resetCategories();
182

183 184
        expect(nativeAppRemoveEventListener).to.have.been.calledOnce;
      });
185 186
    });
  });
187 188 189 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", () => {
      NotificationsIOS.backgroundTimeRemaining(time => { });

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