index.ios.spec.js 5.15 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 10 11 12 13 14
  let deviceEvents = [
    "notificationReceivedForeground",
    "notificationReceivedBackground",
    "notificationOpened"
  ];

15 16
  let deviceAddEventListener, deviceRemoveEventListener, nativeAppAddEventListener, nativeAppRemoveEventListener, nativeUpdateNotificationCategories;
  let NotificationsIOS, NotificationAction, NotificationCategory;
17 18 19
  let someHandler = () => {};

  before(() => {
20 21 22 23
    deviceAddEventListener = sinon.spy();
    deviceRemoveEventListener = sinon.spy();
    nativeAppAddEventListener = sinon.spy();
    nativeAppRemoveEventListener = sinon.spy();
24 25 26
    nativeUpdateNotificationCategories = sinon.spy();

    let libUnderTest = proxyquire("../index.ios", {
27 28
      "react-native": {
        NativeModules: {
29 30 31
          RNNotifications: {
            updateNotificationCategories: nativeUpdateNotificationCategories
          }
32
        },
33 34 35 36 37 38 39
        NativeAppEventEmitter: {
          addListener: (...args) => {
            nativeAppAddEventListener(...args);

            return { remove: nativeAppRemoveEventListener };
          }
        },
40 41
        DeviceEventEmitter: {
          addListener: (...args) => {
42
            deviceAddEventListener(...args);
43

44
            return { remove: deviceRemoveEventListener };
45 46 47 48
          }
        },
        "@noCallThru": true
      }
49 50
    });

51
    NotificationsIOS = libUnderTest.default;
52 53
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
54 55 56
  });

  afterEach(() => {
57 58 59 60
    deviceAddEventListener.reset();
    deviceRemoveEventListener.reset();
    nativeAppAddEventListener.reset();
    nativeAppRemoveEventListener.reset();
61
    nativeUpdateNotificationCategories.reset();
62 63 64
  });

  after(() => {
65 66 67 68
    deviceAddEventListener = null;
    deviceRemoveEventListener = null;
    nativeAppAddEventListener = null;
    nativeAppRemoveEventListener = null;
69
    nativeUpdateNotificationCategories = null;
70 71

    NotificationsIOS = null;
72 73
    NotificationAction = null;
    NotificationCategory = null;
74 75
  });

Lidan Hifi's avatar
Lidan Hifi committed
76 77 78
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
79
        NotificationsIOS.addEventListener(event, someHandler);
80

81
        expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func);
82 83 84
      });
    });

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

88
      expect(deviceAddEventListener).to.not.have.been.called;
89 90 91
    });
  });

Lidan Hifi's avatar
Lidan Hifi committed
92 93 94
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
95 96
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
97

98
        expect(deviceRemoveEventListener).to.have.been.calledOnce;
99 100 101
      });
    });

Lidan Hifi's avatar
Lidan Hifi committed
102
    it("should not unsubscribe to unknown device events", () => {
103
      let someUnsupportedEvent = "someUnsupportedEvent";
104 105
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
106

107
      expect(deviceRemoveEventListener).to.not.have.been.called;
108
    });
Lidan Hifi's avatar
Lidan Hifi committed
109
  });
110

111
  describe("Notification actions handling", () => {
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    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"
      });
    });

131 132 133
    describe("register categories", () => {
      it("should call native update categories with array of categories", () => {
        NotificationsIOS.setCategories([someCategory]);
134

135 136 137 138 139 140
        expect(nativeUpdateNotificationCategories).to.have.been.calledWith([{
          identifier: "SOME_CATEGORY",
          actions: [actionOpts],
          context: "default"
        }]);
      });
141

142 143 144 145 146
      it("should call native update categories with empty array if no categories specified", () => {
        NotificationsIOS.setCategories();

        expect(nativeUpdateNotificationCategories).to.have.been.calledWith([]);
      });
147

148 149 150 151
      it("should subscribe to 'notificationActionReceived' event once, with a single event handler", () => {
        NotificationsIOS.setCategories([someCategory]);

        expect(nativeAppAddEventListener).to.have.been.calledOnce;
152
        expect(nativeAppAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
153
      });
154 155
    });

156 157 158
    describe("reset categories", () => {
      it("should remove 'notificationActionReceived' event handler", function () {
        NotificationsIOS.resetCategories();
159

160 161
        expect(nativeAppRemoveEventListener).to.have.been.calledOnce;
      });
162 163
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
164
});